rubysketch 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31a6c8685fd803f34636e81ee5098dfda6c2d82ab500952662e5b77c30601c09
4
- data.tar.gz: e4f22f4dbf9d01a0cc4f5f084648698ae42c0a1eddf582c0848849b194ac5933
3
+ metadata.gz: a700236b7d7c181b106c69193116f9c510025848f07c1bf23f21a034d75fa068
4
+ data.tar.gz: b881bfe8d43d923e48d6554a8a6af8e8f125831edab7fbb8551cbee7f314a7ab
5
5
  SHA512:
6
- metadata.gz: 5df3c266fb39715d6c83cda24268e48f27e922292398cda0f3c84412089fe88bc0a08a6373cd4b97faf342b64a79b3c7d7343794081522c7ae4894e8d7cefdc9
7
- data.tar.gz: 2c182badae8ae8f471647a2fc8289e7151730985de955b54d29f51c04ed8ee242ea89c274303e2f9d5b4d6189186b7ea635bc464d3ece7af5329124729f5c999
6
+ metadata.gz: 5dba04b623e671f1247dc0a8d27b60f8f024fd82d37f03da2a59e82040430c1f3645295e5debac488315faf0a57031e44eab5c0d93214faf30b2c7c75bffcba9
7
+ data.tar.gz: 4766c0a47804872d2aa9fa1119065df62c9b53254521697502a22340630f728bf47483f0bf6f9bf22c3d162e7865050db47bb261a49f7ee9e5c529a2c678c58c
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.3
1
+ 0.1.4
data/examples/clock.rb ADDED
@@ -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
@@ -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
@@ -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
 
@@ -42,14 +47,17 @@ module RubySketch
42
47
  @hsbColor__ = false
43
48
  @colorMaxes__ = [1.0] * 4
44
49
  @angleScale__ = 1.0
50
+ @styleStack = []
45
51
  @mouseX__ =
46
52
  @mouseY__ =
47
53
  @mousePrevX__ =
48
54
  @mousePrevY__ = 0
49
55
  @mousePressed__ = false
50
56
 
51
- colorMode RGB, 255
52
- angleMode RADIANS
57
+ colorMode RGB, 255
58
+ angleMode RADIANS
59
+ rectMode CORNER
60
+ ellipseMode CENTER
53
61
  end
54
62
 
55
63
  # @private
@@ -277,6 +285,8 @@ module RubySketch
277
285
  radian * RAD2DEG__
278
286
  end
279
287
 
288
+ # Define setup block.
289
+ #
280
290
  def setup (&block)
281
291
  @window__.setup = block
282
292
  nil
@@ -286,12 +296,16 @@ module RubySketch
286
296
  def setupDrawBlock__ ()
287
297
  @window__.draw = proc do |e, painter|
288
298
  @painter__ = painter
299
+ pushStyle
289
300
  @drawBlock__.call e if @drawBlock__
301
+ popStyle
290
302
  @frameCount__ += 1
291
303
  updateMousePrevPos__
292
304
  end
293
305
  end
294
306
 
307
+ # Define draw block.
308
+ #
295
309
  def draw (&block)
296
310
  @drawBlock__ = block if block
297
311
  nil
@@ -483,7 +497,7 @@ module RubySketch
483
497
  a, b, c, d = args
484
498
  return parse_color__(a, b || alphaMax__) if a.kind_of?(String)
485
499
 
486
- rgba = case args.size
500
+ rgba = case args.size
487
501
  when 1, 2 then [a, a, a, b || alphaMax__]
488
502
  when 3, 4 then [a, b, c, d || alphaMax__]
489
503
  else raise ArgumentError
@@ -519,6 +533,7 @@ module RubySketch
519
533
  when DEGREES then 1.0
520
534
  else raise ArgumentError, "Invalid angle mode: #{mode}"
521
535
  end
536
+ nil
522
537
  end
523
538
 
524
539
  # @private
@@ -526,6 +541,47 @@ module RubySketch
526
541
  angle * @angleScale__
527
542
  end
528
543
 
544
+ # Sets rect mode. Default is CORNER.
545
+ #
546
+ # CORNER -> rect(left, top, width, height)
547
+ # CORNERS -> rect(left, top, right, bottom)
548
+ # CENTER -> rect(center_x, center_y, width, height)
549
+ # RADIUS -> rect(center_x, center_y, radius_h, radius_v)
550
+ #
551
+ # @param mode [CORNER, CORNERS, CENTER, RADIUS]
552
+ #
553
+ # @return [nil] nil
554
+ #
555
+ def rectMode (mode)
556
+ @rectMode__ = mode
557
+ end
558
+
559
+ # Sets ellipse mode. Default is CENTER.
560
+ #
561
+ # CORNER -> ellipse(left, top, width, height)
562
+ # CORNERS -> ellipse(left, top, right, bottom)
563
+ # CENTER -> ellipse(center_x, center_y, width, height)
564
+ # RADIUS -> ellipse(center_x, center_y, radius_h, radius_v)
565
+ #
566
+ # @param mode [CORNER, CORNERS, CENTER, RADIUS]
567
+ #
568
+ # @return [nil] nil
569
+ #
570
+ def ellipseMode (mode)
571
+ @ellipseMode__ = mode
572
+ end
573
+
574
+ # @private
575
+ def to_xywh__ (mode, a, b, c, d)
576
+ case mode
577
+ when CORNER then [a, b, c, d]
578
+ when CORNERS then [a, b, c - a, d - b]
579
+ when CENTER then [a - c / 2.0, b - d / 2.0, c, d]
580
+ when RADIUS then [a - c, b - d, c * 2, d * 2]
581
+ else raise ArgumentError # ToDo: refine error message
582
+ end
583
+ end
584
+
529
585
  # Clears screen.
