rubysketch 0.3.11 → 0.3.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,81 +4,91 @@ module RubySketch
4
4
  class Window < Reflex::Window
5
5
 
6
6
  attr_accessor :setup, :update, :draw, :before_draw, :after_draw, :resize,
7
+ :key_down, :key_up,
7
8
  :pointer_down, :pointer_up, :pointer_move, :pointer_drag,
8
- :key, :motion
9
+ :motion
9
10
 
10
11
  attr_accessor :auto_resize
11
12
 
12
13
  attr_reader :canvas, :canvas_painter
13
14
 
14
- def initialize (width = 500, height = 500, *args, &block)
15
- @events = []
16
- @auto_resize = true
17
- @error = nil
15
+ def initialize(width = 500, height = 500, *args, **kwargs, &block)
16
+ RubySketch.instance_variable_set :@window, self
17
+
18
+ @canvas =
19
+ @canvas_painter = nil
20
+ @events = []
21
+ @auto_resize = true
22
+ @error = nil
18
23
 
19
24
  painter.miter_limit = 10
20
25
  resize_canvas 1, 1
21
26
 
22
- super *args, size: [width, height], &block
27
+ root.on(:update) {|e| on_canvas_update e}
28
+ root.on(:draw) {|e| on_canvas_draw e}
29
+ root.on(:pointer) {|e| on_canvas_pointer e}
30
+
31
+ super(*args, size: [width, height], **kwargs, &block)
23
32
  end
24
33
 
25
- def start (&block)
34
+ def start(&block)
26
35
  draw_canvas do
27
36
  block.call if block
28
37
  on_setup
29
38
  end
30
39
  end
31
40
 
32
- def event ()
41
+ def event()
33
42
  @events.last
34
43
  end
35
44
 
36
- def on_setup ()
45
+ def on_setup()
37
46
  call_block @setup, nil
38
47
  end
39
48
 
40
- def on_update (e)
41
- call_block @update, e
42
- redraw
49
+ def on_draw(e)
50
+ update_canvas_view
43
51
  end
44
52
 
45
- def on_draw (e)
46
- draw_canvas {call_block @draw, e} if @draw
47
-
48
- x, y, w, h = coord_converter
49
- e.painter.image @canvas, x, y, @canvas.width * w, @canvas.height * h
50
- end
51
-
52
- def on_key (e)
53
- draw_canvas {call_block @key, e} if @key
53
+ def on_resize(e)
54
+ resize_canvas e.width, e.height if @auto_resize
55
+ draw_canvas {call_block @resize, e} if @resize
54
56
  end
55
57
 
56
- def on_pointer (e)
58
+ def on_key(e)
57
59
  block = case e.type
58
- when :down then @pointer_down
59
- when :up then @pointer_up
60
- when :move then e.drag? ? @pointer_drag : @pointer_move
60
+ when :down then @key_down
61
+ when :up then @key_up
61
62
  end
62
63
  draw_canvas {call_block block, e} if block
63
64
  end
64
65
 
65
- def on_motion (e)
66
+ def on_motion(e)
66
67
  draw_canvas {call_block @motion, e} if @motion
67
68
  end
68
69
 
69
- def on_resize (e)
70
- resize_canvas e.width, e.height if @auto_resize
71
- draw_canvas {call_block @resize, e} if @resize
70
+ def on_canvas_update(e)
71
+ call_block @update, e
72
+ redraw
72
73
  end
73
74
 
74
- def to_canvas_coord (x, y)
75
- xx, yy, ww, hh = coord_converter
76
- return (x - xx) / ww, (y - yy) / hh
75
+ def on_canvas_draw(e)
76
+ draw_canvas {call_block @draw, e} if @draw
77
+ e.painter.image @canvas
78
+ end
79
+
80
+ def on_canvas_pointer(e)
81
+ block = case e.type
82
+ when :down then @pointer_down
83
+ when :up then @pointer_up
84
+ when :move then e.drag? ? @pointer_drag : @pointer_move
85
+ end
86
+ draw_canvas {call_block block, e} if block
77
87
  end
78
88
 
79
89
  private
80
90
 
81
- def resize_canvas (width, height, pixel_density = nil)
91
+ def resize_canvas(width, height, pixel_density = nil)
82
92
  return nil if width * height == 0
83
93
 
84
94
  unless width == @canvas&.width &&
@@ -87,9 +97,11 @@ module RubySketch
87
97
 
88
98
  old_canvas = @canvas
