From f1ce936be8d5d9483b3f0cfad44b7d2e2b0bf42e Mon Sep 17 00:00:00 2001
From: Ivica Ico Bukvic <ico@vt.edu>
Date: Mon, 17 Mar 2014 15:12:00 -0400
Subject: [PATCH] *arrays are not transformable *cannot create two structs on
 the same canvas (otherwise how would you know which one you are talking to)?
 *fixed regression where arbitrary font sizes were not honored by the
 drawsymbols and drawnumbers *other minor clean-ups *adjusted help file to
 reflect the new approach to font sizes

---
 .../disis_wiimote/disis_wiimote-help.pd       | 48 ++++++------
 pd/src/g_canvas.h                             |  5 ++
 pd/src/g_scalar.c                             |  9 ++-
 pd/src/g_template.c                           | 75 +++++++++++++++++--
 4 files changed, 103 insertions(+), 34 deletions(-)

diff --git a/l2ork_addons/disis_wiimote/disis_wiimote-help.pd b/l2ork_addons/disis_wiimote/disis_wiimote-help.pd
index fd9d0a76a..d2cab5a3b 100644
--- a/l2ork_addons/disis_wiimote/disis_wiimote-help.pd
+++ b/l2ork_addons/disis_wiimote/disis_wiimote-help.pd
@@ -77,9 +77,9 @@ case. The total acceleration is given by:;
 #X msg 122 488 traverse pd-IR-data \, next;
 #X obj 134 63 route 0 1 2 3;
 #X obj 23 482 unpack 0 0 0;
-#X floatatom 483 44 5 0 0 0 IR-Blob: - -;
-#X floatatom 522 44 5 0 0 0 - - -;
-#X floatatom 562 44 5 0 0 0 - - -;
+#X floatatom 483 44 5 0 0 0 IR-Blob: - -, f 5;
+#X floatatom 522 44 5 0 0 0 - - -, f 5;
+#X floatatom 562 44 5 0 0 0 - - -, f 5;
 #X obj 15 533 set IR-blobs x y;
 #X obj 231 400 pointer;
 #X obj 132 372 unpack 0 0 0;
@@ -163,9 +163,9 @@ case. The total acceleration is given by:;
 #X restore 106 444 pd \$0-nunchuck-stuff;
 #X obj 579 406 cnv 15 275 230 empty empty Expansion: 10 15 0 14 -228992
 -355 0;
-#X floatatom 718 468 7 0 0 1 - #0-nc-accX-set #0-nc-accX;
-#X floatatom 718 482 7 0 0 1 - #0-nc-accY-set #0-nc-accY;
-#X floatatom 718 496 7 0 0 1 - #0-nc-accZ-set #0-nc-accZ;
+#X floatatom 718 468 7 0 0 1 - #0-nc-accX-set #0-nc-accX, f 7;
+#X floatatom 718 482 7 0 0 1 - #0-nc-accY-set #0-nc-accY, f 7;
+#X floatatom 718 496 7 0 0 1 - #0-nc-accZ-set #0-nc-accZ, f 7;
 #X obj 588 468 hsl 128 14 -1 1 0 0 \$0-nc-accX \$0-nc-accX-set X 4
 8 0 10 -261681 -1 -1 0 1;
 #X obj 588 482 hsl 128 14 -1 1 0 0 \$0-nc-accY \$0-nc-accY-set Y 4
@@ -173,7 +173,7 @@ case. The total acceleration is given by:;
 #X obj 588 496 hsl 128 14 -1 1 0 0 \$0-nc-accZ \$0-nc-accZ-set Z 4
 8 0 10 -261681 -1 -1 0 1;
 #X obj 723 530 nbx 3 16 -1e+37 1e+37 0 0 empty \$0-nc-btn-set empty
--80 8 0 10 -261681 -1 -1 0 256;
+-80 8 0 10 -261681 -1 -1 0 256 0;
 #X text 584 452 Acceleration:;
 #N canvas 154 209 610 221 NC-stick 0;
 #X obj 39 34 struct NC-stick float x float y;
