Skip to content

Commit

Permalink
knob: number fixes and include number parameters in properties
Browse files Browse the repository at this point in the history
Update note.c
  • Loading branch information
porres committed Nov 20, 2024
1 parent 2a4ff10 commit 264be28
Show file tree
Hide file tree
Showing 4 changed files with 505 additions and 405 deletions.
10 changes: 5 additions & 5 deletions Documentation/Help-files/knob-help.pd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#N canvas 518 57 564 788 10;
#N canvas 477 53 564 788 10;
#X obj 2 4 cnv 15 301 42 empty empty knob 20 20 2 37 #e0e0e0 #000000 0;
#N canvas 0 22 450 278 (subpatch) 0;
#X coords 0 1 100 -1 302 42 1;
Expand Down Expand Up @@ -322,7 +322,7 @@
#X text 200 345 - sets number font size (default: 12), f 58;
#X text 93 359 numberpos <f \, f>;
#X text 200 359 - sets coordinates relative to object (default: 6 \, -15), f 58;
#N canvas 404 131 860 581 activity/number 1;
#N canvas 404 131 860 581 activity/number 0;
#X msg 193 315 number \$1;
#X floatatom 193 290 3 0 0 0 - - - 0;
#X obj 193 216 vradio 16 1 0 4 empty empty empty 0 -8 0 10 #dfdfdf #000000 #000000 0;
Expand All @@ -333,10 +333,10 @@
#X obj 483 171 knob 65 0 100 0 0 \$0-knob-% empty #dfdfdf #7c7c7c black 0 0 0 0 1 320 0 0 0 empty empty 0 0 0 0 0 0 0;
#X obj 484 244 note 54 14 Menlo \$0-set-note-% 0 0 0 0 0 255 255 255 0 1 0.00 %;
#X obj 599 171 knob 65 -12 12 0 0 \$0-knob-st empty #000000 #7c7c7c #ffffff 0 0 25 1 0 320 0 0 0 empty empty 0 0 0 0 0 0 0;
#X obj 580 244 note 117 13 Menlo \$0-set-note-st 0 0 0 0 0 255 255 255 0 1 +03 semitones;
#X obj 580 244 note 117 13 Menlo \$0-set-note-st 0 0 0 0 0 255 255 255 0 1 0 semitones;
#X obj 731 171 knob 65 -100 100 0 0 \$0-knob-pan empty #000000 #202020 #ff0400 0 0 0 0 1 320 0 0 0 empty empty 0 0 0 0 0 0 0;
#X obj 731 244 note 64 14 Menlo \$0-set-note-pan 0 0 0 0 0 255 255 255 0 1 69.2 L;
#N canvas 802 477 668 447 guts 1;
#X obj 731 244 note 64 14 Menlo \$0-set-note-pan 0 0 0 0 0 255 255 255 0 1 Center;
#N canvas 684 392 668 447 guts 0;
#X obj 269 139 makefilename %.3#g;
#X obj 152 157 moses;
#X obj 269 196 s \$0-set-note-pan;
Expand Down
141 changes: 80 additions & 61 deletions Source/Control/knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ typedef struct _knob{
}t_knob;

// ---------------------- Helper functions ----------------------

static void knob_update_number(t_knob *x){
/* post("x->x_fval = %f", x->x_fval);
post("(int)x->x_fval = %d", (int)rint(x->x_fval));
int val = (int)x->x_fval;
post("val = %d", val);
float dif = x->x_fval - (float)val;
post("dif = %f", dif);*/
char nbuf[32];
if(x->x_fval > -1 && x->x_fval < 1)
sprintf(nbuf, "%.5f", x->x_fval);
// else if(x->x_fval == (int)x->x_fval)
// sprintf(nbuf, "%.d", (int)x->x_fval);
else
sprintf(nbuf, "%#.5g", x->x_fval);
pdgui_vmess(0, "crs rs", glist_getcanvas(x->x_glist),
"itemconfigure", x->x_tag_number, "-text", nbuf);
}