89
99
  old_painter = @canvas_painter
100
+ cs = old_canvas&.color_space || Rays::RGBA
101
+ pd = pixel_density ||
102
+ old_painter&.pixel_density ||
103
+ painter .pixel_density
90
104
 
91
- cs = old_canvas&.color_space || Rays::RGBA
92
- pd = pixel_density || painter.pixel_density
93
105
  @canvas = Rays::Image.new width, height, cs, pd
94
106
  @canvas_painter = @canvas.painter
95
107
 
@@ -104,11 +116,7 @@ module RubySketch
104
116
  @canvas_painter
105
117
  end
106
118
 
107
- def resize_window (width, height)
108
- size width, height
109
- end
110
-
111
- def copy_painter_attributes (from, to)
119
+ def copy_painter_attributes(from, to)
112
120
  to.fill = from.fill
113
121
  to.stroke = from.stroke
114
122
  to.stroke_width = from.stroke_width
@@ -118,39 +126,49 @@ module RubySketch
118
126
  to.font = from.font
119
127
  end
120
128
 
121
- def coord_converter ()
129
+ def resize_window(width, height)
130
+ size width, height
131
+ end
132
+
133
+ def update_canvas_view()
134
+ scrollx, scrolly, zoom = get_scroll_and_zoom
135
+ root.scroll_to scrollx, scrolly
136
+ root.zoom zoom
137
+ end
138
+
139
+ def get_scroll_and_zoom()
122
140
  ww, wh = width.to_f, height.to_f
123
141
  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
142
+ return [0, 0, 1] if ww == 0 || wh == 0 || cw == 0 || ch == 0
125
143
 
126
144
  wratio, cratio = ww / wh, cw / ch
127
145
  if wratio >= cratio
128
146
  scaled_w = wh * cratio
129
- return (ww - scaled_w) / 2, 0, scaled_w / cw, wh / ch
147
+ return (ww - scaled_w) / 2, 0, scaled_w / cw
130
148
  else
131
149
  scaled_h = ww / cratio
132
- return 0, (wh - scaled_h) / 2, ww / cw, scaled_h / ch
150
+ return 0, (wh - scaled_h) / 2, ww / cw
133
151
  end
134
152
  end
135
153
 
136
- def draw_canvas (&block)
154
+ def draw_canvas(&block)
137
155
  begin_draw
138
156
  block.call
139
157
  ensure
140
158
  end_draw
141
159
  end
142
160
 
143
- def begin_draw ()
161
+ def begin_draw()
144
162
  @canvas_painter.__send__ :begin_paint
145
163
  @before_draw&.call
146
164
  end
147
165
 
148
- def end_draw ()
166
+ def end_draw()
149
167
  @after_draw&.call
150
168
  @canvas_painter.__send__ :end_paint
151
169
  end
152
170
 
153
- def call_block (block, event, *args)
171
+ def call_block(block, event, *args)
154
172
  @events.push event
155
173
  block.call event, *args if block && !@error
156
174
  rescue Exception => e
data/test/helper.rb CHANGED
@@ -12,7 +12,7 @@ require 'rubysketch'
12
12
  include Xot::Test
13
13
 
14
14
 
15
- def assert_equal_vector (v1, v2, delta = 0.000001)
15
+ def assert_equal_vector(v1, v2, delta = 0.000001)
16
16
  assert_in_delta v1.x, v2.x, delta
17
17
  assert_in_delta v1.y, v2.y, delta
18
18
  assert_in_delta v1.z, v2.z, delta
@@ -6,15 +6,11 @@ require_relative '../helper'
6
6
 
7
7
  class TestProcessingUtility < Test::Unit::TestCase
8
8
 
9
- class Context
10
- include RubySketch::Processing::Context
9
+ def context()
10
+ RubySketch::Processing::Context.new RubySketch::Window.new
11
11
  end
12
12
 
13
- def context
14
- Context.new
15
- end
16
-
17
- def test_random ()
13
+ def test_random()
18
14
  c = context
19
15
 
20
16
  assert_equal Float, c.random(1).class
@@ -6,23 +6,20 @@ require_relative '../helper'
6
6
 
7
7
  class TestProcessingVector < Test::Unit::TestCase
8
8
 
9
- P = RubySketch::Processing
10
-
11
- V = P::Vector
12
-
13
- M = Math
14
-
9
+ P = RubySketch::Processing
10
+ V = P::Vector
11
+ M = Math
15
12
  PI = M::PI
