rubysketch 0.3.6 → 0.3.7

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: 1a40d65c055cc2baacd0a7489ec511a0db37b8e534f58fc02d5c095f78ecca66
4
- data.tar.gz: 9c8634c610e0df8201655809b82bc8e7dbc2adab8cad405e2033ecc321c09137
3
+ metadata.gz: 41b3a75aeb6c8bdc443d2d87e44c60361a527e4d6b8b3918e8b1eaa2503f92e4
4
+ data.tar.gz: 7f373d0f3fdb31d38bb76d8e8ed88ce2f3cedca0c696d8a5f571ded1e78fb15d
5
5
  SHA512:
6
- metadata.gz: 04d5e9096e7cef9ea5d6c16e3397d5965bc8da07f699fc3b241e91ad982c755fe7b4faf4296b6ce74612da690cc7d7cfd507612387f43276b0e2ab0d05566ddf
7
- data.tar.gz: 224f32277392e5b84c84b6ef13a437813859f960e47dce696f97dd547273d0d86a3e62685a171a7055981989fd99679e60b843e12b839bc2b501c4e9fbb7e622
6
+ metadata.gz: ffcba1a2e5ae98525c4c1512230d249995071e6be9183918b6f62ed5293d9ac8f530d38aa5f544d2d4fc431ca17f38c7746955df313e9609e0ee528551045a51
7
+ data.tar.gz: cb49daa453cdf6dd9027a59c508363a63fed2e210b0acbcfa311e66334b107a88471e5b8b59dac7f77b5aa04423aaa2d0cd2b2fa0c4b1221b76997a93ce89e79
@@ -1,6 +1,15 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.7] - 2020-11-18
5
+
6
+ - add Capture class
7
+ - add log(), exp(), sqrt() and PI
8
+ - add examples/camera.rb
9
+ - add examples/breakout.rb
10
+ - fix error on calling image()
11
+
12
+
4
13
  ## [0.3.6] - 2020-08-02
5
14
 
