image.c 17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <string.h>
#include <m_pd.h>
#include "g_canvas.h"
#include "s_stuff.h"
#include <stdio.h>

#ifdef _MSC_VER
#pragma warning( disable : 4244 )
#pragma warning( disable : 4305 )
#endif

/* ------------------------ image ----------------------------- */

static t_class *image_class;

typedef struct _image
{
18 19 20 21 22 23 24 25 26 27 28 29 30
    t_object x_obj;
    t_glist * x_glist;
    int x_width;
    int x_height;
    int x_img_width;
    int x_img_height;
    int x_gop_spill;
    int x_click;
    //t_float x_clicked;
    t_symbol* x_fname;
    t_symbol* x_receive;
    //int x_selected;
    //t_symbol* send;
31 32 33
} t_image;

/* widget helper functions */
34
static void image_select(t_gobj *z, t_glist *glist, int state);
35

36
t_symbol *image_trytoopen(t_image* x)
37 38 39
{
    char fname[FILENAME_MAX];
    FILE *file;
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    if (x->x_fname == &s_)
    {
        return 0;
    }
    t_glist *glist = glist_getcanvas(x->x_glist);
    canvas_makefilename(glist_getcanvas(x->x_glist), x->x_fname->s_name,
        fname, FILENAME_MAX);
    // try to open the file
    if (file = fopen(fname, "r"))
    {
        fclose(file);
        return gensym(fname);
    }
    else
    {
        return 0;
    }
}

59
static void image_drawme(t_image *x, t_glist *glist, int firstime)
60
{
61 62
    if (firstime)
    {
63 64 65 66 67 68 69 70 71 72
        t_symbol *fname = image_trytoopen(x);
        // make a new gobj, border, etc.
        gui_vmess("gui_gobj_new", "xxsiii",
            glist_getcanvas(glist),
            x,
            "obj",
            text_xpix(&x->x_obj, glist),
            text_ypix(&x->x_obj, glist),
            glist_istoplevel(glist));
        if (fname) {
73 74
            gui_vmess("gui_load_image", "xxs",
                glist_getcanvas(glist), x, fname->s_name);
75 76 77
        }
        else
        {
78 79
            gui_vmess("gui_load_default_image", "xx",
                glist_getcanvas(glist), x);
80 81
        }
        // draw the new canvas image
82
        gui_vmess("gui_gobj_draw_image", "xxxs",
83 84
            glist_getcanvas(glist),
            x,
85
            x,
86 87 88 89 90
            "center");
        //sys_vgui("catch {.x%lx.c delete %xS}\n", glist_getcanvas(glist), x);
        //sys_vgui(".x%x.c create image %d %d -tags %xS\n",
        //    glist_getcanvas(glist),text_xpix(&x->x_obj, glist),
        //    text_ypix(&x->x_obj, glist), x);
91 92
        gui_vmess("gui_image_size_callback", "xxs",
            glist_getcanvas(glist), x, x->x_receive->s_name);
93 94 95
    }
    else
    {
96 97 98 99 100 101 102 103 104
        // move the gobj
        //sys_vgui(".x%x.c coords %xS %d %d\n",
        //    glist_getcanvas(glist), x,
        //    text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist));
        gui_vmess("gui_image_coords", "xxii",
            glist_getcanvas(glist),
            x,
            text_xpix(&x->x_obj, glist),
            text_ypix(&x->x_obj, glist));
105
    }
106 107
}

108
static void image_erase(t_image* x,t_glist* glist)
109
{
110 111
    gui_vmess("gui_gobj_erase", "xx", glist_getcanvas(glist), x);
    //sys_vgui("catch {.x%x.c delete %xS}\n",glist_getcanvas(glist), x);
112
    //sys_vgui("catch {image delete $img%x}\n", x);
113
    //sys_vgui("catch {.x%x.c delete %xSEL}\n",glist_getcanvas(glist), x);
114 115 116 117 118 119
}

static t_symbol *get_filename(t_int argc, t_atom *argv)
{
    t_symbol *fname;
    fname = atom_getsymbolarg(0, argc, argv);
120
    if (argc > 1)
121 122 123 124
    {
        int i;
        char buf[MAXPDSTRING];
        strcpy(buf, fname->s_name);
125
        for (i = 1; i < argc; i++)
126
        {
127 128 129 130 131 132 133 134 135
            if (argv[i].a_type == A_SYMBOL)
            {
                strcat(buf, " ");
                strcat(buf, atom_getsymbolarg(i, argc, argv)->s_name);
            }
            else
            {
                break;
            }
136 137 138 139 140 141 142 143 144 145 146
        }
        fname = gensym(buf);
    }
    return fname;
}