@@ -184,9 +184,9 @@ case. The total acceleration is given by:;
 #X text 785 452 Stick:;
 #X obj 579 5 cnv 15 275 400 empty empty Wiimote: 10 15 0 14 -261689
 -143491 0;
-#X floatatom 742 80 7 0 0 1 _X #0-wii-accX-set #0-wii-accX;
-#X floatatom 742 94 7 0 0 1 _Y #0-wii-accY-set #0-wii-accY;
-#X floatatom 742 108 7 0 0 1 _Z #0-wii-accZ-set #0-wii-accZ;
+#X floatatom 742 80 7 0 0 1 _X #0-wii-accX-set #0-wii-accX, f 7;
+#X floatatom 742 94 7 0 0 1 _Y #0-wii-accY-set #0-wii-accY, f 7;
+#X floatatom 742 108 7 0 0 1 _Z #0-wii-accZ-set #0-wii-accZ, f 7;
 #X obj 612 80 hsl 128 14 -1 1 0 0 \$0-wii-accX \$0-wii-accX-set empty
 -2 -8 0 10 -225271 -1 -1 0 1;
 #X obj 612 94 hsl 128 14 -1 1 0 0 \$0-wii-accY \$0-wii-accY-set empty
@@ -200,23 +200,25 @@ case. The total acceleration is given by:;
 #X scalar IR-blobs 419 760 blob4 \;;
 #X coords 0 0 1024 768 256 196 1;
 #X restore 590 183 pd IR-data;
-#N canvas 303 253 342 204 IR-blobs 0;
+#N canvas 136 244 342 204 IR-blobs 0;
 #X obj 39 34 struct IR-blobs float x float y symbol s;
 #X obj 39 71 filledcurve 9 30 0 0 -30 -30 0 0 30 30 0;
-#X obj 65 109 drawsymbol s -60 -75 0 1 blob;
+#X obj 63 109 drawsymbol s -35 -75 0 24 blob;
 #X restore 776 384 pd IR-blobs;
 #X text 648 166 IR Blobs (1024x768):;
-#X floatatom 742 128 7 0 0 1 _PITCH #0-wii-pitch-set #0-wii-pitch;
+#X floatatom 742 128 7 0 0 1 _PITCH #0-wii-pitch-set #0-wii-pitch,
+f 7;
 #X obj 612 128 hsl 128 14 -3.14156 3.14159 0 0 \$0-wii-pitch \$0-wii-pitch-set
 empty -2 -8 0 10 -225280 -1 -1 0 1;
-#X floatatom 742 142 7 0 0 1 _ROLL #0-wii-roll-set #0-wii-roll;
+#X floatatom 742 142 7 0 0 1 _ROLL #0-wii-roll-set #0-wii-roll, f
+7;
 #X obj 612 142 hsl 128 14 -3.14156 3.14159 0 0 \$0-wii-roll \$0-wii-roll-set
 empty -2 -8 0 10 -225280 -1 -1 0 1;
 #X text 608 64 Acceleration (-1 to 1):;
 #X obj 779 23 nbx 2 16 -1e+37 1e+37 0 0 empty \$0-wii-btn01-set empty
--100 10 0 12 -225280 -1 -1 0 256;
+-100 10 0 12 -225280 -1 -1 0 256 0;
 #X obj 814 23 nbx 2 16 -1e+37 1e+37 0 0 empty \$0-wii-btn02-set empty
-0 10 0 12 -225280 -1 -1 0 256;
+0 10 0 12 -225280 -1 -1 0 256 0;
 #N canvas 0 0 330 216 \$0-button-stuff 0;
 #X obj 41 33 inlet;
 #X obj 41 86 unpack 0 0;
@@ -383,11 +385,11 @@ balance battery;
 #X text 166 63 <- You can connect to a specific address;
 #X msg 6 366 status;
 #X msg 6 344 debug;
