Skip to content

Commit

Permalink
add norm flag and allow canvas units for pos
Browse files Browse the repository at this point in the history
  • Loading branch information
fdch committed Jun 18, 2023
1 parent e102cf2 commit 863ea38
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 17 deletions.
37 changes: 24 additions & 13 deletions help/scroll-help.pd
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#X restore 438 423 pd meta;
#X text 440 452 fd_lib v0.1;
#X text 60 410 0 outlets;
#N canvas 532 401 207 150 canvas-1 1;
#N canvas 729 167 355 184 canvas-1 0;
#X text 996 52 Top Right;
#X text 51 48 Top Left;
#X text 60 644 Bottom Left;
Expand Down Expand Up @@ -66,27 +66,36 @@
#X msg 172 96 \; msg yaxis 10;
#X msg 167 172 \; msg yaxis -10;
#X text 324 190 The canvases:;
#N canvas 0 22 662 551 messages 0;
#N canvas 0 22 703 614 messages 0;
#X msg 185 149 symbol pd-canvas-2;
#X msg 217 184 symbol pd-canvas-1;
#X msg 241 222 symbol fake;
#X text 179 124 Change canvas to scroll in:;
#X obj 62 450 s msg;
#X obj 41 574 s msg;
#X msg 67 80 xaxis \$1;
#X msg 144 78 yaxis \$1;
#X msg 66 49 10;
#X msg 99 49 -10;
#X msg 145 48 10;
#X msg 178 48 -10;
#X text 64 25 Scroll the canvas:;
#X floatatom 286 281 5 0 0 0 - - - 0;
#X msg 286 305 xpos \$1;
#X msg 367 315 ypos \$1;
#X floatatom 370 288 5 0 0 0 - - - 0;
#X text 330 262 normalized scroll (0-1);
#X obj 288 377 tgl 20 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000 0 1;
#X msg 288 402 pages \$1;
#X text 366 399 set to page-based scrolling \; defaults to units;
#X floatatom 260 444 5 0 0 0 - - - 0;
#X msg 260 468 xpos \$1;
#X msg 341 478 ypos \$1;
#X floatatom 344 451 5 0 0 0 - - - 0;
#X text 408 421 normalized scroll (0-1);
#X obj 269 275 tgl 20 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000 0 1;
#X text 337 296 set to page-based scrolling \; defaults to units;
#X obj 255 369 tgl 20 0 empty empty empty 0 -10 0 12 #fcfcfc #000000 #000000 0 1;
#X msg 255 394 norm \$1;
#X text 414 459 canvas units;
#X text 442 483 x: 0..canvas width;
#X text 441 509 y: 0..-canvas height;
#X text 315 364 set xpos/ypos to normalized scrolling or canvas units \; defaults to unnormalized;
#X msg 259 299 page \$1;
#X msg 489 254 \; msg xaxis 1;
#X msg 387 255 \; msg xaxis -1;
#X msg 490 201 \; msg xaxis 2;
#X connect 0 0 4 0;
#X connect 1 0 4 0;
#X connect 2 0 4 0;
Expand All @@ -100,8 +109,10 @@
#X connect 13 0 4 0;
#X connect 14 0 4 0;
#X connect 15 0 14 0;
#X connect 17 0 18 0;
#X connect 18 0 4 0;
#X connect 17 0 25 0;
#X connect 19 0 20 0;
#X connect 20 0 4 0;
#X connect 25 0 4 0;
#X restore 378 75 pd messages;
#X text 60 284 1: symbol: pd-canvas name;
#X text 60 340 1: see pd-messages;
Expand Down
34 changes: 30 additions & 4 deletions src/scroll.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ You should have received a copy of the GNU General Public License along with thi

typedef struct scroll {
t_object x_obj;
t_symbol *x_sym;
t_canvas *canvas;
t_float x_page;
t_symbol *x_sym; // the canvas name
t_canvas *canvas; // the canvas on which to scroll
t_float x_page; // define 'page' (1) or 'units' (0) (default=0)
t_float x_norm; // flag to user normalized coordinates (1) or not (0) (default=0)
} t_scroll;

static void setcanvassym_scroll(t_scroll *x, t_symbol *s)
Expand All @@ -35,16 +36,37 @@ static void scroll_page(t_scroll *x, t_float f)
x->x_page = (int)f > 0 ? 1 : 0;
}

static void scroll_norm(t_scroll *x, t_float f)
{
x->x_norm = (int)f > 0 ? 1 : 0;
}

static void scroll_symbol(t_scroll *x, t_symbol *s)
{
setcanvassym_scroll(x, s);
}

static int scroll_get_screenx(t_scroll *x)
{
return x->canvas->gl_screenx2 - x->canvas->gl_screenx1;
}

static int scroll_get_screeny(t_scroll *x)
{
return x->canvas->gl_screeny2 - x->canvas->gl_screeny1;
}

static void scroll_moveto(t_scroll *x, int d, float f)
{
if (!sys_havegui())return;
if (!(glist_isvisible(x->canvas))) canvas_vis(x->canvas, 1);
float t = f > 1 ? 1 : f < 0 ? 0 : (float) f;
// allow unnormalized scrolling by normalizing the input
if (!x->x_norm) {
// adjust to x or y axis
f /= d == 0 ? scroll_get_screenx(x) : scroll_get_screeny(x);
}
// restrict bounds to 0-1
float t = f > 1 ? 1 : f < 0 ? 0 : f;
sys_vgui(".x%lx.c %sview moveto %f \n", x->canvas, d == 0 ? "x" : "y", t);
}

Expand Down Expand Up @@ -88,6 +110,7 @@ static void *scroll_new(t_symbol *s)
} else
setcanvassym_scroll(x, s);
x->x_page = 0; // default to units
x->x_norm = 0; // default to unnormalized scrolling
return(x);
}

Expand All @@ -100,4 +123,7 @@ void scroll_setup()
class_addmethod(scroll_class, (t_method)scroll_xmoveto, gensym("xpos"), A_FLOAT, 0);
class_addmethod(scroll_class, (t_method)scroll_ymoveto, gensym("ypos"), A_FLOAT, 0);
class_addmethod(scroll_class, (t_method)scroll_page, gensym("page"), A_FLOAT, 0);
class_addmethod(scroll_class, (t_method)scroll_norm, gensym("norm"), A_FLOAT, 0);
class_addmethod(scroll_class, (t_method)scroll_get_screenx, gensym("screenx"), A_FLOAT, 0);
class_addmethod(scroll_class, (t_method)scroll_get_screeny, gensym("screeny"), A_FLOAT, 0);
}

0 comments on commit 863ea38

Please sign in to comment.