6
15
  - random() can take array or nothing
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.6
1
+ 0.3.7
@@ -0,0 +1,212 @@
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
+ PADDING = 50
9
+ BRICK_COUNT = 10
10
+
11
+ $objs = []
12
+ $bar = nil
13
+ $gameover = false
14
+
15
+ setup do
16
+ addWalls
17
+ addBricks
18
+
19
+ colorMode HSB, 360, 100, 100
20
+ ellipseMode CORNER
21
+ background 0
22
+ noStroke
23
+ fill 0, 0, 100
24
+ end
25
+
26
+ draw do
27
+ background 0
28
+ $objs.each do |o|
29
+ o.update
30
+ o.draw
31
+ end
32
+ drawTexts
33
+ end
34
+
35
+ mousePressed do
36
+ start unless started?
37
+ end
38
+
39
+ mouseDragged do
40
+ $bar.pos.x = mouseX - $bar.w / 2 if $bar
41
+ end
42
+
43
+ def start()
44
+ $bar = Bar.new
45
+ $objs += [$bar, Ball.new($bar)]
46
+ end
47
+
48
+ def started?()
49
+ $bar
50
+ end
51
+
52
+ def gameover?()
53
+ started? &&
54
+ $objs.count {|o| o.kind_of? Ball} == 0
55
+ end
56
+
57
+ def cleared?()
58
+ started? && !gameover? &&
59
+ $objs.count {|o| o.kind_of? Brick} == 0
60
+ end
61
+
62
+ def addWalls()
63
+ left = Obj.new 0, 0, 10, height
64
+ top = Obj.new 0, 0, width, 10
65
+ right = Obj.new width - 10, 0, 10, height
66
+ bottom = Bottom.new 0, height - 10, width, 10
67
+ $objs += [top, bottom, left, right]
68
+ end
69
+
70
+ def addBricks()
71
+ brickW = (width - PADDING * 2) / BRICK_COUNT
72
+ 5.times do |y|
73
+ BRICK_COUNT.times do |x|
74
+ xx = PADDING + brickW * x
75
+ yy = PADDING + 30 * y
76
+ $objs.push Brick.new(xx, yy, brickW - 5, 20, y * 60)
77
+ end
78
+ end
79
+ end
80
+
81
+ def drawTexts()
82
+ push do
83
+ textAlign CENTER, CENTER
84
+
85
+ if !started?
86
+ fill 50, 100, 100
87
+ textSize 50
88
+ text "BREAKOUT", 0, 0, width, height
89
+ textSize 20
90
+ translate 0, 100
91
+ text "Tap to start!", 0, 0, width, height
92
+ elsif cleared?
93
+ fill 100, 80, 100
94
+ textSize 50
95
+ text "CLEAR!", 0, 0, width, height
96
+ elsif gameover?
97
+ fill 0, 20, 100
98
+ textSize 50
99
+ text "GAMEOVER", 0, 0, width, height
100
+ end
101
+ end
102
+ end
103
+
104
+ class Obj
105
+ attr_reader :pos, :w, :h, :vel
106
+
107
+ def initialize(x, y, w, h, vx = 0, vy = 0)
108
+ @pos = createVector x, y
109
+ @w, @h = w, h
110
+ @vel = createVector vx, vy
111
+ end
112
+
113
+ def update()
114
+ @pos.add @vel
115
+ end
116
+
117
+ def draw()
118
+ rect @pos.x, @pos.y, @w, @h
119
+ end
120
+
121
+ def bounds()
122
+ x, y = @pos.x, @pos.y
123
+ return x, y, x + @w, y + @h
124
+ end
125
+
126
+ def center()
127
+ createVector @pos.x + @w / 2, @pos.y + @h / 2
128
+ end
129
+ end
130
+
131
+ class Ball < Obj
132
+ def initialize(bar)
133
+ super bar.pos.x, bar.pos.y - bar.h, 20, 20
134
+ self.vel = createVector random(-1, 1), -1
135
+ end
136
+
137
+ def vel=(v)
138
+ @vel = v.dup.normalize.mult 8
139
+ end
140
+
141
+ def update()
142
+ b = bounds.dup
143
+ super
144
+ checkHit b
145
+ end
146
+
147
+ def checkHit(prevBounds)
148
+ x1, y1, x2, y2 = prevBounds
149
+ hitH = hitV = false
150
+ hits = []
151
+ $objs.each do |o|
152
+ next if o == self
153
+ next unless intersect? o
154
+ hits.push o
155
+ ox1, oy1, ox2, oy2 = o.bounds
156
+ hitH ||= !overlap?(x1, x2, ox1, ox2)
157
+ hitV ||= !overlap?(y1, y2, oy1, oy2)
158
+ end
159
+ vel.x *= -1 if hitH
160
+ vel.y *= -1 if hitV
161
+
162
+ hits.each {|o| hit o}
163
+ end
164
+
165
+ def intersect?(o)
166
+ x1, y1, x2, y2 = bounds
167
+ ox1, oy1, ox2, oy2 = o.bounds
168
+ overlap?(x1, x2, ox1, ox2) && overlap?(y1, y2, oy1, oy2)
169
+ end
170
+
171
+ def overlap?(a1, a2, b1, b2)
172
+ a1 <= b2 && b1 <= a2
173
+ end
174
+
175
+ def hit(o)
176
+ case o
177
+ when Bar then self.vel = center.sub o.center
178
+ when Brick then $objs.delete o
179
+ when Bottom then $objs.delete self unless cleared?
180
+ end
181
+ end
182
+ end
183
+
184
+ class Bar < Obj
185
+ def initialize()
186
+ w = 100
187
+ super (width - w) / 2, height - 50, w, 20
188
+ end
189
+ end
190
+
191
+ class Brick < Obj
192
+ def initialize(x, y, w, h, hue)
193
+ super x, y, w, h
194
+ @hue = hue
195
+ end
196
+
197
+ def draw()
198
+ push do
199
+ fill @hue, 50, 100
200
+ super
201
+ end
202
+ end
203
+ end
204
+
205
+ class Bottom < Obj
206
+ def draw()
207
+ push do
208
+ fill 0, 0, 50
209
+ super
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,14 @@
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
+ cam = Capture.new
9
+ cam.start
10
+
11
+ draw do
12
+ background 0
13
+ image cam, 0, 0
14
+ end
@@ -2,10 +2,18 @@ require 'rubysketch'
2
2
 