/* ------------------------ image widgetbehaviour----------------------------- */

static void image_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
    int width, height;
    t_image* x = (t_image*)z;
    //printf("image_getrect %d %d\n", *xp1, *yp1);

    if (!x->x_gop_spill && (x->x_img_width + x->x_img_height) >= 2) {
        width = x->x_img_width;
        height = x->x_img_height;
    }
    else
    {
        width = x->x_width;
        height = x->x_height;
    }
    *xp1 = text_xpix(&x->x_obj, glist) - width/2;
    *yp1 = text_ypix(&x->x_obj, glist) - height/2;
    *xp2 = text_xpix(&x->x_obj, glist) + width/2;
    *yp2 = text_ypix(&x->x_obj, glist) + height/2;

    // if we have click detection disabled and we are in runmode,
    // return 0 size to allow for click relegation to hidden objects below
    // CAREFUL: this code is not reusable for objects that have more than
    // one inlet or outlet because it will cram them together
    if ((glist_getcanvas(glist) != glist && !x->x_click) || (!glist->gl_edit && !x->x_click))
    {
        *xp2 = *xp1;
        // only if we have an image loaded and we are placed within a GOP obliterate the height
        //if (glist_getcanvas(glist) != glist && (x->x_img_width + x->x_img_height) >= 2)
        //{
            //printf("blah\n");
            //*yp2 = *yp1;
        //}
    }
    //fprintf(stderr,"image_getrect %d %d %d %d\n", *xp1, *yp1, *xp2, *yp2);
180 181 182 183 184
}

static void image_displace(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
185
    //fprintf(stderr,"image displace\n");
186 187 188
    t_image *x = (t_image *)z;
    x->x_obj.te_xpix += dx;
    x->x_obj.te_ypix += dy;
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
    if (!x->x_gop_spill && (x->x_img_width + x->x_img_height) >= 2)
    {
        sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n",
            glist_getcanvas(glist), x,
            text_xpix(&x->x_obj, glist) - x->x_img_width/2,
            text_ypix(&x->x_obj, glist) - x->x_img_height/2,
            text_xpix(&x->x_obj, glist) + x->x_img_width/2,
            text_ypix(&x->x_obj, glist) + x->x_img_height/2);
    }
    else
    {
        sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n",
            glist_getcanvas(glist), x,
            text_xpix(&x->x_obj, glist) - x->x_width/2,
            text_ypix(&x->x_obj, glist) - x->x_height/2,
            text_xpix(&x->x_obj, glist) + x->x_width/2,
            text_ypix(&x->x_obj, glist) + x->x_height/2);
        /*if (x->x_img_width + x->x_img_height == 0)
            sys_vgui(".x%x.c coords %xMT %d %d %d %d\n",
                glist_getcanvas(glist), x,
                text_xpix(&x->x_obj, glist) - x->x_width/2,
                text_ypix(&x->x_obj, glist) - x->x_height/2,
                text_xpix(&x->x_obj, glist) + x->x_width/2,
                text_ypix(&x->x_obj, glist) + x->x_height/2);*/
    }
214 215 216 217 218 219 220
    image_drawme(x, glist, 0);
    canvas_fixlinesfor(glist,(t_text*) x);
}

static void image_displace_wtag(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
221
    //fprintf(stderr,"image displace_wtag\n");
222 223 224 225
    t_image *x = (t_image *)z;
    x->x_obj.te_xpix += dx;
    x->x_obj.te_ypix += dy;
    /*sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n",
226 227 228
           glist_getcanvas(glist), x,
           text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
           text_xpix(&x->x_obj, glist) + x->x_width, text_ypix(&x->x_obj, glist) + x->x_height);
229 230 231 232 233 234 235

    image_drawme(x, glist, 0);*/
    canvas_fixlinesfor(glist,(t_text*) x);
}

