rubysketch 0.1.3 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31a6c8685fd803f34636e81ee5098dfda6c2d82ab500952662e5b77c30601c09
4
- data.tar.gz: e4f22f4dbf9d01a0cc4f5f084648698ae42c0a1eddf582c0848849b194ac5933
3
+ metadata.gz: cb7fa5f9cc69b67a9339f9f7170c8901999d0007e6b6e74de7d7b55b36bc09d3
4
+ data.tar.gz: c5158bda3a8ac90ec2255c3bfda5f8c3dbb8e9815bd74b9d5df0f8bfa601024e
5
5
  SHA512:
6
- metadata.gz: 5df3c266fb39715d6c83cda24268e48f27e922292398cda0f3c84412089fe88bc0a08a6373cd4b97faf342b64a79b3c7d7343794081522c7ae4894e8d7cefdc9
7
- data.tar.gz: 2c182badae8ae8f471647a2fc8289e7151730985de955b54d29f51c04ed8ee242ea89c274303e2f9d5b4d6189186b7ea635bc464d3ece7af5329124729f5c999
6
+ metadata.gz: b691e41b1e7ba08ad1a732f81333febe97cc68e30244ad7f8aea76b7660b7af5b47ae60542610f797340cc25bf9bc6f673dc893a19dd27e0b83583a19a8c99d8
7
+ data.tar.gz: 969de4e7e4f840c590c0758af55a53bb28838b05cf63a84d9c389bd99b5493df23cb06a5020f25c8531a906d67be4da014b5e888929e4734c83f18d6f47a87e0
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.2.2
@@ -0,0 +1,66 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+ COLORS = %w[ #F99292 #FFBC61 #FFC679 #FFF4E0 ]
8
+
9
+ def now ()
10
+ Time.now.to_f
11
+ end
12
+
13
+ start = now
14
+
15
+ setup do
16
+ colorMode RGB, 1
17
+ angleMode DEGREES
18
+ end
19
+
20
+ draw do
21
+ background 0
22
+
23
+ begin
24
+ pushMatrix
25
+ translate width / 2, height / 2
26
+
27
+ begin
28
+ pushMatrix
29
+ fill COLORS[0]
30
+ ellipse 0, 0, 20, 20
31
+ rotate (now - start) / 60.0 * 360
32
+ stroke COLORS[0]
33
+ strokeWeight 5
34
+ line 0, 0, 200, 0
35
+ fill 1
36
+ popMatrix
37
+ end
38
+
39
+ begin
40
+ pushMatrix
41
+ strokeWeight 3
42
+ 60.times do
43
+ rotate 6
44
+ stroke COLORS[1]
45
+ line 200, 0, 210, 0
46
+ end
47
+ popMatrix
48
+ end
49
+
50
+ begin
51
+ pushMatrix
52
+ strokeWeight 5
53
+ 12.times do
54
+ rotate 30
55
+ stroke COLORS[3]
56
+ line 190, 0, 210, 0
57
+ end
58
+ popMatrix
59
+ end
60
+
61
+ popMatrix
62
+ end
63
+
64
+ textSize 20
65
+ text "#{frameRate.to_i} FPS", 10, 10
66
+ end
@@ -6,6 +6,7 @@ require 'rubysketch-processing'
6
6
 
7
7
 
8
8
  draw do
9
+ background 0, 10
9
10
  textSize 50
10
- text 'hello, world!', 10, 10
11
+ text 'hello, world!', mouseX, mouseY
11
12
  end
@@ -0,0 +1,13 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ icon = loadImage 'https://xord.org/rubysketch/rubysketch.png'
9
+
10
+ draw do
11
+ background 0, 10
12
+ image icon, mouseX, mouseY, icon.width / 10, icon.height / 10
13
+ end
@@ -0,0 +1,121 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ setup do
9
+ colorMode RGB, 1
10
+ angleMode DEGREES
11
+ end
12
+
13
+ draw do
14
+ background 0
15
+
16
+ fill 1
17
+ stroke 1, 0.5, 0.2
18
+
19
+ translate 10, 10
20
+
21
+ push
22
+
23
+ text 'point', 0, 0
24
+ point 0, 30
25
+
26
+ translate 0, 100
27
+
28
+ text 'point with strokeWeight', 0, 0
29
+ strokeWeight 10
30
+ point 0, 30
31
+ strokeWeight 0
32
+
33
+ translate 0, 100
34
+
35
+ text 'line', 0, 0
36
+ line 0, 30, 100, 50
37
+
38
+ translate 0, 100
39
+
40
+ text 'line with strokeWeight (very slow)', 0, 0
41
+ strokeWeight 10
42
+ line 0, 30, 100, 50
43
+ strokeWeight 1
44
+
45
+ translate 0, 100
46
+
47
+ text 'rect with rectMode(CORNER)', 0, 0
48
+ rectMode CORNER
49
+ rect 20, 30, 100, 50
50
+
51
+ translate 0, 100
52
+
53
+ text 'rect with rectMode(CORNERS)', 0, 0
54
+ rectMode CORNERS
55
+ rect 20, 30, 120, 80
56
+
57
+ translate 0, 100
58
+
59
+ text 'rect with rectMode(CENTER)', 0, 0
60
+ rectMode CENTER
61
+ rect 70, 55, 100, 50
62
+
63
+ translate 0, 100
64
+
65
+ text 'rect with rectMode(RADIUS)', 0, 0
66
+ rectMode RADIUS
67
+ rect 70, 55, 50, 25
68
+
69
+ pop
70
+ translate 200, 0
71
+ push
72
+
73
+ text 'circle', 0, 0
74
+ circle 70, 55, 25
75
+
76
+ translate 0, 100
77
+
78
+ text 'arc', 0, 0
79
+ arc 70, 55, 100, 50, 45, 270
80
+
81
+ translate 0, 100
82
+
83
+ text 'square', 0, 0
84
+ square 20, 30, 50
85
+
86
+ translate 0, 100
87
+
88
+ text 'triangle', 0, 0
89
+ triangle 70, 30, 120, 80, 20, 80
90
+
91
+ translate 0, 100
92
+
93
+ text 'quad', 0, 0
94
+ quad 20, 30, 120, 30, 150, 80, 50, 80
95
+
96
+ translate 0, 100
97
+
98
+ text 'ellipse with ellipseMode(CORNER)', 0, 0
99
+ ellipseMode CORNER
100
+ ellipse 20, 30, 100, 50
101
+
102
+ translate 0, 100
103
+
104
+ text 'ellipse with ellipseMode(CORNERS)', 0, 0
105
+ ellipseMode CORNERS
106
+ ellipse 20, 30, 120, 80
107
+
108
+ translate 0, 100
109
+
110
+ text 'ellipse with ellipseMode(CENTER)', 0, 0
111
+ ellipseMode CENTER
112
+ ellipse 70, 55, 100, 50
113
+
114
+ translate 0, 100
115
+
116
+ text 'ellipse with ellipseMode(RADIUS)', 0, 0
117
+ ellipseMode RADIUS
118
+ ellipse 70, 55, 50, 25
119
+
120
+ pop
121
+ end
@@ -1,3 +1,8 @@
1
+ require 'digest/sha1'
2
+ require 'pathname'
3
+ require 'tmpdir'
4
+ require 'open-uri'
5
+
1
6
  require 'reflex'
2
7
  require 'rubysketch/module'
3
8
  require 'rubysketch/starter'
@@ -2,7 +2,7 @@ module RubySketch
2
2
 
3
3
 
4
4
  # @private
5
- class GLSL1
5
+ class GLSL
6
6
 
7
7
  extend Starter
8
8
 
@@ -27,10 +27,7 @@ module RubySketch
27
27
  end
28
28
  end
29
29
 
30
- end# GLSL1
31
-
32
-
33
- GLSL = GLSL1
30
+ end# GLSL
34
31
 
35
32
 
36
33
  end# RubySketch
@@ -1,9 +1,9 @@
1
1
  module RubySketch
2
2
 
3
3
 
4
- # Processing compatible API v1
4
+ # Processing compatible API
5
5
  #
6
- class Processing1
6
+ class Processing
7
7
 
8
8
  include Math
9
9
 
@@ -30,6 +30,11 @@ module RubySketch
30
30
  #
31
31
  DEGREES = :DEGREES
32
32
 
33
+ CORNER = :CORNER
34
+ CORNERS = :CORNERS
35
+ CENTER = :CENTER
36
+ RADIUS = :RADIUS
37
+
33
38
  # @private
34
39
  DEG2RAD__ = PI / 180.0
35
40
 
@@ -38,6 +43,8 @@ module RubySketch
38
43
 
39
44
  # @private
40
45
  def initialize ()
46
+ @matrixStack__ = []
47
+ @styleStack__ = []
41
48
  @frameCount__ = 0
42
49
  @hsbColor__ = false
43
50
  @colorMaxes__ = [1.0] * 4
@@ -48,8 +55,10 @@ module RubySketch
48
55
  @mousePrevY__ = 0
49
56
  @mousePressed__ = false
50
57
 
51
- colorMode RGB, 255
52
- angleMode RADIANS
58
+ colorMode RGB, 255
59
+ angleMode RADIANS
60
+ rectMode CORNER
61
+ ellipseMode CENTER
53
62
  end
54
63
 
55
64
  # @private
@@ -277,21 +286,32 @@ module RubySketch
277
286
  radian * RAD2DEG__
278
287
  end
279
288
 
289
+ # Define setup block.
290
+ #
280
291
  def setup (&block)
281
292
  @window__.setup = block
282
293
  nil
283
294
  end
284
295
 
285
296
  # @private
286
- def setupDrawBlock__ ()
297
+ private def setupDrawBlock__ ()
287
298
  @window__.draw = proc do |e, painter|
288
299
  @painter__ = painter
289
- @drawBlock__.call e if @drawBlock__
300
+ @matrixStack__.clear
301
+ @styleStack__.clear
302
+ begin
303
+ push
304
+ @drawBlock__.call e if @drawBlock__
305
+ ensure
306
+ pop
307
+ end
290
308
  @frameCount__ += 1
291
309
  updateMousePrevPos__
292
310
  end
293
311
  end
294
312
 
313
+ # Define draw block.
314
+ #
295
315
  def draw (&block)
296
316
  @drawBlock__ = block if block
297
317
  nil
@@ -303,7 +323,7 @@ module RubySketch
303
323
  end
304
324
 
305
325
  # @private
306
- def setupMousePressedBlock__ ()
326
+ private def setupMousePressedBlock__ ()
307
327
  @window__.pointer_down = proc do |e|
308
328
  updateMouseState__ e.x, e.y, true
309
329
  @mousePressedBlock__.call e if @mousePressedBlock__
@@ -316,7 +336,7 @@ module RubySketch
316
336
  end
317
337
 
318
338
  # @private
319
- def setupMouseReleasedBlock__ ()
339
+ private def setupMouseReleasedBlock__ ()
320
340
  @window__.pointer_up = proc do |e|
321
341
  updateMouseState__ e.x, e.y, false
322
342
  @mouseReleasedBlock__.call e if @mouseReleasedBlock__
@@ -329,7 +349,7 @@ module RubySketch
329
349
  end
330
350
 
331
351
  # @private
332
- def setupMouseMovedBlock__ ()
352
+ private def setupMouseMovedBlock__ ()
333
353
  @window__.pointer_move = proc do |e|
334
354
  updateMouseState__ e.x, e.y
335
355
  @mouseMovedBlock__.call e if @mouseMovedBlock__
@@ -342,7 +362,7 @@ module RubySketch
342
362
  end
343
363
 
344
364
  # @private
345
- def setupMouseDraggedBlock__ ()
365
+ private def setupMouseDraggedBlock__ ()
346
366
  @window__.pointer_drag = proc do |e|
347
367
  updateMouseState__ e.x, e.y
348
368
  @mouseDraggedBlock__.call e if @mouseDraggedBlock__
@@ -355,20 +375,20 @@ module RubySketch
355
375
  end
356
376
 
357
377
  # @private
358
- def updateMouseState__ (x, y, pressed = nil)
378
+ private def updateMouseState__ (x, y, pressed = nil)
359
379
  @mouseX__ = x
360
380
  @mouseY__ = y
361
381
  @mousePressed__ = pressed if pressed != nil
362
382
  end
363
383
 
364
384
  # @private
365
- def updateMousePrevPos__ ()
385
+ private def updateMousePrevPos__ ()
366
386
  @mousePrevX__ = @mouseX__
367
387
  @mousePrevY__ = @mouseY__
368
388
  end
369
389
 
370
390
  # @private
371
- def size__ (width, height)
391
+ private def size__ (width, height)
372
392
  raise 'size() must be called on startup or setup block' if @started__
373
393
 
374
394
  @painter__.send :end_paint
@@ -467,7 +487,7 @@ module RubySketch
467
487
  # @return [nil] nil
468
488
  #
469
489
  def colorMode (mode, *maxes)
470
- raise ArgumentError, "Invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
490
+ raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
471
491
  raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)
