rubysketch 0.3.11 → 0.3.16

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: f9d6862ce564503738c10d66ea1b76000d57ffa7100a0e47bf0b96c8083e02dd
4
- data.tar.gz: 44825a802755cd03c1efa129ec0a0349872f330795433839f2232ef2b9b83a24
3
+ metadata.gz: e7c911427a4145fa18e7395c65f49ed58077c6d294f93ee9d24a840f621bcfeb
4
+ data.tar.gz: e5ecc52cb87042b8e59fd64f48c358f19638a3a4f35a696072b3f49d95998eba
5
5
  SHA512:
6
- metadata.gz: ed6efaf98ea1b9e816d337ebf82d57516f3f2fb30877d693f2a18c42d24d85eaf981fa804c1f1761ec0bd86ec73e8a5f88fcb50103c5bd5979e46bf58a2c0162
7
- data.tar.gz: 5d8d25591fdbc056dc0f793d5a8c93cae50547731e10128c6ec27e9432491f238d5e547f9cc74701beb85903a39114da045cd320cf1903f235afa7975bef42ca
6
+ metadata.gz: ce775dec5c87470a3c3aa88e37fae94ecfaab96616c381bf71cbab5c17325ec0b93ab6640be326185de6f4b5e44b6aec9438418f1e57a00a9955ac5e148d20e1
7
+ data.tar.gz: 433efaf677bb1538096b1453591e5d28608fc20793bbcdd9e648f54e286eff7d63923496375520e5c560904883eb9385ac2837f67e4aabe7988f7d1d0fa4f23a
data/ChangeLog.md CHANGED
@@ -1,6 +1,32 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.16] - 2021-2-14
5
+
6
+ - add key, keyCode and keyPressed system values
7
+ - add keyPressed(), keyReleased() and keyTyped() functions
8
+
9
+
10
+ ## [0.3.15] - 2020-12-12
11
+
12
+ - delete temporary directory on launch
13
+
14
+
15
+ ## [0.3.14] - 2020-12-12
16
+
17
+ - fix loadImage() fails when Encoding.default_internal is not nil
18
+
19
+
20
+ ## [0.3.13] - 2020-12-12
21
+
22
+ - size(), createCanvas(): default pixelDensity is same as current value
23
+
24
+
25
+ ## [0.3.12] - 2020-12-10
26
+
27
+ - size() and createCanvas() take 'pixelDensity' parameter and default value is 1
28
+
29
+
4
30
  ## [0.3.11] - 2020-12-9
5
31
 
6
32
  - add size(), createCanvas() and pixelDensity()
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.11
1
+ 0.3.16
data/examples/image.rb CHANGED
@@ -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
@@ -18,6 +18,6 @@ begin
18
18
  window.__send__ :begin_draw
19
19
  at_exit do
20
20
  window.__send__ :end_draw
21
- Reflex.start {window.show} unless $!
21
+ RubySketch::App.new {window.show}.start unless $!
22
22
  end
23
23
  end
data/lib/rubysketch.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'set'
1
2
  require 'digest/sha1'
2
3
  require 'pathname'
3
4
  require 'tmpdir'
@@ -5,6 +6,7 @@ require 'open-uri'
5
6
 
6
7
  require 'reflex'
7
8
  require 'rubysketch/module'
9
+ require 'rubysketch/app'
8
10
  require 'rubysketch/window'
9
11
  require 'rubysketch/processing'
10
12
  require 'rubysketch/glsl'
@@ -0,0 +1,13 @@
1
+ module RubySketch
2
+
3
+
4
+ class App < Reflex::Application
5
+
6
+ def on_motion(e)
7
+ RubySketch.instance_variable_get(:@window)&.on_motion e
8
+ end
9
+
10
+ end# App
11
+
12
+
13
+ end# RubySketch
@@ -4,12 +4,12 @@ module RubySketch
4
4
  # @private
5
5
  class GLSL
6
6
 
7
- def initialize (glsl)
7
+ def initialize(glsl)
8
8
  @shader = Reflex::Shader.new glsl
9
9
  end
10
10
 
11
11
  # @private
12
- def on_start__ (window)
12
+ def on_start__(window)
13
13
  start = Time.now.to_f
14
14
 
15
15
  window.draw = proc do |e, painter|
@@ -5,15 +5,15 @@ module RubySketch
5
5
 
6
6
  module_function
7
7
 
8
- def name ()
8
+ def name()
9
9
  super.split('::')[-2]
10
10
  end
11
11
 
12
- def version ()
12
+ def version()
13
13
  open(root_dir 'VERSION') {|f| f.readline.chomp}
14
14
  end
15
15
 
16
- def root_dir (path = '')
16
+ def root_dir(path = '')
17
17
  File.expand_path "../../#{path}", __dir__
18
18
  end
19
19
 
@@ -34,7 +34,7 @@ module RubySketch
34
34
  # @param v [Vector] vector object to copy
35
35
  # @param a [Array] array like [x, y, z]
36
36
  #
37
- def initialize (x = 0, y = 0, z = 0, context: nil)
37
+ def initialize(x = 0, y = 0, z = 0, context: nil)
38
38
  @point = case x
39
39
  when Rays::Point then x.dup
40
40
  when Vector then x.getInternal__.dup
@@ -46,7 +46,7 @@ module RubySketch
46
46
 
47
47
  # Initializer for dup or clone
48
48
  #
49
- def initialize_copy (o)
49
+ def initialize_copy(o)
50
50
  @point = o.getInternal__.dup
51
51
  end
52
52
 
@@ -72,8 +72,8 @@ module RubySketch
72
72
  #
73
73
  # @return [nil] nil
74
74
  #
75
- def set (*args)
76
- initialize *args
75
+ def set(*args)
76
+ initialize(*args)
77
77
  self
78
78
  end
79
79
 
@@ -81,7 +81,7 @@ module RubySketch
81
81
  #
82
82
  # @return [Numeric] x value of vector
83
83
  #
84
- def x ()
84
+ def x()
85
85
  @point.x
86
86
  end
87
87
 
@@ -89,7 +89,7 @@ module RubySketch
89
89
  #
90
90
  # @return [Numeric] y value of vector
91
91
  #
92
- def y ()
92
+ def y()
93
93
  @point.y
94
94
  end
95
95
 
@@ -97,7 +97,7 @@ module RubySketch
97
97
  #
98
98
  # @return [Numeric] z value of vector
99
99
  #
100
- def z ()
100
+ def z()
101
101
  @point.z
102
102
  end
103
103
 
@@ -105,7 +105,7 @@ module RubySketch
105
105
  #
106
106
  # @return [Numeric] x value of vector
107
107
  #
108
- def x= (x)
108
+ def x=(x)
109
109
  @point.x = x
110
110
  end
111
111
 
@@ -113,7 +113,7 @@ module RubySketch
113
113
  #
114
114
  # @return [Numeric] y value of vector
115
115
  #
116
- def y= (y)
116
+ def y=(y)
117
117
  @point.y = y
118
118
  end
119
119
 
@@ -121,7 +121,7 @@ module RubySketch
121
121
  #
122
122
  # @return [Numeric] z value of vector
123
123
  #
124
- def z= (z)
124
+ def z=(z)
125
125
  @point.z = z
126
126
  end
127
127
 
@@ -139,8 +139,8 @@ module RubySketch
139
139
  #
140
140
  # @return [Vector] interporated vector
141
141
  #
142
- def lerp (*args, amount)
143
- v = toVector__ *args
142
+ def lerp(*args, amount)
143
+ v = toVector__(*args)
144
144
  self.x = x + (v.x - x) * amount
145
145
  self.y = y + (v.y - y) * amount
146
146
  self.z = z + (v.z - z) * amount
@@ -155,7 +155,7 @@ module RubySketch
155
155
  #
156
156
  # @return [Vector] interporated vector
157
157
  #
158
- def self.lerp (v1, v2, amount)
158
+ def self.lerp(v1, v2, amount)
159
159
  v1.dup.lerp v2, amount
160
160
  end
161
161
 
@@ -163,7 +163,7 @@ module RubySketch
163
163
  #
164
164
  # @return [Array] array of x, y, z
165
165
  #
166
- def array ()
166
+ def array()
167
167
  @point.to_a 3
168
168
  end
169
169
 
@@ -180,7 +180,7 @@ module RubySketch
180
180
  #
181
181
  # @return [Vector] added vector
182
182
  #
183
- def add (*args)
183
+ def add(*args)
184
184
  @point += toVector__(*args).getInternal__
185
185
  self
186
186
  end
@@ -198,7 +198,7 @@ module RubySketch
198
198
  #
199
199
  # @return [Vector] subtracted vector
200
200
  #
201
- def sub (*args)
201
+ def sub(*args)
202
202
  @point -= toVector__(*args).getInternal__
203
203
  self
204
204
  end
@@ -209,7 +209,7 @@ module RubySketch
209
209
  #
210
210
  # @return [Vector] multiplied vector
211
211
  #
212
- def mult (num)
212
+ def mult(num)
213
213
  @point *= num
214
214
  self
215
215
  end
