processing 0.5.33 → 0.5.34

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,8 @@ module Processing
3
3
 
4
4
  # Shape object.
5
5
  #
6
+ # @see https://processing.org/reference/PShape.html
7
+ #
6
8
  class Shape
7
9
 
8
10
  # @private
@@ -18,6 +20,8 @@ module Processing
18
20
  #
19
21
  # @return [Numeric] width of shape
20
22
  #
23
+ # @see https://processing.org/reference/PShape_width.html
24
+ #
21
25
  def width()
22
26
  polygon = getInternal__ or return 0
23
27
  (@bounds ||= polygon.bounds).width
@@ -27,6 +31,8 @@ module Processing
27
31
  #
28
32
  # @return [Numeric] height of shape
29
33
  #
34
+ # @see https://processing.org/reference/PShape_height.html
35
+ #
30
36
  def height()
31
37
  polygon = getInternal__ or return 0
32
38
  (@bounds ||= polygon.bounds).height
@@ -39,6 +45,8 @@ module Processing
39
45
  #
40
46
  # @return [Boolean] visible or not
41
47
  #
48
+ # @see https://processing.org/reference/PShape_isVisible_.html
49
+ #
42
50
  def isVisible()
43
51
  @visible
44
52
  end
@@ -49,11 +57,19 @@ module Processing
49
57
  #
50
58
  # @return [nil] nil
51
59
  #
60
+ # @see https://processing.org/reference/PShape_setVisible_.html
61
+ #
52
62
  def setVisible(visible)
53
63
  @visible = !!visible
54
64
  nil
55
65
  end
56
66
 
67
+ # Starts shape data definition.
68
+ #
69
+ # @return [nil] nil
70
+ #
71
+ # @see https://processing.org/reference/PShape_beginShape_.html
72
+ #
57
73
  def beginShape(type = nil)
58
74
  raise "beginShape() cannot be called twice" if drawingShape__
59
75
  @type = type
@@ -67,6 +83,12 @@ module Processing
67
83
  nil
68
84
  end
69
85
 
86
+ # Ends shape data definition.
87
+ #
88
+ # @return [nil] nil
89
+ #
90
+ # @see https://processing.org/reference/PShape_endShape_.html
91
+ #
70
92
  def endShape(close = nil)
71
93
  raise "endShape() must be called after beginShape()" unless drawingShape__
72
94
  @close = close == GraphicsContext::CLOSE || @contours.size > 0
@@ -78,12 +100,24 @@ module Processing
78
100
  nil
79
101
  end
80
102
 
103
+ # Starts a new contour definition.
104
+ #
105
+ # @return [nil] nil
106
+ #
107
+ # @see https://processing.org/reference/PShape_beginContour_.html
108
+ #
81
109
  def beginContour()
82
110
  raise "beginContour() must be called after beginShape()" unless drawingShape__
83
111
  @contourPoints, @contourColors, @contourTexCoords = [], [], []
84
112
  nil
85
113
  end
86
114
 
115
+ # Ends contour definition.
116
+ #
117
+ # @return [nil] nil
118
+ #
119
+ # @see https://processing.org/reference/PShape_endContour_.html
120
+ #
87
121
  def endContour()
88
122
  raise "endContour() must be called after beginContour()" unless drawingContour__