472
492
 
473
493
  @hsbColor__ = mode.upcase == HSB
@@ -479,11 +499,11 @@ module RubySketch
479
499
  end
480
500
 
481
501
  # @private
482
- def to_rgba__ (*args)
502
+ private def toRGBA__ (*args)
483
503
  a, b, c, d = args
484
- return parse_color__(a, b || alphaMax__) if a.kind_of?(String)
504
+ return parseColor__(a, b || alphaMax__) if a.kind_of?(String)
485
505
 
486
- rgba = case args.size
506
+ rgba = case args.size
487
507
  when 1, 2 then [a, a, a, b || alphaMax__]
488
508
  when 3, 4 then [a, b, c, d || alphaMax__]
489
509
  else raise ArgumentError
@@ -494,16 +514,16 @@ module RubySketch
494
514
  end
495
515
 
496
516
  # @private
497
- def parse_color__ (str, alpha)
517
+ private def parseColor__ (str, alpha)
498
518
  result = str.match /^\s*##{'([0-9a-f]{2})' * 3}\s*$/i
499
- raise ArgumentError, "Invalid color code: '#{str}'" unless result
519
+ raise ArgumentError, "invalid color code: '#{str}'" unless result
500
520
 
501
521
  rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) / 255.0}
502
522
  return *rgb, (alpha / alphaMax__)