530
586
  #
531
587
  # @overload background(str)
@@ -545,7 +601,16 @@ module RubySketch
545
601
  # @return [nil] nil
546
602
  #
547
603
  def background (*args)
548
- @painter__.background(*to_rgba__(*args))
604
+ rgba = to_rgba__ *args
605
+ if rgba[3] == 1
606
+ @painter__.background *rgba
607
+ else
608
+ f = @painter__.fill *rgba
609
+ s = @painter__.stroke nil
610
+ @painter__.rect 0, 0, width, height
611
+ @painter__.fill f
612
+ @painter__.stroke s
613
+ end
549
614
  nil
550
615
  end
551
616
 
@@ -647,6 +712,20 @@ module RubySketch
647
712
  nil
648
713
  end
649
714
 
715
+ # Draws a point.
716
+ #
717
+ # @param x [Numeric] horizontal position
718
+ # @param y [Numeric] vertical position
719
+ #
720
+ # @return [nil] nil
721
+ #
722
+ def point (x, y)
723
+ w = @painter__.stroke_width
724
+ w = 1 if w == 0
725
+ @painter__.ellipse x - (w / 2.0), y - (w / 2.0), w, w
726
+ nil
727
+ end
728
+
650
729
  # Draws a line.
651
730
  #
652
731
  # @param x1 [Numeric] horizontal position for first point
@@ -663,14 +742,14 @@ module RubySketch
663
742
 
664
743
  # Draws a rectangle.
665
744
  #
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)
745
+ # @overload rect(a, b, c, d)
746
+ # @overload rect(a, b, c, d, r)
747
+ # @overload rect(a, b, c, d, tl, tr, br, bl)
669
748
  #
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
749
+ # @param a [Numeric] horizontal position of the shape by default
750
+ # @param b [Numeric] vertical position of the shape by default
751
+ # @param c [Numeric] width of the shape by default
752
+ # @param d [Numeric] height of the shape by default
674
753
  # @param r [Numeric] radius for all corners
675
754
  # @param tl [Numeric] radius for top-left corner
676
755
  # @param tr [Numeric] radius for top-right corner
@@ -679,7 +758,8 @@ module RubySketch
679
758
  #
680
759
  # @return [nil] nil
681
760
  #
682
- def rect (x, y, w, h, *args)
761
+ def rect (a, b, c, d, *args)
762
+ x, y, w, h = to_xywh__ @rectMode__, a, b, c, d
683
763
  case args.size
684
764
  when 0 then @painter__.rect x, y, w, h
685
765
  when 1 then @painter__.rect x, y, w, h, round: args[0]
@@ -689,24 +769,25 @@ module RubySketch
689
769
  nil
690
770
  end
691
771
 
692
- # Draws a ellipse.
772
+ # Draws an ellipse.
693
773
  #
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
774
+ # @param a [Numeric] horizontal position of the shape
775
+ # @param b [Numeric] vertical position of the shape
776
+ # @param c [Numeric] width of the shape
777
+ # @param d [Numeric] height of the shape
698
778
  #
699
779
  # @return [nil] nil
700
780
  #
701
- def ellipse (x, y, w, h = w)
702
- @painter__.ellipse (x - w / 2.0), (y - h / 2.0), w, h
781
+ def ellipse (a, b, c, d)
782
+ x, y, w, h = to_xywh__ @ellipseMode__, a, b, c, d
783
+ @painter__.ellipse x, y, w, h
703
784
  nil
704
785
  end
705
786
 
706
787
  # Draws a circle.
707
788
  #
708
- # @param x [Numeric] horizontal position
709
- # @param y [Numeric] vertical position
789
+ # @param x [Numeric] horizontal position of the shape
790
+ # @param y [Numeric] vertical position of the shape
710
791
  # @param extent [Numeric] width and height of the shape
711
792
  #
712
793
  # @return [nil] nil
@@ -717,31 +798,66 @@ module RubySketch
717
798
 
718
799
  # Draws an arc.
719
800
  #
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
801
+ # @param a [Numeric] horizontal position of the shape
802
+ # @param b [Numeric] vertical position of the shape
803
+ # @param c [Numeric] width of the shape
804
+ # @param d [Numeric] height of the shape
724
805
  # @param start [Numeric] angle to start the arc
