imagebang.c 15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
#include <m_pd.h>
#include <g_canvas.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

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

/* Append " x " to the following line to show debugging messages */
#define DEBUG(x)



/* ------------------------ imagebang ----------------------------- */

static t_class *imagebang_class;
t_widgetbehavior imagebang_widgetbehavior;

typedef struct _imagebang
{
     t_object x_obj;
     t_glist * glist;
     int width;
     int height;
     t_symbol*  image_a;
     t_symbol*  image_b;
     t_symbol* receive;
     t_symbol* send;
     t_clock* clock_flash;
     t_clock* clock_brk;
34 35
     int         flashing;
     t_outlet* outlet;
36 37 38 39
} t_imagebang;

static void imagebang_bang(t_imagebang *x)
{
40 41 42 43 44 45
    t_glist* glist = glist_getcanvas(x->glist);
    char image_key[MAXPDSTRING];
    if (x->flashing)
    {
        sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n",
            glist, x,x->image_a);
46 47
        clock_delay(x->clock_brk, 50);
        //x->flashed = 1;
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        // key_a:
        sprintf(image_key, "%lx_a", (long unsigned int)x);
        gui_vmess("gui_image_configure", "xxss",
            glist_getcanvas(x->glist),
            x,
            image_key,
            "nw");
    }
    else
    {
        sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n",
            glist, x,x->image_b);
        // key_b:
        sprintf(image_key, "%lx_b", (long unsigned int)x);
        gui_vmess("gui_image_configure", "xxss",
            glist_getcanvas(x->glist),
            x,
            image_key,
            "nw");
67 68 69 70 71 72
        x->flashing = 1;
    }
    clock_delay(x->clock_flash, 250);
       
    outlet_bang(x->outlet);
    
73
    if (x->send && x->send->s_thing) pd_bang(x->send->s_thing);
74 75 76 77
}

static void imagebang_flash_timeout(t_imagebang *x)
{
78 79
    t_glist* glist = glist_getcanvas(x->glist);
    char key_a[MAXPDSTRING];
80
    x->flashing = 0;
81 82 83 84 85 86 87 88 89
    sprintf(key_a, "%lx_a", (long unsigned int)x);
    gui_vmess("gui_image_configure", "xxss",
        glist_getcanvas(x->glist),
        x,
        key_a,
        "nw");
    x->flashing = 1;
    //sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n",
    //    glist, x,x->image_a);
90 91 92 93 94
    
}

static void imagebang_brk_timeout(t_imagebang *x)
{
95 96
    t_glist* glist = glist_getcanvas(x->glist);
    char key_b[MAXPDSTRING];
97
    x->flashing = 1;
98 99 100 101 102 103 104 105
    //sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n",
    //    glist, x,x->image_b);
    sprintf(key_b, "%lx_b", (long unsigned int)x);
    gui_vmess("gui_image_configure", "xxss",
        glist_getcanvas(x->glist),
        x,
        key_b,
        "nw");
106 107 108 109 110
}

/* widget helper functions */

static const char* imagebang_get_filename(t_imagebang *x,char *file) {
111 112 113 114 115 116 117 118 119 120 121 122 123 124
    static char fname[MAXPDSTRING];
    char *bufptr;
    int fd;
    
    fd=open_via_path(canvas_getdir(glist_getcanvas(x->glist))->s_name, 
        file, "",fname, &bufptr, MAXPDSTRING, 1);
    if(fd>0){
          fname[strlen(fname)]='/';
          DEBUG(post("image file: %s",fname);)
          close(fd);
          return fname;
    } else {
        return 0;
    }
125 126 127
}

static int imagebang_click(t_imagebang *x, struct _glist *glist,
Ivica Bukvic's avatar
Ivica Bukvic committed
128 129
    int xpos, int ypos, int shift, int alt, int dbl, int doit)
{
130 131 132
    //DEBUG(post("x:%i y:%i dbl:%i doit:%i",xpos,ypos,dbl,doit);)
    if (doit) imagebang_bang(x);
    return (1);
Ivica Bukvic's avatar
Ivica Bukvic committed
133
}
134