503
523
  end
504
524
 
505
525
  # @private
506
- def alphaMax__ ()
526
+ private def alphaMax__ ()
507
527
  @colorMaxes__[3]
508
528
  end
509
529
 
@@ -517,15 +537,57 @@ module RubySketch
517
537
  @angleScale__ = case mode
518
538
  when RADIANS then RAD2DEG__
519
539
  when DEGREES then 1.0
520
- else raise ArgumentError, "Invalid angle mode: #{mode}"
540
+ else raise ArgumentError, "invalid angle mode: #{mode}"
521
541
  end
542
+ nil
522
543
  end
523
544
 
524
545
  # @private
525
- def to_angle__ (angle)
546
+ private def toAngle__ (angle)
526
547
  angle * @angleScale__
527
548
  end
528
549
 
550
+ # Sets rect mode. Default is CORNER.
551
+ #
552
+ # CORNER -> rect(left, top, width, height)
553
+ # CORNERS -> rect(left, top, right, bottom)
554
+ # CENTER -> rect(center_x, center_y, width, height)
555
+ # RADIUS -> rect(center_x, center_y, radius_h, radius_v)
556
+ #
557
+ # @param mode [CORNER, CORNERS, CENTER, RADIUS]
558
+ #
559
+ # @return [nil] nil
560
+ #
561
+ def rectMode (mode)
562
+ @rectMode__ = mode
563
+ end
564
+
565
+ # Sets ellipse mode. Default is CENTER.
566
+ #
567
+ # CORNER -> ellipse(left, top, width, height)
568
+ # CORNERS -> ellipse(left, top, right, bottom)
569
+ # CENTER -> ellipse(center_x, center_y, width, height)
570
+ # RADIUS -> ellipse(center_x, center_y, radius_h, radius_v)
571
+ #
572
+ # @param mode [CORNER, CORNERS, CENTER, RADIUS]
573
+ #
574
+ # @return [nil] nil
575
+ #
576
+ def ellipseMode (mode)
577
+ @ellipseMode__ = mode
578
+ end
579
+
580
+ # @private
581
+ private def toXYWH__ (mode, a, b, c, d)
582
+ case mode
583
+ when CORNER then [a, b, c, d]
584
+ when CORNERS then [a, b, c - a, d - b]
585
+ when CENTER then [a - c / 2.0, b - d / 2.0, c, d]
586
+ when RADIUS then [a - c, b - d, c * 2, d * 2]
587
+ else raise ArgumentError # ToDo: refine error message
588
+ end
589
+ end
590
+
529
591
  # Clears screen.
