Skip to content

Commit

Permalink
Merge pull request #42 from timothyschoen/clean-up
Browse files Browse the repository at this point in the history
Better way to manage currently active object, small API change, updated documentation, more...
  • Loading branch information
agraef committed Mar 4, 2024
2 parents d7f6fe8 + 1d1dbdd commit 1f6e054
Show file tree
Hide file tree
Showing 8 changed files with 509 additions and 510 deletions.
59 changes: 30 additions & 29 deletions doc/graphics.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ NOTE: The "paint" method (see below) is required by all pdlua objects which want

Painting:
--------------
You can paint by defining the "paint" function, for example:
You can paint by defining the "paint" function, for example:

function yourclass:paint()
gfx.set_color(250, 200, 240)
gfx.fill_all()
function yourclass:paint(g)
g:set_color(250, 200, 240)
g:fill_all()
end


Expand All @@ -29,45 +29,46 @@ end
API overview
--------------
-- Callback functions you can define
paint() -- Paint callback. This is the only place where you're allowed to call any of the paint functions

pd:Class:mouse_down(x, y) -- Mouse down callback, called when the mouse is clicked
pd:Class:mouse_up(x, y) -- Mouse up callback, called when the mouse button is released
pd:Class:mouse_move(x, y) -- Mouse move callback, called when the mouse is moved while not being down
pd:Class:mouse_drag(x, y) -- Mouse drag callback, called when the mouse is moved while also being down

-- Functions you can call
pd:Class:repaint() -- Request a repaint, after this the "paint" callback will occur

gfx.set_size(w, h) -- Sets the size of the drawing object.
pd:Class:repaint() -- Request a repaint, after this the "paint" callback will occur
pd:Class:paint(g) -- Paint callback, returns a graphics_context object (commonly called g) that you can call these drawing functions on:
g:set_size(w, h) -- Sets the size of the object.
width, height = g:get_size(w, h) -- Gets the size of the object.

gfx.set_color(r, g, b, a=1.0) -- Sets the color for the next drawing operation.
g:set_color(r, g, b, a=1.0) -- Sets the color for the next drawing operation.

gfx.fill_ellipse(x, y, w, h) -- Draws a filled ellipse at the specified position and size.
gfx.stroke_ellipse(x, y, w, h, line_width) -- Draws the outline of an ellipse at the specified position and size.
g:fill_ellipse(x, y, w, h) -- Draws a filled ellipse at the specified position and size.
g:stroke_ellipse(x, y, w, h, line_width) -- Draws the outline of an ellipse at the specified position and size.

gfx.fill_rect(x, y, w, h) -- Draws a filled rectangle at the specified position and size.
gfx.stroke_rect(x, y, w, h, line_width) -- Draws the outline of a rectangle at the specified position and size.
g:fill_rect(x, y, w, h) -- Draws a filled rectangle at the specified position and size.
g:stroke_rect(x, y, w, h, line_width) -- Draws the outline of a rectangle at the specified position and size.

gfx.fill_rounded_rect(x, y, w, h, corner_radius) -- Draws a filled rounded rectangle at the specified position and size.
gfx.troke_rounded_rect(x, y, w, h, corner_radius, line_width) -- Draws the outline of a rounded rectangle at the specified position and size.
g:fill_rounded_rect(x, y, w, h, corner_radius) -- Draws a filled rounded rectangle at the specified position and size.
g:stroke_rounded_rect(x, y, w, h, corner_radius, line_width) -- Draws the outline of a rounded rectangle at the specified position and size.

gfx.draw_line(x1, y1, x2, y2) -- Draws a line between two points.
gfx.draw_text(text, x, y, w, fontsize) -- Draws text at the specified position and size.
g:draw_line(x1, y1, x2, y2) -- Draws a line between two points.
g:draw_text(text, x, y, w, fontsize) -- Draws text at the specified position and size.

gfx.start_path(x, y) -- Initiates a new path at the specified point.
gfx.line_to(x, y) -- Adds a line segment to the current path.
gfx.quad_to(x1, y1, x2, y2) -- Adds a quadratic Bezier curve to the current path.
gfx.cubic_to(x1, y1, x2, y2, x3, y) -- Adds a cubic Bezier curve to the current path.
gfx.close_path() -- Closes the current path.
gfx.stroke_path(line_width) -- Draws the outline of the current path with the specified line width.
gfx.fill_path() -- Fills the current path.
g:fill_all() -- Fills the entire drawing area with the current color. Also will draw an object outline in the style of the host (ie. pure-data or plugdata)

gfx.fill_all() -- Fills the entire drawing area with the current color. Also will draw an object outline in the style of the host (ie. pure-data or plugdata)
g:translate(tx, ty) -- Translates the coordinate system by the specified amounts.
g:scale(sx, sy) -- Scales the coordinate system by the specified factors. This will always happen after the translation
g:reset_transform() -- Resets current scale and translation

