Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions pd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@ pd._perform_dsp = function (object, ...)
end
end

-- draw properties
pd._properties = function (object)
local obj = pd._objects[object]
if nil ~= obj and type(obj.properties) == "function" then
obj:properties()
return true
else
pd.post("not found")
return false
end
end

-- set properties
pd._set_properties = function (object, method, args)
local obj = pd._objects[object]
if obj ~= nil and type(obj[method]) == "function" then
local propertiesmethod = obj[method]
propertiesmethod(obj, args) -- Passa `obj` explicitamente como `self`
else
pd._error(obj._object, "method ".. method .. " does not exist")
end
end

-- repaint method dispatcher
pd._repaint = function (object)
local obj = pd._objects[object]
Expand Down Expand Up @@ -457,6 +480,26 @@ function pd.Class:set_args(args)
pd._set_args(self._object, args)
end

function pd.Class:addproperties()
pd._properties_add(self._object)
end

function pd.Class:newframe(title, max_col)
pd._properties_newframe(self._object, title, max_col)
end

function pd.Class:addcheckbox(text, method, init_value)
pd._properties_addcheckbox(self._object, text, method, init_value)
end

function pd.Class:addtextinput(text, method, init_value, width)
pd._properties_addtextinput(self._object, text, method, init_value, width)
end

function pd.Class:addcolorpicker(text, method)
pd._properties_addcolorpicker(self._object, text, method)
end

function pd.Class:canvas_realizedollar(s)
return pd._canvas_realizedollar(self._object, s)
end
Expand Down
24 changes: 24 additions & 0 deletions pdlua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,11 @@ static int pdlua_class_new(lua_State *L)
pdlua_widgetbehavior.w_visfn = pdlua_vis;
pdlua_widgetbehavior.w_activatefn = pdlua_activate;
class_setwidget(c_gfx, &pdlua_widgetbehavior);

// NOTE: It is possible to do this just for the object, not all gui objects
class_setpropertiesfn(c_gfx, pdlua_properties);
class_addmethod(c_gfx, (t_method)pdlua_properties_receiver, gensym("_properties"), A_GIMME, 0);

}

lua_pushlightuserdata(L, c);
Expand Down Expand Up @@ -2802,6 +2807,25 @@ static void pdlua_init(lua_State *L)
lua_pushstring(L, "_error");
lua_pushcfunction(L, pdlua_error);
lua_settable(L, -3);

// properties
lua_pushstring(L, "_properties_add");
lua_pushcfunction(L, pdlua_properties_add);
lua_settable(L, -3);

lua_pushstring(L, "_properties_newframe");
lua_pushcfunction(L, pdlua_properties_newframe);
lua_settable(L, -3);
lua_pushstring(L, "_properties_addcheckbox");
lua_pushcfunction(L, pdlua_properties_addcheckbox);
lua_settable(L, -3);
lua_pushstring(L, "_properties_addtextinput");
lua_pushcfunction(L, pdlua_properties_addtextinput);
lua_settable(L, -3);
lua_pushstring(L, "_properties_addcolorpicker");
lua_pushcfunction(L, pdlua_properties_addcolorpicker);
lua_settable(L, -3);

/* 20240906 ag: Added TIMEUNITPERMSEC, systime and timesince, to make
clock_set useable. NOTE: TIMEUNITPERMSEC is the time unit for systime,
timesince, and clock_set and is from m_sched.c. It isn't in the Pd
Expand Down
11 changes: 11 additions & 0 deletions pdlua.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ typedef struct _pdlua_gfx
// Variables to keep track of mouse button state and drag position
int mouse_drag_x, mouse_drag_y, mouse_down;
int first_draw;

// variables to set properties
t_symbol *current_frame;
t_symbol *properties_receiver;
int frame_count;
int max_col, max_row;
int current_col, current_row;

int checkbox_count;
int numberbox_count;
int colorpicker_count;

#else
int current_layer;
Expand Down
5 changes: 5 additions & 0 deletions pdlua/tutorial/examples/properties-help.pd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#N canvas 963 23 951 1016 10;
#X declare -lib pdlua;
#X obj 5 7 declare -lib pdlua;
#X obj 254 59 tgl 18 0 empty empty empty 0 -9 0 10 #fcfcfc #000000 #000000 0 1;
#X obj 66 69 properties;
64 changes: 64 additions & 0 deletions pdlua/tutorial/examples/properties.pd_lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local properties = pd.Class:new():register("properties")

function properties:initialize(sel, atoms)
self.inlets = 1
self.outlets = 1
self.phase = 0
self:addproperties()
self:set_size(127, 127)

self.checkbox1 = 1
self.checkbox2 = 1
self.color = {155,155,155}

self.textinput1 = "Hello"
self.textinput2 = 40
self.bigstring = "Uidsja hasd asdhj asdy asdhjasd"

return true
end

function properties:properties()
self:newframe("First CheckBox", 2)
self:addcheckbox("Check Box 1", "updatecheckbox1", self.checkbox1)
self:addcheckbox("Check Box 2", "updatecheckbox2", self.checkbox2)

self:newframe("First textinput", 2)
self:addtextinput("Check textinput 1", "updatetext1", self.textinput1, 5)
self:addtextinput("Check textinput 2", "updatetext2", self.textinput2, 5)

self:newframe("My Color Picker", 1)
self:addcolorpicker("Background", "updatecolorbg");
end

function properties:updatecolorbg(args)
self.color[1] = args[1][1]
self.color[2] = args[1][2]
self.color[3] = args[1][3]
self:repaint(1)
end

function properties:updatetext1(args)
self.textinput1 = args[1]
pd.post("textinput1 is now " .. self.textinput1);
end

function properties:updatetext2(args)
self.textinput2 = args[1]
pd.post("textinput2 is now " .. self.textinput2);
end

function properties:updatecheckbox1(args)
self.checkbox1 = args[1]
pd.post("checkbox1 is now " .. self.checkbox1);
end

function properties:updatecheckbox2(args)
self.checkbox2 = args[1]
pd.post("checkbox2 is now " .. self.checkbox2);
end

function properties:paint(g)
g:set_color(table.unpack(self.color))
g:fill_all()
end
Loading