530
592
  #
531
593
  # @overload background(str)
@@ -545,7 +607,14 @@ module RubySketch
545
607
  # @return [nil] nil
546
608
  #
547
609
  def background (*args)
548
- @painter__.background(*to_rgba__(*args))
610
+ rgba = toRGBA__ *args
611
+ if rgba[3] == 1
612
+ @painter__.background *rgba
613
+ else
614
+ @painter__.push fill: rgba, stroke: nil do |_|
615
+ @painter__.rect 0, 0, width, height
616
+ end
617
+ end
549
618
  nil
550
619
  end
551
620
 
@@ -568,7 +637,7 @@ module RubySketch
568
637
  # @return [nil] nil
569
638
  #
570
639
  def fill (*args)
571
- @painter__.fill(*to_rgba__(*args))
640
+ @painter__.fill(*toRGBA__(*args))
572
641
  nil
573
642
  end
574
643
 
@@ -591,7 +660,7 @@ module RubySketch
591
660
  # @return [nil] nil
592
661
  #
593
662
  def stroke (*args)
594
- @painter__.stroke(*to_rgba__(*args))
663
+ @painter__.stroke(*toRGBA__(*args))
595
664
  nil
596
665
  end
597
666
 
@@ -647,6 +716,20 @@ module RubySketch
647
716
  nil