-#X floatatom 317 409 5 0 0 0 - - -;
-#X floatatom 317 427 5 0 0 0 - - -;
-#X floatatom 283 409 5 0 0 0 - - -;
-#X floatatom 283 427 5 0 0 0 - - -;
-#X floatatom 391 407 5 0 0 0 - - -;
+#X floatatom 317 409 5 0 0 0 - - -, f 5;
+#X floatatom 317 427 5 0 0 0 - - -, f 5;
+#X floatatom 283 409 5 0 0 0 - - -, f 5;
+#X floatatom 283 427 5 0 0 0 - - -, f 5;
+#X floatatom 391 407 5 0 0 0 - - -, f 5;
 #X text 263 441 balance board (kg);
 #X text 425 400 battery level;
 #X text 423 412 (click on the status msg);
@@ -431,8 +433,8 @@ balance battery;
 1 4 0;
 #X restore 356 174 pd CL-R-stick;
 #X obj 690 3 unpack 0 0;
-#X floatatom 690 25 5 0 0 0 - - -;
-#X floatatom 747 25 5 0 0 0 - - -;
+#X floatatom 690 25 5 0 0 0 - - -, f 5;
+#X floatatom 747 25 5 0 0 0 - - -, f 5;
 #N canvas 185 133 481 416 CL-R-data 0;
 #X scalar CL-R-stick 15 15 \;;
 #X coords 0 30 30 0 80 80 1 0 0;
diff --git a/pd/src/g_canvas.h b/pd/src/g_canvas.h
index 89e582ebf..060ce24a1 100644
--- a/pd/src/g_canvas.h
+++ b/pd/src/g_canvas.h
@@ -262,6 +262,11 @@ typedef struct _template
     t_pd t_pdobj;               /* header */
     struct _gtemplate *t_list;  /* list of "struct"/gtemplate objects */
     t_symbol *t_sym;            /* name */
+    int t_transformable;        /* counts number of arrays in template
+                                   drawn objects that depend on this
+                                   template can only be transformed
+                                   (scaled/skewed,rotated, etc.)
+                                   if this var is 0 */
     int t_n;                    /* number of dataslots (fields) */
     t_dataslot *t_vec;          /* array of dataslots */
 } t_template;
