Skip to content

Commit 6140f9d

Browse files
committed
refactor vis(), drawme, and new to make for cleaner new vs update handling
1 parent 9a2f716 commit 6140f9d

File tree

2 files changed

+40
-35
lines changed

2 files changed

+40
-35
lines changed

filterview.c

+12-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ typedef struct filterview
1616

1717
/* IDs for Tk widgets */
1818
t_symbol* receive_name; /* name to bind to to receive callbacks */
19-
char canvas_id[MAXPDSTRING];
19+
char tkcanvas[MAXPDSTRING];
2020
char tag[MAXPDSTRING];
2121
char my[MAXPDSTRING];
2222

@@ -29,14 +29,6 @@ static t_widgetbehavior filterview_widgetbehavior;
2929

3030
/* time to set up! */
3131

32-
static void set_tkwidgets_ids(t_filterview* x, t_canvas* canvas)
33-
{
34-
x->x_canvas = canvas;
35-
snprintf(x->canvas_id, MAXPDSTRING, ".x%lx.c", (long unsigned int) canvas);
36-
snprintf(x->tag, MAXPDSTRING, "T%lx", (long unsigned int)x);
37-
snprintf(x->my, MAXPDSTRING, "::N%lx", (long unsigned int)x);
38-
}
39-
4032
static void filterview_biquad_callback(t_filterview *x, t_symbol *s, int argc, t_atom* argv)
4133
{
4234
outlet_list(x->x_data_outlet, &s_list, argc, argv);
@@ -63,8 +55,8 @@ static void filterview_displace(t_gobj *z, t_glist *glist, int dx, int dy)
6355
if (glist_isvisible(glist))
6456
{
6557
sys_vgui("%s move %s %d %d\n",
66-
x->canvas_id, x->tag, dx, dy);
67-
sys_vgui("%s move RSZ %d %d\n", x->canvas_id, dx, dy);
58+
x->tkcanvas, x->tag, dx, dy);
59+
sys_vgui("%s move RSZ %d %d\n", x->tkcanvas, dx, dy);
6860
canvas_fixlinesfor(glist_getcanvas(glist), (t_text*) x);
6961
}
7062
}
@@ -85,15 +77,15 @@ static void filterview_vis(t_gobj *z, t_glist *glist, int vis)
8577
t_filterview* x = (t_filterview*)z;
8678
if (vis)
8779
{
88-
set_tkwidgets_ids(x, glist_getcanvas(glist));
89-
sys_vgui("filterview::new %s %s %s %s %d %d %d %d\n",
90-
x->my, x->canvas_id, x->receive_name->s_name, x->tag,
80+
x->x_canvas = glist_getcanvas(glist);
81+
snprintf(x->tkcanvas, MAXPDSTRING, ".x%lx.c", (long unsigned int) x->x_canvas);
82+
sys_vgui("filterview::drawme %s %s %s %s %d %d %d %d %s\n",
83+
x->my, x->tkcanvas, x->receive_name->s_name, x->tag,
9184
text_xpix(&x->x_obj, glist),
9285
text_ypix(&x->x_obj, glist),
9386
text_xpix(&x->x_obj, glist)+x->width,
94-
text_ypix(&x->x_obj, glist)+x->height);
95-
sys_vgui("filterview::setfiltertype %s %s\n", x->my, x->filtertype->s_name);
96-
sys_vgui("filterview::drawme %s\n", x->my);
87+
text_ypix(&x->x_obj, glist)+x->height,
88+
x->filtertype->s_name);
9789
}
9890
else
9991
{
@@ -196,6 +188,9 @@ static void *filterview_new(t_symbol* s)
196188
x->filtertype = gensym("peaking");
197189
x->x_glist = canvas_getcurrent();
198190

191+
snprintf(x->tag, MAXPDSTRING, "T%lx", (long unsigned int)x);
192+
snprintf(x->my, MAXPDSTRING, "::N%lx", (long unsigned int)x);
193+
199194
sprintf(buf, "#R%lx", (long unsigned int)x);
200195
x->receive_name = gensym(buf);
201196
pd_bind(&x->x_obj.ob_pd, x->receive_name);

filterview.tcl

+28-18
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
# This line continues for Tcl, but is a single line for 'sh' \
33
exec /usr/bin/wish "$0" -- ${1+"$@"}
44

5-
# TODO accept biquad lists on the inlet
65
# TODO handle changes in samplerate using [block~]
7-
# TODO make Tk tags local to each instance...
86

97
#------------- .mmb edits ----------------
108
#
@@ -605,6 +603,7 @@ proc filterview::changebandwidth {my x y} {
605603
}
606604
set filterwidth [expr $filterx2 - $filterx1]
607605
set filtercenter [expr $filterx1 + ($filterwidth/2)]
606+
608607
$tkcanvas coords bandleft$tag $filterx1 $framey1 $filterx1 $framey2
609608
$tkcanvas coords bandcenter$tag $filtercenter $framey1 $filtercenter $framey2
610609
$tkcanvas coords bandright$tag $filterx2 $framey1 $filterx2 $framey2
@@ -731,14 +730,13 @@ proc filterview::set_for_editmode {mytoplevel} {
731730

732731
#------------------------------------------------------------------------------#
733732

734-
# sets up an instance of the class
735-
proc filterview::new {my canvas name t x1 y1 x2 y2} {
733+
# sets up an new instance of the class
734+
proc filterview::new {my canvas rname t x1 y1 x2 y2} {
736735
namespace eval $my {
737-
#------------------------------
738-
# per-instance variables
739-
variable tag "tag"
740-
variable tkcanvas ".tkcanvas"
741-
variable receive_name "receive_name"
736+
# init all here to make sure they are not blank
737+
variable tag "tag" ;# unique ID for canvas elements
738+
variable tkcanvas ".tkcanvas" ;# Tk canvas this is drawn on
739+
variable receive_name "receive_name" ;# Pd name to send callbacks to
742740

743741
variable currentfiltertype "peaking"
744742

@@ -752,23 +750,31 @@ proc filterview::new {my canvas name t x1 y1 x2 y2} {
752750
variable b1 0
753751
variable b2 0
754752
}
755-
variable ${my}::tkcanvas $canvas
756-
variable ${my}::receive_name $name
753+
754+
variable ${my}::receive_name $rname
757755
variable ${my}::tag $t
758756

757+
update $my $canvas $x1 $y1 $x2 $y2
758+
}
759+
760+
# updates an existing instance when its about to be drawn again
761+
proc filterview::update {my canvas x1 y1 x2 y2} {
762+
variable ${my}::tkcanvas $canvas
763+
759764
# convert these all to floats so the math works properly
760765
variable ${my}::framex1 [expr $x1 * 1.0]
761766
variable ${my}::framey1 [expr $y1 * 1.0]
762767
variable ${my}::framex2 [expr $x2 * 1.0]
763768
variable ${my}::framey2 [expr $y2 * 1.0]
764-
769+
765770
variable ${my}::midpoint [expr (($framey2 - $framey1) / 2) + $framey1]
766771
variable ${my}::hzperpixel [expr 20000.0 / ($framex2 - $framex1)]
767772
variable ${my}::magnatudeperpixel [expr 0.5 / ($framey2 - $framey1)]
768773
769774
# TODO make these set by something else, saved state?
770775
variable ${my}::filterx1 [expr $framex1 + 120.0]
771776
variable ${my}::filterx2 [expr $framex1 + 180.0]
777+
# TODO what about gain?
772778
773779
variable ${my}::filtergain $midpoint
774780
variable ${my}::filterwidth [expr $filterx2 - $filterx1]
@@ -809,11 +815,17 @@ proc filterview::setfiltertype {my filtertype} {
809815
update_coefficients $my
810816
}
811817
812-
proc filterview::drawme {my} {
818+
proc filterview::drawme {my canvas name t x1 y1 x2 y2 filtertype} {
819+
# if the $my namespace already exists, that means we already
820+
# have an instance active and setup.
821+
if {[namespace exists $my]} {
822+
update $my $canvas $x1 $y1 $x2 $y2
823+
} else {
824+
new $my $canvas $name $t $x1 $y1 $x2 $y2
825+
}
813826
variable ${my}::tkcanvas
814827
variable ${my}::receive_name
815828
variable ${my}::tag
816-
variable ${my}::currentfiltertype
817829
variable ${my}::framex1
818830
variable ${my}::framey1
819831
variable ${my}::framex2
@@ -878,7 +890,7 @@ proc filterview::drawme {my} {
878890
$framex2 $outlety $outletx $outlety $outletx $framey2 \
879891
-tags [list $tag nlet$tag]
880892
881-
setfiltertype $my $currentfiltertype
893+
setfiltertype $my $filtertype
882894
883895
# run to set things up
884896
stop_editing $my
@@ -936,10 +948,8 @@ proc filterview::setup {} {
936948
wm geometry . 400x400+500+40
937949
canvas $tkcanvas
938950
pack $tkcanvas -side left -expand 1 -fill both
939-
filterview::new $my $tkcanvas FAKE_RECEIVE_NAME $tag 30.0 30.0 330.0 230.0
951+
filterview::drawme $my $tkcanvas FAKE_RECEIVE_NAME $tag 30.0 30.0 330.0 230.0 "peaking"
940952
filterview::set_for_editmode .
941-
filterview::setfiltertype $my "peaking"
942-
filterview::drawme $my
943953
}
944954
}
945955

0 commit comments

Comments
 (0)