648
717
  end
649
718
 
719
+ # Draws a point.
720
+ #
721
+ # @param x [Numeric] horizontal position
722
+ # @param y [Numeric] vertical position
723
+ #
724
+ # @return [nil] nil
725
+ #
726
+ def point (x, y)
727
+ w = @painter__.stroke_width
728
+ w = 1 if w == 0
729
+ @painter__.ellipse x - (w / 2.0), y - (w / 2.0), w, w
730
+ nil
731
+ end
732
+
650
733
  # Draws a line.
651
734
  #
652
735
  # @param x1 [Numeric] horizontal position for first point
@@ -663,14 +746,14 @@ module RubySketch
663
746
 
664
747
  # Draws a rectangle.
665
748
  #
666
- # @overload rect(x, y, w, h)
667
- # @overload rect(x, y, w, h, r)
668
- # @overload rect(x, y, w, h, tl, tr, br, bl)
749
+ # @overload rect(a, b, c, d)
750
+ # @overload rect(a, b, c, d, r)
751
+ # @overload rect(a, b, c, d, tl, tr, br, bl)
669
752
  #
670
- # @param x [Numeric] horizontal position
671
- # @param y [Numeric] vertical position
672
- # @param w [Numeric] width of the shape
673
- # @param h [Numeric] height of the shape
753
+ # @param a [Numeric] horizontal position of the shape by default
754
+ # @param b [Numeric] vertical position of the shape by default
755
+ # @param c [Numeric] width of the shape by default
756
+ # @param d [Numeric] height of the shape by default
674
757
  # @param r [Numeric] radius for all corners
675
758
  # @param tl [Numeric] radius for top-left corner
676
759
  # @param tr [Numeric] radius for top-right corner
@@ -679,7 +762,8 @@ module RubySketch
679
762
  #
680
763
  # @return [nil] nil
681
764
  #
682
- def rect (x, y, w, h, *args)
765
+ def rect (a, b, c, d, *args)
766
+ x, y, w, h = toXYWH__ @rectMode__, a, b, c, d
683
767
  case args.size
684
768
  when 0 then @painter__.rect x, y, w, h
685
769
  when 1 then @painter__.rect x, y, w, h, round: args[0]
@@ -689,24 +773,25 @@ module RubySketch
689
773
  nil
690
774
  end
691
775
 
692
- # Draws a ellipse.
776
+ # Draws an ellipse.
693
777
  #
694
- # @param x [Numeric] horizontal position
695
- # @param y [Numeric] vertical position
696
- # @param w [Numeric] width of the shape
697
- # @param h [Numeric] height of the shape
778
+ # @param a [Numeric] horizontal position of the shape
779
+ # @param b [Numeric] vertical position of the shape
780
+ # @param c [Numeric] width of the shape
781
+ # @param d [Numeric] height of the shape
698
782
  #
699
783
  # @return [nil] nil
700
784
  #
701
- def ellipse (x, y, w, h = w)
702
- @painter__.ellipse (x - w / 2.0), (y - h / 2.0), w, h
785
+ def ellipse (a, b, c, d)
786
+ x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
787
+ @painter__.ellipse x, y, w, h
703
788
  nil
704
789
  end
705
790
 
706
791
  # Draws a circle.
707
792
  #
708
- # @param x [Numeric] horizontal position
709
- # @param y [Numeric] vertical position
793
+ # @param x [Numeric] horizontal position of the shape
794
+ # @param y [Numeric] vertical position of the shape
710
795
  # @param extent [Numeric] width and height of the shape
711
796
  #
712
797
  # @return [nil] nil
@@ -717,31 +802,66 @@ module RubySketch
717
802
 
718
803
  # Draws an arc.
719
804
  #
720
- # @param x [Numeric] horizontal position
721
- # @param y [Numeric] vertical position
722
- # @param w [Numeric] width of the shape
723
- # @param h [Numeric] height of the shape
805
+ # @param a [Numeric] horizontal position of the shape
806
+ # @param b [Numeric] vertical position of the shape
807
+ # @param c [Numeric] width of the shape
808
+ # @param d [Numeric] height of the shape
724
809
  # @param start [Numeric] angle to start the arc
725
810
  # @param stop [Numeric] angle to stop the arc
726
811
  #
727
812
  # @return [nil] nil
728
813
  #