// get value from motion/position
static t_float knob_getfval(t_knob *x){
double fval;
Expand Down Expand Up @@ -227,17 +246,16 @@ static void knob_config_number(t_knob *x){ // show or hide number value
(x->x_obj.te_ypix + x->x_ypos)*x->x_zoom);
}

static void show_number(t_knob *x){ // show or hide number value
static void show_number(t_knob *x, int force){ // show or hide number value
if(x->x_number == 1 || (x->x_number == 2 && x->x_clicked))
x->x_shownumber = 1; // mode 1 or mode 2 and clicked
else if(x->x_number == 3 && x->x_typing)
x->x_shownumber = 1; // mode 3 and typing
else
x->x_shownumber = 0;
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist)){
if((glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist)) || force)
pdgui_vmess(0, "crs rs", x->x_cv, "itemconfigure", x->x_tag_number,
"-state", x->x_shownumber ? "normal" : "hidden");
}
"-state", x->x_shownumber ? "normal" : "hidden");
}

static void knob_config_bg(t_knob *x){
Expand Down Expand Up @@ -494,13 +512,8 @@ char *tags_center[] = {x->x_tag_center_circle, x->x_tag_obj};
knob_config_wcenter(x);
knob_config_number(x);
knob_update(x);
if(x->x_shownumber){
char buf[32]; // number buffer
sprintf(buf, "%#.5g", x->x_fval);
pdgui_vmess(0, "crs rs", glist_getcanvas(x->x_glist),
"itemconfigure", x->x_tag_number, "-text", buf);
}
show_number(x);
knob_update_number(x);
show_number(x, 1);
pdgui_vmess(0, "crs rs", x->x_cv, "itemconfigure", x->x_tag_sel,
"-outline", x->x_sel ? "blue" : "black"); // ??????
pdgui_vmess(0, "crs rs", x->x_cv, "itemconfigure", x->x_tag_number,
Expand Down Expand Up @@ -704,16 +717,7 @@ static void knob_set(t_knob *x, t_floatarg f){
if(x->x_pos != old){
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist))
knob_update(x);
}
}

static void knob_update_number(t_knob *x){
if(x->x_shownumber){
char buf[32];
sprintf(buf, "%#.5g", x->x_fval);
pdgui_vmess(0, "crs rs", glist_getcanvas(x->x_glist),
"itemconfigure", x->x_tag_number, "-text", buf);
post(buf, "number: %f / %g", x->x_fval, x->x_fval);
knob_update_number(x);
}
}

Expand All @@ -737,7 +741,6 @@ static void knob_bang(t_knob *x){
empty:
if(x->x_var != gensym("empty"))
value_setfloat(x->x_var, x->x_fval);
knob_update_number(x);
}