135 136 137 138 139 140 141 142 143 144 145 146 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
static void imagebang_drawme(t_imagebang *x, t_glist *glist, int firsttime)
{
    if (firsttime)
    {
        char key_a[MAXPDSTRING];
        char key_b[MAXPDSTRING];
        //DEBUG(post("Rendering: \n   %x_imagebang:%s \n   %x_imagebang:%s",x->image_a,x->image_a->s_name,x->image_b,x->image_b->s_name);)

        //sys_vgui(".x%x.c create image %d %d -anchor nw -image %x_imagebang -disabledimage %x_imagebang -tags %ximage\n", 
        //    glist_getcanvas(glist),
        //    text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),x->image_a,x->image_b,x);

        //sys_vgui("pd [concat %s _imagesize [image width %x_imagebang] [image height %x_imagebang] \\;]\n",x->receive->s_name,x->image_a,x->image_a);

        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));
        sprintf(key_a, "%lx_a", (long unsigned int)x);
        gui_vmess("gui_gobj_draw_image", "xxss",
            glist_getcanvas(glist),
            x,
            key_a,
            "nw");
        gui_vmess("gui_image_size_callback", "xss",
            glist_getcanvas(glist), key_a, x->receive->s_name);
    }
    else
    {
        //sys_vgui(".x%x.c coords %ximage %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));
    }
176 177 178 179
}

void imagebang_erase(t_imagebang* x,t_glist* glist)
{
180 181 182 183
    int n;
    sys_vgui(".x%x.c delete %ximage\n",
        glist_getcanvas(glist), x);
    gui_vmess("gui_gobj_erase", "xx", glist_getcanvas(glist), x);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
}


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


static void imagebang_getrect(t_gobj *z, t_glist *glist,
    int *xp1, int *yp1, int *xp2, int *yp2)
{
    int width, height;
    t_imagebang* x = (t_imagebang*)z;

    width = x->width;
    height = x->height;
    *xp1 = text_xpix(&x->x_obj, glist);
    *yp1 = text_ypix(&x->x_obj, glist);
    *xp2 = text_xpix(&x->x_obj, glist) + width;
    *yp2 = text_ypix(&x->x_obj, glist) + height;
202
    //fprintf(stderr,"%d %d %d %d\n", *xp1, *yp1, *xp2, *yp2);
203 204 205 206 207 208 209 210
}

