rubysketch 0.3.10 → 0.3.11

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: 756b461427f81652025d1b032458a21e02a0405cbe876f93f42868a2a0d63091
4
- data.tar.gz: 484f88ebc98855c074c7b2b8a1d11c0384d9acc9267775c9f72b68cd730c3967
3
+ metadata.gz: f9d6862ce564503738c10d66ea1b76000d57ffa7100a0e47bf0b96c8083e02dd
4
+ data.tar.gz: 44825a802755cd03c1efa129ec0a0349872f330795433839f2232ef2b9b83a24
5
5
  SHA512:
6
- metadata.gz: bbfb538eaf51488a522b98f41e6c193d40aabf960421118cfd63d76072bada647ba3dbb993e23bf52d025701b348c2736a893308a9dc220e4347e7caa91bafff
7
- data.tar.gz: 79871d6ed4f4c2fa0799ccfdeb62062a963c9dce05b7b17feafa5b23dba937d69cb3839efb986ed51f9f334c370accf82aebe0417305a9c186ea3f9b05a04415
6
+ metadata.gz: ed6efaf98ea1b9e816d337ebf82d57516f3f2fb30877d693f2a18c42d24d85eaf981fa804c1f1761ec0bd86ec73e8a5f88fcb50103c5bd5979e46bf58a2c0162
7
+ data.tar.gz: 5d8d25591fdbc056dc0f793d5a8c93cae50547731e10128c6ec27e9432491f238d5e547f9cc74701beb85903a39114da045cd320cf1903f235afa7975bef42ca
@@ -1,6 +1,11 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.11] - 2020-12-9
5
+
6
+ - add size(), createCanvas() and pixelDensity()
7
+
8
+
4
9
  ## [0.3.10] - 2020-12-1
5
10
 
6
11
  - invert angle parameter value for arc() to fix compatibility to processing API
data/README.md CHANGED
@@ -1,3 +1,11 @@
1
- # RubySketch - Processing like Creative Coding Framework
1
+ # RubySketch - Processing compatible Creative Coding Framework
2
2
 
3
3
  by xordog@gmail.com
