rubysketch 0.3.10 → 0.3.15

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: 75d494d2c77928d0cac0bd8d6c29f28004098baed821870169a5829673eedb54
4
+ data.tar.gz: 29f153c1caa566e87efc6b3158ff8e17367ef57c78ed981a4e4f5a5a0a410ee1
5
5
  SHA512:
6
- metadata.gz: bbfb538eaf51488a522b98f41e6c193d40aabf960421118cfd63d76072bada647ba3dbb993e23bf52d025701b348c2736a893308a9dc220e4347e7caa91bafff
7
- data.tar.gz: 79871d6ed4f4c2fa0799ccfdeb62062a963c9dce05b7b17feafa5b23dba937d69cb3839efb986ed51f9f334c370accf82aebe0417305a9c186ea3f9b05a04415
6
+ metadata.gz: 7076a1988f49df4df59499eeeee4871eee1b1f228fb7f61be118bfd0f8cbe2fd7b77d5ffddb76a2972bb11931a8f07de91b413afedc54896870f41689d03743f
7
+ data.tar.gz: e26017b018da1cf2866ea84479c8b6fa3d246956bf4a2ffd307cf3c34e891fe5b7284017cb32dfed54d8313576742240ef6e9cb282b3f6a3f2876387ce64df76
@@ -1,6 +1,31 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.15] - 2020-12-12
5
+
6
+ - delete temporary directory on launch
7
+
8
+
9
+ ## [0.3.14] - 2020-12-12
10
+
11
+ - fix loadImage() fails when Encoding.default_internal is not nil
12
+
13
+
14
+ ## [0.3.13] - 2020-12-12
15
+
16
+ - size(), createCanvas(): default pixelDensity is same as current value
17
+
18
+
19
+ ## [0.3.12] - 2020-12-10
20
+
21
+ - size() and createCanvas() take 'pixelDensity' parameter and default value is 1
22
+
23
+
24
+ ## [0.3.11] - 2020-12-9
25
+
26
+ - add size(), createCanvas() and pixelDensity()
27
+
28
+
4
29
  ## [0.3.10] - 2020-12-1
5
30
 
6
31
  - 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.15
@@ -5,7 +5,7 @@
5
5
  require 'rubysketch-processing'
6
6
 
7
7
 
8
- icon = loadImage 'https://xord.org/rubysketch/rubysketch.png'
8
+ icon = loadImage 'https://xord.org/rubysketch/images/rubysketch128.png'
9
9
 
10
10
  draw do
11
11
  background 0, 10
@@ -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.
@@ -1761,15 +1764,16 @@ module RubySketch
1761
1764
  def initialize (window)
1762
1765
  @@context__ = self
1763
1766
 
1767
+ tmpdir__.tap {|dir| FileUtils.rm_r dir.to_s if dir.directory?}
1768
+
1764
1769
  @window__ = window
1765
- @image__ = @window__.canvas
1766
- setup__ @window__.canvas_painter.paint {background 0.8}
1770
+ init__ @window__.canvas, @window__.canvas_painter.paint {background 0.8}
1767
1771
 
1768
1772
  @loop__ = true
1769
1773
  @redraw__ = false
1770
1774
  @frameCount__ = 0
1771
1775
  @mousePos__ =
1772
- @mousePrevPos__ = Rays::Point.new 0
1776
+ @mousePrevPos__ = [0, 0]
1773
1777
  @mousePressed__ = false
1774
1778
  @touches__ = []
1775
1779
 
@@ -1777,8 +1781,7 @@ module RubySketch
1777
1781
  @window__.after_draw = proc {endDraw__}
1778
1782
 