gfx.translate(tx, ty) -- Translates the coordinate system by the specified amounts.
gfx.scale(sx, sy) -- Scales the coordinate system by the specified factors. This will always happen after the translation
gfx.reset_transform() -- Resets current scale and translation
p = Path(x, y) -- Initiates a new path at the specified point
p:line_to(x, y) -- Adds a line segment to the path.
p:quad_to(x1, y1, x2, y2) -- Adds a quadratic Bezier curve to the path.
p:cubic_to(x1, y1, x2, y2, x3, y) -- Adds a cubic Bezier curve to the path.
p:close_path() -- Closes the path.

g:stroke_path(p, line_width) -- Draws the outline of the path with the specified line width.
g:fill_path(p) -- Fills the current path.

Basic example
---------------------
Expand All @@ -90,7 +91,7 @@ function example:mouse_drag(x, y)
self.circle_y = y - 15

-- Request a repaint
self:repaint()
self:repaint()
end

function example:paint()
Expand Down
23 changes: 13 additions & 10 deletions pd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -400,29 +400,32 @@ end

function pd.Class:initialize(sel, atoms) end

function pd.Class:postinitialize() end

function pd.Class:finalize() end

function pd.Class:set_args(args)
pd._set_args(self._object, args)
end

function pd.Class:repaint()
if type(self.paint) == "function" then
local g = _gfx_internal.gfx_new()
local can_paint = _gfx_internal.start_paint();
if can_paint then
local g = _gfx_internal.start_paint(self._object);
if g ~= nil then
self:paint(g);
_gfx_internal.end_paint();
_gfx_internal.end_paint(g);
end
end
end

function pd.Class:get_size()
return _gfx_internal.get_size()
return _gfx_internal.get_size(self._object)
end

function pd.Class:set_size(width, height)
return _gfx_internal.set_size(width, height)
return _gfx_internal.set_size(self._object, width, height)
end

function pd.Class:postinitialize() end

function pd.Class:finalize() end

