Skip to content

Commit

Permalink
knob plugdata friendly design
Browse files Browse the repository at this point in the history
fill whole background, 85% proportion with outline
  • Loading branch information
porres committed Nov 12, 2024
1 parent 50944e6 commit b22f279
Showing 1 changed file with 87 additions and 47 deletions.
134 changes: 87 additions & 47 deletions Source/Control/knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ typedef struct _knob{
int x_log;
t_float x_load; // value when loading patch
t_float x_start; // arc start value
t_float x_radius;
int x_start_angle;
int x_fill_bg;
int x_end_angle;
int x_range;
int x_offset;
Expand Down Expand Up @@ -105,10 +107,10 @@ typedef struct _knob{
int x_zoom;
int x_discrete;
char x_tag_obj[128];
char x_tag_circle[128];
char x_tag_base_circle[128];
char x_tag_bg_arc[128];
char x_tag_arc[128];
char x_tag_center[128];
char x_tag_center_circle[128];
char x_tag_wiper[128];
char x_tag_wpr_c[128];
char x_tag_ticks[128];
Expand Down Expand Up @@ -238,10 +240,13 @@ static void show_number(t_knob *x){ // show or hide number value
}

static void knob_config_bg(t_knob *x){
pdgui_vmess(0, "crs rs", x->x_cv, "itemconfigure", x->x_tag_circle,
pdgui_vmess(0, "crs rs", x->x_cv, "itemconfigure", x->x_tag_base_circle,
"-fill", x->x_bg->s_name);
pdgui_vmess(0, "crs rsrs", x->x_cv, "itemconfigure", x->x_tag_center,
pdgui_vmess(0, "crs rsrs", x->x_cv, "itemconfigure", x->x_tag_center_circle,
"-outline", x->x_bg->s_name, "-fill", x->x_bg->s_name);
if(x->x_outline && x->x_fill_bg)
pdgui_vmess(0, "crs rs", x->x_cv, "itemconfigure", x->x_tag_outline,
"-fill", x->x_bg->s_name);
}

// configure size
Expand All @@ -251,6 +256,8 @@ static void knob_config_size(t_knob *x){
int y1 = text_ypix(&x->x_obj, x->x_glist);
int x2 = x1 + x->x_size * z;
int y2 = y1 + x->x_size * z;
int circle_width = x->x_size * (x->x_outline ? x->x_radius : 1);
int offset = (x->x_size - circle_width) * 0.5;
// inlet, outlet, outline
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_in,
x1, y1, x1 + IOWIDTH*z, y1 + IHEIGHT*z);
Expand All @@ -259,31 +266,36 @@ static void knob_config_size(t_knob *x){
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_outline,
x1, y1, x2, y2);
pdgui_vmess(0, "crs ri", x->x_cv, "itemconfigure", x->x_tag_outline,
"-width", x->x_zoom);
// wiper's width/circle
int r = x->x_size * x->x_zoom / 2; // radius
int xc = x1 + r, yc = y1 + r; // center coords
int w_width = r / 10; // width 1/10 of radius
"-width", z);
// wiper's width, wiper's center circle
int w_width = circle_width / 20; // wiper width is 1/20 of circle size
pdgui_vmess(0, "crs ri", x->x_cv, "itemconfigure", x->x_tag_wiper,
"-width", w_width < z ? z : w_width); // wiper width
int half_size = x->x_size * z / 2; // half_size
int xc = x1 + half_size, yc = y1 + half_size; // center coords
int wx1 = rint(xc - w_width), wy1 = rint(yc - w_width);
int wx2 = rint(xc + w_width), wy2 = rint(yc + w_width);
pdgui_vmess(0, "crs ri", x->x_cv, "itemconfigure", x->x_tag_wiper,
"-width", w_width); // wiper width
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_wpr_c,
wx1, wy1, wx2, wy2); // wiper center circle
// knob arc
// knob circle stuff
// knob arc
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_bg_arc,
x1 + x->x_zoom, y1 + x->x_zoom, x2 - x->x_zoom, y2 - x->x_zoom);
x1 + z + offset, y1 + z + offset,
x2 - z - offset, y2 - z - offset);
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_arc,
x1 + x->x_zoom, y1 + x->x_zoom, x2 - x->x_zoom, y2 - x->x_zoom);
// knob circle (base)
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_circle,
x1, y1, x2, y2);
// knob center
int arcwidth = r / 5; // arc width is 1/5 of radius
x1 + z + offset, y1 + z + offset,
x2 - z - offset, y2 - z - offset);
// knob circle (base)
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_base_circle,
x1 + offset, y1 + offset,
x2 - offset, y2 - offset);
// knob center
int arcwidth = circle_width * 0.1; // arc width is 1/10 of circle
if(arcwidth < 1)
arcwidth = 1;
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_center,
x1 + arcwidth, y1 + arcwidth, x2 - arcwidth, y2 - arcwidth);
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_center_circle,
x1 + arcwidth + offset, y1 + arcwidth + offset,
x2 - arcwidth - offset, y2 - arcwidth - offset);
}

