rubysketch 0.3.21 → 0.3.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,32 +21,42 @@ module RubySketch
21
21
  end
22
22
  end
23
23
 
24
- attr_accessor :setup, :update, :draw, :before_draw, :after_draw, :resize,
24
+ attr_accessor :setup, :update, :draw, :resize,
25
25
  :key_down, :key_up,
26
26
  :pointer_down, :pointer_up, :pointer_move, :pointer_drag,
27
- :motion
27
+ :motion,
28
+ :before_draw, :after_draw, :update_canvas
28
29
 
29
30
  attr_accessor :auto_resize
30
31
 
31
- attr_reader :canvas_image, :canvas_painter
32
-
33
32
  def initialize(width = 500, height = 500, *args, **kwargs, &block)
34
33
  RubySketch.instance_variable_set :@window, self
35
34
 
36
- @canvas_image =
37
- @canvas_painter = nil
38
- @events = []
39
- @auto_resize = true
40
- @error = nil
41
-
42
- painter.miter_limit = 10
43
- resize_canvas 1, 1
44
-
35
+ @events = []
36
+ @error = nil
37
+ @auto_resize = true
38
+ @canvas = Canvas.new self
45
39
  @canvas_view = add CanvasView.new name: :canvas
46
40
 
47
41
  super(*args, size: [width, height], **kwargs, &block)
48
42
  end
49
43
 
44
+ def canvas_image()
45
+ @canvas.image
46
+ end
47
+
48
+ def canvas_painter()
49
+ @canvas.painter
50
+ end
51
+
52
+ def window_painter()
53
+ self.painter
54
+ end
55
+
56
+ def event()
57
+ @events.last
58
+ end
59
+
50
60
  def start(&block)
51
61
  draw_canvas do
52
62
  block.call if block
@@ -54,8 +64,11 @@ module RubySketch
54
64
  end
55
65
  end
56
66
 
57
- def event()
58
- @events.last
67
+ def resize_canvas(width, height, pixel_density = nil)
68
+ if @canvas.resize width, height, pixel_density
69
+ @update_canvas.call canvas_image, canvas_painter if @update_canvas
70
+ size width, height
71
+ end
59
72
  end
60
73
 
61
74
  def on_setup()
@@ -67,6 +80,9 @@ module RubySketch
67
80
  end
68
81
 
69
82
  def on_draw(e)
83
+ window_painter.pixel_density.tap do |pd|
84
+ resize_canvas width, height, pd if pd != canvas_painter.pixel_density
85
+ end
70
86
  update_canvas_view
71
87
  end
72
88
 
@@ -89,7 +105,7 @@ module RubySketch
89
105
 
90
106
  def on_canvas_draw(e)
91
107
  draw_canvas {call_block @draw, e} if @draw
92
- e.painter.image @canvas_image
108
+ draw_screen e.painter
93
109
  end
94
110
 
95
111
  def on_canvas_pointer(e)
@@ -102,55 +118,12 @@ module RubySketch
102
118
  end
103
119
 
104
120
  def on_canvas_resize(e)
105
- resize_canvas e.width, e.height if @auto_resize
121
+ resize_canvas e.width, e.height, window_painter.pixel_density if @auto_resize
106
122
  draw_canvas {call_block @resize, e} if @resize
107
123
  end
108
124
 
109
125
  private
110
126
 
111
- def resize_canvas(width, height, pixel_density = nil)
112
- return nil if width * height == 0
113
-
114
- unless width == @canvas_image&.width &&
115
- height == @canvas_image&.height &&
116
- pixel_density == @canvas_painter&.pixel_density
117
-
118
- old_image = @canvas_image
119
- old_painter = @canvas_painter
120
- cs = old_image&.color_space || Rays::RGBA
121
- pd = pixel_density ||
122
- old_painter&.pixel_density ||
123
- painter .pixel_density
124
-
125
- @canvas_image = Rays::Image.new width, height, cs, pd
126
- @canvas_painter = @canvas_image.painter
127
-
128
- if old_image
129
- @canvas_painter.paint {image old_image}
130
- copy_painter_attributes old_painter, @canvas_painter
131
- end
132
-
133
- resize_window width, height
134
- GC.start
135
- end
136
-
137
- @canvas_painter
138
- end
139
-
140
- def copy_painter_attributes(from, to)
141
- to.fill = from.fill
142
- to.stroke = from.stroke
143
- to.stroke_width = from.stroke_width
144
- to.stroke_cap = from.stroke_cap
145
- to.stroke_join = from.stroke_join
146
- to.miter_limit = from.miter_limit
147
- to.font = from.font
148
- end
149
-
150
- def resize_window(width, height)
151
- size width, height
152
- end
153
-
154
127
  def update_canvas_view()