static void imagebang_displace(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
    t_imagebang *x = (t_imagebang *)z;
    x->x_obj.te_xpix += dx;
    x->x_obj.te_ypix += dy;
211 212 213 214
    //sys_vgui(".x%x.c coords %xSEL %d %d %d %d\n",
    //       glist_getcanvas(glist), x,
    //       text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
    //       text_xpix(&x->x_obj, glist) + x->width, text_ypix(&x->x_obj, glist) + x->height);
215 216 217 218 219

    imagebang_drawme(x, glist, 0);
    canvas_fixlinesfor(glist,(t_text*) x);
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
static void imagebang_displace_wtag(t_gobj *z, t_glist *glist,
    int dx, int dy)
{
    //fprintf(stderr,"image displace_wtag\n");
    t_imagebang *x = (t_imagebang *)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",
           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);

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

236 237
static void imagebang_select(t_gobj *z, t_glist *glist, int state)
{
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    t_imagebang *x = (t_imagebang *)z;
    if (state) {
     sys_vgui(".x%x.c create rectangle \
           %d %d %d %d -tags %xSEL -outline $select_color\n",
           glist_getcanvas(glist),
           text_xpix(&x->x_obj, glist), text_ypix(&x->x_obj, glist),
           text_xpix(&x->x_obj, glist) + x->width,
           text_ypix(&x->x_obj, glist) + x->height,
           x);
        gui_vmess("gui_gobj_select", "xx", glist_getcanvas(glist), x);
    }
    else {
        sys_vgui(".x%x.c delete %xSEL\n",
            glist_getcanvas(glist), x);
        gui_vmess("gui_gobj_deselect", "xx", glist_getcanvas(glist), x);
    }
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
}

static void imagebang_activate(t_gobj *z, t_glist *glist, int state)
{
/*    t_text *x = (t_text *)z;
    t_rtext *y = glist_findrtext(glist, x);
    if (z->g_pd != gatom_class) rtext_activate(y, state);*/
}

static void imagebang_delete(t_gobj *z, t_glist *glist)
{
    t_text *x = (t_text *)z;
    //canvas_deletelinesfor(glist_getcanvas(glist), x);
    canvas_deletelinesfor(glist, x);
}
Ivica Bukvic's avatar
Ivica Bukvic committed
269
      
270 271 272
static void imagebang_vis(t_gobj *z, t_glist *glist, int vis)
{
    t_imagebang* s = (t_imagebang*)z;
273 274 275 276
    if (vis)
        imagebang_drawme(s, glist, 1);
    else
        imagebang_erase(s,glist);
277 278 279 280 281 282 283 284
}

static void imagebang_size(t_imagebang* x,t_floatarg w,t_floatarg h) {
     x->width = w;
     x->height = h;
}

static void imagebang_imagesize_callback(t_imagebang *x, t_float w, t_float h) {
285 286 287 288 289
    DEBUG(post("received w %f h %f",w,h);)
    post("received w %f h %f",w,h);
    x->width = w;
    x->height = h;
    canvas_fixlinesfor(x->glist,(t_text*) x);
290
}
291
    
292
static void imagebang_free(t_imagebang *x) {
293 294 295
    char key_a[MAXPDSTRING];
    char key_b[MAXPDSTRING]; 
    // check first if variable has been unset and image is unused
296 297 298 299 300 301 302 303 304 305
    // then delete image and unset variable
     DEBUG(sys_vgui("pd [concat DEBUG b in use [image inuse %x_imagebang] \\;]\n",x->image_b);)
     DEBUG(sys_vgui("pd [concat DEBUG a in use [image inuse %x_imagebang] \\;]\n",x->image_a);)
    
    sys_vgui("if { [info exists %x_imagebang] == 1 && [image inuse %x_imagebang] == 0} { image delete %x_imagebang \n unset %x_imagebang\n} \n",x->image_b,x->image_b,x->image_b,x->image_b);
    sys_vgui("if { [info exists %x_imagebang] == 1 && [image inuse %x_imagebang] == 0} { image delete %x_imagebang \n unset %x_imagebang\n} \n",x->image_a,x->image_a,x->image_a,x->image_a);
    
    DEBUG(sys_vgui("pd [concat DEBUG b exists [info exists %x_imagebang] \\;]\n",x->image_b);)
     DEBUG(sys_vgui("pd [concat DEBUG a exists [info exists %x_imagebang] \\;]\n",x->image_a);)
    
306 307
    sprintf(key_a, "%lx_a", (long unsigned int)x);
    sprintf(key_b, "%lx_b", (long unsigned int)x);
308 309
    gui_vmess("gui_image_free", "s", key_a);
    gui_vmess("gui_image_free", "s", key_b);
310
    if (x->receive) {
311 312 313 314
        pd_unbind(&x->x_obj.ob_pd,x->receive);
    }
    clock_free(x->clock_flash);
    clock_free(x->clock_brk);
315 316
    
}
317
    
318 319 320
static void *imagebang_new(t_symbol *s, int argc, t_atom *argv)
{
    t_imagebang *x = (t_imagebang *)pd_new(imagebang_class);
321 322 323 324 325 326 327 328 329 330 331 332
    char key_a[MAXPDSTRING];
    char key_b[MAXPDSTRING];
    t_atom sane_defaults[2];
    if (!argc)
    {
        post("imagebang: warning: no arguments provided: setting defaults to "
             "[imagebang dummy dummy]");
        SETSYMBOL(sane_defaults, gensym("dummy"));
        SETSYMBOL(sane_defaults+1, gensym("dummy"));
        argc = 2;
        argv = sane_defaults;
    }
333 334 335 336
    x->glist = (t_glist*) canvas_getcurrent();
    // Set up a callback to get the size
    x->width = 10;
    x->height = 10;
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442

    x->flashing = 0;

    x->image_a = NULL;
    x->image_b = NULL;

    t_symbol* image_a = NULL;
    t_symbol* image_b = NULL;

    const char *fname;

    // CREATE IMAGES
    // images are only created if they have not been created yet
    // we use the symbol pointer to distinguish between image files

    if ( argc && (argv)->a_type == A_SYMBOL )
    {
        image_a= atom_getsymbol(argv);
        sprintf(key_a, "%lx_a", (long unsigned int)x);

        // Get image file path
        fname = imagebang_get_filename(x,image_a->s_name);
        if (fname)
        {
            x->image_a = gensym(fname);
            //sys_vgui("set %x_a \"%s\" \n",x,fname);
            // Create the image only if the class has not already loaded the
            // same image (with the same symbolic path name)
            //sys_vgui("if { [info exists %x_imagebang] == 0 } "
            //         "{ image create photo %x_imagebang "
            //         "-file \"%s\"\n set %x_imagebang 1\n} \n",
            //             x->image_a,x->image_a,fname,x->image_a); 
            //sys_vgui("pd [concat test %x_imagebang \\;]\n",x->image_a);
            gui_vmess("gui_load_image", "xss",
                canvas_getcurrent(),
                key_a,
                fname);
        }
        else
        {
            x->image_a = &s_;
            pd_error(x, "could not find image \"%s\": using default image",
                image_a->s_name);
            gui_vmess("gui_load_default_image", "xs",
                canvas_getcurrent(),
                key_a);
        }
    }
    if ( argc > 1 && (argv+1)->a_type == A_SYMBOL )
    {
        image_b= atom_getsymbol(argv+1);
        sprintf(key_b, "%lx_b", (long unsigned int)x);

        // Get image file path
        fname = imagebang_get_filename(x,image_b->s_name);
        if (fname)
        {
            x->image_b = gensym(fname);
            //sys_vgui("set %x_b \"%s\" \n",x,fname);
            //sys_vgui("if { [info exists %x_imagebang] == 0} "
            //         "{ image create photo %x_imagebang "
            //         "-file \"%s\"\n set %x_imagebang 1\n} \n",
            //             x->image_b,x->image_b,fname,x->image_b);
            //sys_vgui("pd [concat test %x_imagebang \\;]\n",x->image_b);
            gui_vmess("gui_load_image", "xss",
                canvas_getcurrent(),
                key_b,
                fname);
        }
        else
        {
            x->image_b = &s_;
            pd_error(x, "could not find image \"%s\": using default image",
                image_b->s_name);
            gui_vmess("gui_load_default_image", "xs",
                canvas_getcurrent(),
                key_b);
        }
    }

    // Stop if no images    
    //if ( x->image_a == NULL || x->image_b == NULL)
    //{
    //    post("Could not create [imagebang]... "
    //         "either no gif images defined or found!");
    //    return NULL;
    //}

    x->send = NULL;
    if ( argc > 2 && (argv+2)->a_type == A_SYMBOL )
    {
        x->send = atom_getsymbol(argv+2);
    }

    if ( argc > 3 && (argv+3)->a_type == A_SYMBOL )
    {
        x->receive = atom_getsymbol(argv+3);
    }
    else
    {
       // Create default receiver if none set
        char buf[MAXPDSTRING];
        sprintf(buf, "#%lx", (long)x);
        x->receive = gensym(buf);
    }

443
    pd_bind(&x->x_obj.ob_pd, x->receive );
444 445 446 447 448

    x->clock_flash = clock_new(x, (t_method)imagebang_flash_timeout);
    x->clock_brk = clock_new(x, (t_method)imagebang_brk_timeout);
    x->outlet = outlet_new(&x->x_obj, &s_float);
    return (x);
449 450 451 452
}

void imagebang_setup(void)
{
Ivica Bukvic's avatar
Ivica Bukvic committed
453
    imagebang_class = class_new(gensym("imagebang"),
454 455
        (t_newmethod)imagebang_new, (t_method)imagebang_free,
        sizeof(t_imagebang),0, A_GIMME,0);
456

457 458
    class_addmethod(imagebang_class, (t_method)imagebang_imagesize_callback,
        gensym("_imagesize"), A_DEFFLOAT, A_DEFFLOAT, 0);
459 460 461

    class_addbang(imagebang_class,(t_method)imagebang_bang);
    
462 463 464 465 466 467 468
    imagebang_widgetbehavior.w_getrectfn =    imagebang_getrect;
    imagebang_widgetbehavior.w_displacefn =    imagebang_displace;
    imagebang_widgetbehavior.w_selectfn =    imagebang_select;
    imagebang_widgetbehavior.w_activatefn =    imagebang_activate;
    imagebang_widgetbehavior.w_deletefn =    imagebang_delete;
    imagebang_widgetbehavior.w_visfn =        imagebang_vis;
    imagebang_widgetbehavior.w_displacefnwtag =    imagebang_displace_wtag;
469 470 471 472 473

    imagebang_widgetbehavior.w_clickfn = (t_clickfn)imagebang_click;
    

#if PD_MINOR_VERSION < 37
474
    imagebang_widgetbehavior.w_propertiesfn = NULL; 
Ivica Bukvic's avatar
Ivica Bukvic committed
475
    //imagebang_widgetbehavior.w_savefn = imagebang_save;
476 477 478 479 480 481 482 483
#endif
    
    class_setwidget(imagebang_class,&imagebang_widgetbehavior);
#if PD_MINOR_VERSION >= 37
   // class_setsavefn(imagebang_class,&imagebang_save);
#endif
}