@@ -220,7 +220,7 @@ module RubySketch
220
220
  #
221
221
  # @return [Vector] divided vector
222
222
  #
223
- def div (num)
223
+ def div(num)
224
224
  @point /= num
225
225
  self
226
226
  end
@@ -231,7 +231,7 @@ module RubySketch
231
231
  #
232
232
  # @return [Vector] added vector
233
233
  #
234
- def + (v)
234
+ def +(v)
235
235
  dup.add v
236
236
  end
237
237
 
@@ -241,7 +241,7 @@ module RubySketch
241
241
  #
242
242
  # @return [Vector] subtracted vector
243
243
  #
244
- def - (v)
244
+ def -(v)
245
245
  dup.sub v
246
246
  end
247
247
 
@@ -251,7 +251,7 @@ module RubySketch
251
251
  #
252
252
  # @return [Vector] multiplied vector
253
253
  #
254
- def * (num)
254
+ def *(num)
255
255
  dup.mult num
256
256
  end
257
257
 
@@ -261,7 +261,7 @@ module RubySketch
261
261
  #
262
262
  # @return [Vector] divided vector
263
263
  #
264
- def / (num)
264
+ def /(num)
265
265
  dup.div num
266
266
  end
267
267
 
@@ -276,7 +276,7 @@ module RubySketch
276
276
  #
277
277
  # @return [Vector] added vector
278
278
  #
279
- def self.add (v1, v2, target = nil)
279
+ def self.add(v1, v2, target = nil)
280
280
  v = v1 + v2
281
281
  target.set v if self === target
282
282
  v
@@ -293,7 +293,7 @@ module RubySketch
293
293
  #
294
294
  # @return [Vector] subtracted vector
295
295
  #
296
- def self.sub (v1, v2, target = nil)
296
+ def self.sub(v1, v2, target = nil)
297
297
  v = v1 - v2
298
298
  target.set v if self === target
299
299
  v
@@ -310,7 +310,7 @@ module RubySketch
310
310
  #
311
311
  # @return [Vector] multiplied vector
312
312
  #
313
- def self.mult (v1, num, target = nil)
313
+ def self.mult(v1, num, target = nil)
314
314
  v = v1 * num
315
315
  target.set v if self === target
316
316
  v
@@ -327,7 +327,7 @@ module RubySketch
327
327
  #
328
328
  # @return [Vector] divided vector
329
329
  #
330
- def self.div (v1, num, target = nil)
330
+ def self.div(v1, num, target = nil)
331
331
  v = v1 / num
332
332
  target.set v if self === target
333
333
  v
@@ -337,7 +337,7 @@ module RubySketch
337
337
  #
338
338
  # @return [Numeric] length
339
339
  #
340
- def mag ()
340
+ def mag()
341
341
  @point.length
342
342
  end
343
343
 
@@ -345,7 +345,7 @@ module RubySketch
345
345
  #
346
346
  # @return [Numeric] squared length
347
347
  #
348
- def magSq ()
348
+ def magSq()
349
349
  Rays::Point::dot(@point, @point)
350
350
  end
351
351
 
@@ -359,7 +359,7 @@ module RubySketch
359
359
  #
360
360
  # @return [Vector] vector with new length
361
361
  #
362
- def setMag (target = nil, len)
362
+ def setMag(target = nil, len)
363
363
  (target || self).set @point.normal * len
364
364
  end
365
365
 
@@ -369,7 +369,7 @@ module RubySketch
369
369
  #
370
370
  # @return [Vector] normalized vector
371
371
  #
372
- def normalize (target = nil)
372
+ def normalize(target = nil)
373
373
  (target || self).set @point.normal
374
374
  end
375
375
 
@@ -379,7 +379,7 @@ module RubySketch
379
379
  #
380
380
  # @return [Vector] new vector
381
381
  #
382
- def limit (max)
382
+ def limit(max)
383
383
  setMag max if magSq > max ** 2
384
384
  self
385
385
  end
@@ -390,7 +390,7 @@ module RubySketch
390
390
  #
391
391
  # @return [Numeric] the distance
392
392
  #
393
- def dist (v)
393
+ def dist(v)
394
394
  (self - v).mag
395
395
  end
396
396
 
@@ -401,7 +401,7 @@ module RubySketch
401
401
  #
402
402
  # @return [Numeric] the distance
403
403
  #
404
- def self.dist (v1, v2)
404
+ def self.dist(v1, v2)
405
405
  v1.dist v2
406
406
  end
407
407
 
@@ -418,7 +418,7 @@ module RubySketch
418
418
  #
419
419
  # @return [Numeric] result of dot product
420
420
  #
421
- def dot (*args)
421
+ def dot(*args)
422
422
  Rays::Point::dot getInternal__, toVector__(*args).getInternal__
423
423
  end
424
424
 
@@ -429,7 +429,7 @@ module RubySketch
429
429
  #
430
430
  # @return [Numeric] result of dot product
431
431
  #
432
- def self.dot (v1, v2)
432
+ def self.dot(v1, v2)
433
433
  v1.dot v2
434
434
  end
435
435
 
@@ -446,7 +446,7 @@ module RubySketch
446
446
  #
447
447
  # @return [Numeric] result of cross product
448
448
  #
449
- def cross (a, *rest)
449
+ def cross(a, *rest)
450
450
  target = self.class === rest.last ? rest.pop : nil
451
451
  v = self.class.new Rays::Point::cross getInternal__, toVector__(a, *rest).getInternal__
452
452
  target.set v if self.class === target
@@ -460,7 +460,7 @@ module RubySketch
460
460
  #
461
461
  # @return [Numeric] result of cross product
462
462
  #
463
- def self.cross (v1, v2, target = nil)
463
+ def self.cross(v1, v2, target = nil)
464
464
  v1.cross v2, target
465
465
  end
466
466
 
@@ -470,7 +470,7 @@ module RubySketch
470
470
  #
471
471
  # @return [Vector] rotated this object
472
472
  #
473
- def rotate (angle)
473
+ def rotate(angle)
474
474
  angle = @context ? @context.toAngle__(angle) : angle * RAD2DEG__
475
475
  @point.rotate! angle
476
476
  self
@@ -480,7 +480,7 @@ module RubySketch
480
480
  #
481
481
  # @return [Numeric] the angle in radians
482
482
  #
483
- def heading ()
483
+ def heading()
484
484
  Math.atan2 y, x
485
485
  end
486
486
 
@@ -491,7 +491,7 @@ module RubySketch
491
491
  #
492
492
  # @return [Vector] rotated vector
493
493
  #
494
- def self.fromAngle (angle, target = nil)
494
+ def self.fromAngle(angle, target = nil)
495
495
  v = self.new(1, 0, 0).rotate(angle)
496
496
  target.set v if target
497
497
  v
@@ -504,7 +504,7 @@ module RubySketch
504
504
  #
505
505
  # @return [Numeric] angle in radians
506
506
  #
507
- def self.angleBetween (v1, v2)
507
+ def self.angleBetween(v1, v2)
508
508
  x1, y1, z1 = v1.array
509
509
  x2, y2, z2 = v2.array
510
510
  return 0 if (x1 == 0 && y1 == 0 && z1 == 0) || (x2 == 0 && y2 == 0 && z2 == 0)
@@ -521,7 +521,7 @@ module RubySketch
521
521
  #
522
522
  # @return [Vector] a random vector
523
523
  #
524
- def self.random2D (target = nil)
524
+ def self.random2D(target = nil)
525
525
  v = self.fromAngle rand 0.0...(Math::PI * 2)
526
526
  target.set v if target
527
527
  v
@@ -533,9 +533,9 @@ module RubySketch
533
533
  #
534
534
  # @return [Vector] a random vector
535
535
  #
536
- def self.random3D (target = nil)
536
+ def self.random3D(target = nil)
537
537
  angle = rand 0.0...(Math::PI * 2)
538
- z = rand -1.0..1.0
538
+ z = rand(-1.0..1.0)
539
539
  z2 = z ** 2
540
540
  x = Math.sqrt(1.0 - z2) * Math.cos(angle)
541
541
  y = Math.sqrt(1.0 - z2) * Math.sin(angle)
@@ -545,22 +545,22 @@ module RubySketch
545
545
  end
546
546
 
547
547
  # @private
548
- def inspect ()
548
+ def inspect()
549
549
  "<##{self.class.name} #{x}, #{y}, #{z}>"
550
550
  end
551
551
 
552
552
  # @private
553
- def <=> (o)
553
+ def <=>(o)
554
554
  @point <=> o.getInternal__
555
555
  end
556
556
 
557
557
  # @private
558
- protected def getInternal__ ()
558
+ protected def getInternal__()
559
559
  @point
560
560
  end
561
561
 
562
562
  # @private
563
- private def toVector__ (*args)
563
+ private def toVector__(*args)
564
564
  self.class === args.first ? args.first : self.class.new(*args)
565
565
  end
566
566
 
@@ -572,7 +572,7 @@ module RubySketch
572
572
  class Image
573
573
 
574
574
  # @private
575
- def initialize (image)
575
+ def initialize(image)
576
576
  @image = image