16
13
 
17
- def vec (*args)
18
- V.new *args
14
+ def vec(*args, **kwargs)
15
+ V.new(*args, **kwargs)
19
16
  end
20
17
 
21
- def point (*args)
22
- Rays::Point.new *args
18
+ def point(*args, **kwargs)
19
+ Rays::Point.new(*args, **kwargs)
23
20
  end
24
21
 
25
- def test_initialize ()
22
+ def test_initialize()
26
23
  assert_equal_vector vec(0, 0, 0), vec()
27
24
  assert_equal_vector vec(1, 0, 0), vec(1)
28
25
  assert_equal_vector vec(1, 2, 0), vec(1, 2)
@@ -37,7 +34,7 @@ class TestProcessingVector < Test::Unit::TestCase
37
34
  assert_equal_vector vec(1, 2, 3), vec(point 1, 2, 3)
38
35
  end
39
36
 
40
- def test_set ()
37
+ def test_set()
41
38
  v0 = vec 9, 9, 9
42
39
 
43
40
  v = v0.dup; v.set; assert_equal_vector vec(0, 0, 0), v
@@ -54,7 +51,7 @@ class TestProcessingVector < Test::Unit::TestCase
54
51
  v = v0.dup; v.set point(1, 2, 3); assert_equal_vector vec(1, 2, 3), v
55
52
  end
56
53
 
57
- def test_dup ()
54
+ def test_dup()
58
55
  v1 = vec 1, 2, 3
59
56
  assert_equal_vector vec(1, 2, 3), v1
60
57
 
@@ -67,7 +64,7 @@ class TestProcessingVector < Test::Unit::TestCase
67
64
  assert_equal_vector vec(1, 2, 3), v2
68
65
  end
69
66
 
70
- def test_copy ()
67
+ def test_copy()
71
68
  v1 = vec 1, 2, 3
72
69
  assert_equal_vector vec(1, 2, 3), v1
73
70
 
@@ -80,7 +77,7 @@ class TestProcessingVector < Test::Unit::TestCase
80
77
  assert_equal_vector vec(1, 2, 3), v2
81
78
  end
82
79
 
83
- def test_xyz ()
80
+ def test_xyz()
84
81
  v = vec 1, 2, 3
85
82
  assert_equal_vector vec(1, 2, 3), v
86
83
  assert_equal [1, 2, 3], [v.x, v.y, v.z]
@@ -95,11 +92,11 @@ class TestProcessingVector < Test::Unit::TestCase
95
92
  assert_equal_vector vec(7, 8, 9), v
96
93
  end
97
94
 
98
- def test_array ()
95
+ def test_array()
99
96
  assert_equal [1, 2, 3], vec(1, 2, 3).array
100
97
  end
101
98
 
102
- def test_add ()
99
+ def test_add()
103
100
  v = vec 1, 2, 3
104
101
  v.add 4, 5, 6
105
102
  assert_equal_vector vec(5, 7, 9), v
@@ -118,7 +115,7 @@ class TestProcessingVector < Test::Unit::TestCase
118
115
  assert_equal_vector vec(5, 7, 9), vec(1, 2, 3).add(point(4, 5, 6))
119
116
  end
120
117
 
121
- def test_sub ()
118
+ def test_sub()
122
119
  v = vec 9, 8, 7
123
120
  v.sub 1, 2, 3
124
121
  assert_equal_vector vec(8, 6, 4), v
@@ -137,19 +134,19 @@ class TestProcessingVector < Test::Unit::TestCase
137
134
  assert_equal_vector vec(8, 6, 4), vec(9, 8, 7).sub(point(1, 2, 3))
138
135
  end
139
136
 
140
- def test_mult ()
137
+ def test_mult()
141
138
  v = vec 1, 2, 3
142
139
  v.mult 2
143
140
  assert_equal_vector vec(2, 4, 6), v
144
141
  end
145
142
 
146
- def test_div ()
143
+ def test_div()
147
144
  v = vec 2, 4, 6
148
145
  v.div 2
149
146
  assert_equal_vector vec(1, 2, 3), v
150
147
  end
151
148
 
152
- def test_op_add ()
149
+ def test_op_add()
153
150
  v1 = vec 1, 2, 3
154
151
  v2 = vec 4, 5, 6
155
152
  assert_equal_vector vec(5, 7, 9), v1 + v2