static void knob_float(t_knob *x, t_floatarg f){
Expand Down Expand Up @@ -1053,7 +1056,7 @@ static void knob_lb(t_knob *x, t_floatarg f){

static void knob_number(t_knob *x, t_floatarg f){
x->x_number = f < 0 ? 0 : f > 3 ? 3 : (int)f;
show_number(x);
show_number(x, 0);
}

static void knob_numbersize(t_knob *x, t_floatarg f){
Expand Down Expand Up @@ -1093,19 +1096,20 @@ static void knob_properties(t_gobj *z, t_glist *owner){
knob_get_var(x);
knob_get_rcv(x);
pdgui_stub_vnew(&x->x_obj.ob_pd, "knob_dialog",
owner, "ffffi ss ifi sss iiiiii fss iii",
owner, "ffffi ss ifi sss iiiiii fss iii iiii",
(float)(x->x_size / x->x_zoom), x->x_lower, x->x_upper, x->x_load, x->x_circular,
x->x_snd_raw->s_name, x->x_rcv_raw->s_name,
x->x_expmode, x->x_exp, x->x_jump,
x->x_bg->s_name, x->x_fg->s_name, x->x_mg->s_name,
x->x_discrete, x->x_steps, x->x_arc, x->x_range, x->x_offset, x->x_square,
x->x_start, x->x_param->s_name, x->x_var_raw->s_name,
x->x_savestate, x->x_loadbang, x->x_showticks);
x->x_savestate, x->x_loadbang, x->x_showticks,
x->x_fontsize, x->x_xpos, x->x_ypos, x->x_shownumber);
}

static void knob_apply(t_knob *x, t_symbol *s, int ac, t_atom *av){
x->x_ignore = s;
t_atom undo[25];
t_atom undo[29];
SETFLOAT(undo+0, x->x_size);
SETFLOAT(undo+1, x->x_lower);
SETFLOAT(undo+2, x->x_upper);
Expand All @@ -1131,7 +1135,11 @@ static void knob_apply(t_knob *x, t_symbol *s, int ac, t_atom *av){
SETFLOAT(undo+22, x->x_savestate);
SETFLOAT(undo+23, x->x_loadbang);
SETFLOAT(undo+24, x->x_showticks);
pd_undo_set_objectstate(x->x_glist, (t_pd*)x, gensym("dialog"), 25, undo, ac, av);
SETFLOAT(undo+25, x->x_fontsize);
SETFLOAT(undo+26, x->x_xpos);
SETFLOAT(undo+27, x->x_ypos);
SETFLOAT(undo+28, x->x_shownumber);
pd_undo_set_objectstate(x->x_glist, (t_pd*)x, gensym("dialog"), 29, undo, ac, av);
int size = (int)atom_getintarg(0, ac, av);
float min = atom_getfloatarg(1, ac, av);
float max = atom_getfloatarg(2, ac, av);
Expand All @@ -1157,6 +1165,10 @@ static void knob_apply(t_knob *x, t_symbol *s, int ac, t_atom *av){
x->x_savestate = atom_getintarg(22, ac, av);
x->x_loadbang = atom_getintarg(23, ac, av);
int ticks = atom_getintarg(24, ac, av);
int fontsize = atom_getintarg(25, ac, av);
int xpos = atom_getintarg(26, ac, av);
int ypos = atom_getintarg(27, ac, av);
int shownumber = atom_getintarg(28, ac, av);
knob_config_io(x); // for outline/square
if(expmode == 0){
knob_log(x, 0);
Expand Down Expand Up @@ -1193,6 +1205,9 @@ static void knob_apply(t_knob *x, t_symbol *s, int ac, t_atom *av){
}
knob_param(x, param);
knob_var(x, var);
knob_numbersize(x, fontsize);
knob_numberpos(x, xpos, ypos);
knob_number(x, shownumber);
}

// --------------- click + motion stuff --------------------
Expand Down Expand Up @@ -1224,43 +1239,16 @@ static void knob_arrow_motion(t_knob *x, t_floatarg dir){
x->x_pos = pos;
t_float fval = x->x_fval;
x->x_fval = knob_getfval(x);
if(fval != x->x_fval)
if(fval != x->x_fval){
knob_bang(x);
knob_update_number(x);
}
if(old != x->x_pos){
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist))
knob_update(x);
}
}

static void knob_list(t_knob *x, t_symbol *sym, int ac, t_atom *av){ // get key events
if(!ac){
knob_bang(x);
return;
}
if(ac == 1 && av->a_type == A_FLOAT){
knob_float(x, atom_getfloat(av));
return;
}
if(ac != 2 || x->x_edit)
return;
int flag = (int)atom_getfloat(av); // 1 for press, 0 for release
sym = atom_getsymbol(av+1); // get key name
if(sym == gensym("Meta_L")){
x->x_ctrl = flag;
return;
}
if(!x->x_clicked)
return;
int dir = 0;
if(flag && (sym == gensym("Up") || (sym == gensym("Right"))))
dir = 1;
else if(flag && (sym == gensym("Down") || (sym == gensym("Left"))))
dir = -1;
else
return;
knob_arrow_motion(x, dir);
}

static void knob_motion(t_knob *x, t_floatarg dx, t_floatarg dy){
if(dx == 0 && dy == 0)
return;
Expand All @@ -1286,14 +1274,45 @@ static void knob_motion(t_knob *x, t_floatarg dx, t_floatarg dy){
x->x_pos = pos > 1 ? 1 : pos < 0 ? 0 : pos; // should go up in non circular (to do)
t_float fval = x->x_fval;
x->x_fval = knob_getfval(x);
if(fval != x->x_fval)
if(fval != x->x_fval){
knob_bang(x);
knob_update_number(x);
}
if(old != x->x_pos){
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist))
knob_update(x);
}
}

static void knob_list(t_knob *x, t_symbol *sym, int ac, t_atom *av){ // get key events
if(!ac){
knob_bang(x);
return;
}
if(ac == 1 && av->a_type == A_FLOAT){
knob_float(x, atom_getfloat(av));
return;
}
if(ac != 2 || x->x_edit)
return;
int flag = (int)atom_getfloat(av); // 1 for press, 0 for release
sym = atom_getsymbol(av+1); // get key name
if(sym == gensym("Meta_L")){
x->x_ctrl = flag;
return;
}
if(!x->x_clicked)
return;
int dir = 0;
if(flag && (sym == gensym("Up") || (sym == gensym("Right"))))
dir = 1;
else if(flag && (sym == gensym("Down") || (sym == gensym("Left"))))
dir = -1;
else
return;
knob_arrow_motion(x, dir);
}

static void knob_key(void *z, t_symbol *keysym, t_floatarg fkey){
t_knob *x = z;
x->x_ignore = keysym;
Expand All @@ -1314,7 +1333,7 @@ static void knob_key(void *z, t_symbol *keysym, t_floatarg fkey){
knob_float(x, x->x_buf[0] == 0 ? x->x_fval : atof(x->x_buf));
x->x_buf[0] = 0;
x->x_typing = 0;
show_number(x);
show_number(x, 0);
return;
}
else if(((c >= '0') && (c <= '9')) || (c == '.') || (c == '-') ||
Expand All @@ -1334,7 +1353,7 @@ static void knob_key(void *z, t_symbol *keysym, t_floatarg fkey){
goto update_number;
}
update_number:{
show_number(x);
show_number(x, 0);
char *cp = x->x_buf;
int sl = (int)strlen(x->x_buf);
x->x_buf[sl] = '|';
Expand Down Expand Up @@ -1395,7 +1414,7 @@ static int knob_click(t_gobj *z, struct _glist *glist, int xpix, int ypix, int s
x->x_buf[0] = 0;
// pd_bind(&x->x_obj.ob_pd, gensym("#keyname")); // listen to key events
x->x_clicked = 1;
show_number(x);
show_number(x, 0);
knob_config_wcenter(x);
x->x_shift = shift;
if(x->x_circular){
Expand Down
2 changes: 1 addition & 1 deletion Source/Control/note.c
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ void note_setup(void){
" label $id.justification.oll -text \"Outline:\"\n"
" tk_optionMenu $id.justification.just $var_just Left Center Right\n"
" label $id.justification.lbj -text \"Justification:\"\n"
" pack $id.justification.oll $id.justification.ol $id.justification.lbj $id.justification.just $id.justification.lbj $id.justification.just -side left\n"
" pack $id.justification.oll $id.justification.ol $id.justification.lbj $id.justification.just -side left\n"
"\n"
" frame $id.ul_bg\n"
" pack $id.ul_bg -side top\n"
Expand Down
Loading

0 comments on commit 264be28

Please sign in to comment.