3
3
 
4
4
  begin
5
- include RubySketch::Processing::Context
5
+ window = RubySketch::Window.new {start}
6
+ context = RubySketch::Processing::Context.new window
6
7
 
7
- window = RubySketch::Window.new {start}
8
- setup__ window
8
+ (context.methods - Object.instance_methods).each do |method|
9
+ define_method method do |*args, &block|
10
+ context.__send__ method, *args, &block
11
+ end
12
+ end
13
+
14
+ context.class.constants.each do |const|
15
+ self.class.const_set const, context.class.const_get(const)
16
+ end
9
17
 
10
18
  window.__send__ :begin_draw
11
19
  at_exit do
@@ -141,9 +141,9 @@ module RubySketch
141
141
  #
142
142
  def lerp (*args, amount)
143
143
  v = toVector__ *args
144
- self.x = Context.lerp x, v.x, amount
145
- self.y = Context.lerp y, v.y, amount
146
- self.z = Context.lerp z, v.z, amount
144
+ self.x = x + (v.x - x) * amount
145
+ self.y = y + (v.y - y) * amount
146
+ self.z = z + (v.z - z) * amount
147
147
  self
148
148
  end
149
149
 
@@ -728,27 +728,92 @@ module RubySketch
728
728
  end# Touch
729
729
 
730
730
 
731
+ # Camera object.
732
+ #
733
+ class Capture
734
+
735
+ # Returns a list of available camera device names
736
+ #
737
+ # @return [Array] device name list
738
+ #
739
+ def self.list ()
740
+ ['default']
741
+ end
742
+
743
+ # Initialize camera object.
744
+ #
745
+ def initialize ()
746
+ @camera = Rays::Camera.new
747
+ end
748
+
749
+ # Start capturing.
750
+ #
751
+ # @return [Capture] self
752
+ #
753
+ def start ()
754
+ raise "Failed to start capture" unless @camera.start
755
+ self
756
+ end
757
+
758
+ # Stop capturing.
759
+ #
760
+ # @return [nil] nil
761
+ #
762
+ def stop ()
763
+ @camera.stop
764
+ nil
765
+ end
766
+
767
+ # Returns is the next captured image available?
768
+ #
769
+ # @return [Boolean] true means object has next frame
770
+ #
771
+ def available ()
772
+ @camera.active?
773
+ end
774
+
775
+ # Reads next frame image
776
+ #
777
+ def read ()
778
+ @camera.image
779
+ end
780
+
781
+ # @private
782
+ def getInternal__ ()
783
+ @camera.image || dummyImage__
784
+ end
785
+
786
+ # @private
787
+ private def dummyImage__ ()
788
+ @dummy ||= Rays::Image.new 1, 1
789
+ end
790
+
791
+ end# Capture
792
+
793
+
731
794
  # Drawing context
732
795
  #
733
796
  module GraphicsContext
734
797
 
735
- Vector = Processing::Vector
798
+ # PI
799
+ #
800
+ PI = Math::PI
736
801
 
737
802
  # PI / 2
738
803
  #
739
- HALF_PI = Math::PI / 2
804
+ HALF_PI = PI / 2
740
805
 
741
806
  # PI / 4
742
807
  #
743
- QUARTER_PI = Math::PI / 4
808
+ QUARTER_PI = PI / 4
744
809
 
745
810
  # PI * 2
746
811
  #
747
- TWO_PI = Math::PI * 2
812
+ TWO_PI = PI * 2
748
813
 