4
+
5
+
6
+ # How to release
7
+
8
+ ```
9
+ $ rake version:bump:patch message="..."
10
+ $ git push origin --tags
11
+ ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.10
1
+ 0.3.11
@@ -913,10 +913,7 @@ module RubySketch
913
913
  #
914
914
  SQUARE = :square
915
915
 
916
- def setup__ (painter)
917
- @painter__ = painter
918
- @painter__.miter_limit = 10
919
-
916
+ def init__ (image, painter)
920
917
  @drawing__ = false
921
918
  @hsbColor__ = false
922
919
  @colorMaxes__ = [1.0] * 4
@@ -929,6 +926,8 @@ module RubySketch
929
926
  @matrixStack__ = []
930
927
  @styleStack__ = []
931
928
 
929
+ updateCanvas__ image, painter
930
+
932
931
  colorMode RGB, 255
933
932
  angleMode RADIANS
934
933
  rectMode CORNER
@@ -940,6 +939,10 @@ module RubySketch
940
939
  stroke 0
941
940
  end
942
941
 
942
+ def updateCanvas__ (image, painter)
943
+ @image__, @painter__ = image, painter
944
+ end
945
+
943
946
  # @private
944
947
  def beginDraw__ ()
945
948
  @matrixStack__.clear
@@ -1712,8 +1715,8 @@ module RubySketch
1712
1715
  # Initialize graphics object.
1713
1716
  #
1714
1717
  def initialize (width, height)
1715
- @image__ = Rays::Image.new width, height
1716
- setup__ @image__.painter
1718
+ image = Rays::Image.new width, height
1719
+ init__ image, image.painter
1717
1720
  end
1718
1721
 
1719
1722
  # Start drawing.
@@ -1762,14 +1765,13 @@ module RubySketch
1762
1765
  @@context__ = self
1763
1766
 
1764
1767
  @window__ = window
1765
- @image__ = @window__.canvas
1766
- setup__ @window__.canvas_painter.paint {background 0.8}
1768
+ init__ @window__.canvas, @window__.canvas_painter.paint {background 0.8}
1767
1769
 
1768
1770
  @loop__ = true
1769
1771
  @redraw__ = false
1770
1772
  @frameCount__ = 0
1771
1773
  @mousePos__ =
1772
- @mousePrevPos__ = Rays::Point.new 0
1774
+ @mousePrevPos__ = [0, 0]
1773
1775
  @mousePressed__ = false
1774
1776
  @touches__ = []
1775
1777
 
@@ -1777,8 +1779,7 @@ module RubySketch
1777
1779
  @window__.after_draw = proc {endDraw__}
1778
1780
 
1779
1781
  drawFrame = -> {
1780
- @image__ = @window__.canvas
1781
- @painter__ = @window__.canvas_painter
1782
+ updateCanvas__ @window__.canvas, @window__.canvas_painter
1782
1783
  begin
1783
1784
  push
1784
1785
  @drawBlock__.call if @drawBlock__
@@ -1797,9 +1798,11 @@ module RubySketch
1797
1798
  end
1798
1799
 
1799
1800
  updatePointerStates = -> event, pressed = nil {
1800
- @mousePos__ = event.pos
1801
+ @mousePos__ = @window__.to_canvas_coord event.pos.x, event.pos.y
1801
1802
  @mousePressed__ = pressed if pressed != nil
1802
- @touches__ = event.positions.map {|pos| Touch.new pos.x, pos.y}
1803
+ @touches__ = event.positions.map {|pos|
1804
+ Touch.new *@window__.to_canvas_coord(pos.x, pos.y)
1805
+ }
1803
1806
  }
1804
1807
 
1805
1808
  @window__.pointer_down = proc do |e|
@@ -1877,15 +1880,59 @@ module RubySketch
1877
1880
  nil
1878
1881
  end
1879
1882
 
1883
+ # Changes canvas size.
1884
+ #
1885
+ # @param width [Integer] new width
1886
+ # @param height [Integer] new height
1887
+ #
1888
+ # @return [nil] nil
1889
+ #
1890
+ def size (width, height)
1891
+ resizeCanvas__ :size, width, height, pixelDensity
1892
+ nil
1893
+ end
1894
+
1895
+ # Changes canvas size.
1896
+ #
1897
+ # @param width [Integer] new width
1898
+ # @param height [Integer] new height
1899
+ #
1900
+ # @return [nil] nil
1901
+ #
1902
+ def createCanvas (width, height)
1903
+ resizeCanvas__ :createCanvas, width, height, pixelDensity
1904
+ nil
1905
+ end
1906
+
1907
+ # Changes and returns canvas pixel density.
1908
+ #
1909
+ # @param density [Numeric] new pixel density
1910
+ #
1911
+ # @return [Numeric] current pixel density
1912
+ #
1913
+ def pixelDensity (density = nil)
1914
+ resizeCanvas__ :pixelDensity, width, height, density if density
1915
+ @painter__.pixel_density
1916
+ end
1917
+
1880
1918
  # @private
1881
- private def size__ (width, height)
1882
- raise 'size() must be called on startup or setup block' if @started__
1919
+ def resizeCanvas__ (name, width, height, pixelDensity)
1920
+ raise '#{name}() must be called on startup or setup block' if @started__
1883
1921
 
1884
1922
  @painter__.__send__ :end_paint
1885
- @window__.__send__ :reset_canvas, width, height
1923
+ @window__.__send__ :resize_canvas, width, height, pixelDensity
1924
+ updateCanvas__ @window__.canvas, @window__.canvas_painter
1886
1925
  @painter__.__send__ :begin_paint
1887
1926
 
1888
- @auto_resize__ = false
1927
+ @window__.auto_resize = false
1928
+ end
1929
+
1930
+ # Returns pixel density of display.
1931
+ #
1932
+ # @return [Numeric] pixel density
1933
+ #
1934
+ def displayDensity ()
1935
+ @window__.painter.pixel_density
1889
1936
  end
1890
1937
 
1891
1938
  def windowWidth ()
@@ -1912,20 +1959,12 @@ module RubySketch
1912
1959
  @window__.event.fps
1913
1960
  end
1914
1961
 
1915
- # Returns pixel density
1916
- #
1917
- # @return [Numeric] pixel density
1918
- #
1919
- def displayDensity ()
1920
- @painter__.pixel_density
1921
- end
1922
-
1923
1962
  # Returns mouse x position
1924
1963
  #
1925
1964
  # @return [Numeric] horizontal position of mouse
1926
1965
  #
1927
1966
  def mouseX ()
1928
- @mousePos__.x
1967
+ @mousePos__[0]
1929
1968
  end
1930
1969
 
1931
1970
  # Returns mouse y position
@@ -1933,7 +1972,7 @@ module RubySketch
1933
1972
  # @return [Numeric] vertical position of mouse
1934
1973
  #
1935
1974
  def mouseY ()
1936
- @mousePos__.y
1975
+ @mousePos__[1]
1937
1976
  end
1938
1977
 
1939
1978
  # Returns mouse x position in previous frame
@@ -1941,7 +1980,7 @@ module RubySketch
1941
1980
  # @return [Numeric] horizontal position of mouse
1942
1981
  #
1943
1982
  def pmouseX ()
1944
- @mousePrevPos__.x
1983
+ @mousePrevPos__[0]
1945
1984
  end
1946
1985
 
1947
1986
  # Returns mouse y position in previous frame
@@ -1949,7 +1988,7 @@ module RubySketch
1949
1988
  # @return [Numeric] vertical position of mouse
1950
1989
  #
1951
1990
  def pmouseY ()
1952
- @mousePrevPos__.y
1991
+ @mousePrevPos__[1]
1953
1992
  end
1954
1993
 
1955
1994
  # Returns array of touches
@@ -16,7 +16,8 @@ module RubySketch
16
16
  @auto_resize = true
17
17
  @error = nil
18
18
 
19
- reset_canvas 1, 1
19
+ painter.miter_limit = 10
20
+ resize_canvas 1, 1
20
21
 
21
22
  super *args, size: [width, height], &block
22
23
  end
@@ -43,7 +44,9 @@ module RubySketch
43
44
 
44
45
  def on_draw (e)
45
46
  draw_canvas {call_block @draw, e} if @draw
46
- e.painter.image @canvas
47
+
48
+ x, y, w, h = coord_converter
49
+ e.painter.image @canvas, x, y, @canvas.width * w, @canvas.height * h
47
50
  end
48
51
 
49
52
  def on_key (e)
@@ -64,27 +67,45 @@ module RubySketch
64
67
  end
65
68
 
66
69
  def on_resize (e)
67
- reset_canvas e.width, e.height if @auto_resize
70
+ resize_canvas e.width, e.height if @auto_resize
68
71
  draw_canvas {call_block @resize, e} if @resize
69
72
  end
70
73
 
74
+ def to_canvas_coord (x, y)
75
+ xx, yy, ww, hh = coord_converter
76
+ return (x - xx) / ww, (y - yy) / hh
77
+ end
78
+
71
79
  private
72
80
 
73
- def reset_canvas (width, height)
74
- return if width * height == 0
75
- return if width == @canvas&.width && height == @canvas&.height
81
+ def resize_canvas (width, height, pixel_density = nil)
82
+ return nil if width * height == 0
76
83
 
77
- old_canvas = @canvas
78
- old_painter = @canvas_painter
84
+ unless width == @canvas&.width &&
85
+ height == @canvas&.height &&
86
+ pixel_density == @canvas_painter&.pixel_density
79
87
 
80
- cs = old_canvas&.color_space || Rays::RGBA
81
- @canvas = Rays::Image.new width, height, cs, painter.pixel_density
82
- @canvas_painter = @canvas.painter
88
+ old_canvas = @canvas
89
+ old_painter = @canvas_painter
83
90
 
84
- if old_canvas
85
- @canvas_painter.paint {image old_canvas}
86
- copy_painter_attributes old_painter, @canvas_painter
91
+ cs = old_canvas&.color_space || Rays::RGBA
92
+ pd = pixel_density || painter.pixel_density
93
+ @canvas = Rays::Image.new width, height, cs, pd
94
+ @canvas_painter = @canvas.painter
95
+
96
+ if old_canvas
97
+ @canvas_painter.paint {image old_canvas}
98
+ copy_painter_attributes old_painter, @canvas_painter
99
+ end
100
+
101
+ resize_window width, height
87
102
  end
103
+
104
+ @canvas_painter
105
+ end
106
+
107
+ def resize_window (width, height)
108
+ size width, height
88
109
  end
89
110
 
90
111
  def copy_painter_attributes (from, to)
@@ -97,6 +118,21 @@ module RubySketch
97
118
  to.font = from.font
98
119
  end
99
120
 
121
+ def coord_converter ()
122
+ ww, wh = width.to_f, height.to_f
123
+ cw, ch = @canvas.width.to_f, @canvas.height.to_f
124
+ return [0, 0, 1, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
125
+
126
+ wratio, cratio = ww / wh, cw / ch
127
+ if wratio >= cratio
128
+ scaled_w = wh * cratio
129
+ return (ww - scaled_w) / 2, 0, scaled_w / cw, wh / ch
130
+ else
131
+ scaled_h = ww / cratio
132
+ return 0, (wh - scaled_h) / 2, ww / cw, scaled_h / ch
133
+ end
134
+ end
135
+
100
136
  def draw_canvas (&block)
101
137
  begin_draw
102
138
  block.call
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.10
4
+ version: 0.3.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-30 00:00:00.000000000 Z
11
+ date: 2020-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reflexion