89
123
  @contours << Rays::Polyline.new(
@@ -93,6 +127,21 @@ module Processing
93
127
  nil
94
128
  end
95
129
 
130
+ # Append vertex for shape polygon.
131
+ #
132
+ # @overload vertex(x, y)
133
+ # @overload vertex(x, y, u, v)
134
+ #
135
+ # @param x [Numeric] x position of vertex
136
+ # @param y [Numeric] y position of vertex
137
+ # @param u [Numeric] u texture coordinate of vertex
138
+ # @param v [Numeric] v texture coordinate of vertex
139
+ #
140
+ # @return [nil] nil
141
+ #
142
+ # @see https://processing.org/reference/vertex_.html
143
+ # @see https://p5js.org/reference/#/p5/vertex
144
+ #
96
145
  def vertex(x, y, u = nil, v = nil)
97
146
  raise "vertex() must be called after beginShape()" unless drawingShape__
98
147
  raise "Either 'u' or 'v' is missing" if (u == nil) != (v == nil)
@@ -108,8 +157,19 @@ module Processing
108
157
  @colors << color
109
158
  @texcoords << u << v
110
159
  end
160
+ nil
111
161
  end
112
162
 
163
+ # Append curve vertex for shape polygon.
164
+ #
165
+ # @param x [Numeric] x position of vertex
166
+ # @param y [Numeric] y position of vertex
167
+ #
168
+ # @return [nil] nil
169
+ #
170
+ # @see https://processing.org/reference/curveVertex_.html
171
+ # @see https://p5js.org/reference/#/p5/curveVertex
172
+ #
113
173
  def curveVertex(x, y)
114
174
  raise "curveVertex() must be called after beginShape()" unless drawingShape__
115
175
  @curvePoints << x << y
@@ -121,6 +181,16 @@ module Processing
121
181
  nil
122
182
  end
123
183
 
184
+ # Append bezier vertex for shape polygon.
185
+ #
186
+ # @param x [Numeric] x position of vertex
187
+ # @param y [Numeric] y position of vertex
188
+ #
189
+ # @return [nil] nil
190
+ #
191
+ # @see https://processing.org/reference/bezierVertex_.html
192
+ # @see https://p5js.org/reference/#/p5/bezierVertex
193
+ #
124
194
  def bezierVertex(x2, y2, x3, y3, x4, y4)
125
195
  raise "bezierVertex() must be called after beginShape()" unless drawingShape__
126
196
  x1, y1 = @points[-2, 2]
@@ -131,6 +201,16 @@ module Processing
131
201
  nil
132
202
  end
133
203
 
204
+ # Append quadratic vertex for shape polygon.
205
+ #
206
+ # @param x [Numeric] x position of vertex
207
+ # @param y [Numeric] y position of vertex
208
+ #
209
+ # @return [nil] nil
210
+ #
211
+ # @see https://processing.org/reference/quadraticVertex_.html
212
+ # @see https://p5js.org/reference/#/p5/quadraticVertex
213
+ #
134
214
  def quadraticVertex(cx, cy, x3, y3)
135
215
  x1, y1 = @points[-2, 2]
136
216
  raise "vertex() is required before calling quadraticVertex()" unless x1 && y1
@@ -151,15 +231,57 @@ module Processing
151
231
  @contourPoints
152
232
  end
153
233
 
234
+ # Sets fill color.
235
+ #
236
+ # @overload fill(gray)
237
+ # @overload fill(gray, alpha)
238
+ # @overload fill(r, g, b)
239
+ # @overload fill(r, g, b, alpha)
240
+ #
241
+ # @param gray [Integer] gray value (0..255)
242
+ # @param r [Integer] red value (0..255)
243
+ # @param g [Integer] green value (0..255)
244
+ # @param b [Integer] blue value (0..255)
245
+ # @param alpha [Integer] alpha value (0..255)
246
+ #
247
+ # @return [nil] nil
248
+ #
249
+ # @see https://processing.org/reference/fill_.html
250
+ # @see https://p5js.org/reference/#/p5/fill
251
+ #
154
252
  def fill(*args)
155
253
  @fill = @context.rawColor__(*args)
254
+ nil
156
255
  end
157
256
 
257
+ # Sets the vertex at the index position.
258
+ #
259
+ # @overload setVertex(index, x, y)
260
+ # @overload setVertex(index, vec)
261
+ #
262
+ # @param index [Integer] the index fo the vertex
263
+ # @param x [Numeric] x position of the vertex
264
+ # @param y [Numeric] y position of the vertex
265
+ # @param vec [Vector] position for the vertex
266
+ #
267
+ # @return [nil] nil
268
+ #
269
+ # @see https://processing.org/reference/PShape_setVertex_.html
270
+ #
158
271
  def setVertex(index, point)
159
272
  return nil unless @points && @points[index * 2, 2]&.size == 2
160
273
  @points[index * 2, 2] = [point.x, point.y]
274
+ nil
161
275
  end
162
276
 
277
+ # Returns the vertex at the index position.
278
+ #
279
+ # @param index [Integer] the index fo the vertex
280
+ #
281
+ # @return [Vector] the vertex position
282
+ #
283
+ # @see https://processing.org/reference/PShape_getVertex_.html
284
+ #
163
285
  def getVertex(index)
164
286
  return nil unless @points
165
287
  point = @points[index * 2, 2]
@@ -167,11 +289,34 @@ module Processing
167
289
  @context.createVector(*point)
168
290
  end
169
291
 
292
+ # Returns the total number of vertices.
293
+ #
294
+ # @return [Integer] vertex count
295
+ #
296
+ # @see https://processing.org/reference/PShape_getVertexCount_.html
297
+ #
170
298
  def getVertexCount()
171
299
  return 0 unless @points
172
300
  @points.size / 2
173
301
  end
174
302
 
303
+ # Sets the fill color.
304
+ #
305
+ # @overload fill(gray)
306
+ # @overload fill(gray, alpha)
307
+ # @overload fill(r, g, b)
308
+ # @overload fill(r, g, b, alpha)
309
+ #
310
+ # @param gray [Integer] gray value (0..255)
311
+ # @param r [Integer] red value (0..255)
312
+ # @param g [Integer] green value (0..255)
313
+ # @param b [Integer] blue value (0..255)
314
+ # @param alpha [Integer] alpha value (0..255)
315
+ #
316
+ # @return [nil] nil
317
+ #
318
+ # @see https://processing.org/reference/PShape_setFill_.html
319
+ #
175
320
  def setFill(*args)
176
321
  color = @context.rawColor__(*args)
177
322
  count = getVertexCount
@@ -187,51 +332,154 @@ module Processing
187
332
  polylines.map {|pl| pl.with colors: pl.points.map {color}}
188
333
  end
189
334
  end
335
+ nil
190
336
  end
191
337
 
192
- def addChild(child)
338
+ # @private
339
+ def setStroke__()
340
+ raise NotImplementedError
341
+ end
342
+
343
+ # Adds a new child shape.
344
+ #
345
+ # @param child [Shape] child shape
346
+ # @param index [Integer] insert position
347
+ #
348
+ # @return [nil] nil
349
+ #
350
+ # @see https://processing.org/reference/PShape_addChild_.html
351
+ #
352
+ def addChild(child, index = -1)
193
353
  return unless @children
194
- @children.push child
354
+ if index < 0
355
+ @children.push child
356
+ else
357
+ @children.insert index, child
358
+ end
195
359
  nil
196
360
  end
197
361
 
362
+ # Returns a child shape at the index position.
363
+ #
364
+ # @param index [Integer] child index
365
+ #
366
+ # @return [nil] nil
367
+ #
368
+ # @see https://processing.org/reference/PShape_getChild_.html
369
+ #
198
370
  def getChild(index)
199
371
  @children&.[](index)
200
372
  end
201
373
 
374
+ # Returns the number of children.
375
+ #
376
+ # @return [Integer] child count
377
+ #
378
+ # @see https://processing.org/reference/PShape_getChildCount_.html
379
+ #
202
380
  def getChildCount()
203
381
  @children&.size || 0
204
382
  end
205
383
 
384
+ # Applies translation matrix to the shape.
385
+ #
386
+ # @overload translate(x, y)
387
+ # @overload translate(x, y, z)
388
+ #
389
+ # @param x [Numeric] left/right translation
390
+ # @param y [Numeric] up/down translation
391
+ # @param z [Numeric] forward/backward translation
392
+ #
393
+ # @return [nil] nil
394
+ #
395
+ # @see https://processing.org/reference/PShape_translate_.html
396
+ #
206
397
  def translate(x, y, z = 0)
207
398
  matrix__.translate! x, y, z
208
399
  nil
209
400
  end
210
401
 
211
- def rotate(angle)
212
- matrix__.rotate! @context.toDegrees__(angle)
402
+ # Applies scale matrix to the shape.
403
+ #
404
+ # @overload scale(s)
405
+ # @overload scale(x, y)
406
+ # @overload scale(x, y, z)
407
+ #
408
+ # @param s [Numeric] horizontal and vertical scale
409
+ # @param x [Numeric] horizontal scale
410
+ # @param y [Numeric] vertical scale
411
+ # @param z [Numeric] depth scale
412
+ #
413
+ # @return [nil] nil
414
+ #
415
+ # @see https://processing.org/reference/PShape_scale_.html
416
+ #
417
+ def scale(x, y = nil, z = 1)
418
+ matrix__.scale! x, (y || x), z
213
419
  nil
214
420
  end
215
421
 
216
- def scale(x, y, z = 1)
217
- matrix__.scale! x, y, z
422
+ # Applies rotation matrix to the shape.
423
+ #
424
+ # @param angle [Numeric] angle for rotation
425
+ #
426
+ # @return [nil] nil
427
+ #
428
+ # @see https://processing.org/reference/PShape_rotate_.html
429
+ #
430
+ def rotate(angle)
431
+ matrix__.rotate! @context.toDegrees__(angle)
218
432
  nil
219
433
  end
220
434
 
221
- def resetMatrix()
222
- @matrix = nil
223
- end
224
-
435
+ # Applies rotation around the x-axis to the shape.
436
+ #
437
+ # @param angle [Numeric] angle for rotation
438
+ #
439
+ # @return [nil] nil
440
+ #
441
+ # @see https://processing.org/reference/PShape_rotateX_.html
442
+ #
225
443
  def rotateX(angle)
226
444
  matrix__.rotate! @context.toDegrees__(angle), 1, 0, 0
445
+ nil
227
446
  end
228
447
 
448
+ # Applies rotation around the y-axis to the shape.
449
+ #
450
+ # @param angle [Numeric] angle for rotation
451
+ #
452
+ # @return [nil] nil
453
+ #
454
+ # @see https://processing.org/reference/PShape_rotateY_.html
455
+ #
229
456
  def rotateY(angle)
230
457
  matrix__.rotate! @context.toDegrees__(angle), 0, 1, 0
458
+ nil
231
459
  end
232
460
 
461
+ # Applies rotation around the z-axis to the shape.
462
+ #
463
+ # @param angle [Numeric] angle for rotation
464
+ #
465
+ # @return [nil] nil
466
+ #
467
+ # @see https://processing.org/reference/PShape_rotateZ_.html
468
+ #
233
469
  def rotateZ(angle)
234
470
  matrix__.rotate! @context.toDegrees__(angle), 0, 0, 1
471
+ nil
472
+ end
473
+
474
+ # Reset the transformation matrix.
475
+ #
476
+ # @return [nil] nil
477
+ #
478
+ # @see https://processing.org/reference/PShape_resetMatrix_.html
479
+ #
480
+ def resetMatrix()
481
+ @matrix = nil
482
+ nil
235
483
  end
236
484
 
237
485
  # @private