577
577
  end
578
578
 
@@ -580,7 +580,7 @@ module RubySketch
580
580
  #
581
581
  # @return [Numeric] width of image
582
582
  #
583
- def width ()
583
+ def width()
584
584
  @image.width
585
585
  end
586
586
 
@@ -588,7 +588,7 @@ module RubySketch
588
588
  #
589
589
  # @return [Numeric] height of image
590
590
  #
591
- def height ()
591
+ def height()
592
592
  @image.height
593
593
  end
594
594
 
@@ -599,7 +599,7 @@ module RubySketch
599
599
  #
600
600
  # @return [nil] nil
601
601
  #
602
- def resize (width, height)
602
+ def resize(width, height)
603
603
  @image = Rays::Image.new(width, height).paint do |painter|
604
604
  painter.image @image, 0, 0, width, height
605
605
  end
@@ -623,7 +623,7 @@ module RubySketch
623
623
  #
624
624
  # @return [nil] nil
625
625
  #
626
- def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
626
+ def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
627
627
  img ||= self
628
628
  @image.paint do |painter|
629
629
  painter.image img.getInternal__, sx, sy, sw, sh, dx, dy, dw, dh
@@ -634,12 +634,12 @@ module RubySketch
634
634
  #
635
635
  # @param filename [String] file name to save image
636
636
  #
637
- def save (filename)
637
+ def save(filename)
638
638
  @image.save filename
639
639
  end
640
640
 
641
641
  # @private
642
- def getInternal__ ()
642
+ def getInternal__()
643
643
  @image
644
644
  end
645
645
 
@@ -651,7 +651,7 @@ module RubySketch
651
651
  class Font
652
652
 
653
653
  # @private
654
- def initialize (font)
654
+ def initialize(font)
655
655
  @font = font
656
656
  end
657
657
 
@@ -668,7 +668,7 @@ module RubySketch
668
668
  #
669
669
  # @return [TextBounds] bounding box for text
670
670
  #
671
- def textBounds (str, x = 0, y = 0, fontSize = nil)
671
+ def textBounds(str, x = 0, y = 0, fontSize = nil)
672
672
  f = fontSize ? Rays::Font.new(@font.name, fontSize) : @font
673
673
  TextBounds.new x, y, x + f.width(str), y + f.height
674
674
  end
@@ -697,7 +697,7 @@ module RubySketch
697
697
  attr_reader :h
698
698
 
699
699
  # @private
700
- def initialize (x, y, w, h)
700
+ def initialize(x, y, w, h)
701
701
  @x, @y, @w, @h = x, y, w, h
702
702
  end
703
703
 
@@ -717,11 +717,11 @@ module RubySketch
717
717
  attr_reader :y
718
718
 
719
719
  # @private
720
- def initialize (x, y)
720
+ def initialize(x, y)
721
721
  @x, @y = x, y
722
722
  end
723
723
 
724
- def id ()
724
+ def id()
725
725
  raise NotImplementedError
726
726
  end
727
727
 
@@ -736,7 +736,7 @@ module RubySketch
736
736
  #
737
737
  # @return [Array] device name list
738
738
  #
739
- def self.list ()
739
+ def self.list()
740
740
  Rays::Camera.device_names
741
741
  end
742
742
 
@@ -751,7 +751,7 @@ module RubySketch
751
751
  # @param requestHeight [Integer] captured image height
752
752
  # @param cameraName [String] camera device name
753
753
  #
754
- def initialize (*args)
754
+ def initialize(*args)
755
755
  width, height, name =
756
756
  if args.empty?
757
757
  [-1, -1, nil]
@@ -769,7 +769,7 @@ module RubySketch
769
769
  #
770
770
  # @return [nil] nil
771
771
  #
772
- def start ()
772
+ def start()
773
773
  raise "Failed to start capture" unless @camera.start
774
774
  nil
775
775
  end
@@ -778,7 +778,7 @@ module RubySketch
778
778
  #
779
779
  # @return [nil] nil
780
780
  #
781
- def stop ()
781
+ def stop()
782
782
  @camera.stop
783
783
  nil
784
784
  end
@@ -787,13 +787,13 @@ module RubySketch
787
787
  #
788
788
  # @return [Boolean] true means object has next frame
789
789
  #
790
- def available ()
790
+ def available()
791
791
  @camera.active?
792
792
  end
793
793
 
794
794
  # Reads next frame image
795
795
  #
796
- def read ()
796
+ def read()
797
797
  @camera.image
798
798
  end
799
799
 
@@ -801,7 +801,7 @@ module RubySketch
801
801
  #
802
802
  # @return [Numeric] the width of captured image
803
803
  #
804
- def width ()
804
+ def width()
805
805
  @camera.image&.width || 0
806
806
  end
807
807
 
@@ -809,17 +809,17 @@ module RubySketch
809
809
  #
810
810
  # @return [Numeric] the height of captured image
811
811
  #
812
- def height ()
812
+ def height()
813
813
  @camera.image&.height || 0
814
814
  end
815
815
 
816
816
  # @private
817
- def getInternal__ ()
817
+ def getInternal__()
818
818
  @camera.image || dummyImage__
819
819
  end
820
820
 
821
821
  # @private
822
- private def dummyImage__ ()
822
+ private def dummyImage__()
823
823
  @dummy ||= Rays::Image.new 1, 1
824
824
  end
825
825
 
@@ -852,50 +852,99 @@ module RubySketch
852
852
 
853
853
  # RGB mode for colorMode().
854
854
  #
855
- RGB = :RGB
855
+ RGB = :rgb
856
856
 
857
857
  # HSB mode for colorMode().
858
858
  #
859
- HSB = :HSB
859
+ HSB = :hsb
860
860
 
861
861
  # Radian mode for angleMode().
862
862
  #
863
- RADIANS = :RADIANS
863
+ RADIANS = :radians
864
864
 
865
865
  # Degree mode for angleMode().
866
866
  #
867
- DEGREES = :DEGREES
867
+ DEGREES = :degrees
868
868
 
869
869
  # Mode for rectMode(), ellipseMode() and imageMode().
870
870
  #
871
- CORNER = :CORNER
871
+ CORNER = :corner
872
872
 
873
873
  # Mode for rectMode(), ellipseMode() and imageMode().
874
874
  #
875
- CORNERS = :CORNERS
875
+ CORNERS = :corners
876
876
 
877
877
  # Mode for rectMode(), ellipseMode(), imageMode() and textAlign().
878
878
  #
879
- CENTER = :CENTER
879
+ CENTER = :center
880
880
 
881
881
  # Mode for rectMode() and ellipseMode().
882
882
  #
883
- RADIUS = :RADIUS
883
+ RADIUS = :radius
884
+
885
+ # Key codes.
886
+ ENTER = :enter
887
+ SPACE = :space
888
+ TAB = :tab
889
+ DELETE = :delete
890
+ BACKSPACE = :backspace
891
+ ESC = :escape
892
+ HOME = :home
893
+ #END = :end
894
+ PAGEUP = :pageup
895
+ PAGEDOWN = :pagedown
896
+ CLEAR = :clear
897
+ SHIFT = :shift
898
+ CONTROL = :control
899
+ ALT = :alt
900
+ WIN = :win
901
+ COMMAND = :command
902
+ OPTION = :option
903
+ FUNCTION = :function
904
+ CAPSLOCK = :capslock
905
+ SECTION = :section
906
+ HELP = :help
907
+ F1 = :f1
908
+ F2 = :f2
909
+ F3 = :f3
910
+ F4 = :f4
911
+ F5 = :f5
912
+ F6 = :f6
913
+ F7 = :f7
914
+ F8 = :f8
915
+ F9 = :f9
916
+ F10 = :f10
917
+ F11 = :f11
918
+ F12 = :f12
919
+ F13 = :f13
920
+ F14 = :f14
921
+ F15 = :f15
922
+ F16 = :f16
923
+ F17 = :f17
924
+ F18 = :f18
925
+ F19 = :f19
926
+ F20 = :f20
927
+ F21 = :f21
928
+ F22 = :f22
929
+ F23 = :f23
930
+ F24 = :f24
931
+ UP = :up
932
+ DOWN = :down
933
+
934
+ # Key code or Mode for textAlign().
935
+ LEFT = :left
936
+
937
+ # Key code or Mode for textAlign().
938
+ RIGHT = :right
884
939
 
885
940
  # Mode for textAlign().
886
- LEFT = :LEFT
941
+ TOP = :top
887
942
 
888
943
  # Mode for textAlign().
889
- RIGHT = :RIGHT
944
+ BOTTOM = :bottom
890
945
 
891
946
  # Mode for textAlign().
892
- TOP = :TOP
893
-
894
- # Mode for textAlign().
895
- BOTTOM = :BOTTOM
896
-
897
- # Mode for textAlign().
898
- BASELINE = :BASELINE
947
+ BASELINE = :baseline
899
948
 
900
949
  # Mode for strokeCap().
901
950
  #
@@ -913,7 +962,7 @@ module RubySketch
913
962
  #
914
963
  SQUARE = :square
