diff options
-rwxr-xr-x | gui/envgen.c | 625 | ||||
-rwxr-xr-x | gui/envgen.h | 109 | ||||
-rwxr-xr-x | gui/w_envgen.h | 1011 |
3 files changed, 882 insertions, 863 deletions
diff --git a/gui/envgen.c b/gui/envgen.c index 054d836..5df03df 100755 --- a/gui/envgen.c +++ b/gui/envgen.c @@ -1,299 +1,326 @@ -/* (C) Guenter Geiger <geiger@epy.co.at> */
-
-
-#include <m_pd.h>
-#ifdef NT
-#pragma warning( disable : 4244 )
-#pragma warning( disable : 4305 )
-#endif
-
-/* ------------------------ envgen~ ----------------------------- */
-
-#define NONE 0
-#define ATTACK 1
-#define SUSTAIN 3
-#define STATES 100
-
-#include "envgen.h"
-#include "w_envgen.h"
-
-static t_class *envgen_class;
-
-
-char dumpy[2000];
-
-/* initialize envelope with argument vector */
-
-#include <stdio.h>
-
-/*
- envgen crashes frequently when reallocating memory ....
- I really don't know why, it crashes during resizebytes,
- which means it is unable to realloc() the memory ?????
- the pointer seems to be ok, I don't know what else could
- cause the problem. for the moment we prevent from reallocating
- by setting the STATES variable to 100 */
-
-void envgen_resize(t_envgen* x,int ns)
-{
- if (ns > x->args) {
- int newargs = ns*sizeof(t_float);
-
- x->duration = resizebytes(x->duration,x->args*sizeof(t_float),newargs);
- x->finalvalues = resizebytes(x->finalvalues,x->args*sizeof(t_float),newargs);
- x->args = ns;
- }
-}
-
-
-
-void envgen_totaldur(t_envgen* x,t_float dur)
-{
- int i;
- float f = dur/x->duration[x->last_state];
-
- if (dur < 10) {
- post("envgen: duration too small %f",dur);
- return;
- }
-
- for (i=1;i<=x->last_state;i++)
- x->duration[i]*=f;
-}
-
-
-static void envgen_dump(t_envgen* e)
-{
- t_atom argv[50];
- int argc= 0;
- t_atom* a = argv;
- int i;
-
- SETFLOAT(a,e->finalvalues[0]);argc++;
- for (i=1;i <= e->last_state;i++) {
- SETFLOAT(argv+argc,e->duration[i] - e->duration[i-1]);
- argc++;
- SETFLOAT(argv+argc,e->finalvalues[i]);
- argc++;
- }
- outlet_list(e->out2,&s_list,argc,(t_atom*)&argv);
-
-}
-
-void envgen_init(t_envgen *x,int argc,t_atom* argv)
-{
- t_float* dur;
- t_float* val;
- t_float tdur = 0;
-
- if (!argc) return;
-
- x->duration[0] = 0;
-
- x->last_state = argc>>1;
- envgen_resize(x,argc>>1);
-
- dur = x->duration;
- val = x->finalvalues;
-
- if (argc) {
- *val = atom_getfloat(argv++);
- *dur = 0.0;
- }
- dur++;val++;argc--;
- for (;argc > 0;argc--) {
- tdur += atom_getfloat(argv++);
-#ifdef DEBUG
- post("dur =%f",tdur);
-#endif
- *dur++ = tdur;
- argc--;
- if (argc > 0)
- *val++ = atom_getfloat(argv++);
- else
- *val++ = 0;
-#ifdef DEBUG
- post("val =%f",*(val-1));
-#endif
-
- }
-
-}
-
-
-
-
-
-void envgen_list(t_envgen *x,t_symbol* s, int argc,t_atom* argv)
-{
- envgen_init(x,argc,argv);
- if (glist_isvisible(x->w.glist)) {
- envgen_drawme(x, x->w.glist, 0);
- }
-}
-
-void envgen_setresize(t_envgen *x, t_floatarg f)
-{
- x->resizeable = f;
-}
-
-
-void envgen_float(t_envgen *x, t_floatarg f)
-{
- int state = 0;
- float val;
-
- while (x->duration[state] < f && state < x->last_state) state++;
-
- if (state == 0 || f >= x->duration[x->last_state]) {
- outlet_float(x->x_obj.ob_outlet,x->finalvalues[state]*(x->max-x->min));
- return;
- }
-
- val = x->finalvalues[state-1] +
- (f - x->duration[state-1])*
- (x->finalvalues[state] - x->finalvalues[state-1])/
- (x->duration[state] - x->duration[state-1]);
-
- val *= (x->max - x->min);
- outlet_float(x->x_obj.ob_outlet,val);
-}
-
-
-void envgen_bang(t_envgen *x)
-{
- t_atom a[2];
-
- SETFLOAT(a,x->finalvalues[NONE]);
- SETFLOAT(a+1,0);
- outlet_list(x->x_obj.ob_outlet,&s_list,2,(t_atom*)&a);
-
-/* we don't force the first value anymore, so the first value
- is actually with what we have left off at the end ...
- this reduces clicks
-*/
- x->x_state = ATTACK;
- x->x_val = x->finalvalues[NONE];
-
- SETFLOAT(a,x->finalvalues[x->x_state]*(x->max-x->min));
- SETFLOAT(a+1,x->duration[x->x_state]);
-
- outlet_list(x->x_obj.ob_outlet,&s_list,2,(t_atom*)&a);
- clock_delay(x->x_clock,x->duration[x->x_state]);
-}
-
-
-static void envgen_sustain(t_envgen *x, t_floatarg f)
-{
- if (f > 0 && f < x->last_state)
- x->sustain_state = f;
-}
-
-
-static void envgen_tick(t_envgen* x)
-{
- t_atom a[2];
- x->x_state++;
- if (x->x_state <= x->last_state) {
- float del = x->duration[x->x_state] - x->duration[x->x_state-1];
- clock_delay(x->x_clock,del);
- SETFLOAT(a,x->finalvalues[x->x_state]*(x->max-x->min));
- SETFLOAT(a+1,del);
-
- outlet_list(x->x_obj.ob_outlet,&s_list,2,(t_atom*)&a);
- }
- else
- clock_unset(x->x_clock);
-}
-
-static void envgen_freeze(t_envgen* x, t_floatarg f)
-{
- x->x_freeze = f;
-}
-
-static void *envgen_new(t_symbol *s,int argc,t_atom* argv)
-{
- t_envgen *x = (t_envgen *)pd_new(envgen_class);
-
- x->args = STATES;
- x->finalvalues = getbytes( x->args*sizeof(t_float));
- x->duration = getbytes( x->args*sizeof(t_float));
-#ifdef DEBUG
- post("finalvalues %x",x->finalvalues);
-#endif
- /* widget */
-
- x->w.grabbed = 0;
- x->resizing = 0;
- x->resizeable = 0;
-
- x->w.glist = (t_glist*) canvas_getcurrent();
-
- x->w.width = 200;
- if (argc) x->w.width = atom_getfloat(argv++),argc--;
- x->w.height = 140;
- if (argc) x->w.height = atom_getfloat(argv++),argc--;
- x->max = 1.0;
- if (argc) x->max = atom_getfloat(argv++),argc--;
- x->min = 0.0;
- if (argc) x->min = atom_getfloat(argv++),argc--;
-
- if (argc)
- envgen_init(x,argc,argv);
- else {
- t_atom a[5];
- SETFLOAT(a,0);
- SETFLOAT(a+1,50);
- SETFLOAT(a+2,1);
- SETFLOAT(a+3,50);
- SETFLOAT(a+4,0);
- envgen_init(x,5,a);
- }
-
- x->x_val = 0.0;
- x->x_state = NONE;
- x->sustain_state = SUSTAIN;
- x->x_freeze = 0;
-
- outlet_new(&x->x_obj, &s_float);
- x->out2 = outlet_new(&x->x_obj, &s_float);
-
- x->x_clock = clock_new(x, (t_method) envgen_tick);
- return (x);
-}
-
-
-void envgen_motion(t_envgen *x, t_floatarg dx, t_floatarg dy);
-void envgen_click(t_envgen *x,
- t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl,
- t_floatarg alt);
-void envgen_key(t_envgen *x, t_floatarg f);
-
-
-void envgen_setup(void)
-{
- envgen_class = class_new(gensym("envgen"), (t_newmethod)envgen_new, 0,
- sizeof(t_envgen), 0,A_GIMME,0);
-
- class_addcreator((t_newmethod)envgen_new,gensym("envgen~"),A_GIMME,0);
- class_addfloat(envgen_class, envgen_float);
-
- class_addbang(envgen_class,envgen_bang);
- class_addlist(envgen_class,envgen_list);
- class_addmethod(envgen_class,(t_method)envgen_sustain,gensym("sustain"),A_FLOAT,A_NULL);
-
- class_addmethod(envgen_class, (t_method)envgen_click, gensym("click"),
- A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0);
- class_addmethod(envgen_class, (t_method)envgen_motion, gensym("motion"),
- A_FLOAT, A_FLOAT, 0);
- class_addmethod(envgen_class, (t_method)envgen_key, gensym("key"),
- A_FLOAT, 0);
-
- class_addmethod(envgen_class,(t_method)envgen_totaldur,gensym("duration"),A_FLOAT,NULL);
- class_addmethod(envgen_class,(t_method)envgen_freeze,gensym("freeze"),A_FLOAT,NULL);
- class_addmethod(envgen_class,(t_method)envgen_setresize,gensym("resize"),A_FLOAT,A_NULL);
-
- envgen_setwidget();
- class_setwidget(envgen_class,&envgen_widgetbehavior);
- class_addmethod(envgen_class,(t_method)envgen_dump,gensym("dump"),A_NULL);
-}
+/* (C) Guenter Geiger <geiger@epy.co.at> */ + +#include <m_pd.h> + +//#define DEBUG +/* ------------------------ envgen~ ----------------------------- */ + +#define NONE 0 +#define ATTACK 1 +#define SUSTAIN 3 +#define STATES 100 + +#include "envgen.h" +#include "w_envgen.h" + +static t_class *envgen_class; + + +#define OUT_LIST(x,nr,a) \ + outlet_list(x->x_obj.ob_outlet,&s_list,nr,(t_atom*)&a);\ + if (x->s_sym != &s_ && x->s_sym->s_thing) pd_list(x->s_sym->s_thing, &s_list, nr, (t_atom*)&a); + + +char dumpy[2000]; + +/* initialize envelope with argument vector */ + +#include <stdio.h> + +/* + envgen crashes frequently when reallocating memory .... + I really don't know why, it crashes during resizebytes, + which means it is unable to realloc() the memory ????? + the pointer seems to be ok, I don't know what else could + cause the problem. for the moment we prevent from reallocating + by setting the STATES variable to 100 */ + +void envgen_resize(t_envgen* x,int ns) +{ + if (ns > x->args) { + int newargs = ns*sizeof(t_float); + + x->duration = resizebytes(x->duration,x->args*sizeof(t_float),newargs); + x->finalvalues = resizebytes(x->finalvalues,x->args*sizeof(t_float),newargs); + x->args = ns; + } +} + + + +void envgen_totaldur(t_envgen* x,t_float dur) +{ + int i; + float f = dur/x->duration[x->last_state]; + + if (dur < 10) { + post("envgen: duration too small %f",dur); + return; + } + + for (i=1;i<=x->last_state;i++) + x->duration[i]*=f; +} + + +static void envgen_dump(t_envgen* e) +{ + t_atom argv[50]; + int argc= 0; + t_atom* a = argv; + int i; + + SETFLOAT(a,e->finalvalues[0]);argc++; + for (i=1;i <= e->last_state;i++) { + SETFLOAT(argv+argc,e->duration[i] - e->duration[i-1]); + argc++; + SETFLOAT(argv+argc,e->finalvalues[i]); + argc++; + } + outlet_list(e->out2,&s_list,argc,(t_atom*)&argv); + +} + +void envgen_init(t_envgen *x,int argc,t_atom* argv) +{ + t_float* dur; + t_float* val; + t_float tdur = 0; + + if (!argc) return; + + x->duration[0] = 0; + + x->last_state = argc>>1; + envgen_resize(x,argc>>1); + + dur = x->duration; + val = x->finalvalues; + + if (argc) { + *val = atom_getfloat(argv++); + *dur = 0.0; + } + dur++;val++;argc--; + for (;argc > 0;argc--) { + tdur += atom_getfloat(argv++); +#ifdef DEBUG + post("dur =%f",tdur); +#endif + *dur++ = tdur; + argc--; + if (argc > 0) + *val++ = atom_getfloat(argv++); + else + *val++ = 0; +#ifdef DEBUG + post("val =%f",*(val-1)); +#endif + + } + +} + + + + + +void envgen_list(t_envgen *x,t_symbol* s, int argc,t_atom* argv) +{ + envgen_init(x,argc,argv); + if (glist_isvisible(x->w.glist)) { + envgen_drawme(x, x->w.glist, 0); + } +} + +void envgen_setresize(t_envgen *x, t_floatarg f) +{ + x->resizeable = f; +} + + +void envgen_float(t_envgen *x, t_floatarg f) +{ + int state = 0; + float val; + + while (x->duration[state] < f && state < x->last_state) state++; + + if (state == 0 || f >= x->duration[x->last_state]) { + val = x->finalvalues[state]*(x->max-x->min); + outlet_float(x->x_obj.ob_outlet,f); + if (x->s_sym != &s_) pd_float(x->s_sym->s_thing, f); + return; + } + + val = x->finalvalues[state-1] + + (f - x->duration[state-1])* + (x->finalvalues[state] - x->finalvalues[state-1])/ + (x->duration[state] - x->duration[state-1]); + + val *= (x->max - x->min); + outlet_float(x->x_obj.ob_outlet,val); + if (x->s_sym != &s_) pd_float(x->s_sym->s_thing, val); +} + + +void envgen_bang(t_envgen *x) +{ + t_atom a[2]; + + SETFLOAT(a,x->finalvalues[NONE]); + SETFLOAT(a+1,0); + + OUT_LIST(x,2,a); + +/* we don't force the first value anymore, so the first value + is actually with what we have left off at the end ... + this reduces clicks +*/ + x->x_state = ATTACK; + x->x_val = x->finalvalues[NONE]; + + SETFLOAT(a,x->finalvalues[x->x_state]*(x->max-x->min)); + SETFLOAT(a+1,x->duration[x->x_state]); + + OUT_LIST(x,2,a); + clock_delay(x->x_clock,x->duration[x->x_state]); +} + + +static void envgen_sustain(t_envgen *x, t_floatarg f) +{ + if (f > 0 && f < x->last_state) + x->sustain_state = f; +} + + +static void envgen_tick(t_envgen* x) +{ + t_atom a[2]; + x->x_state++; + if (x->x_state <= x->last_state) { + float del = x->duration[x->x_state] - x->duration[x->x_state-1]; + clock_delay(x->x_clock,del); + SETFLOAT(a,x->finalvalues[x->x_state]*(x->max-x->min)); + SETFLOAT(a+1,del); + OUT_LIST(x,2,a); + } + else + clock_unset(x->x_clock); +} + +static void envgen_freeze(t_envgen* x, t_floatarg f) +{ + x->x_freeze = f; +} + + +static void bindsym(t_pd* x,t_symbol* o,t_symbol* s) +{ + if (o != &s_) pd_unbind(x,o); + o = s; + pd_bind(x,s); +} + +static void *envgen_new(t_symbol *s,int argc,t_atom* argv) +{ + t_envgen *x = (t_envgen *)pd_new(envgen_class); + + x->args = STATES; + x->finalvalues = getbytes( x->args*sizeof(t_float)); + x->duration = getbytes( x->args*sizeof(t_float)); +#ifdef DEBUG + post("finalvalues %x",x->finalvalues); +#endif + /* widget */ + + x->w.grabbed = 0; + x->resizing = 0; + x->resizeable = 1; + + x->w.glist = (t_glist*) canvas_getcurrent(); + + x->w.width = 200; + if (argc) x->w.width = atom_getfloat(argv++),argc--; + x->w.height = 140; + if (argc) x->w.height = atom_getfloat(argv++),argc--; + x->max = 1.0; + if (argc) x->max = atom_getfloat(argv++),argc--; + x->min = 0.0; + if (argc) x->min = atom_getfloat(argv++),argc--; + + x->r_sym = &s_; + if (argc) { + t_symbol* n; + + n = atom_getsymbol(argv++); + bindsym(&x->x_obj.ob_pd,x->r_sym,n); + x->r_sym = n; + argc--; + } + // post("recv %s",x->r_sym->s_name); + + x->s_sym = &s_; + if (argc) x->s_sym = atom_getsymbol(argv++),argc--; + // post("send %s",x->s_sym->s_name); + + if (argc) + envgen_init(x,argc,argv); + else { + t_atom a[5]; + SETFLOAT(a,0); + SETFLOAT(a+1,50); + SETFLOAT(a+2,1); + SETFLOAT(a+3,50); + SETFLOAT(a+4,0); + envgen_init(x,5,a); + } + + x->x_val = 0.0; + x->x_state = NONE; + x->sustain_state = SUSTAIN; + x->x_freeze = 0; + + outlet_new(&x->x_obj, &s_float); + x->out2 = outlet_new(&x->x_obj, &s_float); + + x->x_clock = clock_new(x, (t_method) envgen_tick); + return (x); +} + + +void envgen_motion(t_envgen *x, t_floatarg dx, t_floatarg dy); +void envgen_click(t_envgen *x, + t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl, + t_floatarg alt); +void envgen_key(t_envgen *x, t_floatarg f); + + +void envgen_setup(void) +{ + envgen_class = class_new(gensym("envgen"), (t_newmethod)envgen_new, 0, + sizeof(t_envgen), 0,A_GIMME,0); + + class_addcreator((t_newmethod)envgen_new,gensym("envgen~"),A_GIMME,0); + class_addfloat(envgen_class, envgen_float); + + class_addbang(envgen_class,envgen_bang); + class_addlist(envgen_class,envgen_list); + class_addmethod(envgen_class,(t_method)envgen_sustain,gensym("sustain"),A_FLOAT,A_NULL); + + class_addmethod(envgen_class, (t_method)envgen_click, gensym("click"), + A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, A_FLOAT, 0); + class_addmethod(envgen_class, (t_method)envgen_motion, gensym("motion"), + A_FLOAT, A_FLOAT, 0); + class_addmethod(envgen_class, (t_method)envgen_key, gensym("key"), + A_FLOAT, 0); + + class_addmethod(envgen_class,(t_method)envgen_totaldur,gensym("duration"),A_FLOAT,NULL); + class_addmethod(envgen_class,(t_method)envgen_freeze,gensym("freeze"),A_FLOAT,NULL); + class_addmethod(envgen_class,(t_method)envgen_setresize,gensym("resize"),A_FLOAT,A_NULL); + + envgen_setwidget(); + class_setwidget(envgen_class,&envgen_widgetbehavior); + class_addmethod(envgen_class,(t_method)envgen_dump,gensym("dump"),A_NULL); +} diff --git a/gui/envgen.h b/gui/envgen.h index 905c80a..a3a8721 100755 --- a/gui/envgen.h +++ b/gui/envgen.h @@ -1,53 +1,56 @@ -#ifndef __GG_ENVGEN_H__
-#define __GG_ENVGEN_H__
-
-#include "g_canvas.h"
-
-typedef struct _wenvgen {
- t_glist* glist;
- int width;
- int height;
- int numdoodles;
- int grabbed; /* for moving points */
- int shift; /* move 100th */
- float pointerx;
- float pointery;
- t_clock* numclock;
-} t_wenvgen;
-
-typedef struct _envgen
-{
- t_object x_obj;
-
- t_float x_val;
-
- int x_state;
- int last_state;
- int sustain_state;
- int envchanged;
-
- t_float* finalvalues;
- t_float* duration;
- t_float totaldur;
- t_int args; /* get rid of that */
- t_int resizing;
- t_int resizeable;
-
- t_float min;
- t_float max;
-
- t_clock* x_clock;
- t_float x_freeze;
-
- t_outlet* out2;
- /* widget parameters */
- t_wenvgen w;
-} t_envgen;
-
-
-t_widgetbehavior envgen_widgetbehavior;
-void envgen_drawme(t_envgen *x, t_glist *glist, int firsttime);
-int envgen_set_values(t_envgen * x);
-void envgen_resize(t_envgen* x,int ns);
-
-#endif
+#ifndef __GG_ENVGEN_H__ +#define __GG_ENVGEN_H__ + +#include "g_canvas.h" + +typedef struct _wenvgen { + t_glist* glist; + int width; + int height; + int numdoodles; + int grabbed; /* for moving points */ + int shift; /* move 100th */ + float pointerx; + float pointery; + t_clock* numclock; +} t_wenvgen; + +typedef struct _envgen +{ + t_object x_obj; + + t_float x_val; + + int x_state; + int last_state; + int sustain_state; + int envchanged; + + t_float* finalvalues; + t_float* duration; + t_float totaldur; + t_int args; /* get rid of that */ + t_int resizing; + t_int resizeable; + + t_symbol* r_sym; + t_symbol* s_sym; + + t_float min; + t_float max; + + t_clock* x_clock; + t_float x_freeze; + + t_outlet* out2; + /* widget parameters */ + t_wenvgen w; +} t_envgen; + + +t_widgetbehavior envgen_widgetbehavior; +void envgen_drawme(t_envgen *x, t_glist *glist, int firsttime); +int envgen_set_values(t_envgen * x); +void envgen_resize(t_envgen* x,int ns); + +#endif diff --git a/gui/w_envgen.h b/gui/w_envgen.h index c436947..aa4b8c6 100755 --- a/gui/w_envgen.h +++ b/gui/w_envgen.h @@ -1,511 +1,500 @@ -#include <stdio.h>
-#include <string.h>
-#include <math.h>
-#ifdef NT
-#pragma warning( disable : 4244 )
-#pragma warning( disable : 4305 )
-#define abs fabs
-#endif
-
-#define PD_VERSION_MINOR 32
-#define BACKGROUND "-fill grey"
-#define BACKGROUNDCOLOR "grey"
-#define BORDER 2
-
-#if 0 /* backwards compatiblity */
-#define text_xpix(a,b) a.te_xpos
-#define text_ypix(a,b) a.te_ypos
-#endif
-
-static void draw_inlets(t_envgen *x, t_glist *glist, int firsttime, int nin, int nout)
-{
- int n = nout;
- int nplus, i;
- nplus = (n == 1 ? 1 : n-1);
- for (i = 0; i < n; i++)
- {
- int onset = x->x_obj.te_xpix + (x->w.width-2*BORDER) * i / nplus;
- if (firsttime)
- sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xo%d\n",
- glist_getcanvas(glist),
- onset, x->x_obj.te_ypix + x->w.height - 1 + 2*BORDER,
- onset + IOWIDTH, x->x_obj.te_ypix + x->w.height + 2*BORDER,
- x, i);
- else
- sys_vgui(".x%x.c coords %xo%d %d %d %d %d\n",
- glist_getcanvas(glist), x, i,
- onset, x->x_obj.te_ypix + x->w.height - 1 + 2*BORDER,
- onset + IOWIDTH, x->x_obj.te_ypix + x->w.height + 2*BORDER);
- }
- n = nin;
- nplus = (n == 1 ? 1 : n-1);
- for (i = 0; i < n; i++)
- {
- int onset = x->x_obj.te_xpix + (x->w.width - IOWIDTH) * i / nplus - BORDER;
- if (firsttime)
- sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xi%d\n",
- glist_getcanvas(glist),
- onset, x->x_obj.te_ypix - BORDER,
- onset + IOWIDTH, x->x_obj.te_ypix + 1 - BORDER,
- x, i);
- else
- sys_vgui(".x%x.c coords %xi%d %d %d %d %d\n",
- glist_getcanvas(glist), x, i,
- onset, x->x_obj.te_ypix - BORDER,
- onset + IOWIDTH, x->x_obj.te_ypix + 1 - BORDER);
-
- }
-}
-
-
-
-static int envgen_next_doodle(t_envgen *x, int xpos,int ypos)
-{
- int ret = -1;
- float xscale,yscale;
- int dxpos,dypos;
- float minval = 100000.0;
- float tval;
- int i;
- int insertpos = -1;
-
-
- xscale = x->w.width/x->duration[x->last_state];
- yscale = x->w.height;
-
- dxpos = x->x_obj.te_xpix;/* + BORDER */;
- dypos = x->x_obj.te_ypix + BORDER;
-
- for (i=0;i<=x->last_state;i++) {
- float dx2 = (dxpos + (x->duration[i] * xscale)) - xpos;
- float dy2 = (dypos + yscale - (x->finalvalues[i] * yscale)) - ypos;
-
- dx2*=dx2;
- dy2*=dy2;
- tval = sqrt(dx2+dy2);
-
- if (tval <= minval) {
- minval = tval;
- insertpos = i;
- }
- }
-
- /* decide if we want to make a new one */
- if (minval > /*5*/ 8 && insertpos >= 0 && !x->x_freeze) {
-
- while (((dxpos + (x->duration[insertpos] * xscale)) - xpos) < 0)
- insertpos++;
- while (((dxpos + (x->duration[insertpos-1] * xscale)) - xpos) > 0)
- insertpos--;
-
- if (x->last_state+1 >= x->args)
- envgen_resize(x,x->args+1);
-
- for (i=x->last_state;i>=insertpos;i--) {
- x->duration[i+1] = x->duration[i];
- x->finalvalues[i+1] = x->finalvalues[i];
- }
-
- x->duration[insertpos] = (float)(xpos-dxpos)/x->w.width*x->duration[x->last_state++];
-
- x->w.pointerx = xpos;
- x->w.pointery = ypos;
- }
- else {
- x->w.pointerx = x->x_obj.te_xpix + x->duration[insertpos]*x->w.width/x->duration[x->last_state];
-
-
- x->w.pointery = x->x_obj.te_ypix +
- (1.f - x->finalvalues[insertpos])*x->w.height;
- }
-
- x->w.grabbed = insertpos;
- return insertpos;
-}
-
-static void envgen_create_doodles(t_envgen *x, t_glist *glist)
-{
- float xscale,yscale;
- int xpos,ypos;
- int i;
- char guistr[255];
-
- xscale = x->w.width/x->duration[x->last_state];
- yscale = x->w.height;
-
- xpos = x->x_obj.te_xpix;
- ypos = (int) (x->x_obj.te_ypix + x->w.height);
- for (i=0;i<=x->last_state;i++) {
- sprintf(guistr,".x%x.c create oval %d %d %d %d -tags %xD%d",(unsigned int)glist_getcanvas(glist),
- (int) (xpos+(x->duration[i] * xscale) - 2),
- (int) (ypos - x->finalvalues[i]*yscale - 2),
- (int) (xpos+(x->duration[i] * xscale)+2),
- (int) (ypos - x->finalvalues[i]*yscale + 2),
- (unsigned int)x,i);
-
- if (i == x->w.grabbed) strcat(guistr," -fill red\n");
- else strcat(guistr,"\n");
- sys_vgui("%s",guistr);
- }
- x->w.numdoodles = i;
-}
-
-
-static void envgen_delete_doodles(t_envgen *x, t_glist *glist)
-{
- int i;
- for (i=0;i<=x->w.numdoodles;i++) {
- sys_vgui(".x%x.c delete %xD%d\n",glist_getcanvas(glist),x,i);
- }
-}
-
-static void envgen_update_doodles(t_envgen *x, t_glist *glist)
-{
-
- envgen_delete_doodles(x,glist);
-/* LATER only create new doodles if necessary */
- envgen_create_doodles(x, glist);
-}
-
-
-static void envgen_delnum(t_envgen *x)
-{
- sys_vgui(".x%x.c delete %xT\n",glist_getcanvas(x->w.glist),x);
-}
-
-
-static void envgen_shownum(t_envgen *x)
-{
- float xscale,yscale;
- int xpos,ypos;
- int i= x->w.grabbed;
-
- xscale = x->w.width/x->duration[x->last_state];
- yscale = x->w.height;
-
- xpos = x->x_obj.te_xpix;
- ypos = (int) (x->x_obj.te_ypix + x->w.height);
-
- envgen_delnum(x);
- - sys_vgui(".x%x.c create text %d %d -text %fx%f -tags %xT\n",
- (unsigned int)glist_getcanvas(x->w.glist),
-
- (int) (xpos+(x->duration[i] * xscale) + 12),
- (int) (ypos - x->finalvalues[i]*yscale - 2),
-
- x->finalvalues[i]*(x->max-x->min),
- x->duration[i],
- (unsigned int)x);
- clock_delay(x->w.numclock,700);
-}
-
-
-
-static void envgen_create(t_envgen *x, t_glist *glist)
-{
- int i;
- static char buf[1024];
- float xscale,yscale;
- int xpos,ypos;
- char num[40];
-
- x->w.numclock = clock_new(x, (t_method) envgen_delnum);
- sys_vgui(".x%x.c create rectangle \
-%d %d %d %d -tags %xS "BACKGROUND"\n",
- glist_getcanvas(glist),
- x->x_obj.te_xpix-BORDER, x->x_obj.te_ypix-BORDER,
- x->x_obj.te_xpix + x->w.width+2*BORDER, x->x_obj.te_ypix + x->w.height+2*BORDER,
- x);
-
- xscale = x->w.width/x->duration[x->last_state];
- yscale = x->w.height;
-
- sprintf(buf,".x%x.c create line",(unsigned int)glist_getcanvas(glist));
- xpos = x->x_obj.te_xpix;
- ypos = (int) (x->x_obj.te_ypix + x->w.height);
- for (i=0;i<=x->last_state;i++) {
- sprintf(num," %d %d ",(int)(xpos + x->duration[i]*xscale),
- (int)(ypos - x->finalvalues[i]*yscale));
- strcat(buf,num);
- }
-
- sprintf(num,"-tags %pP\n",x);
- strcat(buf,num);
- sys_vgui("%s",buf);
- envgen_create_doodles(x,glist);
-}
-
-
-static void envgen_update(t_envgen *x, t_glist *glist)
-{
-int i;
- static char buf[1024];
- float xscale,yscale;
- int xpos,ypos;
- char num[40];
-
- sys_vgui(".x%x.c coords %xS \
-%d %d %d %d\n",
- glist_getcanvas(glist), x,
- x->x_obj.te_xpix - BORDER, x->x_obj.te_ypix -BORDER,
- x->x_obj.te_xpix + x->w.width+2*BORDER, x->x_obj.te_ypix + x->w.height+2*BORDER);
-
-
- xscale = x->w.width/x->duration[x->last_state];
- yscale = x->w.height;
-
- sprintf(buf,".x%x.c coords %pP",(unsigned int)glist_getcanvas(glist),x);
- xpos = x->x_obj.te_xpix;
- ypos = (int) (x->x_obj.te_ypix + x->w.height);
- for (i=0;i<=x->last_state;i++) {
- sprintf(num," %d %d ",(int)(xpos + x->duration[i]*xscale),
- (int) (ypos - x->finalvalues[i]*yscale));
- strcat(buf,num);
- }
- strcat(buf,"\n");
- sys_vgui("%s",buf);
- envgen_update_doodles(x,glist);
- draw_inlets(x, glist, 0,1,2);
-}
-
-
-
-void envgen_drawme(t_envgen *x, t_glist *glist, int firsttime)
-{
-
- if (firsttime) envgen_create(x,glist);
- else envgen_update(x,glist);
-
- draw_inlets(x, glist, firsttime, 1,2);
-}
-
-
-
-
-void envgen_erase(t_envgen* x,t_glist* glist)
-{
- int n;
- sys_vgui(".x%x.c delete %xS\n",
- glist_getcanvas(glist), x);
-
- sys_vgui(".x%x.c delete %pP\n",
- glist_getcanvas(glist), x);
-
- - sys_vgui(".x%x.c delete %xi0\n",glist_getcanvas(glist),x);
- sys_vgui(".x%x.c delete %xo0\n",glist_getcanvas(glist),x);
- sys_vgui(".x%x.c delete %xo1\n",glist_getcanvas(glist),x);
- envgen_delete_doodles(x,glist);
-}
-
-
-
-/* ------------------------ envgen widgetbehaviour----------------------------- */
-
-
-static void envgen_getrect(t_gobj *z, t_glist *owner,
- int *xp1, int *yp1, int *xp2, int *yp2)
-{
- int width, height;
- t_envgen* s = (t_envgen*)z;
-
-
- width = s->w.width + 2*BORDER;
- height = s->w.height + 2*BORDER;
- *xp1 = s->x_obj.te_xpix-BORDER;
- *yp1 = s->x_obj.te_ypix-BORDER;
- *xp2 = s->x_obj.te_xpix + width;
- *yp2 = s->x_obj.te_ypix + height;
-}
-
-static void envgen_displace(t_gobj *z, t_glist *glist,
- int dx, int dy)
-{
- t_envgen *x = (t_envgen *)z;
- x->x_obj.te_xpix += dx;
- x->x_obj.te_ypix += dy;
-
- envgen_drawme(x, glist, 0);
- canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x);
-}
-
-static void envgen_select(t_gobj *z, t_glist *glist, int state)
-{
- t_envgen *x = (t_envgen *)z;
- sys_vgui(".x%x.c itemconfigure %xS -fill %s\n", glist,
- x, (state? "blue" : BACKGROUNDCOLOR));
-}
-
-
-static void envgen_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 envgen_delete(t_gobj *z, t_glist *glist)
-{
- t_text *x = (t_text *)z;
- canvas_deletelinesfor(glist_getcanvas(glist), x);
-}
-
-
-static void envgen_vis(t_gobj *z, t_glist *glist, int vis)
-{
- t_envgen* s = (t_envgen*)z;
- if (vis)
- envgen_drawme(s, glist, 1);
- else
- envgen_erase(s,glist);
-}
-
-/* can we use the normal text save function ?? */
-
-static void envgen_save(t_gobj *z, t_binbuf *b)
-{
- t_envgen *x = (t_envgen *)z;
- binbuf_addv(b, "ssiisiiff", gensym("#X"),gensym("obj"),
- (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix,
- gensym("envgen"),x->w.width,x->w.height,x->max,x->min);
- binbuf_addv(b, ";");
-}
-
-
-
-static void envgen_followpointer(t_envgen* x)
-{
- float dur;
-
- float xscale = x->duration[x->last_state]/x->w.width;
-
-
- if ((x->w.grabbed > 0) && (x->w.grabbed < x->last_state)) {
-
- dur = (x->w.pointerx - x->x_obj.te_xpix)*xscale;
- if (dur < x->duration[x->w.grabbed-1])
- dur = x->duration[x->w.grabbed-1];
- if (dur > x->duration[x->w.grabbed+1])
- dur = x->duration[x->w.grabbed+1];
-
- x->duration[x->w.grabbed] = dur;
- }
-
-
- x->finalvalues[x->w.grabbed] = 1.0f - (x->w.pointery - (float)x->x_obj.te_ypix)/(float)x->w.height;
- if (x->finalvalues[x->w.grabbed] < 0.0)
- x->finalvalues[x->w.grabbed]= 0.0;
- else if (x->finalvalues[x->w.grabbed] > 1.0)
- x->finalvalues[x->w.grabbed]= 1.0;
-
-}
-
-
-void envgen_motion(t_envgen *x, t_floatarg dx, t_floatarg dy)
-{
- if (x->w.shift) {
- x->w.pointerx+=dx/1000.f;
- x->w.pointery+=dy/1000.f;
- }
- else
- {
- x->w.pointerx+=dx;
- x->w.pointery+=dy;
- }
-
- if (!x->resizing)
- envgen_followpointer(x);
- else {
- if (x->w.shift) {
- x->w.width+=dx;
- x->w.height+=dy;
- }
- else
- {
- x->w.pointerx+=dx;
- x->w.pointery+=dy;
- }
- }
- envgen_shownum(x);
- envgen_update(x,x->w.glist);
-}
-
-void envgen_key(t_envgen *x, t_floatarg f)
-{
- if (f == 8.0 && x->w.grabbed < x->last_state && x->w.grabbed > 0) {
- int i;
-
- for (i=x->w.grabbed;i<=x->last_state;i++) {
- x->duration[i] = x->duration[i+1];
- x->finalvalues[i] = x->finalvalues[i+1];
- }
-
- x->last_state--;
- x->w.grabbed--;
- envgen_update(x,x->w.glist);
- }
-}
-
-
-
-void envgen_click(t_envgen *x,
- t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl,
- t_floatarg alt)
-{
- /* check if user wants to resize */
- float wxpos = x->x_obj.te_xpix;
- float wypos = (int) (x->x_obj.te_ypix + x->w.height);
-
- envgen_next_doodle(x,xpos,ypos);
-#if (PD_VERSION_MINOR > 31)
- glist_grab(x->w.glist, &x->x_obj.te_g, (t_glistmotionfn) envgen_motion,
- (t_glistkeyfn) envgen_key, xpos, ypos);
-#else
- glist_grab(x->w.glist, &x->x_obj.te_g, xpos, ypos);
-#endif
- x->resizing = 0;
- if (x->resizeable && (xpos > wxpos + x->w.width - 3) &&
- (fabs(ypos -2 - wypos) < 3.)) {
- x->resizing = 1;
- return;
- }
-
- x->w.shift = shift;
- envgen_followpointer(x);
- envgen_shownum(x);
- envgen_update(x,x->w.glist);
-}
-
-
-#if (PD_VERSION_MINOR > 31)
-static int envgen_newclick(t_gobj *z, struct _glist *glist,
- int xpix, int ypix, int shift, int alt, int dbl, int doit)
-{
- if (doit)
- envgen_click((t_envgen *)z, (t_floatarg)xpix, (t_floatarg)ypix,
- (t_floatarg)shift, 0, (t_floatarg)alt);
- return (1);
-}
-#endif
-
-
-
-t_widgetbehavior envgen_widgetbehavior;
-
-void envgen_setwidget(void)
-{
- envgen_widgetbehavior.w_getrectfn = envgen_getrect;
- envgen_widgetbehavior.w_displacefn = envgen_displace;
- envgen_widgetbehavior.w_selectfn = envgen_select;
- envgen_widgetbehavior.w_activatefn = envgen_activate;
- envgen_widgetbehavior.w_deletefn = envgen_delete;
- envgen_widgetbehavior.w_visfn = envgen_vis;
-#if (PD_VERSION_MINOR > 31)
- envgen_widgetbehavior.w_clickfn = envgen_newclick;
- envgen_widgetbehavior.w_propertiesfn = NULL;
-#endif
- envgen_widgetbehavior.w_savefn = envgen_save;
-
-}
-
+#include <stdio.h> +#include <string.h> +#include <math.h> +#ifdef NT +#pragma warning( disable : 4244 ) +#pragma warning( disable : 4305 ) +#define abs fabs +#endif + +#define PD_VERSION_MINOR 32 +#define BACKGROUNDCOLOR "grey" +#define BORDER 2 + +#if 0 /* backwards compatiblity */ +#define text_xpix(a,b) a.te_xpos +#define text_ypix(a,b) a.te_ypos +#endif + +static void draw_inlets(t_envgen *x, t_glist *glist, int firsttime, int nin, int nout) +{ + int n = nout; + int nplus, i; + nplus = (n == 1 ? 1 : n-1); + for (i = 0; i < n; i++) + { + int onset = x->x_obj.te_xpix + (x->w.width-2*BORDER) * i / nplus; + if (firsttime) + sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xo%d\n", + glist_getcanvas(glist), + onset, x->x_obj.te_ypix + x->w.height - 1 + 2*BORDER, + onset + IOWIDTH, x->x_obj.te_ypix + x->w.height + 2*BORDER, + x, i); + else + sys_vgui(".x%x.c coords %xo%d %d %d %d %d\n", + glist_getcanvas(glist), x, i, + onset, x->x_obj.te_ypix + x->w.height - 1 + 2*BORDER, + onset + IOWIDTH, x->x_obj.te_ypix + x->w.height + 2*BORDER); + } + n = nin; + nplus = (n == 1 ? 1 : n-1); + for (i = 0; i < n; i++) + { + int onset = x->x_obj.te_xpix + (x->w.width - IOWIDTH) * i / nplus - BORDER; + if (firsttime) + sys_vgui(".x%x.c create rectangle %d %d %d %d -tags %xi%d\n", + glist_getcanvas(glist), + onset, x->x_obj.te_ypix - BORDER, + onset + IOWIDTH, x->x_obj.te_ypix + 1 - BORDER, + x, i); + else + sys_vgui(".x%x.c coords %xi%d %d %d %d %d\n", + glist_getcanvas(glist), x, i, + onset, x->x_obj.te_ypix - BORDER, + onset + IOWIDTH, x->x_obj.te_ypix + 1 - BORDER); + + } +} + + + +static int envgen_next_doodle(t_envgen *x, int xpos,int ypos) +{ + int ret = -1; + float xscale,yscale; + int dxpos,dypos; + float minval = 100000.0; + float tval; + int i; + int insertpos = -1; + + if (xpos > x->x_obj.te_xpix + x->w.width) xpos = x->x_obj.te_xpix + x->w.width; + + xscale = x->w.width/x->duration[x->last_state]; + yscale = x->w.height; + + dxpos = x->x_obj.te_xpix;/* + BORDER */; + dypos = x->x_obj.te_ypix + BORDER; + + for (i=0;i<=x->last_state;i++) { + float dx2 = (dxpos + (x->duration[i] * xscale)) - xpos; + float dy2 = (dypos + yscale - (x->finalvalues[i] * yscale)) - ypos; + + dx2*=dx2; + dy2*=dy2; + tval = sqrt(dx2+dy2); + + if (tval <= minval) { + minval = tval; + insertpos = i; + } + } + + /* decide if we want to make a new one */ + if (minval > /*5*/ 8 && insertpos >= 0 && !x->x_freeze) { + + while (((dxpos + (x->duration[insertpos] * xscale)) - xpos) < 0) + insertpos++; + while (((dxpos + (x->duration[insertpos-1] * xscale)) - xpos) > 0) + insertpos--; + + if (x->last_state+1 >= x->args) + envgen_resize(x,x->args+1); + + for (i=x->last_state;i>=insertpos;i--) { + x->duration[i+1] = x->duration[i]; + x->finalvalues[i+1] = x->finalvalues[i]; + } + x->duration[insertpos] = (float)(xpos-dxpos)/x->w.width*x->duration[x->last_state++]; + x->w.pointerx = xpos; + x->w.pointery = ypos; + } + else { + x->w.pointerx = x->x_obj.te_xpix + x->duration[insertpos]*x->w.width/x->duration[x->last_state]; + + + x->w.pointery = x->x_obj.te_ypix + + (1.f - x->finalvalues[insertpos])*x->w.height; + } + + x->w.grabbed = insertpos; + return insertpos; +} + +static void envgen_create_doodles(t_envgen *x, t_glist *glist) +{ + float xscale,yscale; + int xpos,ypos; + int i; + char guistr[255]; + + xscale = x->w.width/x->duration[x->last_state]; + yscale = x->w.height; + + xpos = x->x_obj.te_xpix; + ypos = (int) (x->x_obj.te_ypix + x->w.height); + for (i=0;i<=x->last_state;i++) { + sprintf(guistr,".x%x.c create oval %d %d %d %d -tags %xD%d",(unsigned int)glist_getcanvas(glist), + (int) (xpos+(x->duration[i] * xscale) - 2), + (int) (ypos - x->finalvalues[i]*yscale - 2), + (int) (xpos+(x->duration[i] * xscale)+2), + (int) (ypos - x->finalvalues[i]*yscale + 2), + (unsigned int)x,i); + + if (i == x->w.grabbed) strcat(guistr," -fill red\n"); + else strcat(guistr,"\n"); + sys_vgui("%s",guistr); + } + x->w.numdoodles = i; +} + + +static void envgen_delete_doodles(t_envgen *x, t_glist *glist) +{ + int i; + for (i=0;i<=x->w.numdoodles;i++) { + sys_vgui(".x%x.c delete %xD%d\n",glist_getcanvas(glist),x,i); + } +} + +static void envgen_update_doodles(t_envgen *x, t_glist *glist) +{ + + envgen_delete_doodles(x,glist); +/* LATER only create new doodles if necessary */ + envgen_create_doodles(x, glist); +} + + +static void envgen_delnum(t_envgen *x) +{ + sys_vgui(".x%x.c delete %xT\n",glist_getcanvas(x->w.glist),x); +} + + +static void envgen_shownum(t_envgen *x) +{ + float xscale,yscale; + int xpos,ypos; + int i= x->w.grabbed; + + xscale = x->w.width/x->duration[x->last_state]; + yscale = x->w.height; + + xpos = x->x_obj.te_xpix; + ypos = (int) (x->x_obj.te_ypix + x->w.height); + + envgen_delnum(x); + + sys_vgui(".x%x.c create text %d %d -text %fx%f -tags %xT\n", + (unsigned int)glist_getcanvas(x->w.glist), + + (int) (xpos+(x->duration[i] * xscale) + 12), + (int) (ypos - x->finalvalues[i]*yscale - 2), + + x->finalvalues[i]*(x->max-x->min), + x->duration[i], + (unsigned int)x); + clock_delay(x->w.numclock,700); +} + + + +static void envgen_create(t_envgen *x, t_glist *glist) +{ + int i; + static char buf[1024]; + float xscale,yscale; + int xpos,ypos; + char num[40]; + + x->w.numclock = clock_new(x, (t_method) envgen_delnum); + sys_vgui(".x%x.c create rectangle \ +%d %d %d %d -tags %xS -fill "BACKGROUNDCOLOR"\n", + glist_getcanvas(glist), + x->x_obj.te_xpix-BORDER, x->x_obj.te_ypix-BORDER, + x->x_obj.te_xpix + x->w.width+2*BORDER, x->x_obj.te_ypix + x->w.height+2*BORDER, + x); + + xscale = x->w.width/x->duration[x->last_state]; + yscale = x->w.height; + + sprintf(buf,".x%x.c create line",(unsigned int)glist_getcanvas(glist)); + xpos = x->x_obj.te_xpix; + ypos = (int) (x->x_obj.te_ypix + x->w.height); + for (i=0;i<=x->last_state;i++) { + sprintf(num," %d %d ",(int)(xpos + x->duration[i]*xscale), + (int)(ypos - x->finalvalues[i]*yscale)); + strcat(buf,num); + } + + sprintf(num,"-tags %pP\n",x); + strcat(buf,num); + sys_vgui("%s",buf); + envgen_create_doodles(x,glist); +} + + +static void envgen_update(t_envgen *x, t_glist *glist) +{ +int i; + static char buf[1024]; + float xscale,yscale; + int xpos,ypos; + char num[40]; + + sys_vgui(".x%x.c coords %xS \ +%d %d %d %d\n", + glist_getcanvas(glist), x, + x->x_obj.te_xpix - BORDER, x->x_obj.te_ypix -BORDER, + x->x_obj.te_xpix + x->w.width+2*BORDER, x->x_obj.te_ypix + x->w.height+2*BORDER); + + + xscale = x->w.width/x->duration[x->last_state]; + yscale = x->w.height; + + sprintf(buf,".x%x.c coords %pP",(unsigned int)glist_getcanvas(glist),x); + xpos = x->x_obj.te_xpix; + ypos = (int) (x->x_obj.te_ypix + x->w.height); + for (i=0;i<=x->last_state;i++) { + sprintf(num," %d %d ",(int)(xpos + x->duration[i]*xscale), + (int) (ypos - x->finalvalues[i]*yscale)); + strcat(buf,num); + } + strcat(buf,"\n"); + sys_vgui("%s",buf); + envgen_update_doodles(x,glist); + draw_inlets(x, glist, 0,1,2); +} + + + +void envgen_drawme(t_envgen *x, t_glist *glist, int firsttime) +{ + + if (firsttime) envgen_create(x,glist); + else envgen_update(x,glist); + + draw_inlets(x, glist, firsttime, 1,2); +} + + + + +void envgen_erase(t_envgen* x,t_glist* glist) +{ + int n; + sys_vgui(".x%x.c delete %xS\n", + glist_getcanvas(glist), x); + + sys_vgui(".x%x.c delete %pP\n", + glist_getcanvas(glist), x); + + + sys_vgui(".x%x.c delete %xi0\n",glist_getcanvas(glist),x); + sys_vgui(".x%x.c delete %xo0\n",glist_getcanvas(glist),x); + sys_vgui(".x%x.c delete %xo1\n",glist_getcanvas(glist),x); + envgen_delete_doodles(x,glist); +} + + + +/* ------------------------ envgen widgetbehaviour----------------------------- */ + + +static void envgen_getrect(t_gobj *z, t_glist *owner, + int *xp1, int *yp1, int *xp2, int *yp2) +{ + int width, height; + t_envgen* s = (t_envgen*)z; + + + width = s->w.width + 2*BORDER; + height = s->w.height + 2*BORDER; + *xp1 = s->x_obj.te_xpix-BORDER; + *yp1 = s->x_obj.te_ypix-BORDER; + *xp2 = s->x_obj.te_xpix + width + 4; + *yp2 = s->x_obj.te_ypix + height + 4; +} + +static void envgen_displace(t_gobj *z, t_glist *glist, + int dx, int dy) +{ + t_envgen *x = (t_envgen *)z; + x->x_obj.te_xpix += dx; + x->x_obj.te_ypix += dy; + + envgen_drawme(x, glist, 0); + canvas_fixlinesfor(glist_getcanvas(glist),(t_text*) x); +} + +static void envgen_select(t_gobj *z, t_glist *glist, int state) +{ + t_envgen *x = (t_envgen *)z; + sys_vgui(".x%x.c itemconfigure %xS -fill %s\n", glist, + x, (state? "blue" : BACKGROUNDCOLOR)); +} + + +static void envgen_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 envgen_delete(t_gobj *z, t_glist *glist) +{ + t_text *x = (t_text *)z; + canvas_deletelinesfor(glist_getcanvas(glist), x); +} + + +static void envgen_vis(t_gobj *z, t_glist *glist, int vis) +{ + t_envgen* s = (t_envgen*)z; + if (vis) + envgen_drawme(s, glist, 1); + else + envgen_erase(s,glist); +} + +/* can we use the normal text save function ?? */ + +static void envgen_save(t_gobj *z, t_binbuf *b) +{ + t_envgen *x = (t_envgen *)z; + binbuf_addv(b, "ssiisiiffss", gensym("#X"),gensym("obj"), + (t_int)x->x_obj.te_xpix, (t_int)x->x_obj.te_ypix, + gensym("envgen"),x->w.width,x->w.height,x->max,x->min,x->r_sym,x->s_sym); + binbuf_addv(b, ";"); +} + + + +static void envgen_followpointer(t_envgen* x) +{ + float dur; + + float xscale = x->duration[x->last_state]/x->w.width; + + + if ((x->w.grabbed > 0) && (x->w.grabbed < x->last_state)) { + + dur = (x->w.pointerx - x->x_obj.te_xpix)*xscale; + if (dur < x->duration[x->w.grabbed-1]) + dur = x->duration[x->w.grabbed-1]; + if (dur > x->duration[x->w.grabbed+1]) + dur = x->duration[x->w.grabbed+1]; + + x->duration[x->w.grabbed] = dur; + } + + + x->finalvalues[x->w.grabbed] = 1.0f - (x->w.pointery - (float)x->x_obj.te_ypix)/(float)x->w.height; + if (x->finalvalues[x->w.grabbed] < 0.0) + x->finalvalues[x->w.grabbed]= 0.0; + else if (x->finalvalues[x->w.grabbed] > 1.0) + x->finalvalues[x->w.grabbed]= 1.0; + +} + + +void envgen_motion(t_envgen *x, t_floatarg dx, t_floatarg dy) +{ + if (x->w.shift) { + x->w.pointerx+=dx/1000.f; + x->w.pointery+=dy/1000.f; + } + else + { + x->w.pointerx+=dx; + x->w.pointery+=dy; + } + if (!x->resizing) + envgen_followpointer(x); + else { + x->w.width+=dx; + x->w.height+=dy; + } + envgen_shownum(x); + envgen_update(x,x->w.glist); +} + +void envgen_key(t_envgen *x, t_floatarg f) +{ + if (f == 8.0 && x->w.grabbed < x->last_state && x->w.grabbed > 0) { + int i; + + for (i=x->w.grabbed;i<=x->last_state;i++) { + x->duration[i] = x->duration[i+1]; + x->finalvalues[i] = x->finalvalues[i+1]; + } + + x->last_state--; + x->w.grabbed--; + envgen_update(x,x->w.glist); + } +} + + + +void envgen_click(t_envgen *x, + t_floatarg xpos, t_floatarg ypos, t_floatarg shift, t_floatarg ctrl, + t_floatarg alt) +{ + /* check if user wants to resize */ + float wxpos = x->x_obj.te_xpix; + float wypos = (int) (x->x_obj.te_ypix + x->w.height); + envgen_next_doodle(x,xpos,ypos); +#if (PD_VERSION_MINOR > 31) + glist_grab(x->w.glist, &x->x_obj.te_g, (t_glistmotionfn) envgen_motion, + (t_glistkeyfn) envgen_key, xpos, ypos); +#else + glist_grab(x->w.glist, &x->x_obj.te_g, xpos, ypos); +#endif + x->resizing = 0; + if (x->resizeable && (xpos > wxpos + x->w.width) && + (ypos > wypos)) { + x->resizing = 1; + return; + } + + x->w.shift = shift; + envgen_followpointer(x); + envgen_shownum(x); + envgen_update(x,x->w.glist); +} + + +#if (PD_VERSION_MINOR > 31) +static int envgen_newclick(t_gobj *z, struct _glist *glist, + int xpix, int ypix, int shift, int alt, int dbl, int doit) +{ + if (doit) + envgen_click((t_envgen *)z, (t_floatarg)xpix, (t_floatarg)ypix, + (t_floatarg)shift, 0, (t_floatarg)alt); + return (1); +} +#endif + + + +t_widgetbehavior envgen_widgetbehavior; + +void envgen_setwidget(void) +{ + envgen_widgetbehavior.w_getrectfn = envgen_getrect; + envgen_widgetbehavior.w_displacefn = envgen_displace; + envgen_widgetbehavior.w_selectfn = envgen_select; + envgen_widgetbehavior.w_activatefn = envgen_activate; + envgen_widgetbehavior.w_deletefn = envgen_delete; + envgen_widgetbehavior.w_visfn = envgen_vis; +#if (PD_VERSION_MINOR > 31) + envgen_widgetbehavior.w_clickfn = envgen_newclick; + envgen_widgetbehavior.w_propertiesfn = NULL; +#endif + envgen_widgetbehavior.w_savefn = envgen_save; + +} + |