function pd.Class:dofilex(file)
-- in case of register being called, make sure
-- classes in other paths aren't getting affected
Expand Down
16 changes: 8 additions & 8 deletions pdlua-help.pd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#N canvas 466 36 564 576 10;
#N canvas 466 36 560 603 10;
#X declare -lib pdlua -path pdlua;
#X declare -path pdlua/examples;
#X text 16 358 See also:;
Expand Down Expand Up @@ -174,7 +174,7 @@
#X connect 111 0 65 0;
#X connect 112 0 67 0;
#X restore 446 359 pd quickstart;
#N canvas 21 133 772 759 graphics 0;
#N canvas 106 35 1079 763 graphics 0;
#X obj 8 77 hello-gui;
#X text 8 376 function yourclass:initialize(sel \, atoms);
#X text 24 409 return true;
Expand All @@ -190,13 +190,13 @@
#X text 25 725 pd.post(tostring(y));
#X text 8 788 -------------- API: --------------;
#X text 8 509 function yourclass:paint(g);
#X text 25 526 g.set_color(250 \, 200 \, 240);
#X text 25 543 g.fill_all();
#X text 23 394 self.set_size(100 \, 100);
#X text 8 11 pdlua's graphics allow you to draw basic vector graphics and receive mouse events on pure-data and plugdata. Drawing functions need to be called on the graphics context class \, which is passed into the paint() function as the first argument., f 72;
#X text 8 816 pd:Class:repaint() \; \; pd:Class:mouse_down(x \, y) \; pd:Class:mouse_up(x \, y) \; pd:Class:mouse_move(x \, y) \; pd:Class:mouse_drag(x \, y) \; \; \; pd:Class:paint(g) \; \; g.set_size(w \, h) \; g.set_color(r \, g \, b \, a=1.0) \; \; g.fill_ellipse(x \, y \, w \, h) \; g.stroke_ellipse(x \, y \, w \, h \, line_width) \; g.fill_rect(x \, y \, w \, h) \; g.stroke_rect(x \, y \, w \, h \, line_width) \; g.fill_rounded_rect(x \, y \, w \, h \, corner_radius) \; g.stroke_rounded_rect(x \, y \, w \, h \, corner_radius \, line_width) \; \; g.draw_line(x1 \, y1 \, x2 \, y2) \; g.draw_text(text \, x \, y \, w \, fontsize) \; \; g.start_path(x \, y) \; g.line_to(x \, y) \; g.quad_to(x1 \, y1 \, x2 \, y2) \; g.cubic_to(x1 \, y1 \, x2 \, y2 \, x3 \, y) \; g.close_path() \; g.stroke_path(line_width) \; g.fill_path() \; \; g.fill_all() \; \; g.translate(tx \, ty) \; g.scale(sx \, sy) \; g.reset_transform() \;, f 58;
#X text 353 816 -- Request a repaint \, after this the "paint" callback will occur \; \; -- Mouse down callback \, called when the mouse is clicked \; -- Mouse up callback \, called when the mouse button is released \; -- Mouse move callback \, called when the mouse is moved while not being down \; -- Mouse drag callback \, called when the mouse is moved while also being down \; \; \; \; -- Paint callback The argument "g" is the graphics context that you can call these drawing functions on: -- Sets the size of the drawing object. \; -- Sets the color for the next drawing operation. \; \; -- Draws a filled ellipse at the specified position and size. \; -- Draws the outline of an ellipse at the specified position and size. \; -- Draws a filled rectangle at the specified position and size. \; -- Draws the outline of a rectangle at the specified position and size. \; -- Draws a filled rounded rectangle at the specified position and size. \; -- Draws the outline of a rounded rectangle at the specified position and size. \; \; -- Draws a line between two points. \; -- Draws text at the specified position and size. \; \; -- Initiates a new path at the specified point. \; -- Adds a line segment to the current path. \; -- Adds a quadratic Bezier curve to the current path. \; -- Adds a cubic Bezier curve to the current path. \; -- Closes the current path. \; -- Draws the outline of the current path with the specified line width. \; -- Fills the current path. \; \; -- Fills the entire drawing area with the current color. Also will draw an object outline in the style of the host (ie. pure-data or plugdata) \; \; -- Translates the coordinate system by the specified amounts. \; -- Scales the coordinate system by the specified factors. This will always happen after the translation \; -- Resets current scale and translation \;, f 134;
#X text 8 323 Graphics mode is enabled automatically by defining the paint method \, see below. You can also set the size in the constructor \, like this:;
#X text 25 526 g:set_color(250 \, 200 \, 240);
#X text 25 543 g:fill_all();
#X text 8 323 Graphics mode is enabled automatically by defining the paint method \, see below. You can also set the size in the constructor (or in any other function) \, like this:;
#X text 23 394 self:set_size(100 \, 100);
#X text 373 818 -- Mouse down callback \, called when the mouse is clicked \; -- Mouse up callback \, called when the mouse button is released \; -- Mouse move callback \, called when the mouse is moved while not being down \; -- Mouse drag callback \, called when the mouse is moved while also being down \; \; -- Set object size \; -- Get object size \; \; -- Request a repaint \, after this the "paint" callback will occur \; -- Paint callback The argument "g" is the graphics context that you can call these drawing functions on: \; -- Sets the color for the next drawing operation. Colors are in range 0-255 \, optional alpha is in range 0-1 \; -- Draws a filled ellipse at the specified position and size. \; -- Draws the outline of an ellipse at the specified position and size. \; -- Draws a filled rectangle at the specified position and size. \; -- Draws the outline of a rectangle at the specified position and size. \; -- Draws a filled rounded rectangle at the specified position and size. \; -- Draws the outline of a rounded rectangle at the specified position and size. \; \; \; -- Draws a line between two points. \; -- Draws text at the specified position and size. \; \; -- Fills the entire drawing area with the current color. Also will draw an object outline in the style of the host (ie. pure-data or plugdata) \; \; -- Translates the coordinate system by the specified amounts. \; -- Scales the coordinate system by the specified factors. This will always happen after the translation \; -- Resets current scale and translation \; \; -- Initiates a new path at the specified point. \; -- Adds a line segment to the current path. \; -- Adds a quadratic Bezier curve to the current path. \; -- Adds a cubic Bezier curve to the current path. \; -- Closes the current path. \; \; -- Draws the outline of the current path with the specified line width. \; -- Fills the current path. \;, f 115;
#X text 8 816 pd:Class:mouse_down(x \, y) \; pd:Class:mouse_up(x \, y) \; pd:Class:mouse_move(x \, y) \; pd:Class:mouse_drag(x \, y) \; \; pd:Class:set_size(w \, h) \; width \, height = pd:Class:get_size() \; \; pd:Class:repaint() \; pd:Class:paint(g) \; \; g:set_color(r \, g \, b \, a=1.0) \; \; g:fill_ellipse(x \, y \, w \, h) \; g:stroke_ellipse(x \, y \, w \, h \, line_width) \; g:fill_rect(x \, y \, w \, h) \; g:stroke_rect(x \, y \, w \, h \, line_width) \; g:fill_rounded_rect(x \, y \, w \, h \, corner_radius) \; g:stroke_rounded_rect(x \, y \, w \, h \, corner_radius \, line_width) \; \; g:draw_line(x1 \, y1 \, x2 \, y2) \; g:draw_text(text \, x \, y \, w \, fontsize) \; \; g:fill_all() \; \; : g:translate(tx \, ty) \; g:scale(sx \, sy) \; g:reset_transform() \; \; p = path.start(x \, y) \; p:line_to(x \, y) \; p:quad_to(x1 \, y1 \, x2 \, y2) \; p:cubic_to(x1 \, y1 \, x2 \, y2 \, x3 \, y) \; p:close_path() \; \; g:stroke_path(p \, line_width) \; g:fill_path(p) \;, f 58;
#X restore 446 409 pd graphics;
#X text 324 384 Details on how to create GUI objects ------->, f 18;
#X obj 342 227 hello;
Expand Down
Loading

0 comments on commit 1f6e054

Please sign in to comment.