diff --git a/pd/src/g_scalar.c b/pd/src/g_scalar.c
index 6d9f5a222..f46452f18 100644
--- a/pd/src/g_scalar.c
+++ b/pd/src/g_scalar.c
@@ -293,7 +293,7 @@ void scalar_drawselectrect(t_scalar *x, t_glist *glist, int state)
 */
 void scalar_select(t_gobj *z, t_glist *owner, int state)
 {
-    //fprintf(stderr,"scalar_select %d\n", state);
+    fprintf(stderr,"scalar_select %d\n", state);
     t_scalar *x = (t_scalar *)z;
     t_template *tmpl;
     t_symbol *templatesym = x->sc_template;
@@ -305,6 +305,7 @@ void scalar_select(t_gobj *z, t_glist *owner, int state)
     SETPOINTER(&at, &gp);
     if (tmpl = template_findbyname(templatesym))
     {
+        fprintf(stderr,"blah\n");
         template_notify(tmpl, (state ? gensym("select") : gensym("deselect")),
             1, &at);
         templatecanvas = template_findcanvas(tmpl);
@@ -342,9 +343,9 @@ void scalar_select(t_gobj *z, t_glist *owner, int state)
         x->sc_selected = 0;
         sys_vgui(".x%lx.c dtag blankscalar%lx selected\n",
             glist_getcanvas(owner), x);
-                sys_vgui(".x%lx.c dtag .x%lx.x%lx.template%lx selected\n",
-                    glist_getcanvas(owner), glist_getcanvas(owner),
-                    owner, x->sc_vec);
+        sys_vgui(".x%lx.c dtag .x%lx.x%lx.template%lx selected\n",
+            glist_getcanvas(owner), glist_getcanvas(owner),
+            owner, x->sc_vec);
         /* how do we navigate through a t_word list?
         if (x->sc_vec)
         {
diff --git a/pd/src/g_template.c b/pd/src/g_template.c
index 0530ba123..a33a53d5d 100644
--- a/pd/src/g_template.c
+++ b/pd/src/g_template.c
@@ -65,6 +65,7 @@ t_template *template_new(t_symbol *templatesym, int argc, t_atom *argv)
 {
     t_template *x = (t_template *)pd_new(template_class);
     x->t_n = 0;
+    x->t_transformable = 0;
     x->t_vec = (t_dataslot *)t_getbytes(0);
     while (argc > 0)
     {
@@ -574,6 +575,17 @@ another one to add new fields, for example. */
 
 static void *gtemplate_donew(t_symbol *sym, int argc, t_atom *argv)
 {
+    t_canvas *cur = canvas_getcurrent();
+    t_gobj *gob = cur->gl_list;
+    while (gob)
+    {
+        if (pd_class(&gob->g_pd) == gtemplate_class)
+        {
+            error("%s: only one struct allowed per canvas.", cur->gl_name->s_name);
+            return(0);
+        }
+        gob = gob->g_next;
+    }
     t_gtemplate *x = (t_gtemplate *)pd_new(gtemplate_class);
     t_template *t = template_findbyname(sym);
     int i;
@@ -1180,6 +1192,9 @@ static void *draw_new(t_symbol *classsym, t_int argc, t_atom *argv)
 
     pd_bind(&x->x_obj.ob_pd, gensym(buf));
     
+    // hack of unprecedented proportions that doesn't even work
+    //sys_queuegui((t_gobj *)x->x_canvas, 0, (t_guicallbackfn)canvas_redrawallfortemplatecanvas);
+    
     return (x);
 }
 
@@ -1717,7 +1732,8 @@ void draw_doupdatetransform(t_draw *x, t_canvas *c)
     draw_mset(mtx1, 0, 0, 0, 0, 0, 0);
     draw_mset(mtx2, 0, 0, 0, 0, 0, 0);
     t_gobj *g;
-    t_template *template;
+    t_template *template = NULL;
+    t_template *warn_template = NULL;
     t_canvas *visible = c;
     while(visible->gl_isgraph && visible->gl_owner)
         visible = visible->gl_owner;
@@ -1736,9 +1752,14 @@ void draw_doupdatetransform(t_draw *x, t_canvas *c)
         if (glist_isvisible(c) && g->g_pd == scalar_class &&
             x->x_canvas ==
             template_findcanvas((template = template_findbyname(
-                (((t_scalar *)g)->sc_template))))
+                (((t_scalar *)g)->sc_template)))) &&
+            template->t_transformable == 0
            )
         {
+            //fprintf(stderr,"draw_doupdatetransform > template:%lx(%s)
+            //    transform:%d\n", (t_int)template,
+            //    ((t_scalar *)g)->sc_template->s_name,
+            //    template->t_transformable);
             t_float m1, m2, m3, m4, m5, m6;
             draw_parsetransform(x, template, ((t_scalar *)g)->sc_vec,
                 &m1, &m2, &m3, &m4, &m5, &m6);
@@ -1760,6 +1781,12 @@ void draw_doupdatetransform(t_draw *x, t_canvas *c)
                 scalar_drawselectrect((t_scalar *)g, c, 1);
             }
             sys_vgui("pdtk_canvas_getscroll .x%lx.c\n", visible);