915
964
 
916
- def init__ (image, painter)
965
+ def init__(image, painter)
917
966
  @drawing__ = false
918
967
  @hsbColor__ = false
919
968
  @colorMaxes__ = [1.0] * 4
@@ -937,29 +986,30 @@ module RubySketch
937
986
 
938
987
  fill 255
939
988
  stroke 0
989
+ strokeWeight 1
940
990
  end
941
991
 
942
- def updateCanvas__ (image, painter)
992
+ def updateCanvas__(image, painter)
943
993
  @image__, @painter__ = image, painter
944
994
  end
945
995
 
946
996
  # @private
947
- def beginDraw__ ()
997
+ def beginDraw__()
948
998
  @matrixStack__.clear
949
999
  @styleStack__.clear
950
1000
  @drawing__ = true
951
1001
  end
952
1002
 
953
1003
  # @private
954
- def endDraw__ ()
1004
+ def endDraw__()
955
1005
  @drawing__ = false
956
1006
  end
957
1007
 
958
- def width ()
1008
+ def width()
959
1009
  @image__.width
960
1010
  end
961
1011
 
962
- def height ()
1012
+ def height()
963
1013
  @image__.height
964
1014
  end
965
1015
 
@@ -979,8 +1029,8 @@ module RubySketch
979
1029
  #
980
1030
  # @return [nil] nil
981
1031
  #
982
- def colorMode (mode, *maxes)
983
- mode = mode.upcase.to_sym
1032
+ def colorMode(mode, *maxes)
1033
+ mode = mode.downcase.to_sym
984
1034
  raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
985
1035
  raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)
986
1036
 
@@ -993,7 +1043,7 @@ module RubySketch
993
1043
  end
994
1044
 
995
1045
  # @private
996
- private def toRGBA__ (*args)
1046
+ private def toRGBA__(*args)
997
1047
  a, b, c, d = args
998
1048
  return parseColor__(a, b || alphaMax__) if a.kind_of?(String)
999
1049
 
@@ -1008,8 +1058,8 @@ module RubySketch
1008
1058
  end
1009
1059
 
1010
1060
  # @private
1011
- private def parseColor__ (str, alpha)
1012
- result = str.match /^\s*##{'([0-9a-f]{2})' * 3}\s*$/i
1061
+ private def parseColor__(str, alpha)
1062
+ result = str.match(/^\s*##{'([0-9a-f]{2})' * 3}\s*$/i)
1013
1063
  raise ArgumentError, "invalid color code: '#{str}'" unless result
1014
1064
 
1015
1065
  rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) / 255.0}
@@ -1017,7 +1067,7 @@ module RubySketch
1017
1067
  end
1018
1068
 
1019
1069
  # @private
1020
- private def alphaMax__ ()
1070
+ private def alphaMax__()
1021
1071
  @colorMaxes__[3]
1022
1072
  end
1023
1073
 
@@ -1027,8 +1077,8 @@ module RubySketch
1027
1077
  #
1028
1078
  # @return [nil] nil
1029
1079
  #
1030
- def angleMode (mode)
1031
- @angleScale__ = case mode.upcase.to_sym
1080
+ def angleMode(mode)
1081
+ @angleScale__ = case mode.downcase.to_sym
1032
1082
  when RADIANS then RAD2DEG__
1033
1083
  when DEGREES then 1.0
1034
1084
  else raise ArgumentError, "invalid angle mode: #{mode}"
@@ -1037,7 +1087,7 @@ module RubySketch
1037
1087
  end
1038
1088
 
1039
1089
  # @private
1040
- def toAngle__ (angle)
1090
+ def toAngle__(angle)
1041
1091
  angle * @angleScale__
1042
1092
  end
1043
1093
 
@@ -1052,7 +1102,7 @@ module RubySketch
1052
1102
  #
1053
1103
  # @return [nil] nil
1054
1104
  #
1055
- def rectMode (mode)
1105
+ def rectMode(mode)
1056
1106
  @rectMode__ = mode
1057
1107
  end
1058
1108
 
@@ -1067,7 +1117,7 @@ module RubySketch
1067
1117
  #
1068
1118
  # @return [nil] nil
1069
1119
  #
1070
- def ellipseMode (mode)
1120
+ def ellipseMode(mode)
1071
1121
  @ellipseMode__ = mode
1072
1122
  end
1073
1123
 
@@ -1081,12 +1131,12 @@ module RubySketch
1081
1131
  #
1082
1132
  # @return [nil] nil
1083
1133
  #
1084
- def imageMode (mode)
1134
+ def imageMode(mode)
1085
1135
  @imageMode__ = mode
1086
1136
  end
1087
1137
 
1088
1138
  # @private
1089
- private def toXYWH__ (mode, a, b, c, d)
1139
+ private def toXYWH__(mode, a, b, c, d)
1090
1140
  case mode
1091
1141
  when CORNER then [a, b, c, d]
1092
1142
  when CORNERS then [a, b, c - a, d - b]
@@ -1114,7 +1164,7 @@ module RubySketch
1114
1164
  #
1115
1165
  # @return [nil] nil
1116
1166
  #
1117
- def fill (*args)
1167
+ def fill(*args)
1118
1168
  @painter__.fill(*toRGBA__(*args))
1119
1169
  nil
1120
1170
  end
@@ -1137,7 +1187,7 @@ module RubySketch
1137
1187
  #
1138
1188
  # @return [nil] nil
1139
1189
  #
1140
- def stroke (*args)
1190
+ def stroke(*args)
1141
1191
  @painter__.stroke(*toRGBA__(*args))
1142
1192
  nil
1143
1193
  end
@@ -1148,7 +1198,7 @@ module RubySketch
1148
1198
  #
1149
1199
  # @return [nil] nil
1150
1200
  #
1151
- def strokeWeight (weight)
1201
+ def strokeWeight(weight)
1152
1202
  @painter__.stroke_width weight
1153
1203
  nil
1154
1204
  end
@@ -1159,7 +1209,7 @@ module RubySketch
1159
1209
  #
1160
1210
  # @return [nil] nil
1161
1211
  #
1162
- def strokeCap (cap)
1212
+ def strokeCap(cap)
1163
1213
  @painter__.stroke_cap cap
1164
1214
  nil
1165
1215
  end
@@ -1170,7 +1220,7 @@ module RubySketch
1170
1220
  #
1171
1221
  # @return [nil] nil
1172
1222
  #
1173
- def strokeJoin (join)
1223
+ def strokeJoin(join)
1174
1224
  @painter__.stroke_join join
1175
1225
  nil
1176
1226
  end
@@ -1179,7 +1229,7 @@ module RubySketch
1179
1229
  #
1180
1230
  # @return [nil] nil
1181
1231
  #
1182
- def noFill ()
1232
+ def noFill()
1183
1233
  @painter__.fill nil
1184
1234
  nil
1185
1235
  end
@@ -1188,7 +1238,7 @@ module RubySketch
1188
1238
  #
1189
1239
  # @return [nil] nil
1190
1240
  #
1191
- def noStroke ()
1241
+ def noStroke()
1192
1242
  @painter__.stroke nil
1193
1243
  nil
1194
1244
  end
@@ -1200,7 +1250,7 @@ module RubySketch
1200
1250
  #
1201
1251
  # @return [Font] current font
1202
1252
  #
1203
- def textFont (name = nil, size = nil)
1253
+ def textFont(name = nil, size = nil)
1204
1254
  setFont__ name, size if name || size
1205
1255
  Font.new @painter__.font
1206
1256
  end
@@ -1211,30 +1261,30 @@ module RubySketch
1211
1261
  #
1212
1262
  # @return [nil] nil
1213
1263
  #
1214
- def textSize (size)
1264
+ def textSize(size)
1215
1265
  setFont__ @painter__.font.name, size
1216
1266
  nil
1217
1267
  end
1218
1268
 
1219
- def textWidth (str)
1269
+ def textWidth(str)
1220
1270
  @painter__.font.width str
1221
1271
  end
1222
1272
 
1223
- def textAscent ()
1273
+ def textAscent()
1224
1274
  @painter__.font.ascent
1225
1275
  end
1226
1276
 
1227
- def textDescent ()
1277
+ def textDescent()
1228
1278
  @painter__.font.descent
1229
1279
  end
1230
1280
 
1231
- def textAlign (horizontal, vertical = BASELINE)
1281
+ def textAlign(horizontal, vertical = BASELINE)
1232
1282
  @textAlignH__ = horizontal
1233
1283
  @textAlignV__ = vertical
1234
1284
  end
1235
1285
 
1236
1286
  # @private
1237
- def setFont__ (name, size)
1287
+ def setFont__(name, size)
1238
1288
  size = 256 if size && size > 256
1239
1289
  @painter__.font name, size
1240
1290
  end
@@ -1257,11 +1307,11 @@ module RubySketch
1257
1307
  #
1258
1308
  # @return [nil] nil
1259
1309
  #
1260
- def background (*args)
1310
+ def background(*args)
1261
1311
  assertDrawing__