1779
1783
  drawFrame = -> {
1780
- @image__ = @window__.canvas
1781
- @painter__ = @window__.canvas_painter
1784
+ updateCanvas__ @window__.canvas, @window__.canvas_painter
1782
1785
  begin
1783
1786
  push
1784
1787
  @drawBlock__.call if @drawBlock__
@@ -1797,9 +1800,11 @@ module RubySketch
1797
1800
  end
1798
1801
 
1799
1802
  updatePointerStates = -> event, pressed = nil {
1800
- @mousePos__ = event.pos
1803
+ @mousePos__ = @window__.to_canvas_coord event.pos.x, event.pos.y
1801
1804
  @mousePressed__ = pressed if pressed != nil
1802
- @touches__ = event.positions.map {|pos| Touch.new pos.x, pos.y}
1805
+ @touches__ = event.positions.map {|pos|
1806
+ Touch.new *@window__.to_canvas_coord(pos.x, pos.y)
1807
+ }
1803
1808
  }
1804
1809
 
1805
1810
  @window__.pointer_down = proc do |e|
@@ -1837,61 +1842,130 @@ module RubySketch
1837
1842
  nil
1838
1843
  end
1839
1844
 
1840
- def key (&block)
1845
+ # @private
1846
+ private def key__ (&block)
1841
1847
  @window__.key = block
1842
1848
  nil
1843
1849
  end
1844
1850
 
1851
+ # Define mousePressed block.
1852
+ #
1845
1853
  def mousePressed (&block)
1846
1854
  @mousePressedBlock__ = block if block
1847
1855
  @mousePressed__
1848
1856
  end
1849
1857
 
1858
+ # Define mouseReleased block.
1859
+ #
1850
1860
  def mouseReleased (&block)
1851
1861
  @mouseReleasedBlock__ = block if block
1852
1862
  nil
1853
1863
  end
1854
1864
 
1865
+ # Define mouseMoved block.
1866
+ #
1855
1867
  def mouseMoved (&block)
1856
1868
  @mouseMovedBlock__ = block if block
1857
1869
  nil
1858
1870
  end
1859
1871
 
1872
+ # Define mouseDragged block.
1873
+ #
1860
1874
  def mouseDragged (&block)
1861
1875
  @mouseDraggedBlock__ = block if block
1862
1876
  nil
1863
1877
  end
1864
1878
 
1879
+ # Define touchStarted block.
1880
+ #
1865
1881
  def touchStarted (&block)
1866
1882
  @touchStartedBlock__ = block if block
1867
1883
  nil
1868
1884
  end
1869
1885
 
1886
+ # Define touchEnded block.
1887
+ #
1870
1888
  def touchEnded (&block)
1871
1889
  @touchEndedBlock__ = block if block
1872
1890
  nil
1873
1891
  end
1874
1892
 
1893
+ # Define touchMoved block.
1894
+ #
1875
1895
  def touchMoved (&block)
1876
1896
  @touchMovedBlock__ = block if block
1877
1897
  nil
1878
1898
  end
1879
1899
 
1900
+ # Changes canvas size.
1901
+ #
1902
+ # @param width [Integer] new width
1903
+ # @param height [Integer] new height
1904
+ # @param pixelDensity [Numeric] new pixel density
1905
+ #
1906
+ # @return [nil] nil
1907
+ #
1908
+ def size (width, height, pixelDensity: self.pixelDensity)
1909
+ resizeCanvas__ :size, width, height, pixelDensity
1910
+ nil
1911
+ end
1912
+
1913
+ # Changes canvas size.
1914
+ #
1915
+ # @param width [Integer] new width
1916
+ # @param height [Integer] new height
1917
+ # @param pixelDensity [Numeric] new pixel density
1918
+ #
1919
+ # @return [nil] nil
1920
+ #
1921
+ def createCanvas (width, height, pixelDensity: self.pixelDensity)
1922
+ resizeCanvas__ :createCanvas, width, height, pixelDensity
1923
+ nil
1924
+ end
1925
+
1926
+ # Changes and returns canvas pixel density.
1927
+ #
1928
+ # @param density [Numeric] new pixel density
1929
+ #
1930
+ # @return [Numeric] current pixel density
1931
+ #
1932
+ def pixelDensity (density = nil)
1933
+ resizeCanvas__ :pixelDensity, width, height, density if density
1934
+ @painter__.pixel_density
1935
+ end
1936
+
1880
1937
  # @private
1881
- private def size__ (width, height)
1882
- raise 'size() must be called on startup or setup block' if @started__
1938
+ def resizeCanvas__ (name, width, height, pixelDensity)
1939
+ raise '#{name}() must be called on startup or setup block' if @started__
1883
1940
 
1884
1941
  @painter__.__send__ :end_paint
1885
- @window__.__send__ :reset_canvas, width, height
1942
+ @window__.__send__ :resize_canvas, width, height, pixelDensity
1943
+ updateCanvas__ @window__.canvas, @window__.canvas_painter
1886
1944
  @painter__.__send__ :begin_paint
1887
1945
 
1888
- @auto_resize__ = false
1946
+ @window__.auto_resize = false
1889
1947
  end
1890
1948
 
1949
+ # Returns pixel density of display.
1950
+ #
1951
+ # @return [Numeric] pixel density
1952
+ #
1953
+ def displayDensity ()
1954
+ @window__.painter.pixel_density
1955
+ end
1956
+
1957
+ # Returns window width.
1958
+ #
1959
+ # @return [Numeric] window width
1960
+ #
1891
1961
  def windowWidth ()
1892
1962
  @window__.width
1893
1963
  end
1894
1964
 
1965
+ # Returns window height.
1966
+ #
1967
+ # @return [Numeric] window height
1968
+ #
1895
1969
  def windowHeight ()
1896
1970
  @window__.height
1897
1971
  end
@@ -1912,20 +1986,12 @@ module RubySketch
1912
1986
  @window__.event.fps
1913
1987
  end
1914
1988
 
1915
- # Returns pixel density
1916
- #
1917
- # @return [Numeric] pixel density
1918
- #
1919
- def displayDensity ()
1920
- @painter__.pixel_density
1921
- end
1922
-
1923
1989
  # Returns mouse x position
1924
1990
  #
1925
1991
  # @return [Numeric] horizontal position of mouse
1926
1992
  #
1927
1993
  def mouseX ()
1928
- @mousePos__.x
1994
+ @mousePos__[0]
1929
1995
  end
1930
1996
 
1931
1997
  # Returns mouse y position
@@ -1933,7 +1999,7 @@ module RubySketch
1933
1999
  # @return [Numeric] vertical position of mouse
1934
2000
  #
1935
2001
  def mouseY ()
1936
- @mousePos__.y
2002
+ @mousePos__[1]
1937
2003
  end
1938
2004
 
1939
2005
  # Returns mouse x position in previous frame
@@ -1941,7 +2007,7 @@ module RubySketch
1941
2007
  # @return [Numeric] horizontal position of mouse
1942
2008
  #
1943
2009
  def pmouseX ()
1944
- @mousePrevPos__.x
2010
+ @mousePrevPos__[0]
1945
2011
  end
1946
2012
 
1947
2013
  # Returns mouse y position in previous frame
@@ -1949,7 +2015,7 @@ module RubySketch
1949
2015
  # @return [Numeric] vertical position of mouse
1950
2016
  #
1951
2017
  def pmouseY ()
1952
- @mousePrevPos__.y
2018
+ @mousePrevPos__[1]
1953
2019
  end
1954
2020
 
1955
2021
  # Returns array of touches
@@ -2389,14 +2455,16 @@ module RubySketch
2389
2455
  ext ||= File.extname uri
2390
2456
  raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
2391
2457
 
2392
- tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2458
+ tmpdir = tmpdir__
2393
2459
  path = tmpdir + Digest::SHA1.hexdigest(uri)
2394
2460
  path = path.sub_ext ext
2395
2461
 
2396
2462
  unless path.file?
2397
2463
  URI.open uri do |input|
2464
+ input.set_encoding nil# disable default_internal
2398
2465
  tmpdir.mkdir unless tmpdir.directory?
2399
2466
  path.open('w') do |output|
2467
+ output.set_encoding Encoding::ASCII_8BIT
2400
2468
  while buf = input.read(2 ** 16)
2401
2469
  output.write buf
2402
2470
  end
@@ -2406,6 +2474,11 @@ module RubySketch
2406
2474
  path.to_s
2407
2475
  end
2408
2476
 
2477
+ # @private
2478
+ private def tmpdir__ ()
2479
+ Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2480
+ end
2481
+
2409
2482
  end# Context
2410
2483
 
2411
2484
 
@@ -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,47 @@ 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
90
+ cs = old_canvas&.color_space || Rays::RGBA
91
+ pd = pixel_density ||
92
+ old_painter&.pixel_density ||
93
+ painter .pixel_density
83
94
 
84
- if old_canvas
85
- @canvas_painter.paint {image old_canvas}
86
- copy_painter_attributes old_painter, @canvas_painter
95
+ @canvas = Rays::Image.new width, height, cs, pd
96
+ @canvas_painter = @canvas.painter
97
+
98
+ if old_canvas
99
+ @canvas_painter.paint {image old_canvas}
100
+ copy_painter_attributes old_painter, @canvas_painter
101
+ end
102
+
103
+ resize_window width, height
87
104
  end
105
+
106
+ @canvas_painter
107
+ end
108
+
109
+ def resize_window (width, height)
110
+ size width, height
88
111
  end
89
112
 
90
113
  def copy_painter_attributes (from, to)
@@ -97,6 +120,21 @@ module RubySketch
97
120
  to.font = from.font
98
121
  end
99
122
 
123
+ def coord_converter ()
124
+ ww, wh = width.to_f, height.to_f
125
+ cw, ch = @canvas.width.to_f, @canvas.height.to_f
126
+ return [0, 0, 1, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
127
+
128
+ wratio, cratio = ww / wh, cw / ch
129
+ if wratio >= cratio
130
+ scaled_w = wh * cratio
131
+ return (ww - scaled_w) / 2, 0, scaled_w / cw, wh / ch
132
+ else
133
+ scaled_h = ww / cratio
134
+ return 0, (wh - scaled_h) / 2, ww / cw, scaled_h / ch
135
+ end
136
+ end
137
+
100
138
  def draw_canvas (&block)
101
139
  begin_draw
102
140
  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.15
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-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reflexion