725
806
  # @param stop [Numeric] angle to stop the arc
726
807
  #
727
808
  # @return [nil] nil
728
809
  #
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
810
+ def arc (a, b, c, d, start, stop)
811
+ x, y, w, h = to_xywh__ @ellipseMode__, a, b, c, d
812
+ start = to_angle__ start
813
+ stop = to_angle__ stop
814
+ @painter__.ellipse x, y, w, h, from: start, to: stop
733
815
  nil
734
816
  end
735
817
 
736
- # Draws a point.
818
+ # Draws a square.
737
819
  #
738
- # @param x [Numeric] horizontal position
739
- # @param y [Numeric] vertical position
820
+ # @param x [Numeric] horizontal position of the shape
821
+ # @param y [Numeric] vertical position of the shape
822
+ # @param extent [Numeric] width and height of the shape
740
823
  #
741
824
  # @return [nil] nil
742
825
  #
743
- def point (x, y)
744
- @painter__.rect x - 0.5, y - 0.5, 1, 1
826
+ def square (x, y, extent)
827
+ rect x, y, extent, extent
828
+ end
829
+
830
+ # Draws a triangle.
831
+ #
832
+ # @param x1 [Numeric] horizontal position of first point
833
+ # @param y1 [Numeric] vertical position of first point
834
+ # @param x2 [Numeric] horizontal position of second point
835
+ # @param y2 [Numeric] vertical position of second point
836
+ # @param x3 [Numeric] horizontal position of third point
837
+ # @param y3 [Numeric] vertical position of third point
838
+ #
839
+ # @return [nil] nil
840
+ #
841
+ def triangle (x1, y1, x2, y2, x3, y3)
842
+ @painter__.line x1, y1, x2, y2, x3, y3, loop: true
843
+ nil
844
+ end
845
+
846
+ # Draws a quad.
847
+ #
848
+ # @param x1 [Numeric] horizontal position of first point
849
+ # @param y1 [Numeric] vertical position of first point
850
+ # @param x2 [Numeric] horizontal position of second point
851
+ # @param y2 [Numeric] vertical position of second point
852
+ # @param x3 [Numeric] horizontal position of third point
853
+ # @param y3 [Numeric] vertical position of third point
854
+ # @param x4 [Numeric] horizontal position of fourth point
855
+ # @param y4 [Numeric] vertical position of fourth point
856
+ #
857
+ # @return [nil] nil
858
+ #
859
+ def quad (x1, y1, x2, y2, x3, y3, x4, y4)
860
+ @painter__.line x1, y1, x2, y2, x3, y3, x4, y4, loop: true
745
861
  nil
746
862
  end
747
863
 
@@ -751,12 +867,12 @@ module RubySketch
751
867
  # @overload text(str, x, y)
752
868
  #
753
869
  # @param str [String] text to draw
754
- # @param x [Numeric] horizontal position
755
- # @param y [Numeric] vertical position
870
+ # @param x [Numeric] horizontal position of the text
871
+ # @param y [Numeric] vertical position of the text
756
872
  #
757
873
  # @return [nil] nil
758
874
  #
759
- def text (str, x = 0, y = 0)
875
+ def text (str, x, y)
760
876
  @painter__.text str, x, y
761
877
  nil
762
878
  end
@@ -827,6 +943,51 @@ module RubySketch
827
943
  nil
828
944
  end
829
945
 
946
+ # Save current style values to the style stack.
947
+ #
948
+ # @return [nil] nil
949
+ #
950
+ def pushStyle ()
951
+ @painter__.push_state
952
+ @styleStack.push [
953
+ @hsbColor__,
954
+ @colorMaxes__,
955
+ @angleScale__,
956
+ @rectMode__,
957
+ @ellipseMode__
958
+ ]
959
+ nil
960
+ end
961
+
962
+ # Restore style values from the style stack.
963
+ #
964
+ # @return [nil] nil
965
+ #
966
+ def popStyle ()
967
+ @painter__.pop_state
968
+ @hsbColor__, @colorMaxes__, @angleScale__, @rectMode__, @ellipseMode__ =
969
+ @styleStack.pop
970
+ nil
971
+ end
972
+
973
+ # Save current styles and transformations to stack.
974
+ #
975
+ # @return [nil] nil
976
+ #
977
+ def push ()
978
+ pushMatrix
979
+ pushStyle
980
+ end
981
+
982
+ # Restore styles and transformations from stack.
983
+ #
984
+ # @return [nil] nil
985
+ #
986
+ def pop ()
987
+ popMatrix
988
+ popStyle
989
+ end
990
+
830
991
  # Returns the perlin noise value.
831
992
  #
832
993
  # @overload noise(x)
@@ -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.1.4
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-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -107,8 +107,10 @@ 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/shapes.rb
112
114
  - lib/rubysketch-processing.rb
113
115
  - lib/rubysketch.rb
114
116
  - lib/rubysketch/glsl.rb