749
814
  # PI * 2
750
815
  #
751
- TAU = Math::PI * 2
816
+ TAU = PI * 2
752
817
 
753
818
  # RGB mode for colorMode().
754
819
  #
@@ -817,7 +882,7 @@ module RubySketch
817
882
  @painter__ = painter
818
883
  @painter__.miter_limit = 10
819
884
 
820
- @drawing = false
885
+ @drawing__ = false
821
886
  @hsbColor__ = false
822
887
  @colorMaxes__ = [1.0] * 4
823
888
  @angleScale__ = 1.0
@@ -843,11 +908,11 @@ module RubySketch
843
908
  def beginDraw ()
844
909
  @matrixStack__.clear
845
910
  @styleStack__.clear
846
- @drawing = true
911
+ @drawing__ = true
847
912
  end
848
913
 
849
914
  def endDraw ()
850
- @drawing = false
915
+ @drawing__ = false
851
916
  end
852
917
 
853
918
  def width ()
@@ -1411,8 +1476,9 @@ module RubySketch
1411
1476
  #
1412
1477
  def image (img, a, b, c = nil, d = nil)
1413
1478
  assertDrawing__
1414
- x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
1415
- @painter__.image img.getInternal__, x, y, w, h
1479
+ i = img.getInternal__
1480
+ x, y, w, h = toXYWH__ @imageMode__, a, b, c || i.width, d || i.height
1481
+ @painter__.image i, x, y, w, h
1416
1482
  nil
1417
1483
  end
1418
1484
 
@@ -1588,13 +1654,13 @@ module RubySketch
1588
1654
  end
1589
1655
 
1590
1656
  # @private
1591
- private def getInternal__ ()
1657
+ def getInternal__ ()
1592
1658
  @image__
1593
1659
  end
1594
1660
 
1595
1661
  # @private
1596
1662
  private def assertDrawing__ ()
1597
- raise "call beginDraw() before drawing" unless @drawing
1663
+ raise "call beginDraw() before drawing" unless @drawing__
1598
1664
  end
1599
1665
 
1600
1666
  end# GraphicsContext
@@ -1606,17 +1672,23 @@ module RubySketch
1606
1672
 
1607
1673
  include GraphicsContext
1608
1674
 
1675
+ # Initialize graphics object.
1676
+ #
1609
1677
  def initialize (width, height)
1610
1678
  @image__ = Rays::Image.new width, height
1611
1679
  setup__ @image__.painter
1612
1680
  end
1613
1681
 
1682
+ # Start drawing.
1683
+ #
1614
1684
  def beginDraw ()
1615
1685
  @painter__.__send__ :begin_paint
1616
1686
  super
1617
1687
  push
1618
1688
  end
1619
1689
 
1690
+ # End drawing.
1691
+ #
1620
1692
  def endDraw ()
1621
1693
  pop
1622
1694
  super
@@ -1628,9 +1700,13 @@ module RubySketch
1628
1700
 
1629
1701
  # Processing context
1630
1702
  #
1631
- module Context
1703
+ class Context
1704
+
1705
+ include GraphicsContext
1632
1706
 
1633
- include GraphicsContext, Math
1707
+ Vector = Processing::Vector
1708
+ Capture = Processing::Capture
1709
+ Graphics = Processing::Graphics
1634
1710
 
1635
1711
  @@context__ = nil
1636
1712
 
@@ -1639,12 +1715,12 @@ module RubySketch
1639
1715
  end
1640
1716
 
1641
1717
  # @private
1642
- def setup__ (window)
1718
+ def initialize (window)
1643
1719
  @@context__ = self
1644
1720
 
1645
1721
  @window__ = window
1646
1722
  @image__ = @window__.canvas
1647
- super @window__.canvas_painter.paint {background 0.8}
1723
+ setup__ @window__.canvas_painter.paint {background 0.8}
1648
1724
 
1649
1725
  @loop__ = true
1650
1726
  @redraw__ = false