729
- def arc (x, y, w, h, start, stop)
730
- start = to_angle__ start
731
- stop = to_angle__ stop
732
- @painter__.ellipse x - w / 2, y - h / 2, w, h, from: start, to: stop
814
+ def arc (a, b, c, d, start, stop)
815
+ x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
816
+ start = toAngle__ start
817
+ stop = toAngle__ stop
818
+ @painter__.ellipse x, y, w, h, from: start, to: stop
733
819
  nil
734
820
  end
735
821
 
736
- # Draws a point.
822
+ # Draws a square.
737
823
  #
738
- # @param x [Numeric] horizontal position
739
- # @param y [Numeric] vertical position
824
+ # @param x [Numeric] horizontal position of the shape
825
+ # @param y [Numeric] vertical position of the shape
826
+ # @param extent [Numeric] width and height of the shape
740
827
  #
741
828
  # @return [nil] nil
742
829
  #
743
- def point (x, y)
744
- @painter__.rect x - 0.5, y - 0.5, 1, 1
830
+ def square (x, y, extent)
831
+ rect x, y, extent, extent
832
+ end
833
+
834
+ # Draws a triangle.
835
+ #
836
+ # @param x1 [Numeric] horizontal position of first point
837
+ # @param y1 [Numeric] vertical position of first point
838
+ # @param x2 [Numeric] horizontal position of second point
839
+ # @param y2 [Numeric] vertical position of second point
840
+ # @param x3 [Numeric] horizontal position of third point
841
+ # @param y3 [Numeric] vertical position of third point
842
+ #
843
+ # @return [nil] nil
844
+ #
845
+ def triangle (x1, y1, x2, y2, x3, y3)
846
+ @painter__.line x1, y1, x2, y2, x3, y3, loop: true
847
+ nil
848
+ end
849
+
850
+ # Draws a quad.
851
+ #
852
+ # @param x1 [Numeric] horizontal position of first point
853
+ # @param y1 [Numeric] vertical position of first point
854
+ # @param x2 [Numeric] horizontal position of second point
855
+ # @param y2 [Numeric] vertical position of second point
856
+ # @param x3 [Numeric] horizontal position of third point
857
+ # @param y3 [Numeric] vertical position of third point
858
+ # @param x4 [Numeric] horizontal position of fourth point
859
+ # @param y4 [Numeric] vertical position of fourth point
860
+ #
861
+ # @return [nil] nil
862
+ #
863
+ def quad (x1, y1, x2, y2, x3, y3, x4, y4)
864
+ @painter__.line x1, y1, x2, y2, x3, y3, x4, y4, loop: true
745
865
  nil
746
866
  end
747
867
 
@@ -751,16 +871,36 @@ module RubySketch
751
871
  # @overload text(str, x, y)
752
872
  #
753
873
  # @param str [String] text to draw
754
- # @param x [Numeric] horizontal position
755
- # @param y [Numeric] vertical position
874
+ # @param x [Numeric] horizontal position of the text
875
+ # @param y [Numeric] vertical position of the text
756
876
  #
757
877
  # @return [nil] nil
758
878
  #
759
- def text (str, x = 0, y = 0)
879
+ def text (str, x, y)
760
880
  @painter__.text str, x, y
761
881
  nil
762
882
  end
763
883
 
884
+ # Draws an image.
885
+ #
886
+ # @overload image(img, x, y)
887
+ # @overload image(img, x, y, w, h)
888
+ #
889
+ # @param img [Image] image to draw
890
+ # @param x [Numeric] horizontal position of the image
891
+ # @param y [Numeric] vertical position of the image
892
+ # @param w [Numeric] width of the image
893
+ # @param h [Numeric] height of the image
894
+ #
895
+ # @return [nil] nil
896
+ #
897
+ def image (img, x, y, w = nil, h = nil)
898
+ w ||= img.width
899
+ h ||= img.height
900
+ @painter__.image img.internal, x, y, w, h
901
+ nil
902
+ end
903
+
764
904
  # Applies translation matrix to current transformation matrix.
765
905
  #
766
906
  # @param x [Numeric] horizontal transformation
@@ -796,7 +936,7 @@ module RubySketch
796
936
  # @return [nil] nil
797
937
  #
798
938
  def rotate (angle)
799
- @painter__.rotate to_angle__ angle
939
+ @painter__.rotate toAngle__ angle
800
940
  nil
801
941
  end
802
942
 
@@ -805,7 +945,7 @@ module RubySketch
805
945
  # @return [nil] nil
806
946
  #
807
947
  def pushMatrix ()
808
- @painter__.push_matrix
948
+ @matrixStack__.push @painter__.matrix
809
949
  nil
810
950
  end
811
951
 
@@ -814,7 +954,8 @@ module RubySketch
814
954
  # @return [nil] nil
815
955
  #
