processing 0.5.33 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,13 +3,16 @@ 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
9
11
  def initialize(polygon = nil, children = nil, context: nil)
10
- @polygon, @children = polygon, children
11
- @context = context || Context.context__
12
- @visible, @fill, @matrix = true, nil, nil
12
+ @polygon, @children = polygon, children
13
+ @context = context || Context.context__
14
+ @visible = true
15
+ @fill = @stroke = @strokeWeight = @strokeCap = @strokeJoin = @matrix = nil
13
16
  @type = @points = @curvePoints = @colors = @texcoords = @close = nil
14
17
  @contours = @contourPoints = @contourColors = @contourTexCoords = nil
15
18
  end
@@ -18,6 +21,8 @@ module Processing
18
21
  #
19
22
  # @return [Numeric] width of shape
20
23
  #
24
+ # @see https://processing.org/reference/PShape_width.html
25
+ #
21
26
  def width()
22
27
  polygon = getInternal__ or return 0
23
28
  (@bounds ||= polygon.bounds).width
@@ -27,6 +32,8 @@ module Processing
27
32
  #
28
33
  # @return [Numeric] height of shape
29
34
  #
35
+ # @see https://processing.org/reference/PShape_height.html
36
+ #
30
37
  def height()
31
38
  polygon = getInternal__ or return 0
32
39
  (@bounds ||= polygon.bounds).height
@@ -39,6 +46,8 @@ module Processing
39
46
  #
40
47
  # @return [Boolean] visible or not
41
48
  #
49
+ # @see https://processing.org/reference/PShape_isVisible_.html
50
+ #
42
51
  def isVisible()
43
52
  @visible
44
53
  end
@@ -49,13 +58,22 @@ module Processing
49
58
  #
50
59
  # @return [nil] nil
51
60
  #
61
+ # @see https://processing.org/reference/PShape_setVisible_.html
62
+ #
52
63
  def setVisible(visible)
53
64
  @visible = !!visible
54
65
  nil
55
66
  end
56
67
 
68
+ # Starts shape data definition.
69
+ #
70
+ # @return [nil] nil
71
+ #
72
+ # @see https://processing.org/reference/PShape_beginShape_.html
73
+ #
57
74
  def beginShape(type = nil)
58
75
  raise "beginShape() cannot be called twice" if drawingShape__
76
+ @fill = @stroke = @strokeWeight = @strokeCap = @strokeJoin = nil
59
77
  @type = type
60
78
  @points ||= []
61
79
  @curvePoints = []
@@ -67,9 +85,21 @@ module Processing
67
85
  nil
68
86
  end
69
87
 
88
+ # Ends shape data definition.
89
+ #
90
+ # @return [nil] nil
91
+ #
92
+ # @see https://processing.org/reference/PShape_endShape_.html
93
+ #
70
94
  def endShape(close = nil)
71
95
  raise "endShape() must be called after beginShape()" unless drawingShape__
72
- @close = close == GraphicsContext::CLOSE || @contours.size > 0
96
+ painter = @context.getPainter__
97
+ @fill ||= painter.fill
98
+ @stroke ||= painter.stroke
99
+ @strokeWeight ||= painter.stroke_width
100
+ @strokeCap ||= painter.stroke_cap
101
+ @strokeJoin ||= painter.stroke_join
102
+ @close = close == GraphicsContext::CLOSE || @contours.size > 0
73
103
  if @close && @curvePoints.size >= 8
74
104
  x, y = @curvePoints[0, 2]
75
105
  2.times {curveVertex x, y}
@@ -78,12 +108,24 @@ module Processing
78
108
  nil
79
109
  end
80
110
 
111
+ # Starts a new contour definition.
112
+ #
113
+ # @return [nil] nil
114
+ #
115
+ # @see https://processing.org/reference/PShape_beginContour_.html
116
+ #
81
117
  def beginContour()
82
118
  raise "beginContour() must be called after beginShape()" unless drawingShape__
83
119
  @contourPoints, @contourColors, @contourTexCoords = [], [], []
84
120
  nil
85
121
  end
86
122
 
123
+ # Ends contour definition.
124
+ #
125
+ # @return [nil] nil
126
+ #
127
+ # @see https://processing.org/reference/PShape_endContour_.html
128
+ #
87
129
  def endContour()
