Skip to content

Commit

Permalink
Simplified mouse event implementation for pd-vanilla
Browse files Browse the repository at this point in the history
  • Loading branch information
timothyschoen committed Dec 21, 2023
1 parent 336d301 commit 3e0cd72
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 81 deletions.
58 changes: 37 additions & 21 deletions pdlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,33 +594,49 @@ static void pdlua_motion(void *z, t_floatarg dx, t_floatarg dy,
{
#if !PLUGDATA
t_pdlua *x = (t_pdlua *)z;
x->gfx.mouse_x = x->gfx.mouse_x + dx;
x->gfx.mouse_y = x->gfx.mouse_y + dy;
x->gfx.mouse_drag_x = x->gfx.mouse_drag_x + dx;
x->gfx.mouse_drag_y = x->gfx.mouse_drag_y + dy;

if (up)
if (!up)
{
if(!x->gfx.mouse_up)
{
pdlua_gfx_mouse_up((t_object*)x, x->gfx.mouse_x, x->gfx.mouse_y);
}
}
else {
if(x->gfx.mouse_up)
{
pdlua_gfx_mouse_down((t_object*)x, x->gfx.mouse_x, x->gfx.mouse_y);
}
pdlua_gfx_mouse_drag((t_object*)x, x->gfx.mouse_x, x->gfx.mouse_y);
pdlua_gfx_mouse_drag((t_object*)x, x->gfx.mouse_drag_x - text_xpix(&x->pd, x->canvas), x->gfx.mouse_drag_y - text_ypix(&x->pd, x->canvas));
}

x->gfx.mouse_up = up;
#endif
}

static int pdlua_click(t_pdlua *x, t_glist *gl, int xpos, int ypos, int shift, int alt, int dbl, int doit){
alt = dbl = 0; // remove warning
if(doit){
glist_grab(x->canvas, &x->pd.te_g, (t_glistmotionfn)pdlua_motion, (t_glistkeyfn)pdlua_key, xpos, ypos);
if(x->has_gui)
{
int xpix = xpos - text_xpix(&x->pd, gl);
int ypix = ypos - text_ypix(&x->pd, gl);

if(doit){
if(x->gfx.mouse_down)
{
pdlua_gfx_mouse_down((t_object*)x, xpix, ypix);
}

x->gfx.mouse_drag_x = xpos;
x->gfx.mouse_drag_y = ypos;

glist_grab(x->canvas, &x->pd.te_g, (t_glistmotionfn)pdlua_motion, (t_glistkeyfn)pdlua_key, xpos, ypos);
}
else {
pdlua_gfx_mouse_move(x, xpix, ypix);

if(!x->gfx.mouse_down)
{
pdlua_gfx_mouse_up((t_object*)x, xpix, ypix);
}
}

x->gfx.mouse_down = doit;
}
else {
text_widgetbehavior.w_clickfn(x, gl, xpos, ypos, shift, alt, dbl, doit);
}

return(1);
}

Expand Down Expand Up @@ -864,9 +880,9 @@ static int pdlua_object_new(lua_State *L)
o->gfx.scale_y = 1.0f;
o->gfx.translate_x = 0;
o->gfx.translate_y = 0;
o->gfx.mouse_x = 0;
o->gfx.mouse_y = 0;
o->gfx.mouse_up = 1;
o->gfx.mouse_drag_x = 0;
o->gfx.mouse_drag_y = 0;
o->gfx.mouse_down = 1;
#else
o->gfx.plugdata_draw_callback = NULL;
o->gfx.plugdata_callback_target = NULL;
Expand Down
4 changes: 2 additions & 2 deletions pdlua.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ typedef struct _pdlua_gfx
{
#if !PLUGDATA
char object_tag[128];
char active_tags[128][128];
char active_tags[8192][128];
int num_tags;

int* path_segments;
int num_path_segments;
int num_path_segments_allocated;
int path_start_x, path_start_y;

int mouse_x, mouse_y, mouse_up;
int mouse_drag_x, mouse_drag_y, mouse_down;
int translate_x, translate_y;
float scale_x, scale_y;
char current_color[8];
Expand Down
97 changes: 39 additions & 58 deletions pdlua_gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ static int reset_transform(lua_State* L);

void pdlua_gfx_clear(t_pdlua *obj);

typedef struct _pdlua_mouse_proxy{
t_object object;
t_symbol *bind_sym;
t_clock *free_clock;
t_pdlua *parent;
}t_pdlua_mouse_proxy;

void pdlua_gfx_repaint(t_pdlua *o) {
lua_getglobal(__L, "pd");
lua_getfield (__L, -1, "_repaint");
Expand Down Expand Up @@ -145,52 +138,10 @@ int set_gui_callback(void* callback_target, void(*callback)(void*, t_object*)) {
return 0;
}

#if !PLUGDATA
t_class* pdlua_mouse_proxy_class;

static void pdlua_mouse_proxy_any(t_pdlua_mouse_proxy *p, t_symbol*s, int ac, t_atom *av){

if(s == gensym("motion") && p->parent->gfx.mouse_up)
{
t_canvas *cnv = glist_getcanvas(p->parent->canvas);
int zoom = glist_getzoom(cnv);
int x = (int)(av->a_w.w_float / zoom);
int y = (int)((av+1)->a_w.w_float / zoom);
x -= text_xpix(p->parent, cnv);
y -= text_ypix(p->parent, cnv);

if(x > 0 && y > 0 && x < p->parent->gfx.width && y < p->parent->gfx.height) {
pdlua_gfx_mouse_move(p->parent, x, y);
}
}
}

static void pdlua_mouse_proxy_free(t_pdlua_mouse_proxy *p){
pd_unbind(&p->object.ob_pd, p->bind_sym);
clock_free(p->free_clock);
pd_free(&p->object.ob_pd);
}

static t_pdlua_mouse_proxy *pdlua_mouse_proxy_new(t_pdlua *x, t_symbol *s){
t_pdlua_mouse_proxy *p = (t_pdlua_mouse_proxy*)pd_new(pdlua_mouse_proxy_class);
p->parent = x;
pd_bind(&p->object.ob_pd, p->bind_sym = s);
p->free_clock = clock_new(p, (t_method)pdlua_mouse_proxy_free);
return(p);
}
#endif


int pdlua_gfx_setup(lua_State* L) {
// Register functions with Lua
luaL_newlib(L, gfx_lib);
lua_setglobal(L, "gfx");

#if !PLUGDATA
pdlua_mouse_proxy_class = class_new(0, 0, 0, sizeof(t_pdlua_mouse_proxy), CLASS_NOINLET | CLASS_PD, 0);
class_addanything(pdlua_mouse_proxy_class, pdlua_mouse_proxy_any);
#endif

return 1; // Number of values pushed onto the stack
}

Expand Down Expand Up @@ -328,6 +279,16 @@ static int stroke_rounded_rect(lua_State* L) {
return 0;
}

static int draw_line(lua_State* L) {
t_atom args[5];
SETFLOAT(args, luaL_checknumber(L, 1)); // x
SETFLOAT(args + 1, luaL_checknumber(L, 2)); // y
SETFLOAT(args + 2, luaL_checknumber(L, 3)); // w
SETFLOAT(args + 3, luaL_checknumber(L, 4)); // h
SETFLOAT(args + 4, luaL_checknumber(L, 5)); // line width
plugdata_draw(obj, gensym("lua_draw_line"), 5, args);
}

static int draw_text(lua_State* L) {
t_pdlua* obj = get_current_object(L);
const char* text = luaL_checkstring(L, 1);
Expand Down Expand Up @@ -436,7 +397,6 @@ static int reset_transform(lua_State* L) {
static void gfx_free(t_pdlua_gfx* gfx)
{
freebytes(gfx->path_segments, gfx->num_path_segments_allocated * sizeof(int));
clock_delay(((t_pdlua_mouse_proxy*)gfx->mouse_proxy)->free_clock, 0);
}

void pdlua_gfx_clear(t_pdlua *obj) {
Expand All @@ -445,8 +405,10 @@ void pdlua_gfx_clear(t_pdlua *obj) {

for(int i = 0; i < gfx->num_tags; i++)
{
pdgui_vmess(0, "crs", cnv, "delete", gfx->active_tags[i]);
//pdgui_vmess(0, "crs", cnv, "delete", gfx->active_tags[i]);
}

pdgui_vmess(0, "crs", cnv, "delete", gfx->object_tag);
gfx->num_tags = 0;
}

Expand Down Expand Up @@ -494,13 +456,6 @@ static int gfx_initialize(t_pdlua *obj)
snprintf(gfx->object_tag, 128, ".x%lx", obj);
gfx->object_tag[127] = '\0';
pdlua_gfx_repaint(obj);

char buf[MAXPDSTRING];
snprintf(buf, MAXPDSTRING-1, ".x%lx", (unsigned long)cnv);
buf[MAXPDSTRING-1] = 0;

gfx->mouse_proxy = pdlua_mouse_proxy_new(obj, gensym(buf));

return 0;
}

Expand Down Expand Up @@ -690,6 +645,32 @@ static int stroke_rounded_rect(lua_State* L) {
return 0;
}

static int draw_line(lua_State* L) {
t_pdlua* obj = get_current_object(L);
t_pdlua_gfx *gfx = &obj->gfx;
t_canvas *cnv = glist_getcanvas(obj->canvas);

const char* text = luaL_checkstring(L, 1); // Assuming text is a string
int x1 = luaL_checknumber(L, 1);
int y1 = luaL_checknumber(L, 2);
int x2 = luaL_checknumber(L, 3);
int y2 = luaL_checknumber(L, 3);
int lineWidth = luaL_checknumber(L, 4);

int zoom = glist_getzoom(cnv);
x1 *= gfx->scale_x * zoom;
y1 *= gfx->scale_y * zoom;
x2 *= gfx->scale_x * zoom;
y2 *= gfx->scale_y * zoom;
lineWidth *= zoom;

const char* tags[] = { gfx->object_tag, register_drawing(obj) };

pdgui_vmess(0, "crr iiii ri rs rS", cnv, "create", "line", x1, y1, x2, y2,
"-width", lineWidth, "-fill", gfx->current_color, "-tags", 2, tags);
}


static int draw_text(lua_State* L) {
t_pdlua* obj = get_current_object(L);
t_pdlua_gfx *gfx = &obj->gfx;
Expand Down

0 comments on commit 3e0cd72

Please sign in to comment.