816
956
  def popMatrix ()
817
- @painter__.pop_matrix
957
+ raise "matrix stack underflow" if @matrixStack__.empty?
958
+ @painter__.matrix = @matrixStack__.pop
818
959
  nil
819
960
  end
820
961
 
@@ -827,6 +968,61 @@ module RubySketch
827
968
  nil
828
969
  end
829
970
 
971
+ # Save current style values to the style stack.
972
+ #
973
+ # @return [nil] nil
974
+ #
975
+ def pushStyle ()
976
+ @styleStack__.push [
977
+ @painter__.fill,
978
+ @painter__.stroke,
979
+ @painter__.stroke_width,
980
+ @painter__.font,
981
+ @hsbColor__,
982
+ @colorMaxes__,
983
+ @angleScale__,
984
+ @rectMode__,
985
+ @ellipseMode__
986
+ ]
987
+ nil
988
+ end
989
+
990
+ # Restore style values from the style stack.
991
+ #
992
+ # @return [nil] nil
993
+ #
994
+ def popStyle ()
995
+ raise "style stack underflow" if @styleStack__.empty?
996
+ @painter__.fill,
997
+ @painter__.stroke,
998
+ @painter__.stroke_width,
999
+ @painter__.font,
1000
+ @hsbColor__,
1001
+ @colorMaxes__,
1002
+ @angleScale__,
1003
+ @rectMode__,
1004
+ @ellipseMode__ = @styleStack__.pop
1005
+ nil
1006
+ end
1007
+
1008
+ # Save current styles and transformations to stack.
1009
+ #
1010
+ # @return [nil] nil
1011
+ #
1012
+ def push ()
1013
+ pushMatrix
1014
+ pushStyle
1015
+ end
1016
+
1017
+ # Restore styles and transformations from stack.
1018
+ #
1019
+ # @return [nil] nil
1020
+ #
1021
+ def pop ()
1022
+ popMatrix
1023
+ popStyle
1024
+ end
1025
+
830
1026
  # Returns the perlin noise value.
831
1027
  #
832
1028
  # @overload noise(x)
@@ -841,75 +1037,146 @@ module RubySketch
841
1037
  Rays.perlin(x, y) / 2.0 + 1.0
842
1038
  end
843
1039
 
844
- # Font object.
1040
+ # Loads image.
845
1041
  #
846
- class Font
1042
+ # @param filename [String] file name to load image
1043
+ # @param extension [String] type of image to load (ex. 'png')
1044
+ #
1045
+ def loadImage (filename, extension = nil)
1046
+ filename = getImage__ filename, extension if filename =~ %r|^https?://|
1047
+ Image.new Rays::Image.load filename
1048
+ end
847
1049
 
848
- # Initialize font.
849
- #
850
- # @private
851
- #
852
- def initialize (font)
853
- @font = font
1050
+ # @private
1051
+ private def getImage__ (uri, ext)
1052
+ ext ||= File.extname uri
1053
+ raise "unsupported image type" unless ext =~ /^\.?(png)$/i
1054
+
1055
+ tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
1056
+ path = tmpdir + Digest::SHA1.hexdigest(uri)
1057
+ path = path.sub_ext ext
1058
+
1059
+ URI.open uri do |input|
1060
+ tmpdir.mkdir unless tmpdir.directory?
1061
+ path.open('w') do |output|
1062
+ while buf = input.read(2 ** 16)
1063
+ output.write buf
1064
+ end
1065
+ end
854
1066
  end
1067
+ path.to_s
1068
+ end
855
1069
 
856
- # Returns bounding box.
857
- #
858
- # @overload textBounds(str)
859
- # @overload textBounds(str, x, y)
860
- # @overload textBounds(str, x, y, fontSize)
861
- #
862
- # @param str [String] text to calculate bounding box
863
- # @param x [Numeric] horizontal position of bounding box
864
- # @param y [Numeric] vertical position of bounding box
865
- # @param fontSize [Numeric] font size
866
- #
867
- # @return [TextBounds] bounding box for text
868
- #
869
- def textBounds (str, x = 0, y = 0, fontSize = nil)
870
- f = fontSize ? Rays::Font.new(@font.name, fontSize) : @font
871
- TextBounds.new x, y, x + f.width(str), y + f.height
872
- end
1070
+ end# Processing
873
1071
 