@@ -167,7 +164,7 @@ class TestProcessingVector < Test::Unit::TestCase
167
164
  assert_equal_vector vec(5, 7, 9), vec(1, 2, 3) + point(4, 5, 6)
168
165
  end
169
166
 
170
- def test_op_sub ()
167
+ def test_op_sub()
171
168
  v1 = vec 9, 8, 7
172
169
  v2 = vec 1, 2, 3
173
170
  assert_equal_vector vec(8, 6, 4), v1 - v2
@@ -185,19 +182,19 @@ class TestProcessingVector < Test::Unit::TestCase
185
182
  assert_equal_vector vec(8, 6, 4), vec(9, 8, 7) - point(1, 2, 3)
186
183
  end
187
184
 
188
- def test_op_mult ()
185
+ def test_op_mult()
189
186
  v = vec 1, 2, 3
190
187
  assert_equal_vector vec(2, 4, 6), v * 2
191
188
  assert_equal_vector vec(1, 2, 3), v
192
189
  end
193
190
 
194
- def test_op_div ()
191
+ def test_op_div()
195
192
  v = vec 2, 4, 6
196
193
  assert_equal_vector vec(1, 2, 3), v / 2
197
194
  assert_equal_vector vec(2, 4, 6), v
198
195
  end
199
196
 
200
- def test_fun_add ()
197
+ def test_fun_add()
201
198
  v1 = vec 1, 2, 3
202
199
  v2 = vec 4, 5, 6
203
200
  result = vec
@@ -207,7 +204,7 @@ class TestProcessingVector < Test::Unit::TestCase
207
204
  assert_equal_vector vec(5, 7, 9), result
208
205
  end
209
206
 
210
- def test_fun_sub ()
207
+ def test_fun_sub()
211
208
  v1 = vec 9, 8, 7
212
209
  v2 = vec 1, 2, 3
213
210
  result = vec
@@ -217,7 +214,7 @@ class TestProcessingVector < Test::Unit::TestCase
217
214
  assert_equal_vector vec(8, 6, 4), result
218
215
  end
219
216
 
220
- def test_fun_mult ()
217
+ def test_fun_mult()
221
218
  v1 = vec 1, 2, 3
222
219
  result = vec
223
220
  assert_equal_vector vec(2, 4, 6), V.mult(v1, 2, result)
@@ -225,7 +222,7 @@ class TestProcessingVector < Test::Unit::TestCase
225
222
  assert_equal_vector vec(2, 4, 6), result
226
223
  end
227
224
 
228
- def test_fun_div ()
225
+ def test_fun_div()
229
226
  v1 = vec 2, 4, 6
230
227
  result = vec
231
228
  assert_equal_vector vec(1, 2, 3), V.div(v1, 2, result)
@@ -233,17 +230,17 @@ class TestProcessingVector < Test::Unit::TestCase
233
230
  assert_equal_vector vec(1, 2, 3), result
234
231
  end
235
232
 
236
- def test_mag ()
233
+ def test_mag()
237
234
  assert_in_delta M.sqrt(5), vec(1, 2) .mag, 0.000001
238
235
  assert_in_delta M.sqrt(14), vec(1, 2, 3).mag, 0.000001
239
236
  end
240
237
 
241
- def test_magSq ()
238
+ def test_magSq()
242
239
  assert_equal 5, vec(1, 2) .magSq
243
240
  assert_equal 14, vec(1, 2, 3).magSq
244
241
  end
245
242
 
246
- def test_setMag ()
243
+ def test_setMag()
247
244
  v = vec 3, 4, 0
248
245
  assert_equal_vector vec(6, 8, 0), v.setMag(10)
249
246
  assert_equal_vector vec(6, 8, 0), v
@@ -255,7 +252,7 @@ class TestProcessingVector < Test::Unit::TestCase
255
252
  assert_equal_vector vec(6, 8, 0), result
256
253
  end
257
254
 
258
- def test_normalize ()
255
+ def test_normalize()
259
256
  v = vec 1, 2, 3
260
257
  normal = v / v.mag
261
258
  assert_equal_vector normal, v.normalize
@@ -268,7 +265,7 @@ class TestProcessingVector < Test::Unit::TestCase
268
265
  assert_equal_vector normal, result
269
266
  end
270
267
 
271
- def test_limit ()
268
+ def test_limit()
272
269
  v = vec 1, 2, 3
273
270
  assert_in_delta 1, v.limit(1).mag, 0.000001
274
271
  assert_in_delta 1, v .mag, 0.000001