88
130
  raise "endContour() must be called after beginContour()" unless drawingContour__
89
131
  @contours << Rays::Polyline.new(
@@ -93,12 +135,27 @@ module Processing
93
135
  nil
94
136
  end
95
137
 
138
+ # Append vertex for shape polygon.
139
+ #
140
+ # @overload vertex(x, y)
141
+ # @overload vertex(x, y, u, v)
142
+ #
143
+ # @param x [Numeric] x position of vertex
144
+ # @param y [Numeric] y position of vertex
145
+ # @param u [Numeric] u texture coordinate of vertex
146
+ # @param v [Numeric] v texture coordinate of vertex
147
+ #
148
+ # @return [nil] nil
149
+ #
150
+ # @see https://processing.org/reference/vertex_.html
151
+ # @see https://p5js.org/reference/#/p5/vertex
152
+ #
96
153
  def vertex(x, y, u = nil, v = nil)
97
154
  raise "vertex() must be called after beginShape()" unless drawingShape__
98
155
  raise "Either 'u' or 'v' is missing" if (u == nil) != (v == nil)
99
156
  u ||= x
100
157
  v ||= y
101
- color = @fill || @context.getFill__
158
+ color = @fill || @context.getPainter__.fill
102
159
  if drawingContour__
103
160
  @contourPoints << x << y
104
161
  @contourColors << color
@@ -108,8 +165,19 @@ module Processing
108
165
  @colors << color
109
166
  @texcoords << u << v
110
167
  end
168
+ nil
111
169
  end
112
170
 
171
+ # Append curve vertex for shape polygon.
172
+ #
173
+ # @param x [Numeric] x position of vertex
174
+ # @param y [Numeric] y position of vertex
175
+ #
176
+ # @return [nil] nil
177
+ #
178
+ # @see https://processing.org/reference/curveVertex_.html
179
+ # @see https://p5js.org/reference/#/p5/curveVertex
180
+ #
113
181
  def curveVertex(x, y)
114
182
  raise "curveVertex() must be called after beginShape()" unless drawingShape__
115
183
  @curvePoints << x << y
@@ -121,6 +189,16 @@ module Processing
121
189
  nil
122
190
  end
123
191
 
192
+ # Append bezier vertex for shape polygon.
193
+ #
194
+ # @param x [Numeric] x position of vertex
195
+ # @param y [Numeric] y position of vertex
196
+ #
197
+ # @return [nil] nil
198
+ #
199
+ # @see https://processing.org/reference/bezierVertex_.html
200
+ # @see https://p5js.org/reference/#/p5/bezierVertex
201
+ #
124
202
  def bezierVertex(x2, y2, x3, y3, x4, y4)
125
203
  raise "bezierVertex() must be called after beginShape()" unless drawingShape__
126
204
  x1, y1 = @points[-2, 2]
@@ -131,6 +209,16 @@ module Processing
131
209
  nil
132
210
  end
133
211
 
212
+ # Append quadratic vertex for shape polygon.
213
+ #
214
+ # @param x [Numeric] x position of vertex
215
+ # @param y [Numeric] y position of vertex
216
+ #
217
+ # @return [nil] nil
218
+ #
219
+ # @see https://processing.org/reference/quadraticVertex_.html
220
+ # @see https://p5js.org/reference/#/p5/quadraticVertex
221
+ #
134
222
  def quadraticVertex(cx, cy, x3, y3)
135
223
  x1, y1 = @points[-2, 2]
136
224
  raise "vertex() is required before calling quadraticVertex()" unless x1 && y1
@@ -151,15 +239,80 @@ module Processing
151
239
  @contourPoints
152
240
  end
153
241
 
242
+ # Sets fill color.
243
+ #
244
+ # @overload fill(gray)
245
+ # @overload fill(gray, alpha)
246
+ # @overload fill(r, g, b)
247
+ # @overload fill(r, g, b, alpha)
248
+ #
249
+ # @param gray [Integer] gray value (0..255)
250
+ # @param r [Integer] red value (0..255)
251
+ # @param g [Integer] green value (0..255)
252
+ # @param b [Integer] blue value (0..255)
253
+ # @param alpha [Integer] alpha value (0..255)
254
+ #
255
+ # @return [nil] nil
256
+ #
257
+ # @see https://processing.org/reference/fill_.html
258
+ # @see https://p5js.org/reference/#/p5/fill
259
+ #
154
260
  def fill(*args)
155
- @fill = @context.rawColor__(*args)
261
+ @fill = @context.toRawColor__(*args)
262
+ nil
263
+ end
264
+
265
+ # Sets stroke color.
266
+ #
267
+ # @overload stroke(gray)
268
+ # @overload stroke(gray, alpha)
269
+ # @overload stroke(r, g, b)
270
+ # @overload stroke(r, g, b, alpha)
271
+ #
272
+ # @param gray [Integer] gray value (0..255)
273
+ # @param r [Integer] red value (0..255)
274
+ # @param g [Integer] green value (0..255)
275
+ # @param b [Integer] blue value (0..255)
276
+ # @param alpha [Integer] alpha value (0..255)
277
+ #
278
+ # @return [nil] nil
279
+ #
280
+ # @see https://processing.org/reference/stroke_.html
281
+ # @see https://p5js.org/reference/#/p5/stroke
282
+ #
283
+ def stroke(*args)
284
+ @stroke = @context.toRawColor__(*args)
285
+ nil
156
286
  end
157
287
 
288
+ # Sets the vertex at the index position.
289
+ #
290
+ # @overload setVertex(index, x, y)
291
+ # @overload setVertex(index, vec)
292
+ #
293
+ # @param index [Integer] the index fo the vertex
294
+ # @param x [Numeric] x position of the vertex
295
+ # @param y [Numeric] y position of the vertex
296
+ # @param vec [Vector] position for the vertex
297
+ #
298
+ # @return [nil] nil
299
+ #
300
+ # @see https://processing.org/reference/PShape_setVertex_.html
301
+ #
158
302
  def setVertex(index, point)
159
303
  return nil unless @points && @points[index * 2, 2]&.size == 2
160
304
  @points[index * 2, 2] = [point.x, point.y]
305
+ nil
161
306
  end
162
307
 
308
+ # Returns the vertex at the index position.
309
+ #
310
+ # @param index [Integer] the index fo the vertex
311
+ #
312
+ # @return [Vector] the vertex position
313
+ #
314
+ # @see https://processing.org/reference/PShape_getVertex_.html
315
+ #
163
316
  def getVertex(index)
164
317
  return nil unless @points
165
318
  point = @points[index * 2, 2]
@@ -167,71 +320,247 @@ module Processing
167
320
  @context.createVector(*point)
168
321
  end
169
322
 
323
+ # Returns the total number of vertices.
324
+ #
325
+ # @return [Integer] vertex count
326
+ #
327
+ # @see https://processing.org/reference/PShape_getVertexCount_.html
328
+ #
170
329
  def getVertexCount()
171
330
  return 0 unless @points
172
331
  @points.size / 2
173
332
  end
174
333
 
334
+ # Sets the fill color for all vertices.
335
+ #
336
+ # @overload setFill(gray)
337
+ # @overload setFill(gray, alpha)
338
+ # @overload setFill(r, g, b)
339
+ # @overload setFill(r, g, b, alpha)
340
+ #
341
+ # @param gray [Integer] gray value (0..255)
342
+ # @param r [Integer] red value (0..255)
343
+ # @param g [Integer] green value (0..255)
344
+ # @param b [Integer] blue value (0..255)
345
+ # @param alpha [Integer] alpha value (0..255)
346
+ #
347
+ # @return [nil] nil
348
+ #
349
+ # @see https://processing.org/reference/PShape_setFill_.html
350
+ #
175
351
  def setFill(*args)
176
- color = @context.rawColor__(*args)
352
+ fill(*args)
177
353
  count = getVertexCount
178
354
  if count > 0
179
355
  if @colors
180
- @colors.fill color
356
+ @colors.fill @fill
181
357
  else
182
- @colors = [color] * count
358
+ @colors = [@fill] * count
183
359
  end
184
360
  clearCache__
185
361
  elsif @polygon
186
362
  @polygon = @polygon.transform do |polylines|
187
- polylines.map {|pl| pl.with colors: pl.points.map {color}}
363
+ polylines.map {|pl| pl.with colors: pl.points.map {@fill}}
188
364
  end
189
365
  end
366
+ nil
367
+ end
368
+
369
+ # Sets the stroke color.
370
+ #
371
+ # @overload setStroke(gray)
372
+ # @overload setStroke(gray, alpha)
373
+ # @overload setStroke(r, g, b)
374
+ # @overload setStroke(r, g, b, alpha)
375
+ #
376
+ # @param gray [Integer] gray value (0..255)
377
+ # @param r [Integer] red value (0..255)
378
+ # @param g [Integer] green value (0..255)
379
+ # @param b [Integer] blue value (0..255)
380
+ # @param alpha [Integer] alpha value (0..255)
381
+ #
382
+ # @return [nil] nil
383
+ #
384
+ # @see https://processing.org/reference/PShape_setStroke_.html
385
+ #
386
+ def setStroke(*args)
387
+ stroke(*args)
388
+ nil
190
389
  end
191
390
 
192
- def addChild(child)
391
+ # Sets the stroke weight.
392
+ #
393
+ # @param weight [Numeric] stroke weight
394
+ #
395
+ # @return [nil] nil
396
+ #
397
+ def setStrokeWeight(weight)
398
+ @strokeWeight = weight
399
+ nil
400
+ end
401
+
402
+ # Sets the stroke cap.
403
+ #
404
+ # @param cap [ROUND, SQUARE, PROJECT] stroke cap
405
+ #
406
+ # @return [nil] nil
407
+ #
408
+ def setStrokeCap(cap)
409
+ @strokeCap = cap
410
+ nil
411
+ end
412
+
413
+ # Sets the stroke join.
414
+ #
415
+ # @param join [MITER, BEVEL, ROUND] stroke join
416
+ #
417
+ # @return [nil] nil
418
+ #
419
+ def setStrokeJoin(join)
420
+ @strokeJoin = join
421
+ nil
422
+ end
423
+
424
+ # Adds a new child shape.
425
+ #
426
+ # @param child [Shape] child shape
427
+ # @param index [Integer] insert position
428
+ #
429
+ # @return [nil] nil
430
+ #
431
+ # @see https://processing.org/reference/PShape_addChild_.html
432
+ #
433
+ def addChild(child, index = -1)
193
434
  return unless @children
194
- @children.push child
435
+ if index < 0
436
+ @children.push child
437
+ else
438
+ @children.insert index, child
439
+ end
195
440
  nil
196
441
  end
197
442
 
443
+ # Returns a child shape at the index position.
444
+ #
445
+ # @param index [Integer] child index
446
+ #
447
+ # @return [nil] nil
448
+ #
449
+ # @see https://processing.org/reference/PShape_getChild_.html
450
+ #
198
451
  def getChild(index)
199
452
  @children&.[](index)
200
453
  end
201
454
 
455
+ # Returns the number of children.
456
+ #
457
+ # @return [Integer] child count
458
+ #
459
+ # @see https://processing.org/reference/PShape_getChildCount_.html
460
+ #
202
461
  def getChildCount()
203
462
  @children&.size || 0
204
463
  end
205
464
 
465
+ # Applies translation matrix to the shape.
466
+ #
467
+ # @overload translate(x, y)
468
+ # @overload translate(x, y, z)
469
+ #
470
+ # @param x [Numeric] left/right translation
471
+ # @param y [Numeric] up/down translation
472
+ # @param z [Numeric] forward/backward translation
473
+ #
474
+ # @return [nil] nil
475
+ #
476
+ # @see https://processing.org/reference/PShape_translate_.html
477
+ #
206
478
  def translate(x, y, z = 0)
207
479
  matrix__.translate! x, y, z
208
480
  nil
209
481
  end
210
482
 
211
- def rotate(angle)
212
- matrix__.rotate! @context.toDegrees__(angle)
483
+ # Applies scale matrix to the shape.
484
+ #
485
+ # @overload scale(s)
486
+ # @overload scale(x, y)
487
+ # @overload scale(x, y, z)
488
+ #
489
+ # @param s [Numeric] horizontal and vertical scale
490
+ # @param x [Numeric] horizontal scale
491
+ # @param y [Numeric] vertical scale
492
+ # @param z [Numeric] depth scale
493
+ #
494
+ # @return [nil] nil
495
+ #
496
+ # @see https://processing.org/reference/PShape_scale_.html
497
+ #
498
+ def scale(x, y = nil, z = 1)
499
+ matrix__.scale! x, (y || x), z
213
500
  nil
214
501
  end
215
502
 
216
- def scale(x, y, z = 1)
217
- matrix__.scale! x, y, z
503
+ # Applies rotation matrix to the shape.
504
+ #
505
+ # @param angle [Numeric] angle for rotation
506
+ #
507
+ # @return [nil] nil
508
+ #
509
+ # @see https://processing.org/reference/PShape_rotate_.html
510
+ #
511
+ def rotate(angle)
512
+ matrix__.rotate! @context.toDegrees__(angle)
218
513
  nil
219
514
  end
220
515
 
221
- def resetMatrix()
222
- @matrix = nil
223
- end
224
-
516
+ # Applies rotation around the x-axis to the shape.
517
+ #
518
+ # @param angle [Numeric] angle for rotation
519
+ #
520
+ # @return [nil] nil
521
+ #
522
+ # @see https://processing.org/reference/PShape_rotateX_.html
523
+ #
225
524
  def rotateX(angle)
226
525
  matrix__.rotate! @context.toDegrees__(angle), 1, 0, 0
526
+ nil
227
527
  end
228
528
 
529
+ # Applies rotation around the y-axis to the shape.
530
+ #
531
+ # @param angle [Numeric] angle for rotation
532
+ #
533
+ # @return [nil] nil
534
+ #
535
+ # @see https://processing.org/reference/PShape_rotateY_.html
536
+ #
229
537
  def rotateY(angle)
230
538
  matrix__.rotate! @context.toDegrees__(angle), 0, 1, 0
539
+ nil
231
540
  end
232
541
 
542
+ # Applies rotation around the z-axis to the shape.
543
+ #
544
+ # @param angle [Numeric] angle for rotation
545
+ #
546
+ # @return [nil] nil
547
+ #
548
+ # @see https://processing.org/reference/PShape_rotateZ_.html
549
+ #
233
550
  def rotateZ(angle)
234
551
  matrix__.rotate! @context.toDegrees__(angle), 0, 0, 1
552
+ nil
553
+ end
554
+
555
+ # Reset the transformation matrix.
556
+ #
557
+ # @return [nil] nil
558
+ #
559
+ # @see https://processing.org/reference/PShape_resetMatrix_.html
560
+ #
561
+ def resetMatrix()
562
+ @matrix = nil
563
+ nil
235
564
  end
236
565
 
237
566
  # @private
@@ -257,24 +586,36 @@ module Processing
257
586
 
258
587
  # @private
259
588
  def draw__(painter, x, y, w = nil, h = nil)
260
- poly = getInternal__
589
+ p, poly = painter, getInternal__
261
590
 
262
- backup = nil
591
+ matrix_ = nil
263
592
  if @matrix && (poly || @children)
264
- backup = painter.matrix
265
- painter.matrix = backup * @matrix
593
+ matrix_ = p.matrix
594
+ p.matrix = matrix_ * @matrix
266
595
  end
267
596
 
268
597
  if poly
598
+ f_ = s_ = sw_ = sc_ = sj_ = nil
599
+ f_, p.fill = p.fill, '#fff' if @fill
600
+ s_, p.stroke = p.stroke, @stroke if @stroke
601
+ sw_, p.stroke_width = p.stroke_width, @strokeWeight if @strokeWeight
602
+ sc_, p.stroke_cap = p.stroke_cap, @strokeCap if @strokeCap
603
+ sj_, p.stroke_join = p.stroke_join, @strokeJoin if @strokeJoin
269
604
  if w || h
270
- painter.polygon poly, x, y, w,h
605
+ p.polygon poly, x, y, w,h
271
606
  else
272
- painter.polygon poly, x, y
607
+ p.polygon poly, x, y
273
608
  end
609
+ p.fill = f_ if f_
610
+ p.stroke = s_ if s_
611
+ p.stroke_width = sw_ if sw_
612
+ p.stroke_cap = sc_ if sc_
613
+ p.stroke_join = sj_ if sj_
274
614
  end
275
- @children&.each {|o| o.draw__ painter, x, y, w, h}
276
615
 
277
- painter.matrix = backup if backup
616
+ @children&.each {|o| o.draw__ p, x, y, w, h}
617
+
618
+ p.matrix = matrix_ if matrix_
278
619
  end
279
620
 
280
621
  # @private