From 0938d6083c39e11fc0458e4d70f4e936bce488b1 Mon Sep 17 00:00:00 2001 From: Thomas O Fredericks Date: Mon, 21 Dec 2009 02:41:34 +0000 Subject: Added imagebang to tof/test svn path=/trunk/externals/tof/; revision=12842 --- test/homea.gif | Bin 0 -> 2315 bytes test/homeb.gif | Bin 0 -> 2336 bytes test/imagebang-help.pd | 49 +++++++ test/imagebang.c | 382 +++++++++++++++++++++++++++++++++++++++++++++++++ test/panda.gif | Bin 0 -> 25576 bytes test/pandb.gif | Bin 0 -> 20679 bytes 6 files changed, 431 insertions(+) create mode 100644 test/homea.gif create mode 100644 test/homeb.gif create mode 100644 test/imagebang-help.pd create mode 100644 test/imagebang.c create mode 100644 test/panda.gif create mode 100644 test/pandb.gif diff --git a/test/homea.gif b/test/homea.gif new file mode 100644 index 0000000..c216f13 Binary files /dev/null and b/test/homea.gif differ diff --git a/test/homeb.gif b/test/homeb.gif new file mode 100644 index 0000000..be83f9d Binary files /dev/null and b/test/homeb.gif differ diff --git a/test/imagebang-help.pd b/test/imagebang-help.pd new file mode 100644 index 0000000..30341ef --- /dev/null +++ b/test/imagebang-help.pd @@ -0,0 +1,49 @@ +#N canvas 162 41 658 621 10; +#X obj 45 338 print; +#X text 27 17 tags: ui; +#X text 26 3 description: a bang with an image; +#X obj 45 152 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X text 27 49 [imagebang] only works with gifs; +#X text 351 116 1) image for the normal state; +#X text 350 133 2) image for the clicked state; +#X text 350 150 3) send name; +#X text 351 165 4) receive name; +#X text 327 192 The first two arguments are required.; +#X text 314 97 Arguments:; +#X obj 45 177 tof/imagebang panda.gif pandb.gif \$0pandas \$0pandar +; +#X text 253 277 <- This [imagebang] was created with the following: +; +#X obj 204 129 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 203 150 s \$0pandar; +#X obj 209 338 r \$0pandas; +#X obj 212 364 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 42 436 tof/imagebang homea.gif homeb.gif \$0homes \$0homer; +#X obj 42 411 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 42 502 print; +#X obj 48 559 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 -1 +-1; +#X obj 47 532 r \$0homes; +#X obj 122 408 bng 15 250 50 0 empty empty empty 17 7 0 10 -262144 +-1 -1; +#X obj 121 429 s \$0homer; +#X text 112 458 <- This [imagebang] was created with the following: +; +#X text 258 299 [tof/imagebang panda.gif pandb.gif \$0pandas \$0pandar] +; +#X text 118 480 [tof/imagebang homea.gif homeb.gif \$0homes \$0homer] +; +#X text 27 31 contact: mrtoftrash@gmail.com; +#X text 28 69 based on [image] by ggee and moonlib; +#X connect 3 0 11 0; +#X connect 11 0 0 0; +#X connect 13 0 14 0; +#X connect 15 0 16 0; +#X connect 17 0 19 0; +#X connect 18 0 17 0; +#X connect 21 0 20 0; +#X connect 22 0 23 0; diff --git a/test/imagebang.c b/test/imagebang.c new file mode 100644 index 0000000..b140cfe --- /dev/null +++ b/test/imagebang.c @@ -0,0 +1,382 @@ +#include +#include +#include +#include +#include + +#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; + int flashing; + t_outlet* outlet; +} t_imagebang; + + +static void imagebang_bang(t_imagebang *x) +{ + + t_glist* glist = glist_getcanvas(x->glist); + if(x->flashing) { + sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n", glist, x,x->image_a); + clock_delay(x->clock_brk, 50); + //x->flashed = 1; + } else { + sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n", glist, x,x->image_b); + x->flashing = 1; + + } + clock_delay(x->clock_flash, 250); + + + outlet_bang(x->outlet); + + if(x->send && x->send->s_thing ) pd_bang(x->send->s_thing); + +} + +static void imagebang_flash_timeout(t_imagebang *x) +{ + t_glist* glist = glist_getcanvas(x->glist); + x->flashing = 0; + sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n", glist, x,x->image_a); + +} + +static void imagebang_brk_timeout(t_imagebang *x) +{ + t_glist* glist = glist_getcanvas(x->glist); + x->flashing = 1; + sys_vgui(".x%x.c itemconfigure %ximage -image %x_imagebang \n", glist, x,x->image_b); + +} + + +/* widget helper functions */ + +static const char* imagebang_get_filename(t_imagebang *x,char *file) { + 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; + } +} + +static int imagebang_click(t_imagebang *x, struct _glist *glist, + int xpos, int ypos, int shift, int alt, int dbl, int doit) { + //DEBUG(post("x:%i y:%i dbl:%i doit:%i",xpos,ypos,dbl,doit);) + if ( doit) imagebang_bang(x) ; + + return (1); + } + + + + +static void imagebang_drawme(t_imagebang *x, t_glist *glist, int firsttime) { + if (firsttime) { + + 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); + + + } 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)); + } + +} + + +void imagebang_erase(t_imagebang* x,t_glist* glist) +{ + int n; + sys_vgui(".x%x.c delete %ximage\n", + glist_getcanvas(glist), x); + +} + + + +/* ------------------------ 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; +} + +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; + 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); + + imagebang_drawme(x, glist, 0); + canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); +} + +static void imagebang_select(t_gobj *z, t_glist *glist, int state) +{ + t_imagebang *x = (t_imagebang *)z; + if (state) { + sys_vgui(".x%x.c create rectangle \ +%d %d %d %d -tags %xSEL -outline blue\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); + } + else { + sys_vgui(".x%x.c delete %xSEL\n", + glist_getcanvas(glist), x); + } + + + +} + + +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); +} + + +static void imagebang_vis(t_gobj *z, t_glist *glist, int vis) +{ + t_imagebang* s = (t_imagebang*)z; + if (vis) + imagebang_drawme(s, glist, 1); + else + imagebang_erase(s,glist); +} + + + + +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) { + DEBUG(post("received w %f h %f",w,h);) + x->width = w; + x->height = h; + canvas_fixlinesfor(glist_getcanvas(x->glist),(t_text*) x); +} + + +static void imagebang_free(t_imagebang *x) { + + // check first if variable has been unset and image is unused + // 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);) + + if (x->receive) { + pd_unbind(&x->x_obj.ob_pd,x->receive); + } + clock_free(x->clock_flash); + clock_free(x->clock_brk); + +} + + +static void *imagebang_new(t_symbol *s, int argc, t_atom *argv) +{ + t_imagebang *x = (t_imagebang *)pd_new(imagebang_class); + + x->glist = (t_glist*) canvas_getcurrent(); + + // Set up a callback to get the size + x->width = 10; + x->height = 10; + + 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); + fname = imagebang_get_filename(x,image_a->s_name); // Get image file path + 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); + } else { + post("Oups... [imagebang] could not find \"%s\"",image_a->s_name); + } + } + + + + if ( argc > 1 && (argv+1)->a_type == A_SYMBOL ) { + image_b= atom_getsymbol(argv+1); + fname = imagebang_get_filename(x,image_b->s_name); // Get image file path + 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); + } else { + post("Oups... [imagebang] could not find \"%s\"",image_b->s_name); + } + } + + // 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); + } + + pd_bind(&x->x_obj.ob_pd, x->receive ); + + 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); + +} + +void imagebang_setup(void) +{ + + + imagebang_class = class_new(gensym("imagebang"), (t_newmethod)imagebang_new, (t_method)imagebang_free, + sizeof(t_imagebang),0, A_GIMME,0); + + class_addmethod(imagebang_class, (t_method)imagebang_imagesize_callback,\ + gensym("_imagesize"), A_DEFFLOAT, A_DEFFLOAT, 0); + + class_addbang(imagebang_class,(t_method)imagebang_bang); + + 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_clickfn = (t_clickfn)imagebang_click; + + +#if PD_MINOR_VERSION < 37 + imagebang_widgetbehavior.w_propertiesfn = NULL; + //imagebang_widgetbehavior.w_savefn = imagebang_save; +#endif + + + class_setwidget(imagebang_class,&imagebang_widgetbehavior); +#if PD_MINOR_VERSION >= 37 + // class_setsavefn(imagebang_class,&imagebang_save); +#endif + +} + + diff --git a/test/panda.gif b/test/panda.gif new file mode 100644 index 0000000..17f52e2 Binary files /dev/null and b/test/panda.gif differ diff --git a/test/pandb.gif b/test/pandb.gif new file mode 100644 index 0000000..2a0ee9b Binary files /dev/null and b/test/pandb.gif differ -- cgit v1.2.1