static void image_select(t_gobj *z, t_glist *glist, int state)
{
236 237
    //fprintf(stderr,"image_select %d\n", state);
    t_image *x = (t_image *)z;
238 239
    gui_vmess("gui_image_toggle_border", "xxi",
        glist_getcanvas(glist), x, state);
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    if (state)
    {
        if (x->x_glist == glist_getcanvas(glist))
        {
            //x->x_selected = state;
            if (!x->x_gop_spill && (x->x_img_width + x->x_img_height) >= 2)
            {
                sys_vgui(".x%x.c create prect %d %d %d %d \
                        -tags %xSEL -strokewidth 1 -stroke $pd_colors(selection)\n",
                    glist_getcanvas(glist),
                    text_xpix(&x->x_obj, glist) - x->x_img_width/2,
                    text_ypix(&x->x_obj, glist) - x->x_img_height/2,
                    text_xpix(&x->x_obj, glist) + x->x_img_width/2,
                    text_ypix(&x->x_obj, glist) + x->x_img_height/2, x);
            }
            else
            {
                sys_vgui(".x%x.c create prect %d %d %d %d \
                        -tags %xSEL -strokewidth 1 -stroke $pd_colors(selection)\n",
                    glist_getcanvas(glist),
                    text_xpix(&x->x_obj, glist) - x->x_width/2,
                    text_ypix(&x->x_obj, glist) - x->x_height/2,
                    text_xpix(&x->x_obj, glist) + x->x_width/2,
                    text_ypix(&x->x_obj, glist) + x->x_height/2, x);
            }
        }
        //if (glist->gl_owner && !glist_istoplevel(glist))
267 268 269 270
        //sys_vgui(".x%x.c addtag selected withtag %xS\n", glist_getcanvas(glist), x);
        //sys_vgui(".x%x.c addtag selected withtag %xMT\n", glist_getcanvas(glist), x);
        //sys_vgui(".x%x.c addtag selected withtag %xSEL\n", glist_getcanvas(glist), x);
        gui_vmess("gui_gobj_select", "xx", glist_getcanvas(glist), x);
271 272 273
    }
    else
    {
274 275
        //sys_vgui("catch {.x%x.c delete %xSEL}\n",
        //glist_getcanvas(glist), x);
276
        //if (glist->gl_owner && !glist_istoplevel(glist))
277 278 279
        //sys_vgui(".x%lx.c dtag %xS selected\n", glist_getcanvas(glist), x);
        //sys_vgui(".x%lx.c dtag %xMT selected\n", glist_getcanvas(glist), x);
        gui_vmess("gui_gobj_deselect", "xx", glist_getcanvas(glist), x);
280
    }
281 282 283 284
}

static void image_activate(t_gobj *z, t_glist *glist, int state)
{
285 286 287 288 289 290
    /*fprintf(stderr,"activate...\n");
    t_text *x = (t_text *)z;
    t_rtext *y = glist_findrtext(glist, x);
    rtext_activate(y, state);
    t_image *i = (t_image *)z;
    canvas_redraw(i->x_glist);*/
291 292 293 294
}

static void image_delete(t_gobj *z, t_glist *glist)
{
295 296
    t_text *x = (t_text *)z;
    canvas_deletelinesfor(glist, x);
297 298 299 300
}

static void image_vis(t_gobj *z, t_glist *glist, int vis)
{
301 302 303 304 305 306
    //fprintf(stderr,"image_vis %d\n", vis);
    t_image* x = (t_image*)z;
    if (vis)
        image_drawme(x, glist, 1);
    else
        image_erase(x,glist);
307 308 309 310 311 312 313
}

/* can we use the normal text save function ?? */

static void image_save(t_gobj *z, t_binbuf *b)
{
    t_image *x = (t_image *)z;
314
    binbuf_addv(b, "ssiissi", gensym("#X"), gensym("obj"),
315 316
                x->x_obj.te_xpix, x->x_obj.te_ypix,   
                atom_getsymbol(binbuf_getvec(x->x_obj.te_binbuf)),
317
                x->x_fname, x->x_gop_spill);
318 319 320
    binbuf_addv(b, ";");
}

321
static t_widgetbehavior image_widgetbehavior;
322

323
/*void image_size(t_image* x,t_floatarg w,t_floatarg h) {
324 325
     x->x_width = w;
     x->x_height = h;
326
     image_displace((t_gobj*)x, x->x_glist, 0.0, 0.0);
327
}*/
328

329
/*void image_color(t_image* x,t_symbol* col)
330
{
331 332 333 334
     //outlet_bang(x->x_obj.ob_outlet); only bang if there was a bang .. 
     //so color black does the same as bang, but doesn't forward the bang 

}*/
335