1262
- rgba = toRGBA__ *args
1312
+ rgba = toRGBA__(*args)
1263
1313
  if rgba[3] == 1
1264
- @painter__.background *rgba
1314
+ @painter__.background(*rgba)
1265
1315
  else
1266
1316
  @painter__.push fill: rgba, stroke: nil do |_|
1267
1317
  @painter__.rect 0, 0, width, height
@@ -1277,7 +1327,7 @@ module RubySketch
1277
1327
  #
1278
1328
  # @return [nil] nil
1279
1329
  #
1280
- def point (x, y)
1330
+ def point(x, y)
1281
1331
  assertDrawing__
1282
1332
  w = @painter__.stroke_width
1283
1333
  w = 1 if w == 0
@@ -1294,7 +1344,7 @@ module RubySketch
1294
1344
  #
1295
1345
  # @return [nil] nil
1296
1346
  #
1297
- def line (x1, y1, x2, y2)
1347
+ def line(x1, y1, x2, y2)
1298
1348
  assertDrawing__
1299
1349
  @painter__.line x1, y1, x2, y2
1300
1350
  nil
@@ -1318,7 +1368,7 @@ module RubySketch
1318
1368
  #
1319
1369
  # @return [nil] nil
1320
1370
  #
1321
- def rect (a, b, c, d, *args)
1371
+ def rect(a, b, c, d, *args)
1322
1372
  assertDrawing__
1323
1373
  x, y, w, h = toXYWH__ @rectMode__, a, b, c, d
1324
1374
  case args.size
@@ -1339,7 +1389,7 @@ module RubySketch
1339
1389
  #
1340
1390
  # @return [nil] nil
1341
1391
  #
1342
- def ellipse (a, b, c, d)
1392
+ def ellipse(a, b, c, d)
1343
1393
  assertDrawing__
1344
1394
  x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
1345
1395
  @painter__.ellipse x, y, w, h
@@ -1354,7 +1404,7 @@ module RubySketch
1354
1404
  #
1355
1405
  # @return [nil] nil
1356
1406
  #
1357
- def circle (x, y, extent)
1407
+ def circle(x, y, extent)
1358
1408
  ellipse x, y, extent, extent
1359
1409
  end
1360
1410
 
@@ -1369,11 +1419,11 @@ module RubySketch
1369
1419
  #
1370
1420
  # @return [nil] nil
1371
1421
  #
1372
- def arc (a, b, c, d, start, stop)
1422
+ def arc(a, b, c, d, start, stop)
1373
1423
  assertDrawing__
1374
1424
  x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
1375
- start = toAngle__ -start
1376
- stop = toAngle__ -stop
1425
+ start = toAngle__(-start)
1426
+ stop = toAngle__(-stop)
1377
1427
  @painter__.ellipse x, y, w, h, from: start, to: stop
1378
1428
  nil
1379
1429
  end
@@ -1386,7 +1436,7 @@ module RubySketch
1386
1436
  #
1387
1437
  # @return [nil] nil
1388
1438
  #
1389
- def square (x, y, extent)
1439
+ def square(x, y, extent)
1390
1440
  rect x, y, extent, extent
1391
1441
  end
1392
1442
 
@@ -1401,7 +1451,7 @@ module RubySketch
1401
1451
  #
1402
1452
  # @return [nil] nil
1403
1453
  #
1404
- def triangle (x1, y1, x2, y2, x3, y3)
1454
+ def triangle(x1, y1, x2, y2, x3, y3)
1405
1455
  assertDrawing__
1406
1456
  @painter__.line x1, y1, x2, y2, x3, y3, loop: true
1407
1457
  nil
@@ -1420,7 +1470,7 @@ module RubySketch
1420
1470
  #
1421
1471
  # @return [nil] nil
1422
1472
  #
1423
- def quad (x1, y1, x2, y2, x3, y3, x4, y4)
1473
+ def quad(x1, y1, x2, y2, x3, y3, x4, y4)
1424
1474
  assertDrawing__
1425
1475
  @painter__.line x1, y1, x2, y2, x3, y3, x4, y4, loop: true
1426
1476
  nil
@@ -1439,7 +1489,7 @@ module RubySketch
1439
1489
  #
1440
1490
  # @return [nil] nil
1441
1491
  #
1442
- def curve (cx1, cy1, x1, y1, x2, y2, cx2, cy2)
1492
+ def curve(cx1, cy1, x1, y1, x2, y2, cx2, cy2)
1443
1493
  assertDrawing__
1444
1494
  @painter__.curve cx1, cy1, x1, y1, x2, y2, cx2, cy2
1445
1495
  nil
@@ -1458,7 +1508,7 @@ module RubySketch
1458
1508
  #
1459
1509
  # @return [nil] nil
1460
1510
  #
1461
- def bezier (x1, y1, cx1, cy1, cx2, cy2, x2, y2)
1511
+ def bezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2)
1462
1512
  assertDrawing__
1463
1513
  @painter__.bezier x1, y1, cx1, cy1, cx2, cy2, x2, y2
1464
1514
  nil
@@ -1480,7 +1530,7 @@ module RubySketch
1480
1530
  #
1481
1531
  # @return [nil] nil
1482
1532
  #
1483
- def text (str, x, y, x2 = nil, y2 = nil)
1533
+ def text(str, x, y, x2 = nil, y2 = nil)
1484
1534
  assertDrawing__
1485
1535
  if x2
1486
1536
  raise ArgumentError, "missing y2 parameter" unless y2
@@ -1514,7 +1564,7 @@ module RubySketch
1514
1564
  #
1515
1565
  # @return [nil] nil
1516
1566
  #
1517
- def image (img, a, b, c = nil, d = nil)
1567
+ def image(img, a, b, c = nil, d = nil)
1518
1568
  assertDrawing__
1519
1569
  i = img.getInternal__
1520
1570
  x, y, w, h = toXYWH__ @imageMode__, a, b, c || i.width, d || i.height
@@ -1539,7 +1589,7 @@ module RubySketch
1539
1589
  #
1540
1590
  # @return [nil] nil
1541
1591
  #