@@ -1865,7 +1941,9 @@ module RubySketch
1865
1941
  @redraw__ = true
1866
1942
  end
1867
1943
 
1868
- module_function
1944
+ #
1945
+ # Utilities
1946
+ #
1869
1947
 
1870
1948
  # Returns the absolute number of the value.
1871
1949
  #
@@ -1907,6 +1985,26 @@ module RubySketch
1907
1985
  value.round
1908
1986
  end
1909
1987
 
1988
+ # Returns the natural logarithm (the base-e logarithm) of a number.
1989
+ #
1990
+ # @param value [Numeric] number (> 0.0)
1991
+ #
1992
+ # @return [Numeric] result number
1993
+ #
1994
+ def log (n)
1995
+ Math.log n
1996
+ end
1997
+
1998
+ # Returns Euler's number e raised to the power of value.
1999
+ #
2000
+ # @param value [Numeric] number
2001
+ #
2002
+ # @return [Numeric] result number
2003
+ #
2004
+ def exp (n)
2005
+ Math.exp n
2006
+ end
2007
+
1910
2008
  # Returns value raised to the power of exponent.
1911
2009
  #
1912
2010
  # @param value [Numeric] base number
@@ -1928,6 +2026,16 @@ module RubySketch
1928
2026
  value * value
1929
2027
  end
1930
2028
 
2029
+ # Returns squared value.
2030
+ #
2031
+ # @param value [Numeric] number
2032
+ #
2033
+ # @return [Numeric] squared value
2034
+ #
2035
+ def sqrt (value)
2036
+ Math.sqrt value
2037
+ end
2038
+
1931
2039
  # Returns the magnitude (or length) of a vector.
1932
2040
  #
1933
2041
  # @overload mag(x, y)
@@ -2202,6 +2310,14 @@ module RubySketch
2202
2310
  Vector.new *args
2203
2311
  end
2204
2312
 
2313
+ # Creates a camera object as a video input device.
2314
+ #
2315
+ # @return [Capture] camera object
2316
+ #
2317
+ def createCapture (*args)
2318
+ Capture.new *args
2319
+ end
2320
+
2205
2321
  # Creates a new off-screen graphics context object.
2206
2322
  #
2207
2323
  # @param width [Numeric] width of graphics image
@@ -6,27 +6,31 @@ require_relative '../helper'
6
6
 
7
7
  class TestProcessingUtility < Test::Unit::TestCase
8
8
 
9
- C = RubySketch::Processing::Context
9
+ class Context
10
+ include RubySketch::Processing::Context
11
+ end
10
12
 
11
- def setup ()
12
-
13
+ def context
14
+ Context.new
13
15
  end
14
16
 
15
17
  def test_random ()
16
- assert_equal Float, C.random(1).class
17
- assert_equal Float, C.random(1.0).class
18
- assert_equal Symbol, C.random((:a..:z).to_a).class
18
+ c = context
19
+
20
+ assert_equal Float, c.random(1).class
21
+ assert_equal Float, c.random(1.0).class
22
+ assert_equal Symbol, c.random((:a..:z).to_a).class
19
23
 
20
- assert_not_equal C.random, C.random
24
+ assert_not_equal c.random, c.random
21
25
 
22
26
  10000.times do
23
- n = C.random
27
+ n = c.random
24
28
  assert 0 <= n && n < 1
25
29
 
26
- n = C.random 1
30
+ n = c.random 1
27
31
  assert 0 <= n && n < 1
28
32
 
29
- n = C.random 1, 2
33
+ n = c.random 1, 2
30
34
  assert 1.0 <= n && n < 2.0
31
35
  end
32
36
  end
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.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-05 00:00:00.000000000 Z
11
+ date: 2020-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reflexion
@@ -40,6 +40,8 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - VERSION
43
+ - examples/breakout.rb
44
+ - examples/camera.rb
43
45
  - examples/clock.rb
44
46
  - examples/glsl.rb
45
47
  - examples/hello.rb