// configure wiper center
Expand Down Expand Up @@ -333,11 +345,12 @@ static void knob_update(t_knob *x){
"-start", start * -180.0 / M_PI,
"-extent", (angle - start) * -179.99 / M_PI);
// set wiper
int radius = (int)(x->x_size*x->x_zoom / 2.0);
int half_size = (int)(x->x_size*x->x_zoom / 2.0);
int x0 = text_xpix(&x->x_obj, x->x_glist);
int y0 = text_ypix(&x->x_obj, x->x_glist);
int xc = x0 + radius; // center x coordinate
int yc = y0 + radius; // center y coordinate
int xc = x0 + half_size; // center x coordinate
int yc = y0 + half_size; // center y coordinate
int radius = half_size * (x->x_outline ? x->x_radius : 1.0);
int xp = xc + rint(radius * cos(angle)); // circle point x coordinate
int yp = yc + rint(radius * sin(angle)); // circle point x coordinate
pdgui_vmess(0, "crs iiii", x->x_cv, "coords", x->x_tag_wiper, xc, yc, xp, yp);
Expand All @@ -351,13 +364,13 @@ static void knob_draw_ticks(t_knob *x){
if(!x->x_steps || !x->x_showticks)
return;
int z = x->x_zoom;
float r = (float)x->x_size*z / 2.0; // radius
float half_size = (float)x->x_size*z / 2.0; // half_size
int divs = x->x_steps;
if((divs > 1) && ((x->x_range + 360) % 360 != 0)) // ????
divs = divs - 1;
float delta_w = x->x_range / (float)divs;
int x0 = text_xpix(&x->x_obj, x->x_glist), y0 = text_ypix(&x->x_obj, x->x_glist);
int xc = x0 + r, yc = y0 + r; // center coords
int xc = x0 + half_size, yc = y0 + half_size; // center coords
int start = x->x_start_angle - 90.0;
if(x->x_steps == 1){
int width = (x->x_size / 40);
Expand All @@ -367,7 +380,7 @@ static void knob_draw_ticks(t_knob *x){
double pos = knob_getpos(x, x->x_start) * x->x_range;
float w = pos + start; // tick angle
w *= M_PI/180.0; // in radians
float dx = r * cos(w), dy = r * sin(w);
float dx = half_size * cos(w), dy = half_size * sin(w);
int x1 = xc + (int)(dx);
int y1 = yc + (int)(dy);
int x2 = xc + (int)(dx * 0.65);
Expand All @@ -390,7 +403,7 @@ static void knob_draw_ticks(t_knob *x){
width *= 1.5;
float w = (t-1)*delta_w + start; // tick angle
w *= M_PI/180.0; // in radians
float dx = r * cos(w), dy = r * sin(w);
float dx = half_size * cos(w), dy = half_size * sin(w);
int x1 = xc + (int)(dx);
int y1 = yc + (int)(dy);
int x2 = xc + (int)(dx * (thicker ? 0.65 : 0.75));
Expand All @@ -409,11 +422,14 @@ static void knob_draw_ticks(t_knob *x){
static void knob_draw_new(t_knob *x){
int x1 = text_xpix(&x->x_obj, x->x_glist);
int y1 = text_ypix(&x->x_obj, x->x_glist);
// outline
char *tags_outline[] = {x->x_tag_outline, x->x_tag_obj, x->x_tag_sel};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "rectangle",
x1, y1, 0, 0, "-tags", 3, tags_outline);
// base circle
char *tags_circle[] = {x->x_tag_circle, x->x_tag_obj, x->x_tag_sel};
char *tags_circle[] = {x->x_tag_base_circle, x->x_tag_obj, x->x_tag_sel};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "oval",
x1, y1, 0, 0,
"-tags", 3, tags_circle);
x1, y1, 0, 0, "-tags", 3, tags_circle);
// arc
char *tags_arc_bg[] = {x->x_tag_bg_arc, x->x_tag_obj};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "arc",
Expand All @@ -423,12 +439,11 @@ static void knob_draw_new(t_knob *x){
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "arc",
x1, y1, 0, 0,
"-tags", 2, tags_arc);
// center circle
char *tags_center[] = {x->x_tag_center, x->x_tag_obj};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "oval",
x1, y1, 0, 0,
"-tags", 2, tags_center);
// wiper
// center circle on top of arc
char *tags_center[] = {x->x_tag_center_circle, x->x_tag_obj};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "oval",
x1, y1, 0, 0, "-tags", 2, tags_center);
// wiper and wiper center
char *tags_wiper_center[] = {x->x_tag_wpr_c, x->x_tag_obj};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "oval",
x1, y1, 0, 0,
Expand Down Expand Up @@ -456,11 +471,6 @@ static void knob_draw_new(t_knob *x){
x1, y1, 0, 0,
"-fill", "black",
"-tags", 2, tags_out);
// outline
char *tags_outline[] = {x->x_tag_outline, x->x_tag_obj, x->x_tag_sel};
pdgui_vmess(0, "crr iiii rS", x->x_cv, "create", "rectangle",
x1, y1, 0, 0,
"-tags", 3, tags_outline);
// config and set
knob_draw_ticks(x);
knob_config_size(x);
Expand Down Expand Up @@ -691,6 +701,7 @@ static void knob_update_number(t_knob *x){
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);
}
}

