1
- local hello = pd.Class:new():register("hello") function hello:initialize(sel, atoms) self.inlets = 1 self.circle_x = 480 self.circle_y = 15 self.circle_radius = 15 self.animation_speed = 2 self.draggable_rect_x = 550 self.draggable_rect_y = 130 self.draggable_rect_size = 50 self.dragging_rect = false self.mouse_down_pos = {0, 0} self.rect_down_pos = {0, 0} self.gui = 1 gfx.set_size(630, 230) return true end function math.clamp(val, lower, upper) if lower > upper then lower, upper = upper, lower end -- swap if boundaries supplied the wrong way return math.max(lower, math.min(upper, val)) end function hello:postinitialize() self.clock = pd.Clock:new():register(self, "tick") self.clock:delay(16) end function hello:finalize() self.clock:destruct() end function hello:mouse_down(x, y) if x > self.draggable_rect_x and y > self.draggable_rect_y and x < self.draggable_rect_x + self.draggable_rect_size and y < self.draggable_rect_y + self.draggable_rect_size then dragging_rect = true self.mouse_down_pos[0] = x self.mouse_down_pos[1] = y self.rect_down_pos[0] = self.draggable_rect_x self.rect_down_pos[1] = self.draggable_rect_y else dragging_rect = false end end function hello:mouse_drag(x, y) if dragging_rect == true then self.draggable_rect_x = self.rect_down_pos[0] + (x - self.mouse_down_pos[0]) self.draggable_rect_y = self.rect_down_pos[1] + (y - self.mouse_down_pos[1]) self.draggable_rect_x = math.clamp(self.draggable_rect_x, 0, 620 - self.draggable_rect_size) self.draggable_rect_y = math.clamp(self.draggable_rect_y, 0, 230 - self.draggable_rect_size) self:repaint() end end function hello:paint() gfx.set_color(250, 200, 240) gfx.fill_all() -- Filled examples gfx.set_color(66, 207, 201, 0.3) gfx.fill_ellipse(30, 50, 30, 30) gfx.set_color(0, 159, 147, 1) gfx.fill_rect(120, 50, 30, 30) gfx.set_color(250, 84, 108, 1) gfx.fill_rounded_rect(210, 50, 30, 30, 5) gfx.set_color(252, 118, 81, 1) -- Star using line_to paths local starX1, starY1 = 310, 45 local starSize = 15 gfx.start_path(starX1, starY1) -- Star using line_to paths gfx.line_to(starX1 + 5, starY1 + 14) gfx.line_to(starX1 + 20, starY1 + 14) gfx.line_to(starX1 + 8, starY1 + 22) gfx.line_to(starX1 + 14, starY1 + 36) gfx.line_to(starX1, starY1 + 27) gfx.line_to(starX1 - 14, starY1 + 36) gfx.line_to(starX1 - 6, starY1 + 22) gfx.line_to(starX1 - 18, starY1 + 14) gfx.line_to(starX1 - 3, starY1 + 14) gfx.close_path() gfx.fill_path() gfx.set_color(255, 219, 96, 1) -- Bezier curve example gfx.translate(140, 20) gfx.scale(0.5, 1.0) gfx.start_path(450, 50) gfx.cubic_to(500, 30, 550, 70, 600, 50) gfx.close_path() gfx.stroke_path(2) gfx.reset_transform() -- Stroked examples gfx.set_color(66, 207, 201, 1) gfx.stroke_ellipse(30, 150, 30, 30, 2) gfx.set_color(0, 159, 147, 1) gfx.stroke_rect(120, 150, 30, 30, 2) gfx.set_color(250, 84, 108, 1) gfx.stroke_rounded_rect(210, 150, 30, 30, 5, 2) gfx.set_color(252, 118, 81, 1) local starX2, starY2 = 310, 145 local starSize = 15 -- Star using line_to paths gfx.start_path(starX2, starY2) gfx.line_to(starX2 + 5, starY2 + 14) gfx.line_to(starX2 + 20, starY2 + 14) gfx.line_to(starX2 + 8, starY2 + 22) gfx.line_to(starX2 + 14, starY2 + 36) gfx.line_to(starX2, starY2 + 27) gfx.line_to(starX2 - 14, starY2 + 36) gfx.line_to(starX2 - 6, starY2 + 22) gfx.line_to(starX2 - 18, starY2 + 14) gfx.line_to(starX2 - 3, starY2 + 14) gfx.close_path() gfx.stroke_path(2) gfx.set_color(255, 219, 96, 1) -- Bezier curve example gfx.translate(140, 20) gfx.scale(0.5, 1.0) gfx.start_path(450, 150) gfx.cubic_to(500, 130, 550, 170, 600, 150) gfx.fill_path() gfx.reset_transform() -- Draggable rectangle gfx.set_color(66, 207, 201, 1) gfx.fill_rounded_rect(self.draggable_rect_x, self.draggable_rect_y, self.draggable_rect_size, self.draggable_rect_size, 5) gfx.set_color(0, 0, 0, 1) gfx.draw_text("Drag me!", self.draggable_rect_x + 8, self.draggable_rect_y + 20, self.draggable_rect_size, 10) -- Titles gfx.set_color(252, 118, 81, 1) gfx.draw_text("Ellipse", 32, 190, 120, 10) gfx.draw_text("Rectangle", 116, 190, 120, 10) gfx.draw_text("Rounded Rectangle", 188, 190, 120, 10) gfx.draw_text("Paths", 300, 190, 120, 10) gfx.draw_text("Bezier Paths", 380, 190, 120, 10) gfx.draw_text("Animation", 470, 190, 120, 10) gfx.draw_text("Mouse Interaction", 540, 190, 120, 10) gfx.set_color(250, 84, 108, 1) gfx.fill_ellipse(self.circle_x, self.circle_y, self.circle_radius, self.circle_radius) end function hello:tick() self.circle_y = self.circle_y + self.animation_speed if self.circle_y > 160 + self.circle_radius then self.circle_y = -self.circle_radius end self:repaint() self.clock:delay(16) end function hello:in_1_bang() self:repaint() end
1
+ local hello = pd .Class :new ():register (" hello-gui" )
2
+
3
+ function hello :initialize (sel , atoms )
4
+ self .inlets = 1
5
+
6
+ self .circle_x = 480
7
+ self .circle_y = 15
8
+ self .circle_radius = 15
9
+ self .animation_speed = 2
10
+
11
+ self .draggable_rect_x = 550
12
+ self .draggable_rect_y = 130
13
+ self .draggable_rect_size = 50
14
+ self .dragging_rect = false
15
+ self .mouse_down_pos = {0 , 0 }
16
+ self .rect_down_pos = {0 , 0 }
17
+
18
+ self .gui = 1
19
+ gfx .set_size (630 , 230 )
20
+ return true
21
+ end
22
+
23
+ function math .clamp (val , lower , upper )
24
+ if lower > upper then lower , upper = upper , lower end -- swap if boundaries supplied the wrong way
25
+ return math.max (lower , math.min (upper , val ))
26
+ end
27
+
28
+ function hello :postinitialize ()
29
+ self .clock = pd .Clock :new ():register (self , " tick" )
30
+ self .clock :delay (16 )
31
+ end
32
+
33
+ function hello :finalize ()
34
+ self .clock :destruct ()
35
+ end
36
+
37
+ function hello :mouse_down (x , y )
38
+ if x > self .draggable_rect_x and y > self .draggable_rect_y and x < self .draggable_rect_x + self .draggable_rect_size and y < self .draggable_rect_y + self .draggable_rect_size then
39
+ dragging_rect = true
40
+ self .mouse_down_pos [0 ] = x
41
+ self .mouse_down_pos [1 ] = y
42
+ self .rect_down_pos [0 ] = self .draggable_rect_x
43
+ self .rect_down_pos [1 ] = self .draggable_rect_y
44
+ else
45
+ dragging_rect = false
46
+ end
47
+ end
48
+
49
+ function hello :mouse_drag (x , y )
50
+ if dragging_rect == true then
51
+ self .draggable_rect_x = self .rect_down_pos [0 ] + (x - self .mouse_down_pos [0 ])
52
+ self .draggable_rect_y = self .rect_down_pos [1 ] + (y - self .mouse_down_pos [1 ])
53
+ self .draggable_rect_x = math .clamp (self .draggable_rect_x , 0 , 620 - self .draggable_rect_size )
54
+ self .draggable_rect_y = math .clamp (self .draggable_rect_y , 0 , 230 - self .draggable_rect_size )
55
+ self :repaint ()
56
+ end
57
+ end
58
+
59
+ function hello :paint ()
60
+ gfx .set_color (250 , 200 , 240 )
61
+ gfx .fill_all ()
62
+
63
+ -- Filled examples
64
+ gfx .set_color (66 , 207 , 201 , 0.3 )
65
+ gfx .fill_ellipse (30 , 50 , 30 , 30 )
66
+ gfx .set_color (0 , 159 , 147 , 1 )
67
+ gfx .fill_rect (120 , 50 , 30 , 30 )
68
+ gfx .set_color (250 , 84 , 108 , 1 )
69
+ gfx .fill_rounded_rect (210 , 50 , 30 , 30 , 5 )
70
+
71
+ gfx .set_color (252 , 118 , 81 , 1 )
72
+
73
+ -- Star using line_to paths
74
+ local starX1 , starY1 = 310 , 45
75
+ local starSize = 15
76
+
77
+ gfx .start_path (starX1 , starY1 )
78
+
79
+ -- Star using line_to paths
80
+ gfx .line_to (starX1 + 5 , starY1 + 14 )
81
+ gfx .line_to (starX1 + 20 , starY1 + 14 )
82
+ gfx .line_to (starX1 + 8 , starY1 + 22 )
83
+ gfx .line_to (starX1 + 14 , starY1 + 36 )
84
+ gfx .line_to (starX1 , starY1 + 27 )
85
+ gfx .line_to (starX1 - 14 , starY1 + 36 )
86
+ gfx .line_to (starX1 - 6 , starY1 + 22 )
87
+ gfx .line_to (starX1 - 18 , starY1 + 14 )
88
+ gfx .line_to (starX1 - 3 , starY1 + 14 )
89
+ gfx .close_path ()
90
+ gfx .fill_path ()
91
+
92
+ gfx .set_color (255 , 219 , 96 , 1 )
93
+ -- Bezier curve example
94
+ gfx .translate (140 , 20 )
95
+ gfx .scale (0.5 , 1.0 )
96
+ gfx .start_path (450 , 50 )
97
+ gfx .cubic_to (500 , 30 , 550 , 70 , 600 , 50 )
98
+ gfx .close_path ()
99
+ gfx .stroke_path (2 )
100
+ gfx .reset_transform ()
101
+
102
+ -- Stroked examples
103
+ gfx .set_color (66 , 207 , 201 , 1 )
104
+ gfx .stroke_ellipse (30 , 150 , 30 , 30 , 2 )
105
+ gfx .set_color (0 , 159 , 147 , 1 )
106
+ gfx .stroke_rect (120 , 150 , 30 , 30 , 2 )
107
+ gfx .set_color (250 , 84 , 108 , 1 )
108
+ gfx .stroke_rounded_rect (210 , 150 , 30 , 30 , 5 , 2 )
109
+
110
+ gfx .set_color (252 , 118 , 81 , 1 )
111
+
112
+ local starX2 , starY2 = 310 , 145
113
+ local starSize = 15
114
+
115
+ -- Star using line_to paths
116
+ gfx .start_path (starX2 , starY2 )
117
+ gfx .line_to (starX2 + 5 , starY2 + 14 )
118
+ gfx .line_to (starX2 + 20 , starY2 + 14 )
119
+ gfx .line_to (starX2 + 8 , starY2 + 22 )
120
+ gfx .line_to (starX2 + 14 , starY2 + 36 )
121
+ gfx .line_to (starX2 , starY2 + 27 )
122
+ gfx .line_to (starX2 - 14 , starY2 + 36 )
123
+ gfx .line_to (starX2 - 6 , starY2 + 22 )
124
+ gfx .line_to (starX2 - 18 , starY2 + 14 )
125
+ gfx .line_to (starX2 - 3 , starY2 + 14 )
126
+ gfx .close_path ()
127
+ gfx .stroke_path (2 )
128
+
129
+ gfx .set_color (255 , 219 , 96 , 1 )
130
+ -- Bezier curve example
131
+ gfx .translate (140 , 20 )
132
+ gfx .scale (0.5 , 1.0 )
133
+ gfx .start_path (450 , 150 )
134
+ gfx .cubic_to (500 , 130 , 550 , 170 , 600 , 150 )
135
+ gfx .fill_path ()
136
+ gfx .reset_transform ()
137
+
138
+ -- Draggable rectangle
139
+ gfx .set_color (66 , 207 , 201 , 1 )
140
+ gfx .fill_rounded_rect (self .draggable_rect_x , self .draggable_rect_y , self .draggable_rect_size , self .draggable_rect_size , 5 )
141
+ gfx .set_color (0 , 0 , 0 , 1 )
142
+ gfx .draw_text (" Drag me!" , self .draggable_rect_x + 8 , self .draggable_rect_y + 20 , self .draggable_rect_size , 10 )
143
+
144
+ -- Titles
145
+ gfx .set_color (252 , 118 , 81 , 1 )
146
+ gfx .draw_text (" Ellipse" , 32 , 190 , 120 , 10 )
147
+ gfx .draw_text (" Rectangle" , 116 , 190 , 120 , 10 )
148
+ gfx .draw_text (" Rounded Rectangle" , 188 , 190 , 120 , 10 )
149
+ gfx .draw_text (" Paths" , 300 , 190 , 120 , 10 )
150
+ gfx .draw_text (" Bezier Paths" , 380 , 190 , 120 , 10 )
151
+ gfx .draw_text (" Animation" , 470 , 190 , 120 , 10 )
152
+ gfx .draw_text (" Mouse Interaction" , 540 , 190 , 120 , 10 )
153
+
154
+ gfx .set_color (250 , 84 , 108 , 1 )
155
+ gfx .fill_ellipse (self .circle_x , self .circle_y , self .circle_radius , self .circle_radius )
156
+ end
157
+
158
+ function hello :tick ()
159
+ self .circle_y = self .circle_y + self .animation_speed
160
+ if self .circle_y > 160 + self .circle_radius then
161
+ self .circle_y = - self .circle_radius
162
+ end
163
+ self :repaint ()
164
+ self .clock :delay (16 )
165
+ end
166
+
167
+
168
+ function hello :in_1_bang ()
169
+ self :repaint ()
170
+ end
0 commit comments