155
128
  scrollx, scrolly, zoom = get_scroll_and_zoom
156
129
  @canvas_view.scroll_to scrollx, scrolly
@@ -158,8 +131,8 @@ module RubySketch
158
131
  end
159
132
 
160
133
  def get_scroll_and_zoom()
161
- ww, wh = width.to_f, height.to_f
162
- cw, ch = @canvas_image.width.to_f, @canvas_image.height.to_f
134
+ ww, wh = width.to_f, height.to_f
135
+ cw, ch = canvas_image.width.to_f, canvas_image.height.to_f
163
136
  return [0, 0, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
164
137
 
165
138
  wratio, cratio = ww / wh, cw / ch
@@ -180,13 +153,17 @@ module RubySketch
180
153
  end
181
154
 
182
155
  def begin_draw()
183
- @canvas_painter.__send__ :begin_paint
156
+ canvas_painter.__send__ :begin_paint
184
157
  @before_draw&.call
185
158
  end
186
159
 
187
160
  def end_draw()
188
161
  @after_draw&.call
189
- @canvas_painter.__send__ :end_paint
162
+ canvas_painter.__send__ :end_paint
163
+ end
164
+
165
+ def draw_screen(painter)
166
+ window_painter.image canvas_image
190
167
  end
191
168
 
192
169
  def call_block(block, event, *args)
@@ -202,4 +179,54 @@ module RubySketch
202
179
  end# Window
203
180
 
204
181
 
182
+ class Window::Canvas
183
+
184
+ attr_reader :image, :painter
185
+
186
+ def initialize(window)
187
+ @image = nil
188
+ @painter = window.painter
189
+
190
+ resize 1, 1
191
+ painter.miter_limit = 10
192
+ end
193
+
194
+ def resize(width, height, pixel_density = nil)
195
+ return false if width * height == 0
196
+
197
+ return false if
198
+ width == @image&.width &&
199
+ height == @image&.height &&
200
+ pixel_density == @painter.pixel_density
201
+
202
+ old_image = @image
203
+ old_painter = @painter
204
+ cs = old_image&.color_space || Rays::RGBA
205
+ pd = pixel_density || old_painter.pixel_density
206
+
207
+ @image = Rays::Image.new width, height, cs, pd
208
+ @painter = @image.painter
209
+
210
+ @painter.paint {image old_image} if old_image
211
+ copy_painter old_painter, @painter
212
+
213
+ GC.start
214
+ return true
215
+ end
216
+
217
+ private
218
+
219
+ def copy_painter(from, to)
220
+ to.fill = from.fill
221
+ to.stroke = from.stroke
222
+ to.stroke_width = from.stroke_width
223
+ to.stroke_cap = from.stroke_cap
224
+ to.stroke_join = from.stroke_join
225
+ to.miter_limit = from.miter_limit
226
+ to.font = from.font
227
+ end
228
+
229
+ end# Window::Canvas
230
+
231
+
205
232
  end# RubySketch
@@ -6,6 +6,7 @@ def (RubySketch::GLSL).run(
6
6
  title: 'RubySketch',
7
7
  width: 500, height: 500)
8
8
  w = RubySketch::Window.new(title: title, w: width, h: height) {start}
9
- RubySketch::GLSL::Context.new w, shader_source
9
+ c = RubySketch::GLSL::Context.new w
10
+ c.run shader_source
10
11
  at_exit {RubySketch::App.new {w.show}.start unless $!}
11
12
  end
data/rubysketch.gemspec CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.platform = Gem::Platform::RUBY
29
29
  s.required_ruby_version = '>= 2.6.0'
30
30
 
31
- s.add_runtime_dependency 'reflexion', '~> 0.1.28'
31
+ s.add_runtime_dependency 'reflexion', '~> 0.1.29'
32
32
 
33
33
  s.add_development_dependency 'yard'
34
34
 
@@ -0,0 +1,11 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require_relative '../helper'
5
+
6
+
7
+ def graphics(width = 10, height = 10, &block)
8
+ RubySketch::Processing::Graphics.new(width, height).tap do |g|
9
+ g.beginDraw {block.call g, g.getInternal__} if block
10
+ end
11
+ end
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require_relative 'helper'
5
+
6
+
7
+ class TestProcessingGraphics < Test::Unit::TestCase
8
+
9
+ P = RubySketch::Processing
10
+
11
+ def graphics(w = 10, h = 10)
12
+ P::Graphics.new w, h
13
+ end
14
+
15
+ def test_beginDraw()
16
+ g = graphics
17
+ g.beginDraw
18
+ assert_raise {g.beginDraw}
19
+ end
20
+
21
+ end# TestProcessingGraphics
@@ -0,0 +1,47 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require_relative 'helper'
5
+
6
+
7
+ class TestProcessingShader < Test::Unit::TestCase
8
+
9
+ def color(*args)
10
+ Rays::Color.new(*args)
11
+ end
12
+
13
+ def shader(vs = vshader, fs = fshader)
14
+ RubySketch::Processing::Shader.new vs, fs
15
+ end
16
+
17
+ def vshader()
18
+ "void main() {gl_Position = vec4(0.0, 0.0, 0.0, 1.0);}"
19
+ end
20
+
21
+ def fshader()
22
+ "void main() {gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);}"
23
+ end
24
+
25
+ def test_initialize()
26
+ assert shader vshader, fshader
27
+ assert shader nil, fshader
28
+
29
+ assert_raise(ArgumentError) {shader "", fshader}
30
+ assert_raise(ArgumentError) {shader vshader, ""}
31
+ assert_raise(ArgumentError) {shader vshader, nil}
32
+ end
33
+
34
+ def test_uniform()
35
+ sh = shader(nil, <<~END).tap {|s| s.set :val, 1.0}
36
+ uniform float val;
37
+ void main() {gl_FragColor = vec4(val, 0.0, 0.0, 1.0);}
38
+ END
39
+
40
+ graphics do |g, image|
41
+ g.shader sh
42
+ g.rect 0, 0, 10, 10
43
+ assert_equal color(1, 0, 0, 1), image[0, 0]
44
+ end
45
+ end
46
+
47
+ end# TestProcessingShader
@@ -1,34 +1,41 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
 
4
- require_relative '../helper'
4
+ require_relative 'helper'
5
5
 
6
6
 
7
7
  class TestProcessingUtility < Test::Unit::TestCase
8
8
 
9
- def context()
10
- RubySketch::Processing::Context.new RubySketch::Window.new
11
- end
9
+ P = RubySketch::Processing
12
10
 
13
- def test_random()
14
- c = context
11
+ include P::GraphicsContext
15
12
 
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
13
+ def test_random()
14
+ assert_equal Float, random(1).class
15
+ assert_equal Float, random(1.0).class
16
+ assert_equal Symbol, random((:a..:z).to_a).class
19
17
 
20
- assert_not_equal c.random, c.random
18
+ assert_not_equal random, random
21
19
 
22
20
  10000.times do
23
- n = c.random
21
+ n = random
24
22
  assert 0 <= n && n < 1
25
23
 
26
- n = c.random 1
24
+ n = random 1
27
25
  assert 0 <= n && n < 1
28
26
 
29
- n = c.random 1, 2
27
+ n = random 1, 2
30
28
  assert 1.0 <= n && n < 2.0
31
29
  end
32
30
  end
33
31
 
32
+ def test_createVector()
33
+ assert_equal P::Vector, createVector(1, 2).class
34
+ end
35
+
36
+ def test_createShader()
37
+ fs = "void main() {gl_FragColor = vec4(1.0);}"
38
+ assert_equal P::Shader, createShader(nil, fs).class
39
+ end
40
+
34
41
  end# TestProcessingUtility
@@ -1,7 +1,7 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
 
4
- require_relative '../helper'
4
+ require_relative 'helper'
5
5
 
6
6
 
7
7
  class TestProcessingVector < Test::Unit::TestCase
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.21
4
+ version: 0.3.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-09-04 00:00:00.000000000 Z
11
+ date: 2022-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reflexion
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.28
19
+ version: 0.1.29
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.28
26
+ version: 0.1.29
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: yard
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -75,6 +75,9 @@ files:
75
75
  - src/RubySketch.h
76
76
  - src/RubySketch.mm
77
77
  - test/helper.rb
78
+ - test/processing/helper.rb
79
+ - test/processing/test_graphics.rb
80
+ - test/processing/test_shader.rb
78
81
  - test/processing/test_utility.rb
79
82
  - test/processing/test_vector.rb
80
83
  homepage: https://github.com/xord/rubysketch
@@ -101,5 +104,8 @@ specification_version: 4
101
104
  summary: Processing like Creative Coding Framework.
102
105
  test_files:
103
106
  - test/helper.rb
107
+ - test/processing/helper.rb
108
+ - test/processing/test_graphics.rb
109
+ - test/processing/test_shader.rb
104
110
  - test/processing/test_utility.rb
105
111
  - test/processing/test_vector.rb