@@ -279,7 +276,7 @@ class TestProcessingVector < Test::Unit::TestCase
279
276
  assert_in_delta vec(1, 2, 3).mag, vec(1, 2, 3).limit(4).mag, 0.000001
280
277
  end
281
278
 
282
- def test_dist ()
279
+ def test_dist()
283
280
  v1 = vec 1, 2, 3
284
281
  v2 = vec 4, 5, 6
285
282
 
@@ -292,7 +289,7 @@ class TestProcessingVector < Test::Unit::TestCase
292
289
  assert_equal_vector vec(4, 5, 6), v2
293
290
  end
294
291
 
295
- def test_dot ()
292
+ def test_dot()
296
293
  v1 = vec 1, 2, 3
297
294
  v2 = vec 4, 5, 6
298
295
 
@@ -308,7 +305,7 @@ class TestProcessingVector < Test::Unit::TestCase
308
305
  assert_equal_vector vec(4, 5, 6), v2
309
306
  end
310
307
 
311
- def test_cross ()
308
+ def test_cross()
312
309
  v1 = vec 1, 0, 0
313
310
  v2 = vec 0, 1, 0
314
311
 
@@ -328,9 +325,9 @@ class TestProcessingVector < Test::Unit::TestCase
328
325
  assert_equal_vector vec(0, 0, 1), result
329
326
  end
330
327
 
331
- def test_rotate ()
328
+ def test_rotate()
332
329
  angle = PI * 2 * 0.1
333
- context = Object.new.tap {|o| def o.toAngle__ (a); a * 2 * P::RAD2DEG__; end}
330
+ context = Object.new.tap {|o| def o.toAngle__(a); a * 2 * P::RAD2DEG__; end}
334
331
 
335
332
  v = vec 1, 0, 0
336
333
  assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), v.rotate(angle)
@@ -340,7 +337,7 @@ class TestProcessingVector < Test::Unit::TestCase
340
337
  assert_equal_vector vec(M.cos(angle * 2), M.sin(angle * 2), 0), v.rotate(angle)
341
338
  end
342
339
 
343
- def test_fromAngle ()
340
+ def test_fromAngle()
344
341
  angle = PI * 2 * 0.1
345
342
  result = vec
346
343
  assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), V.fromAngle(angle)
@@ -348,25 +345,25 @@ class TestProcessingVector < Test::Unit::TestCase
348
345
  assert_equal_vector vec(M.cos(angle), M.sin(angle), 0), result
349
346
  end
350
347
 
351
- def test_heading ()
348
+ def test_heading()
352
349
  angle = PI * 1 * 0.1
353
- assert_in_delta angle, V.fromAngle( angle).heading, 0.000001
354
- assert_in_delta -angle, V.fromAngle(-angle).heading, 0.000001
350
+ assert_in_delta( angle, V.fromAngle( angle).heading, 0.000001)
351
+ assert_in_delta(-angle, V.fromAngle(-angle).heading, 0.000001)
355
352
  end
356
353
 
357
- def test_angleBetween ()
354
+ def test_angleBetween()
358
355
  v1 = V.fromAngle PI * 0.25
359
356
  v2 = V.fromAngle PI * 0.75
360
357
  assert_in_delta PI / 2, V.angleBetween(v1, v2), 0.000001
361
358
  end
362
359
 
363
- def test_lerp ()
360
+ def test_lerp()
364
361
  assert_equal_vector vec(0.5, 0.5, 0.5), vec(0, 0, 0).lerp(vec(1, 1, 1), 0.5)
365
362
  assert_equal_vector vec(0.5, 0.5, 0.5), vec(0, 0, 0).lerp( 1, 1, 1, 0.5)
366
363
  assert_equal_vector vec(0.5, 0.5, 0.5), V.lerp(vec(0, 0, 0), vec(1, 1, 1), 0.5)
367
364
  end
368
365
 
369
- def test_random2D ()
366
+ def test_random2D()
370
367
  v1 = V.random2D
371
368
  v2 = V.random2D
372
369
  assert v1.x != 0
@@ -380,7 +377,7 @@ class TestProcessingVector < Test::Unit::TestCase
380
377
  assert_in_delta 1, v2.mag, 0.000001
381
378
  end
382
379
 
383
- def test_random3D ()
380
+ def test_random3D()
384
381
  v1 = V.random3D
385
382
  v2 = V.random3D
386
383
  assert v1.x != 0