336
static int image_newclick(t_gobj *z, struct _glist *glist, int xpix, int ypix, int shift, int alt, int dbl, int doit)
337
{
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
    //printf("doit=%d\n", doit);
    t_image *x = (t_image *)z;
    if (doit && x->x_click)
        outlet_bang(x->x_obj.ob_outlet);
    // LATER: figure out how to do click on and click off
    // and provide a toggle button behavior insteadS
    /*{
        x->x_clicked = 1;
        outlet_float(x->x_obj.ob_outlet, x->x_clicked);
    }
    else if (x->x_clicked)
    {
        x->x_clicked = 0;
        outlet_float(x->x_obj.ob_outlet, x->x_clicked);
    }*/
    return(1);
354 355 356 357
}

static void image_click(t_image *x, t_float f)
{
358 359 360 361
    if (f == 0)
        x->x_click = 0;
    else if (f == 1)
        x->x_click = 1;
362
}
363

364
static void image_gop_spill(t_image* x, t_floatarg f)
365
{
366
    x->x_gop_spill = (f >= 0 ? f : 0);
367
    image_displace((t_gobj*)x, x->x_glist, 0.0, 0.0);
368 369
}

370
static void image_gop_spill_size(t_image* x, t_floatarg f)
371
{
372 373 374 375 376 377 378 379 380
    //printf("image_gop_spill_size=%d\n", (int)f);
    // we need a size of at least 3 to have a meaningful
    // selection frame around the selection box
    if ((int)f >= 3)
    {
        x->x_width = (int)f;
        x->x_height = x->x_width;
        image_displace((t_gobj*)x, x->x_glist, 0.0, 0.0);
    }
381 382
}

383
static void image_open(t_image* x, t_symbol *s, t_int argc, t_atom *argv)
384 385
{
    x->x_fname = get_filename(argc, argv);
386 387
    x->x_img_width = 0;
    x->x_img_height = 0;
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    t_symbol *fname = image_trytoopen(x);
    if (fname) {
        gui_vmess("gui_load_image", "xxs",
            glist_getcanvas(x->x_glist), x, fname->s_name);
    }
    else
    {
        gui_vmess("gui_load_default_image", "xx",
            glist_getcanvas(x->x_glist), x);
    }
    if (glist_isvisible(glist_getcanvas(x->x_glist)))
    {
        gui_vmess("gui_image_configure", "xxxs",
            glist_getcanvas(x->x_glist),
            x,
            x,
            "center");
        gui_vmess("gui_image_size_callback", "xxs",
            glist_getcanvas(x->x_glist), x, x->x_receive->s_name);
    }
    //image_vis((t_gobj *)x, x->x_glist, 0);
    //image_vis((t_gobj *)x, x->x_glist, 1);
410 411
}

412
static void image_imagesize_callback(t_image *x, t_float w, t_float h) {
413 414 415 416 417 418 419 420 421
    //fprintf(stderr,"received w %f h %f should %d spill %d\n", w, h, gobj_shouldvis((t_gobj *)x, glist_getcanvas(x->x_glist)), x->x_gop_spill);
    x->x_img_width = w;
    x->x_img_height = h;
    if (x->x_img_width + x->x_img_height == 0)
    {
        //invalid image
        if (strcmp("@pd_extra/ggee/empty_image.png", x->x_fname->s_name) != 0)
        {
            x->x_fname = gensym("@pd_extra/ggee/empty_image.png");
422
            image_trytoopen(x);
423 424 425 426 427 428 429 430 431 432 433 434
            return;
        }
    }
    if (!gobj_shouldvis((t_gobj *)x, x->x_glist) && !x->x_gop_spill)
    {
            //fprintf(stderr,"erasing\n");
            image_erase(x, glist_getcanvas(x->x_glist));
    }
    else
    {
        //sys_vgui("catch {.x%x.c delete %xMT}\n", glist_getcanvas(x->x_glist), x);
        // reselect if we are on a toplevel canvas to adjust the selection rectangle, if necessary
435 436 437 438 439 440 441 442 443

        gui_vmess("gui_image_draw_border", "xxiiii",
            glist_getcanvas(x->x_glist),
            x,
            0 - x->x_img_width/2,
            0 - x->x_img_height/2,
            x->x_img_width,
            x->x_img_height);

444 445 446 447 448 449 450
        if (glist_isselected(x->x_glist, (t_gobj *)x) && glist_getcanvas(x->x_glist) == x->x_glist)
        {
            image_select((t_gobj *)x, glist_getcanvas(x->x_glist), 0);
            image_select((t_gobj *)x, glist_getcanvas(x->x_glist), 1);
        }
        canvas_fixlinesfor(x->x_glist,(t_text*) x);
    }
451 452
}