874
- # Bounding box for text.
875
- #
876
- class TextBounds
877
-
878
- # Horizontal position
879
- #
880
- attr_reader :x
881
-
882
- # Vertical position
883
- #
884
- attr_reader :y
885
-
886
- # Width of bounding box
887
- #
888
- attr_reader :w
889
-
890
- # Height of bounding box
891
- #
892
- attr_reader :h
893
-
894
- # Initialize bouding box.
895
- #
896
- # @param x [Numeric] horizontal position
897
- # @param y [Numeric] vertical position
898
- # @param w [Numeric] width of bounding box
899
- # @param h [Numeric] height of bounding box
900
- #
901
- def initialize (x, y, w, h)
902
- @x, @y, @w, @h = x, y, w, h
903
- end
904
1072
 
905
- end# TextBounds
1073
+ # Image object.
1074
+ #
1075
+ class Processing::Image
1076
+
1077
+ # Initialize image.
1078
+ #
1079
+ def initialize (image)
1080
+ @image = image
1081
+ end
1082
+
1083
+ # Gets width of image.
1084
+ #
1085
+ # @return [Numeric] width of image
1086
+ #
1087
+ def width ()
1088
+ @image.width
1089
+ end
1090
+
1091
+ # Gets height of image.
1092
+ #
1093
+ # @return [Numeric] height of image
1094
+ #
1095
+ def height ()
1096
+ @image.height
1097
+ end
1098
+
1099
+ # Saves image to file.
1100
+ #
1101
+ # @param filename [String] file name to save image
1102
+ #
1103
+ def save (filename)
1104
+ @image.save filename
1105
+ end
1106
+
1107
+ # @private
1108
+ def internal ()
1109
+ @image
1110
+ end
1111
+
1112
+ end# Processing::Image
1113
+
1114
+
1115
+ # Font object.
1116
+ #
1117
+ class Processing::Font
1118
+
1119
+ # Initialize font.
1120
+ #
1121
+ # @private
1122
+ #
1123
+ def initialize (font)
1124
+ @font = font
1125
+ end
1126
+
1127
+ # Returns bounding box.
1128
+ #
1129
+ # @overload textBounds(str)
1130
+ # @overload textBounds(str, x, y)
1131
+ # @overload textBounds(str, x, y, fontSize)
1132
+ #
1133
+ # @param str [String] text to calculate bounding box
1134
+ # @param x [Numeric] horizontal position of bounding box
1135
+ # @param y [Numeric] vertical position of bounding box
1136
+ # @param fontSize [Numeric] font size
1137
+ #
1138
+ # @return [TextBounds] bounding box for text
1139
+ #
1140
+ def textBounds (str, x = 0, y = 0, fontSize = nil)
1141
+ f = fontSize ? Rays::Font.new(@font.name, fontSize) : @font
1142
+ TextBounds.new x, y, x + f.width(str), y + f.height
1143
+ end
1144
+
1145
+ end# Processing::Font
1146
+
1147
+
1148
+ # Bounding box for text.
1149
+ #
1150
+ class Processing::TextBounds
1151
+
1152
+ # Horizontal position
1153
+ #
1154
+ attr_reader :x
906
1155
 
907
- end# Font
1156
+ # Vertical position
1157
+ #
1158
+ attr_reader :y
908
1159
 
909
- end# Processing1
1160
+ # Width of bounding box
1161
+ #
1162
+ attr_reader :w
910
1163
 
1164
+ # Height of bounding box
1165
+ #
1166
+ attr_reader :h
1167
+
1168
+ # Initialize bouding box.
1169
+ #
1170
+ # @param x [Numeric] horizontal position
1171
+ # @param y [Numeric] vertical position
1172
+ # @param w [Numeric] width of bounding box
1173
+ # @param h [Numeric] height of bounding box
1174
+ #
1175
+ def initialize (x, y, w, h)
1176
+ @x, @y, @w, @h = x, y, w, h
1177
+ end
911
1178
 
912
- Processing = Processing1
1179
+ end# Processing::TextBounds
913
1180
 
914
1181
 
915
1182
  end# RubySketch
@@ -100,7 +100,7 @@ module RubySketch
100
100
  def call_block (block, event, *args)
101
101
  @events.push event
102
102
  block.call event, *args if block && !@error
103
- rescue => e
103
+ rescue Exception => e
104
104
  @error = e
105
105
  $stderr.puts e.full_message
106
106
  ensure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-12 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -107,8 +107,11 @@ files:
107
107
  - README.md
108
108
  - Rakefile
109
109
  - VERSION
110
+ - examples/clock.rb
110
111
  - examples/glsl.rb
111
112
  - examples/hello.rb
113
+ - examples/image.rb
114
+ - examples/shapes.rb
112
115
  - lib/rubysketch-processing.rb
113
116
  - lib/rubysketch.rb
114
117
  - lib/rubysketch/glsl.rb