1542
- def copy (img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
1592
+ def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
1543
1593
  assertDrawing__
1544
1594
  src = img&.getInternal__ || @window__.canvas
1545
1595
  @painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
@@ -1552,7 +1602,7 @@ module RubySketch
1552
1602
  #
1553
1603
  # @return [nil] nil
1554
1604
  #
1555
- def translate (x, y)
1605
+ def translate(x, y)
1556
1606
  assertDrawing__
1557
1607
  @painter__.translate x, y
1558
1608
  nil
@@ -1569,7 +1619,7 @@ module RubySketch
1569
1619
  #
1570
1620
  # @return [nil] nil
1571
1621
  #
1572
- def scale (x, y)
1622
+ def scale(x, y)
1573
1623
  assertDrawing__
1574
1624
  @painter__.scale x, y
1575
1625
  nil
@@ -1581,7 +1631,7 @@ module RubySketch
1581
1631
  #
1582
1632
  # @return [nil] nil
1583
1633
  #
1584
- def rotate (angle)
1634
+ def rotate(angle)
1585
1635
  assertDrawing__
1586
1636
  @painter__.rotate toAngle__ angle
1587
1637
  nil
@@ -1591,7 +1641,7 @@ module RubySketch
1591
1641
  #
1592
1642
  # @return [nil] nil
1593
1643
  #
1594
- def pushMatrix (&block)
1644
+ def pushMatrix(&block)
1595
1645
  assertDrawing__
1596
1646
  @matrixStack__.push @painter__.matrix
1597
1647
  if block
@@ -1605,7 +1655,7 @@ module RubySketch
1605
1655
  #
1606
1656
  # @return [nil] nil
1607
1657
  #
1608
- def popMatrix ()
1658
+ def popMatrix()
1609
1659
  assertDrawing__
1610
1660
  raise "matrix stack underflow" if @matrixStack__.empty?
1611
1661
  @painter__.matrix = @matrixStack__.pop
@@ -1616,7 +1666,7 @@ module RubySketch
1616
1666
  #
1617
1667
  # @return [nil] nil
1618
1668
  #
1619
- def resetMatrix ()
1669
+ def resetMatrix()
1620
1670
  assertDrawing__
1621
1671
  @painter__.matrix = 1
1622
1672
  nil
@@ -1626,7 +1676,7 @@ module RubySketch
1626
1676
  #
1627
1677
  # @return [nil] nil
1628
1678
  #
1629
- def pushStyle (&block)
1679
+ def pushStyle(&block)
1630
1680
  assertDrawing__
1631
1681
  @styleStack__.push [
1632
1682
  @painter__.fill,
@@ -1653,7 +1703,7 @@ module RubySketch
1653
1703
  #
1654
1704
  # @return [nil] nil
1655
1705
  #
1656
- def popStyle ()
1706
+ def popStyle()
1657
1707
  assertDrawing__
1658
1708
  raise "style stack underflow" if @styleStack__.empty?
1659
1709
  @painter__.fill,
@@ -1675,7 +1725,7 @@ module RubySketch
1675
1725
  #
1676
1726
  # @return [nil] nil
1677
1727
  #
1678
- def push (&block)
1728
+ def push(&block)
1679
1729
  pushMatrix
1680
1730
  pushStyle
1681
1731
  if block
@@ -1688,18 +1738,18 @@ module RubySketch
1688
1738
  #
1689
1739
  # @return [nil] nil
1690
1740
  #
1691
- def pop ()
1741
+ def pop()
1692
1742
  popMatrix
1693
1743
  popStyle
1694
1744
  end
1695
1745
 
1696
1746
  # @private
1697
- def getInternal__ ()
1747
+ def getInternal__()
1698
1748
  @image__
1699
1749
  end
1700
1750
 
1701
1751
  # @private
1702
- private def assertDrawing__ ()
1752
+ private def assertDrawing__()
1703
1753
  raise "call beginDraw() before drawing" unless @drawing__
1704
1754
  end
1705
1755
 
@@ -1714,14 +1764,14 @@ module RubySketch
1714
1764
 
1715
1765
  # Initialize graphics object.
1716
1766
  #
1717
- def initialize (width, height)
1767
+ def initialize(width, height)
1718
1768
  image = Rays::Image.new width, height
1719
1769
  init__ image, image.painter
1720
1770
  end
1721
1771
 
1722
1772
  # Start drawing.
1723
1773
  #
1724
- def beginDraw (&block)
1774
+ def beginDraw(&block)
1725
1775
  @painter__.__send__ :begin_paint
1726
1776
  beginDraw__
1727
1777
  push
@@ -1733,7 +1783,7 @@ module RubySketch
1733
1783
 
1734
1784
  # End drawing.
1735
1785
  #
1736
- def endDraw ()
1786
+ def endDraw()
1737
1787
  pop
1738
1788
  endDraw__
1739
1789
  @painter__.__send__ :end_paint
@@ -1756,24 +1806,29 @@ module RubySketch
1756
1806
  @@context__ = nil
1757
1807
 
1758
1808
  # @private
1759
- def self.context__ ()
1809
+ def self.context__()
1760
1810
  @@context__
1761
1811
  end
1762
1812
 
1763
1813
  # @private
1764
- def initialize (window)
1814
+ def initialize(window)
1765
1815
  @@context__ = self
1766
1816
 
1817
+ tmpdir__.tap {|dir| FileUtils.rm_r dir.to_s if dir.directory?}
1818
+
1767
1819
  @window__ = window
1768
1820
  init__ @window__.canvas, @window__.canvas_painter.paint {background 0.8}
1769
1821
 
1770
- @loop__ = true
1771
- @redraw__ = false
1772
- @frameCount__ = 0
1773
- @mousePos__ =
1774
- @mousePrevPos__ = [0, 0]
1775
- @mousePressed__ = false
1776
- @touches__ = []
1822
+ @loop__ = true
1823
+ @redraw__ = false
1824
+ @frameCount__ = 0
1825
+ @key__ = nil
1826
+ @keyCode__ = nil
1827
+ @keysPressed__ = Set.new
1828
+ @pointerPos__ =
1829
+ @pointerPrevPos__ = [0, 0]
1830
+ @pointersPressed__ = Set.new
1831
+ @touches__ = []
1777
1832
 
1778
1833
  @window__.before_draw = proc {beginDraw__}
1779
1834
  @window__.after_draw = proc {endDraw__}
@@ -1794,17 +1849,38 @@ module RubySketch
1794
1849
  @redraw__ = false
1795
1850
  drawFrame.call
1796
1851
  end
1797
- @mousePrevPos__ = @mousePos__
1852
+ @pointerPrevPos__ = @pointerPos__
1798
1853
  end
1799
1854
 
1855
+ updateKeyStates = -> event, pressed {
1856
+ @key__ = event.chars
1857
+ @keyCode__ = event.key
1858
+ if pressed != nil
1859
+ set, key = @keysPressed__, event.key
1860
+ pressed ? set.add(key) : set.delete(key)
1861
+ end
1862
+ }
1863
+
1800
1864
  updatePointerStates = -> event, pressed = nil {
1801
- @mousePos__ = @window__.to_canvas_coord event.pos.x, event.pos.y
1802
- @mousePressed__ = pressed if pressed != nil
1803
- @touches__ = event.positions.map {|pos|
1804
- Touch.new *@window__.to_canvas_coord(pos.x, pos.y)
1805
- }
1865
+ @pointerPos__ = event.pos.to_a
1866
+ @touches__ = event.positions.map {|pos| Touch.new(*pos.to_a)}
1867
+ if pressed != nil
1868
+ set, type = @pointersPressed__, event.pointer_type
1869
+ pressed ? set.add(type) : set.delete(type)
1870
+ end
1806
1871
  }
1807
1872
 
1873
+ @window__.key_down = proc do |e|
1874
+ updateKeyStates.call e, true
1875
+ @keyPressedBlock__&.call
1876
+ @keyTypedBlock__&.call unless @key__.empty?
1877
+ end
1878
+
1879
+ @window__.key_up = proc do |e|
1880
+ updateKeyStates.call e, false
1881
+ @keyReleasedBlock__&.call
1882
+ end
1883
+
1808
1884
  @window__.pointer_down = proc do |e|
1809
1885
  updatePointerStates.call e, true
1810
1886
  (@touchStartedBlock__ || @mousePressedBlock__)&.call
@@ -1824,82 +1900,130 @@ module RubySketch
1824
1900
  updatePointerStates.call e
1825
1901
  (@touchMovedBlock__ || @mouseDraggedBlock__)&.call
1826
1902
  end
1903
+
1904
+ @window__.motion = proc do |e|
1905
+ @motionGravity__ = createVector(*e.gravity.to_a(3))
1906
+ @motionBlock__&.call
1907
+ end
1827
1908
  end
1828
1909
 
1829
- # Define setup block.
1910
+ # Defines setup block.
1830
1911
  #
1831
- def setup (&block)
1912
+ def setup(&block)
1832
1913
  @window__.setup = block
1833
1914
  nil
1834
1915
  end
1835
1916
 
1836
- # Define draw block.
1917
+ # Defines draw block.
1837
1918
  #
1838
- def draw (&block)
1919
+ def draw(&block)
1839
1920
  @drawBlock__ = block if block
1840
1921
  nil
1841
1922
  end
1842
1923
 
1843
- def key (&block)
1844
- @window__.key = block
1924
+ # Defines keyPressed block.
1925
+ #
1926
+ # @return [Boolean] is any key pressed or not
1927
+ #
1928
+ def keyPressed(&block)
1929
+ @keyPressedBlock__ = block if block
1930
+ not @keysPressed__.empty?
1931
+ end
1932
+
1933
+ # Defines keyReleased block.
1934
+ #
1935
+ def keyReleased(&block)
1936
+ @keyReleasedBlock__ = block if block
1937
+ nil
1938
+ end
1939
+
1940
+ # Defines keyTyped block.
1941
+ #
1942
+ def keyTyped(&block)
1943
+ @keyTypedBlock__ = block if block
1845
1944
  nil
1846
1945
  end
1847
1946
 
1848
- def mousePressed (&block)
1947
+ # Defines mousePressed block.
1948
+ #
1949
+ # @return [Boolean] is any mouse button pressed or not
1950
+ #
1951
+ def mousePressed(&block)
1849
1952
  @mousePressedBlock__ = block if block
1850
- @mousePressed__
1953
+ not @pointersPressed__.empty?
1851
1954
  end
1852
1955
 
1853
- def mouseReleased (&block)
1956
+ # Defines mouseReleased block.
1957
+ #
1958
+ def mouseReleased(&block)
1854
1959
  @mouseReleasedBlock__ = block if block
1855
1960
  nil
1856
1961
  end
1857
1962
 
1858
- def mouseMoved (&block)
1963
+ # Defines mouseMoved block.
1964
+ #
1965
+ def mouseMoved(&block)
1859
1966
  @mouseMovedBlock__ = block if block
1860
1967
  nil
1861
1968
  end
1862
1969
 
1863
- def mouseDragged (&block)
1970
+ # Defines mouseDragged block.
1971
+ #
1972
+ def mouseDragged(&block)
1864
1973
  @mouseDraggedBlock__ = block if block
1865
1974
  nil
1866
1975
  end
1867
1976
 
1868
- def touchStarted (&block)
1977
+ # Defines touchStarted block.
1978
+ #
1979
+ def touchStarted(&block)
1869
1980
  @touchStartedBlock__ = block if block
1870
1981
  nil
1871
1982
  end
1872
1983
 
1873
- def touchEnded (&block)
1984
+ # Defines touchEnded block.
1985
+ #
1986
+ def touchEnded(&block)
1874
1987
  @touchEndedBlock__ = block if block
1875
1988
  nil
1876
1989
  end
1877
1990
 
1878
- def touchMoved (&block)
1991
+ # Defines touchMoved block.
1992
+ #
1993
+ def touchMoved(&block)
1879
1994
  @touchMovedBlock__ = block if block
1880
1995
  nil
1881
1996
  end
1882
1997
 
1998
+ # Defines motion block.
1999
+ #
2000
+ def motion(&block)
2001
+ @motionBlock__ = block if block
2002
+ nil
2003
+ end
2004
+
1883
2005
  # Changes canvas size.
1884
2006
  #
1885
- # @param width [Integer] new width
1886
- # @param height [Integer] new height
2007
+ # @param width [Integer] new width
2008
+ # @param height [Integer] new height
2009
+ # @param pixelDensity [Numeric] new pixel density
1887
2010
  #
1888
2011
  # @return [nil] nil
1889
2012
  #
1890
- def size (width, height)
2013
+ def size(width, height, pixelDensity: self.pixelDensity)
1891
2014
  resizeCanvas__ :size, width, height, pixelDensity
1892
2015
  nil
1893
2016
  end
1894
2017
 
1895
2018
  # Changes canvas size.
1896
2019
  #
1897
- # @param width [Integer] new width
1898
- # @param height [Integer] new height
2020
+ # @param width [Integer] new width
2021
+ # @param height [Integer] new height
2022
+ # @param pixelDensity [Numeric] new pixel density
1899
2023
  #
1900
2024
  # @return [nil] nil
1901
2025
  #
1902
- def createCanvas (width, height)
2026
+ def createCanvas(width, height, pixelDensity: self.pixelDensity)
1903
2027
  resizeCanvas__ :createCanvas, width, height, pixelDensity
1904
2028
  nil
1905
2029
  end
@@ -1910,13 +2034,13 @@ module RubySketch
1910
2034
  #
1911
2035
  # @return [Numeric] current pixel density
1912
2036
  #
1913
- def pixelDensity (density = nil)
2037
+ def pixelDensity(density = nil)
1914
2038
  resizeCanvas__ :pixelDensity, width, height, density if density
1915
2039
  @painter__.pixel_density
1916
2040
  end
1917
2041
 
1918
2042
  # @private
1919
- def resizeCanvas__ (name, width, height, pixelDensity)
2043
+ def resizeCanvas__(name, width, height, pixelDensity)
1920
2044
  raise '#{name}() must be called on startup or setup block' if @started__
1921
2045
 
1922
2046
  @painter__.__send__ :end_paint
@@ -1931,15 +2055,23 @@ module RubySketch
1931
2055
  #
1932
2056
  # @return [Numeric] pixel density
1933
2057
  #
1934
- def displayDensity ()
2058
+ def displayDensity()
1935
2059
  @window__.painter.pixel_density
1936
2060
  end
1937
2061
 
1938
- def windowWidth ()
2062
+ # Returns window width.
2063
+ #
2064
+ # @return [Numeric] window width
2065
+ #
2066
+ def windowWidth()
1939
2067
  @window__.width
1940
2068
  end
1941
2069
 
1942
- def windowHeight ()
2070
+ # Returns window height.
2071
+ #
2072
+ # @return [Numeric] window height
2073
+ #
2074
+ def windowHeight()
1943
2075
  @window__.height
1944
2076
  end
1945
2077
 
@@ -1947,7 +2079,7 @@ module RubySketch
1947
2079
  #
1948
2080
  # @return [Integer] total number of frames
1949
2081
  #
1950
- def frameCount ()
2082
+ def frameCount()
1951
2083
  @frameCount__
1952
2084
  end
1953
2085
 
@@ -1955,55 +2087,79 @@ module RubySketch
1955
2087
  #
1956
2088
  # @return [Float] frames per second
1957
2089
  #
1958
- def frameRate ()
2090
+ def frameRate()
1959
2091
  @window__.event.fps
1960
2092
  end
1961
2093
 
2094
+ # Returns the last key that was pressed or released.
2095
+ #
2096
+ # @return [String] last key
2097
+ #
2098
+ def key()
2099
+ @key__
2100
+ end
2101
+
2102
+ # Returns the last key code that was pressed or released.
2103
+ #
2104
+ # @return [Symbol] last key code
2105
+ #
2106
+ def keyCode()
2107
+ @keyCode__
2108
+ end
2109
+
1962
2110
  # Returns mouse x position
1963
2111
  #
1964
2112
  # @return [Numeric] horizontal position of mouse
1965
2113
  #
1966
- def mouseX ()
1967
- @mousePos__[0]
2114
+ def mouseX()
2115
+ @pointerPos__[0]
1968
2116
  end
1969
2117
 
1970
2118
  # Returns mouse y position
1971
2119
  #
1972
2120
  # @return [Numeric] vertical position of mouse
1973
2121
  #
1974
- def mouseY ()
1975
- @mousePos__[1]
2122
+ def mouseY()
2123
+ @pointerPos__[1]
1976
2124
  end
1977
2125
 
1978
2126
  # Returns mouse x position in previous frame
1979
2127
  #
1980
2128
  # @return [Numeric] horizontal position of mouse
1981
2129
  #
1982
- def pmouseX ()
1983
- @mousePrevPos__[0]
2130
+ def pmouseX()
2131
+ @pointerPrevPos__[0]
1984
2132
  end
1985
2133
 
1986
2134
  # Returns mouse y position in previous frame
1987
2135
  #
1988
2136
  # @return [Numeric] vertical position of mouse
1989
2137
  #
1990
- def pmouseY ()
1991
- @mousePrevPos__[1]
2138
+ def pmouseY()
2139
+ @pointerPrevPos__[1]
1992
2140
  end
1993
2141
 
1994
2142
  # Returns array of touches
1995
2143
  #
1996
2144
  # @return [Array] Touch objects
1997
2145
  #
1998
- def touches ()
2146
+ def touches()
1999
2147
  @touches__
2000
2148
  end
2001
2149
 
2150
+ # Returns vector for real world gravity
2151
+ #
2152
+ # @return [Vector] gravity vector
2153
+ #
2154
+ def motionGravity()
2155
+ @motionGravity__
2156
+ end
2157
+
2002
2158
  # Enables calling draw block on every frame.
2003
2159
  #
2004
2160
  # @return [nil] nil
2005
2161
  #
2006
- def loop ()
2162
+ def loop()
2007
2163
  @loop__ = true
2008
2164
  end
2009
2165
 
@@ -2011,7 +2167,7 @@ module RubySketch
2011
2167
  #
2012
2168
  # @return [nil] nil
2013
2169
  #
2014
- def noLoop ()
2170
+ def noLoop()
2015
2171
  @loop__ = false
2016
2172
  end
2017
2173
 
@@ -2019,7 +2175,7 @@ module RubySketch
2019
2175
  #
2020
2176
  # @return [nil] nil
2021
2177
  #
2022
- def redraw ()
2178
+ def redraw()
2023
2179
  @redraw__ = true
2024
2180
  end
2025
2181
 
@@ -2033,7 +2189,7 @@ module RubySketch
2033
2189
  #
2034
2190
  # @return [Numeric] absolute number
2035
2191
  #
2036
- def abs (value)
2192
+ def abs(value)
2037
2193
  value.abs
2038
2194
  end
2039
2195
 
@@ -2043,7 +2199,7 @@ module RubySketch
2043
2199
  #
2044
2200
  # @return [Numeric] rounded up number
2045
2201
  #
2046
- def ceil (value)
2202
+ def ceil(value)
2047
2203
  value.ceil
2048
2204
  end
2049
2205
 
@@ -2053,7 +2209,7 @@ module RubySketch
2053
2209
  #
2054
2210
  # @return [Numeric] rounded down number
2055
2211
  #
2056
- def floor (value)
2212
+ def floor(value)
2057
2213
  value.floor
2058
2214
  end
2059
2215
 
@@ -2063,7 +2219,7 @@ module RubySketch
2063
2219
  #
2064
2220
  # @return [Numeric] rounded number
2065
2221
  #
2066
- def round (value)
2222
+ def round(value)
2067
2223
  value.round
2068
2224
  end
2069
2225
 
@@ -2073,7 +2229,7 @@ module RubySketch
2073
2229
  #
2074
2230
  # @return [Numeric] result number
2075
2231
  #
2076
- def log (n)
2232
+ def log(n)
2077
2233
  Math.log n
2078
2234
  end
2079
2235
 
@@ -2083,7 +2239,7 @@ module RubySketch
2083
2239
  #
2084
2240
  # @return [Numeric] result number
2085
2241
  #
2086
- def exp (n)
2242
+ def exp(n)
2087
2243
  Math.exp n
2088
2244
  end
2089
2245
 
@@ -2094,7 +2250,7 @@ module RubySketch
2094
2250
  #
2095
2251
  # @return [Numeric] value ** exponent
2096
2252
  #
2097
- def pow (value, exponent)
2253
+ def pow(value, exponent)
2098
2254
  value ** exponent
2099
2255
  end
2100
2256
 
@@ -2104,7 +2260,7 @@ module RubySketch
2104
2260
  #
2105
2261
  # @return [Numeric] squared value
2106
2262
  #
2107
- def sq (value)
2263
+ def sq(value)
2108
2264
  value * value
2109
2265
  end
2110
2266
 
@@ -2114,7 +2270,7 @@ module RubySketch
2114
2270
  #
2115
2271
  # @return [Numeric] squared value
2116
2272
  #
2117
- def sqrt (value)
2273
+ def sqrt(value)
2118
2274
  Math.sqrt value
2119
2275
  end
2120
2276
 
@@ -2129,7 +2285,7 @@ module RubySketch
2129
2285
  #
2130
2286
  # @return [Numeric] magnitude
2131
2287
  #
2132
- def mag (*args)
2288
+ def mag(*args)
2133
2289
  x, y, z = *args
2134
2290
  case args.size
2135
2291
  when 2 then Math.sqrt x * x + y * y
@@ -2152,7 +2308,7 @@ module RubySketch
2152
2308
  #
2153
2309
  # @return [Numeric] distance between 2 points
2154
2310
  #
2155
- def dist (*args)
2311
+ def dist(*args)
2156
2312
  case args.size
2157
2313
  when 4
2158
2314
  x1, y1, x2, y2 = *args
@@ -2174,7 +2330,7 @@ module RubySketch
2174
2330
  #
2175
2331
  # @return [Numeric] normalized value between 0..1
2176
2332
  #
2177
- def norm (value, start, stop)
2333
+ def norm(value, start, stop)
2178
2334
  (value.to_f - start.to_f) / (stop.to_f - start.to_f)
2179
2335
  end
2180
2336
 
@@ -2186,7 +2342,7 @@ module RubySketch
2186
2342
  #
2187
2343
  # @return [Numeric] interporated number
2188
2344
  #
2189
- def lerp (start, stop, amount)
2345
+ def lerp(start, stop, amount)
2190
2346
  start + (stop - start) * amount
2191
2347
  end
2192
2348
 
@@ -2200,7 +2356,7 @@ module RubySketch
2200
2356
  #
2201
2357
  # @return [Numeric] mapped number
2202
2358
  #
2203
- def map (value, start1, stop1, start2, stop2)
2359
+ def map(value, start1, stop1, start2, stop2)
2204
2360
  lerp start2, stop2, norm(value, start1, stop1)
2205
2361
  end
2206
2362
 
@@ -2217,7 +2373,7 @@ module RubySketch
2217
2373
  #
2218
2374
  # @return [Numeric] minimum value
2219
2375
  #
2220
- def min (*args)
2376
+ def min(*args)
2221
2377
  args.flatten.min
2222
2378
  end
2223
2379
 
@@ -2234,7 +2390,7 @@ module RubySketch
2234
2390
  #
2235
2391
  # @return [Numeric] maximum value
2236
2392
  #
2237
- def max (*args)
2393
+ def max(*args)
2238
2394
  args.flatten.max
2239
2395
  end
2240
2396
 
@@ -2246,7 +2402,7 @@ module RubySketch
2246
2402
  #
2247
2403
  # @return [Numeric] constrained number
2248
2404
  #
2249
- def constrain (value, min, max)
2405
+ def constrain(value, min, max)
2250
2406
  value < min ? min : (value > max ? max : value)
2251
2407
  end
2252
2408
 
@@ -2256,7 +2412,7 @@ module RubySketch
2256
2412
  #
2257
2413
  # @return [Numeric] radian
2258
2414
  #
2259
- def radians (degree)
2415
+ def radians(degree)
2260
2416
  degree * DEG2RAD__
2261
2417
  end
2262
2418
 
@@ -2266,7 +2422,7 @@ module RubySketch
2266
2422
  #
2267
2423
  # @return [Numeric] degree
2268
2424
  #
2269
- def degrees (radian)
2425
+ def degrees(radian)
2270
2426
  radian * RAD2DEG__
2271
2427
  end
2272
2428
 
@@ -2276,7 +2432,7 @@ module RubySketch
2276
2432
  #
2277
2433
  # @return [Numeric] the sine
2278
2434
  #
2279
- def sin (angle)
2435
+ def sin(angle)
2280
2436
  Math.sin angle
2281
2437
  end
2282
2438
 
@@ -2286,7 +2442,7 @@ module RubySketch
2286
2442
  #
2287
2443
  # @return [Numeric] the cosine
2288
2444
  #
2289
- def cos (angle)
2445
+ def cos(angle)
2290
2446
  Math.cos angle
2291
2447
  end
2292
2448
 
@@ -2296,7 +2452,7 @@ module RubySketch
2296
2452
  #
2297
2453
  # @return [Numeric] the tangent
2298
2454
  #
2299
- def tan (angle)
2455
+ def tan(angle)
2300
2456
  Math.tan angle
2301
2457
  end
2302
2458
 
@@ -2306,7 +2462,7 @@ module RubySketch
2306
2462
  #
2307
2463
  # @return [Numeric] the arc sine
2308
2464
  #
2309
- def asin (value)
2465
+ def asin(value)
2310
2466
  Math.asin value
2311
2467
  end
2312
2468
 
@@ -2316,7 +2472,7 @@ module RubySketch
2316
2472
  #
2317
2473
  # @return [Numeric] the arc cosine
2318
2474
  #
2319
- def acos (value)
2475
+ def acos(value)
2320
2476
  Math.acos value
2321
2477
  end
2322
2478
 
@@ -2326,7 +2482,7 @@ module RubySketch
2326
2482
  #
2327
2483
  # @return [Numeric] the arc tangent
2328
2484
  #
2329
- def atan (value)
2485
+ def atan(value)
2330
2486
  Math.atan value
2331
2487
  end
2332
2488
 
@@ -2337,7 +2493,7 @@ module RubySketch
2337
2493
  #
2338
2494
  # @return [Numeric] the angle in radians
2339
2495
  #
2340
- def atan2 (y, x)
2496
+ def atan2(y, x)
2341
2497
  Math.atan2 y, x
2342
2498
  end
2343
2499
 
@@ -2353,7 +2509,7 @@ module RubySketch
2353
2509
  #
2354
2510
  # @return [Numeric] noise value (0.0..1.0)
2355
2511
  #
2356
- def noise (x, y = 0, z = 0)
2512
+ def noise(x, y = 0, z = 0)
2357
2513
  Rays.perlin(x, y, z) / 2.0 + 0.5
2358
2514
  end
2359
2515
 
@@ -2370,7 +2526,7 @@ module RubySketch
2370
2526
  #
2371
2527
  # @return [Float] random number
2372
2528
  #
2373
- def random (*args)
2529
+ def random(*args)
2374
2530
  return args.first.sample if args.first.kind_of? Array
2375
2531
  high, low = args.reverse
2376
2532
  rand (low || 0).to_f...(high || 1).to_f
@@ -2388,16 +2544,16 @@ module RubySketch
2388
2544
  #
2389
2545
  # @return [Vector] new vector
2390
2546
  #
2391
- def createVector (*args)
2392
- Vector.new *args
2547
+ def createVector(*args)
2548
+ Vector.new(*args, context: self)
2393
2549
  end
2394
2550
 
2395
2551
  # Creates a camera object as a video input device.
2396
2552
  #
2397
2553
  # @return [Capture] camera object
2398
2554
  #
2399
- def createCapture (*args)
2400
- Capture.new *args
2555
+ def createCapture(*args)
2556
+ Capture.new(*args)
2401
2557
  end
2402
2558
 
2403
2559
  # Creates a new off-screen graphics context object.
@@ -2407,7 +2563,7 @@ module RubySketch
2407
2563
  #
2408
2564
  # @return [Graphics] graphics object
2409
2565
  #
2410
- def createGraphics (width, height)
2566
+ def createGraphics(width, height)
2411
2567
  Graphics.new width, height
2412
2568
  end
2413
2569
 
@@ -2418,24 +2574,26 @@ module RubySketch
2418
2574
  #
2419
2575
  # @return [Image] loaded image object
2420
2576
  #
2421
- def loadImage (filename, extension = nil)
2577
+ def loadImage(filename, extension = nil)
2422
2578
  filename = getImage__ filename, extension if filename =~ %r|^https?://|
2423
2579
  Image.new Rays::Image.load filename
2424
2580
  end
2425
2581
 
2426
2582
  # @private
2427
- private def getImage__ (uri, ext)
2583
+ private def getImage__(uri, ext)
2428
2584
  ext ||= File.extname uri
2429
2585
  raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
2430
2586
 
2431
- tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2587
+ tmpdir = tmpdir__
2432
2588
  path = tmpdir + Digest::SHA1.hexdigest(uri)
2433
2589
  path = path.sub_ext ext
2434
2590
 
2435
2591
  unless path.file?
2436
2592
  URI.open uri do |input|
2593
+ input.set_encoding nil# disable default_internal
2437
2594
  tmpdir.mkdir unless tmpdir.directory?
2438
2595
  path.open('w') do |output|
2596
+ output.set_encoding Encoding::ASCII_8BIT
2439
2597
  while buf = input.read(2 ** 16)
2440
2598
  output.write buf
2441
2599
  end
@@ -2445,6 +2603,11 @@ module RubySketch
2445
2603
  path.to_s
2446
2604
  end
2447
2605
 
2606
+ # @private
2607
+ private def tmpdir__()
2608
+ Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2609
+ end
2610
+
2448
2611
  end# Context
2449
2612
 
2450
2613