Expand Down Expand Up @@ -817,6 +828,16 @@ static void knob_size(t_knob *x, t_floatarg f){
}
}

/*static void knob_proportion(t_knob *x, t_floatarg f){
float r = (f < 50 ? 50 : f > 100 ? 100 : f) / 100;
if(x->x_radius != r){
x->x_radius = r;
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist))
knob_config_size(x);
knob_update(x);
}
}*/

static void knob_arc(t_knob *x, t_floatarg f){
int arc = f != 0;
if(x->x_arc != arc){
Expand Down Expand Up @@ -880,6 +901,12 @@ static void knob_bgcolor(t_knob *x, t_symbol *s, int ac, t_atom *av){
}
}

/*static void knob_fill_bg(t_knob *x, t_floatarg f){
x->x_fill_bg = (f != 0);
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist))
knob_config_bg(x);
}*/

static void knob_mgcolor(t_knob *x, t_symbol *s, int ac, t_atom *av){
x->x_ignore = s;
if(!ac)
Expand Down Expand Up @@ -1028,9 +1055,18 @@ static void knob_numberpos(t_knob *x, t_floatarg xpos, t_floatarg ypos){
}

static void knob_outline(t_knob *x, t_floatarg f){
x->x_outline = (int)f;
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist))
knob_config_io(x);
int outline = (int)f;
if(x->x_outline != outline){
x->x_outline = (int)f;
if(glist_isvisible(x->x_glist) && gobj_shouldvis((t_gobj *)x, x->x_glist)){
knob_config_io(x);
if(x->x_radius < 1.0){
knob_config_size(x);
knob_update(x);
}
knob_config_bg(x);
}
}
}

static void knob_zoom(t_knob *x, t_floatarg f){
Expand Down Expand Up @@ -1798,19 +1834,21 @@ static void *knob_new(t_symbol *s, int ac, t_atom *av){
x->x_fval = x->x_load = loadvalue;
x->x_pos = knob_getpos(x, x->x_fval);
x->x_edit = x->x_glist->gl_edit;
x->x_radius = 0.85;
x->x_fill_bg = 1;
char buf[MAXPDSTRING];
snprintf(buf, MAXPDSTRING-1, ".x%lx", (unsigned long)x->x_glist);
buf[MAXPDSTRING-1] = 0;
x->x_proxy = edit_proxy_new(x, gensym(buf));
sprintf(x->x_tag_obj, "%pOBJ", x);
sprintf(x->x_tag_circle, "%pCIRCLE", x);
sprintf(x->x_tag_base_circle, "%pBASE_CIRCLE", x);
sprintf(x->x_tag_sel, "%pSEL", x);
sprintf(x->x_tag_arc, "%pARC", x);
sprintf(x->x_tag_bg_arc, "%pBGARC", x);
sprintf(x->x_tag_ticks, "%pTICKS", x);
sprintf(x->x_tag_wiper, "%pWIPER", x);
sprintf(x->x_tag_wpr_c, "%pWIPERC", x);
sprintf(x->x_tag_center, "%pCENTER", x);
sprintf(x->x_tag_center_circle, "%pCENTER_CIRCLE", x);
sprintf(x->x_tag_outline, "%pOUTLINE", x);
sprintf(x->x_tag_in, "%pIN", x);
sprintf(x->x_tag_out, "%pOUT", x);
Expand Down Expand Up @@ -1842,6 +1880,8 @@ void knob_setup(void){
class_addmethod(knob_class, (t_method)knob_log, gensym("log"), A_FLOAT, 0);
class_addmethod(knob_class, (t_method)knob_discrete, gensym("discrete"), A_FLOAT, 0);
class_addmethod(knob_class, (t_method)knob_bgcolor, gensym("bgcolor"), A_GIMME, 0);
// class_addmethod(knob_class, (t_method)knob_fill_bg, gensym("fill_bg"), A_FLOAT, 0);
// class_addmethod(knob_class, (t_method)knob_proportion, gensym("proportion"), A_FLOAT, 0);
class_addmethod(knob_class, (t_method)knob_mgcolor, gensym("arccolor"), A_GIMME, 0);
class_addmethod(knob_class, (t_method)knob_fgcolor, gensym("fgcolor"), A_GIMME, 0);
class_addmethod(knob_class, (t_method)knob_send, gensym("send"), A_DEFSYM, 0);
Expand Down

0 comments on commit b22f279

Please sign in to comment.