+        } 
+        else if (template != NULL && warn_template != template)
+        {
+            /* this is not a transformable template, warn user of that */
+            post("warning: transform ignored on the template %s because it includes an array", template->t_sym->s_name);
+            warn_template = template;
         }
         if (g->g_pd == canvas_class)
         {
@@ -2954,7 +2981,6 @@ static t_gpointer draw_motion_gpointer;
 
 static void draw_motion(void *z, t_floatarg dx, t_floatarg dy)
 {
-    //fprintf(stderr,"draw_motion\n");
     t_draw *x = (t_draw *)z;
     t_float mtx1[3][3];
     t_float mtx2[3][3];
@@ -3673,6 +3699,18 @@ static void *plot_new(t_symbol *classsym, t_int argc, t_atom *argv)
     t_plot *x = (t_plot *)pd_new(plot_class);
     int defstyle = PLOTSTYLE_POLY;
     x->x_canvas = canvas_getcurrent();
+    //fprintf(stderr,"plot new %s\n",
+    //    (canvas_makebindsym(x->x_canvas->gl_name))->s_name);
+    t_template *t = template_findbyname(
+        canvas_makebindsym(x->x_canvas->gl_name));
+    if (t)
+    {
+        /* increment variable of the template
+           to prevent transform as that would
+           make arrays break their hitboxes
+           and all kinds of other bad stuff */
+        t->t_transformable++;
+    }
 
     fielddesc_setfloat_var(&x->x_xpoints, gensym("x"));
     fielddesc_setfloat_var(&x->x_ypoints, gensym("y"));
@@ -4384,6 +4422,7 @@ static void plot_vis(t_gobj *z, t_glist *glist, t_scalar *sc,
     else
     {
         /* un-draw the individual points */
+        //fprintf(stderr,"plot_vis UNVIS\n");
         if (scalarvis != 0)
         {
             int i;
@@ -4437,6 +4476,28 @@ static int plot_click(t_gobj *z, t_glist *glist,
     else return (0);
 }
 
+extern int we_are_undoing;
+
+static void plot_free(t_plot *x)
+{
+    //fprintf(stderr,"plot_free\n");
+    //sys_queuegui(x->x_canvas, 0, canvas_redrawallfortemplatecanvas);
+    /* decrement variable of the template
+       to prevent transform as that would
+       make arrays break their hitboxes
+       and all kinds of other bad stuff */
+    t_template *t = template_findbyname(
+        canvas_makebindsym(x->x_canvas->gl_name)
+    );
+    if (t)
+    {
+        t->t_transformable--;
+        //fprintf(stderr,"plot_free > template:%lx(%s) transform:%d\n",
+        //    (t_int)t, canvas_makebindsym(x->x_canvas->gl_name)->s_name,
+        //    t->t_transformable);
+    }
+}
+
 t_parentwidgetbehavior plot_widgetbehavior =
 {
     plot_getrect,
@@ -4449,8 +4510,8 @@ t_parentwidgetbehavior plot_widgetbehavior =
 
 static void plot_setup(void)
 {
-    plot_class = class_new(gensym("plot"), (t_newmethod)plot_new, 0,
-        sizeof(t_plot), 0, A_GIMME, 0);
+    plot_class = class_new(gensym("plot"), (t_newmethod)plot_new,
+        (t_method)plot_free, sizeof(t_plot), 0, A_GIMME, 0);
     class_setdrawcommand(plot_class);
     class_addfloat(plot_class, plot_float);
     class_setparentwidget(plot_class, &plot_widgetbehavior);
@@ -4656,7 +4717,7 @@ static void drawnumber_vis(t_gobj *z, t_glist *glist, t_scalar *sc,
                 glist_getcanvas(glist), xloc, sys_font, sys_hostfontsize(fontsize), yloc, colorstring, buf);
         /* have to remove fontweight for the time being... */
         sys_vgui(" -fontfamily {%s} -fontsize %d", sys_font,
-                sys_hostfontsize(fontsize));
+                fontsize);
         sys_vgui(" -parent .scalar%lx", data);
         sys_vgui(" -tags {.x%lx.x%lx.template%lx scalar%lx}\n", 
             glist_getcanvas(glist), glist, data, sc);
@@ -5030,7 +5091,7 @@ static void drawsymbol_vis(t_gobj *z, t_glist *glist, t_scalar *sc,
         sys_vgui(".x%lx.c create ptext %d [expr {[font metrics {{%s} %d} -ascent] + %d}] -textanchor start -fill %s -text {%s}\\\n",
                 glist_getcanvas(glist), xloc, sys_font, sys_hostfontsize(fontsize), yloc, colorstring, buf);
         sys_vgui(" -fontfamily {%s} -fontsize %d ", sys_font,
-                sys_hostfontsize(fontsize));
+                fontsize);
         sys_vgui(" -parent .scalar%lx", data);
         sys_vgui(" -tags {.x%lx.x%lx.template%lx scalar%lx}\n", 
             glist_getcanvas(glist), glist, data, sc);
-- 
GitLab