453 454
static void image_setwidget(void)
{
455 456 457 458 459 460 461 462
    image_widgetbehavior.w_getrectfn = image_getrect;
    image_widgetbehavior.w_displacefn = image_displace;
    image_widgetbehavior.w_selectfn = image_select;
    image_widgetbehavior.w_activatefn = image_activate;
    image_widgetbehavior.w_deletefn = image_delete;
    image_widgetbehavior.w_visfn = image_vis;
    image_widgetbehavior.w_clickfn = image_newclick;
    image_widgetbehavior.w_displacefnwtag = image_displace_wtag;
463 464
}

465 466
static void image_free(t_image *x)
{
467
    //sys_vgui("image delete img%x\n", x);
468
    gui_vmess("gui_image_free", "x", x);
469 470 471 472 473 474
    if (x->x_receive)
    {
        pd_unbind(&x->x_obj.ob_pd,x->x_receive);
    }
    //sys_vgui(".x%x.c delete %xSEL\n", x);
    //sys_vgui(".x%x.c delete %xS\n", x);
475
}
476 477 478 479 480 481 482

static void *image_new(t_symbol *s, t_int argc, t_atom *argv)
{
    t_image *x = (t_image *)pd_new(image_class);
    x->x_glist = (t_glist*) canvas_getcurrent();
    x->x_width = 15;
    x->x_height = 15;
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    x->x_img_width = 0;
    x->x_img_height = 0;
    x->x_gop_spill = 0;
    x->x_click = 0;
    //x->x_clicked = 0;
    //x->x_selected = 0;
    x->x_fname = get_filename(argc, argv);
    if (strlen(x->x_fname->s_name) > 0)
    {
        //fprintf(stderr,"get_filename succeeded <%s> <%s>\n", x->x_fname->s_name, atom_getsymbol(argv)->s_name);
        argc--;
        argv++;
    }
    if (argc && argv[0].a_type == A_FLOAT)
    {
        //we have optional gop_spill flag first
        //fprintf(stderr,"gop_spill succeeded\n");
        x->x_gop_spill = (int)atom_getfloat(&argv[0]);
        argc--;
        argv++;
    }
    // Create default receiver
    char buf[MAXPDSTRING];
    sprintf(buf, "#%lx", (long)x);
    x->x_receive = gensym(buf);
508
    pd_bind(&x->x_obj.ob_pd, x->x_receive);
509 510
    outlet_new(&x->x_obj, &s_bang);
    //outlet_new(&x->x_obj, &s_float);
511 512 513 514 515
    return (x);
}

void image_setup(void)
{
516
    image_class = class_new(gensym("image"),
517 518
                (t_newmethod)image_new, (t_method)image_free,
                sizeof(t_image),0, A_GIMME,0);
519
/*
520 521 522 523
    class_addmethod(image_class, (t_method)image_size, gensym("size"),
        A_FLOAT, A_FLOAT, 0);
    class_addmethod(image_class, (t_method)image_color, gensym("color"),
        A_SYMBOL, 0);
524
*/
525 526
    class_addmethod(image_class, (t_method)image_click, gensym("click"),
        A_DEFFLOAT, 0);
527
    class_addmethod(image_class, (t_method)image_open, gensym("open"),
528
        A_GIMME, 0);
529
    class_addmethod(image_class, (t_method)image_gop_spill, gensym("gopspill"),
530 531 532
        A_DEFFLOAT, 0);
    class_addmethod(image_class, (t_method)image_gop_spill_size, gensym("gopspill_size"),
        A_DEFFLOAT, 0);
533 534
    class_addmethod(image_class, (t_method)image_imagesize_callback,\
                     gensym("_imagesize"), A_DEFFLOAT, A_DEFFLOAT, 0);
535

536 537 538 539
    image_setwidget();
    class_setwidget(image_class,&image_widgetbehavior);
    class_setsavefn(image_class,&image_save);
}