rubysketch 0.3.15 → 0.3.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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
 
@@ -708,6 +708,10 @@ module RubySketch
708
708
  #
709
709
  class Touch
710
710
 
711
+ # Identifier of each touch
712
+ #
713
+ attr_reader :id
714
+
711
715
  # Horizontal position of touch
712
716
  #
713
717
  attr_reader :x
@@ -717,12 +721,8 @@ module RubySketch
717
721
  attr_reader :y
718
722
 
719
723
  # @private
720
- def initialize (x, y)
721
- @x, @y = x, y
722
- end
723
-
724
- def id ()
725
- raise NotImplementedError
724
+ def initialize(id, x, y)
725
+ @id, @x, @y = id, x, y
726
726
  end
727
727
 
728
728
  end# Touch
@@ -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,9 +1589,9 @@ 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
- src = img&.getInternal__ || @window__.canvas
1594
+ src = img&.getInternal__ || @window__.canvas_image
1545
1595
  @painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
1546
1596
  end
1547
1597
 
@@ -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,32 +1806,38 @@ 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
 
1767
1817
  tmpdir__.tap {|dir| FileUtils.rm_r dir.to_s if dir.directory?}
1768
1818
 
1769
1819
  @window__ = window
1770
- init__ @window__.canvas, @window__.canvas_painter.paint {background 0.8}
1771
-
1772
- @loop__ = true
1773
- @redraw__ = false
1774
- @frameCount__ = 0
1775
- @mousePos__ =
1776
- @mousePrevPos__ = [0, 0]
1777
- @mousePressed__ = false
1778
- @touches__ = []
1820
+ init__(
1821
+ @window__.canvas_image,
1822
+ @window__.canvas_painter.paint {background 0.8})
1823
+
1824
+ @loop__ = true
1825
+ @redraw__ = false
1826
+ @frameCount__ = 0
1827
+ @key__ = nil
1828
+ @keyCode__ = nil
1829
+ @keysPressed__ = Set.new
1830
+ @pointerPos__ =
1831
+ @pointerPrevPos__ = [0, 0]
1832
+ @pointersPressed__ = []
1833
+ @touches__ = []
1834
+ @motionGravity__ = createVector 0, 0
1779
1835
 
1780
1836
  @window__.before_draw = proc {beginDraw__}
1781
1837
  @window__.after_draw = proc {endDraw__}
1782
1838
 
1783
1839
  drawFrame = -> {
1784
- updateCanvas__ @window__.canvas, @window__.canvas_painter
1840
+ updateCanvas__ @window__.canvas_image, @window__.canvas_painter
1785
1841
  begin
1786
1842
  push
1787
1843
  @drawBlock__.call if @drawBlock__
@@ -1796,17 +1852,47 @@ module RubySketch
1796
1852
  @redraw__ = false
1797
1853
  drawFrame.call
1798
1854
  end
1799
- @mousePrevPos__ = @mousePos__
1855
+ @pointerPrevPos__ = @pointerPos__
1800
1856
  end
1801
1857
 
1858
+ updateKeyStates = -> event, pressed {
1859
+ @key__ = event.chars
1860
+ @keyCode__ = event.key
1861
+ if pressed != nil
1862
+ set, key = @keysPressed__, event.key
1863
+ pressed ? set.add(key) : set.delete(key)
1864
+ end
1865
+ }
1866
+
1867
+ mouseButtonMap = {
1868
+ mouse_left: LEFT,
1869
+ mouse_right: RIGHT,
1870
+ mouse_middle: CENTER
1871
+ }
1872
+
1802
1873
  updatePointerStates = -> event, pressed = nil {
1803
- @mousePos__ = @window__.to_canvas_coord event.pos.x, event.pos.y
1804
- @mousePressed__ = pressed if pressed != nil
1805
- @touches__ = event.positions.map {|pos|
1806
- Touch.new *@window__.to_canvas_coord(pos.x, pos.y)
1807
- }
1874
+ @pointerPos__ = event.pos.to_a
1875
+ @touches__ = event.pointers.map {|p| Touch.new(p.id, *p.pos.to_a)}
1876
+ if pressed != nil
1877
+ array = @pointersPressed__
1878
+ event.types
1879
+ .tap {|types| types.delete :mouse}
1880
+ .map {|type| mouseButtonMap[type] || type}
1881
+ .each {|type| pressed ? array.push(type) : array.delete(type)}
1882
+ end
1808
1883
  }
1809
1884
 
1885
+ @window__.key_down = proc do |e|
1886
+ updateKeyStates.call e, true
1887
+ @keyPressedBlock__&.call
1888
+ @keyTypedBlock__&.call if @key__ && !@key__.empty?
1889
+ end
1890
+
1891
+ @window__.key_up = proc do |e|
1892
+ updateKeyStates.call e, false
1893
+ @keyReleasedBlock__&.call
1894
+ end
1895
+
1810
1896
  @window__.pointer_down = proc do |e|
1811
1897
  updatePointerStates.call e, true
1812
1898
  (@touchStartedBlock__ || @mousePressedBlock__)&.call
@@ -1826,77 +1912,108 @@ module RubySketch
1826
1912
  updatePointerStates.call e
1827
1913
  (@touchMovedBlock__ || @mouseDraggedBlock__)&.call
1828
1914
  end
1915
+
1916
+ @window__.motion = proc do |e|
1917
+ @motionGravity__ = createVector(*e.gravity.to_a(3))
1918
+ @motionBlock__&.call
1919
+ end
1829
1920
  end
1830
1921
 
1831
- # Define setup block.
1922
+ # Defines setup block.
1832
1923
  #
1833
- def setup (&block)
1924
+ def setup(&block)
1834
1925
  @window__.setup = block
1835
1926
  nil
1836
1927
  end
1837
1928
 
1838
- # Define draw block.
1929
+ # Defines draw block.
1839
1930
  #
1840
- def draw (&block)
1931
+ def draw(&block)
1841
1932
  @drawBlock__ = block if block
1842
1933
  nil
1843
1934
  end
1844
1935
 
1845
- # @private
1846
- private def key__ (&block)
1847
- @window__.key = block
1936
+ # Defines keyPressed block.
1937
+ #
1938
+ # @return [Boolean] is any key pressed or not
1939
+ #
1940
+ def keyPressed(&block)
1941
+ @keyPressedBlock__ = block if block
1942
+ not @keysPressed__.empty?
1943
+ end
1944
+
1945
+ # Defines keyReleased block.
1946
+ #
1947
+ def keyReleased(&block)
1948
+ @keyReleasedBlock__ = block if block
1848
1949
  nil
1849
1950
  end
1850
1951
 
1851
- # Define mousePressed block.
1952
+ # Defines keyTyped block.
1852
1953
  #
1853
- def mousePressed (&block)
1954
+ def keyTyped(&block)
1955
+ @keyTypedBlock__ = block if block
1956
+ nil
1957
+ end
1958
+
1959
+ # Defines mousePressed block.
1960
+ #
1961
+ # @return [Boolean] is any mouse button pressed or not
1962
+ #
1963
+ def mousePressed(&block)
1854
1964
  @mousePressedBlock__ = block if block
1855
- @mousePressed__
1965
+ not @pointersPressed__.empty?
1856
1966
  end
1857
1967
 
1858
- # Define mouseReleased block.
1968
+ # Defines mouseReleased block.
1859
1969
  #
1860
- def mouseReleased (&block)
1970
+ def mouseReleased(&block)
1861
1971
  @mouseReleasedBlock__ = block if block
1862
1972
  nil
1863
1973
  end
1864
1974
 
1865
- # Define mouseMoved block.
1975
+ # Defines mouseMoved block.
1866
1976
  #
1867
- def mouseMoved (&block)
1977
+ def mouseMoved(&block)
1868
1978
  @mouseMovedBlock__ = block if block
1869
1979
  nil
1870
1980
  end
1871
1981
 
1872
- # Define mouseDragged block.
1982
+ # Defines mouseDragged block.
1873
1983
  #
1874
- def mouseDragged (&block)
1984
+ def mouseDragged(&block)
1875
1985
  @mouseDraggedBlock__ = block if block
1876
1986
  nil
1877
1987
  end
1878
1988
 
1879
- # Define touchStarted block.
1989
+ # Defines touchStarted block.
1880
1990
  #
1881
- def touchStarted (&block)
1991
+ def touchStarted(&block)
1882
1992
  @touchStartedBlock__ = block if block
1883
1993
  nil
1884
1994
  end
1885
1995
 
1886
- # Define touchEnded block.
1996
+ # Defines touchEnded block.
1887
1997
  #
1888
- def touchEnded (&block)
1998
+ def touchEnded(&block)
1889
1999
  @touchEndedBlock__ = block if block
1890
2000
  nil
1891
2001
  end
1892
2002
 
1893
- # Define touchMoved block.
2003
+ # Defines touchMoved block.
1894
2004
  #
1895
- def touchMoved (&block)
2005
+ def touchMoved(&block)
1896
2006
  @touchMovedBlock__ = block if block
1897
2007
  nil
1898
2008
  end
1899
2009
 
2010
+ # Defines motion block.
2011
+ #
2012
+ def motion(&block)
2013
+ @motionBlock__ = block if block
2014
+ nil
2015
+ end
2016
+
1900
2017
  # Changes canvas size.
1901
2018
  #
1902
2019
  # @param width [Integer] new width
@@ -1905,7 +2022,7 @@ module RubySketch
1905
2022
  #
1906
2023
  # @return [nil] nil
1907
2024
  #
1908
- def size (width, height, pixelDensity: self.pixelDensity)
2025
+ def size(width, height, pixelDensity: self.pixelDensity)
1909
2026
  resizeCanvas__ :size, width, height, pixelDensity
1910
2027
  nil
1911
2028
  end
@@ -1918,7 +2035,7 @@ module RubySketch
1918
2035
  #
1919
2036
  # @return [nil] nil
1920
2037
  #
1921
- def createCanvas (width, height, pixelDensity: self.pixelDensity)
2038
+ def createCanvas(width, height, pixelDensity: self.pixelDensity)
1922
2039
  resizeCanvas__ :createCanvas, width, height, pixelDensity
1923
2040
  nil
1924
2041
  end
@@ -1929,18 +2046,18 @@ module RubySketch
1929
2046
  #
1930
2047
  # @return [Numeric] current pixel density
1931
2048
  #
1932
- def pixelDensity (density = nil)
2049
+ def pixelDensity(density = nil)
1933
2050
  resizeCanvas__ :pixelDensity, width, height, density if density
1934
2051
  @painter__.pixel_density
1935
2052
  end
1936
2053
 
1937
2054
  # @private
1938
- def resizeCanvas__ (name, width, height, pixelDensity)
2055
+ def resizeCanvas__(name, width, height, pixelDensity)
1939
2056
  raise '#{name}() must be called on startup or setup block' if @started__
1940
2057
 
1941
2058
  @painter__.__send__ :end_paint
1942
2059
  @window__.__send__ :resize_canvas, width, height, pixelDensity
1943
- updateCanvas__ @window__.canvas, @window__.canvas_painter
2060
+ updateCanvas__ @window__.canvas_image, @window__.canvas_painter
1944
2061
  @painter__.__send__ :begin_paint
1945
2062
 
1946
2063
  @window__.auto_resize = false
@@ -1950,7 +2067,7 @@ module RubySketch
1950
2067
  #
1951
2068
  # @return [Numeric] pixel density
1952
2069
  #
1953
- def displayDensity ()
2070
+ def displayDensity()
1954
2071
  @window__.painter.pixel_density
1955
2072
  end
1956
2073
 
@@ -1958,7 +2075,7 @@ module RubySketch
1958
2075
  #
1959
2076
  # @return [Numeric] window width
1960
2077
  #
1961
- def windowWidth ()
2078
+ def windowWidth()
1962
2079
  @window__.width
1963
2080
  end
1964
2081
 
@@ -1966,7 +2083,7 @@ module RubySketch
1966
2083
  #
1967
2084
  # @return [Numeric] window height
1968
2085
  #
1969
- def windowHeight ()
2086
+ def windowHeight()
1970
2087
  @window__.height
1971
2088
  end
1972
2089
 
@@ -1974,7 +2091,7 @@ module RubySketch
1974
2091
  #
1975
2092
  # @return [Integer] total number of frames
1976
2093
  #
1977
- def frameCount ()
2094
+ def frameCount()
1978
2095
  @frameCount__
1979
2096
  end
1980
2097
 
@@ -1982,55 +2099,87 @@ module RubySketch
1982
2099
  #
1983
2100
  # @return [Float] frames per second
1984
2101
  #
1985
- def frameRate ()
2102
+ def frameRate()
1986
2103
  @window__.event.fps
1987
2104
  end
1988
2105
 
2106
+ # Returns the last key that was pressed or released.
2107
+ #
2108
+ # @return [String] last key
2109
+ #
2110
+ def key()
2111
+ @key__
2112
+ end
2113
+
2114
+ # Returns the last key code that was pressed or released.
2115
+ #
2116
+ # @return [Symbol] last key code
2117
+ #
2118
+ def keyCode()
2119
+ @keyCode__
2120
+ end
2121
+
1989
2122
  # Returns mouse x position
1990
2123
  #
1991
2124
  # @return [Numeric] horizontal position of mouse
1992
2125
  #
1993
- def mouseX ()
1994
- @mousePos__[0]
2126
+ def mouseX()
2127
+ @pointerPos__[0]
1995
2128
  end
1996
2129
 
1997
2130
  # Returns mouse y position
1998
2131
  #
1999
2132
  # @return [Numeric] vertical position of mouse
2000
2133
  #
2001
- def mouseY ()
2002
- @mousePos__[1]
2134
+ def mouseY()
2135
+ @pointerPos__[1]
2003
2136
  end
2004
2137
 
2005
2138
  # Returns mouse x position in previous frame
2006
2139
  #
2007
2140
  # @return [Numeric] horizontal position of mouse
2008
2141
  #
2009
- def pmouseX ()
2010
- @mousePrevPos__[0]
2142
+ def pmouseX()
2143
+ @pointerPrevPos__[0]
2011
2144
  end
2012
2145
 
2013
2146
  # Returns mouse y position in previous frame
2014
2147
  #
2015
2148
  # @return [Numeric] vertical position of mouse
2016
2149
  #
2017
- def pmouseY ()
2018
- @mousePrevPos__[1]
2150
+ def pmouseY()
2151
+ @pointerPrevPos__[1]
2152
+ end
2153
+
2154
+ # Returns which mouse button was pressed
2155
+ #
2156
+ # @return [Numeric] LEFT, RIGHT, CENTER or 0
2157
+ #
2158
+ def mouseButton()
2159
+ (@pointersPressed__ & [LEFT, RIGHT, CENTER]).last || 0
2019
2160
  end
2020
2161
 
2021
2162
  # Returns array of touches
2022
2163
  #
2023
2164
  # @return [Array] Touch objects
2024
2165
  #
2025
- def touches ()
2166
+ def touches()
2026
2167
  @touches__
2027
2168
  end
2028
2169
 
2170
+ # Returns vector for real world gravity
2171
+ #
2172
+ # @return [Vector] gravity vector
2173
+ #
2174
+ def motionGravity()
2175
+ @motionGravity__
2176
+ end
2177
+
2029
2178
  # Enables calling draw block on every frame.
2030
2179
  #
2031
2180
  # @return [nil] nil
2032
2181
  #
2033
- def loop ()
2182
+ def loop()
2034
2183
  @loop__ = true
2035
2184
  end
2036
2185
 
@@ -2038,7 +2187,7 @@ module RubySketch
2038
2187
  #
2039
2188
  # @return [nil] nil
2040
2189
  #
2041
- def noLoop ()
2190
+ def noLoop()
2042
2191
  @loop__ = false
2043
2192
  end
2044
2193
 
@@ -2046,7 +2195,7 @@ module RubySketch
2046
2195
  #
2047
2196
  # @return [nil] nil
2048
2197
  #
2049
- def redraw ()
2198
+ def redraw()
2050
2199
  @redraw__ = true
2051
2200
  end
2052
2201
 
@@ -2060,7 +2209,7 @@ module RubySketch
2060
2209
  #
2061
2210
  # @return [Numeric] absolute number
2062
2211
  #
2063
- def abs (value)
2212
+ def abs(value)
2064
2213
  value.abs
2065
2214
  end
2066
2215
 
@@ -2070,7 +2219,7 @@ module RubySketch
2070
2219
  #
2071
2220
  # @return [Numeric] rounded up number
2072
2221
  #
2073
- def ceil (value)
2222
+ def ceil(value)
2074
2223
  value.ceil
2075
2224
  end
2076
2225
 
@@ -2080,7 +2229,7 @@ module RubySketch
2080
2229
  #
2081
2230
  # @return [Numeric] rounded down number
2082
2231
  #
2083
- def floor (value)
2232
+ def floor(value)
2084
2233
  value.floor
2085
2234
  end
2086
2235
 
@@ -2090,7 +2239,7 @@ module RubySketch
2090
2239
  #
2091
2240
  # @return [Numeric] rounded number
2092
2241
  #
2093
- def round (value)
2242
+ def round(value)
2094
2243
  value.round
2095
2244
  end
2096
2245
 
@@ -2100,7 +2249,7 @@ module RubySketch
2100
2249
  #
2101
2250
  # @return [Numeric] result number
2102
2251
  #
2103
- def log (n)
2252
+ def log(n)
2104
2253
  Math.log n
2105
2254
  end
2106
2255
 
@@ -2110,7 +2259,7 @@ module RubySketch
2110
2259
  #
2111
2260
  # @return [Numeric] result number
2112
2261
  #
2113
- def exp (n)
2262
+ def exp(n)
2114
2263
  Math.exp n
2115
2264
  end
2116
2265
 
@@ -2121,7 +2270,7 @@ module RubySketch
2121
2270
  #
2122
2271
  # @return [Numeric] value ** exponent
2123
2272
  #
2124
- def pow (value, exponent)
2273
+ def pow(value, exponent)
2125
2274
  value ** exponent
2126
2275
  end
2127
2276
 
@@ -2131,7 +2280,7 @@ module RubySketch
2131
2280
  #
2132
2281
  # @return [Numeric] squared value
2133
2282
  #
2134
- def sq (value)
2283
+ def sq(value)
2135
2284
  value * value
2136
2285
  end
2137
2286
 
@@ -2141,7 +2290,7 @@ module RubySketch
2141
2290
  #
2142
2291
  # @return [Numeric] squared value
2143
2292
  #
2144
- def sqrt (value)
2293
+ def sqrt(value)
2145
2294
  Math.sqrt value
2146
2295
  end
2147
2296
 
@@ -2156,7 +2305,7 @@ module RubySketch
2156
2305
  #
2157
2306
  # @return [Numeric] magnitude
2158
2307
  #
2159
- def mag (*args)
2308
+ def mag(*args)
2160
2309
  x, y, z = *args
2161
2310
  case args.size
2162
2311
  when 2 then Math.sqrt x * x + y * y
@@ -2179,7 +2328,7 @@ module RubySketch
2179
2328
  #
2180
2329
  # @return [Numeric] distance between 2 points
2181
2330
  #
2182
- def dist (*args)
2331
+ def dist(*args)
2183
2332
  case args.size
2184
2333
  when 4
2185
2334
  x1, y1, x2, y2 = *args
@@ -2201,7 +2350,7 @@ module RubySketch
2201
2350
  #
2202
2351
  # @return [Numeric] normalized value between 0..1
2203
2352
  #
2204
- def norm (value, start, stop)
2353
+ def norm(value, start, stop)
2205
2354
  (value.to_f - start.to_f) / (stop.to_f - start.to_f)
2206
2355
  end
2207
2356
 
@@ -2213,7 +2362,7 @@ module RubySketch
2213
2362
  #
2214
2363
  # @return [Numeric] interporated number
2215
2364
  #
2216
- def lerp (start, stop, amount)
2365
+ def lerp(start, stop, amount)
2217
2366
  start + (stop - start) * amount
2218
2367
  end
2219
2368
 
@@ -2227,7 +2376,7 @@ module RubySketch
2227
2376
  #
2228
2377
  # @return [Numeric] mapped number
2229
2378
  #
2230
- def map (value, start1, stop1, start2, stop2)
2379
+ def map(value, start1, stop1, start2, stop2)
2231
2380
  lerp start2, stop2, norm(value, start1, stop1)
2232
2381
  end
2233
2382
 
@@ -2244,7 +2393,7 @@ module RubySketch
2244
2393
  #
2245
2394
  # @return [Numeric] minimum value
2246
2395
  #
2247
- def min (*args)
2396
+ def min(*args)
2248
2397
  args.flatten.min
2249
2398
  end
2250
2399
 
@@ -2261,7 +2410,7 @@ module RubySketch
2261
2410
  #
2262
2411
  # @return [Numeric] maximum value
2263
2412
  #
2264
- def max (*args)
2413
+ def max(*args)
2265
2414
  args.flatten.max
2266
2415
  end
2267
2416
 
@@ -2273,7 +2422,7 @@ module RubySketch
2273
2422
  #
2274
2423
  # @return [Numeric] constrained number
2275
2424
  #
2276
- def constrain (value, min, max)
2425
+ def constrain(value, min, max)
2277
2426
  value < min ? min : (value > max ? max : value)
2278
2427
  end
2279
2428
 
@@ -2283,7 +2432,7 @@ module RubySketch
2283
2432
  #
2284
2433
  # @return [Numeric] radian
2285
2434
  #
2286
- def radians (degree)
2435
+ def radians(degree)
2287
2436
  degree * DEG2RAD__
2288
2437
  end
2289
2438
 
@@ -2293,7 +2442,7 @@ module RubySketch
2293
2442
  #
2294
2443
  # @return [Numeric] degree
2295
2444
  #
2296
- def degrees (radian)
2445
+ def degrees(radian)
2297
2446
  radian * RAD2DEG__
2298
2447
  end
2299
2448
 
@@ -2303,7 +2452,7 @@ module RubySketch
2303
2452
  #
2304
2453
  # @return [Numeric] the sine
2305
2454
  #
2306
- def sin (angle)
2455
+ def sin(angle)
2307
2456
  Math.sin angle
2308
2457
  end
2309
2458
 
@@ -2313,7 +2462,7 @@ module RubySketch
2313
2462
  #
2314
2463
  # @return [Numeric] the cosine
2315
2464
  #
2316
- def cos (angle)
2465
+ def cos(angle)
2317
2466
  Math.cos angle
2318
2467
  end
2319
2468
 
@@ -2323,7 +2472,7 @@ module RubySketch
2323
2472
  #
2324
2473
  # @return [Numeric] the tangent
2325
2474
  #
2326
- def tan (angle)
2475
+ def tan(angle)
2327
2476
  Math.tan angle
2328
2477
  end
2329
2478
 
@@ -2333,7 +2482,7 @@ module RubySketch
2333
2482
  #
2334
2483
  # @return [Numeric] the arc sine
2335
2484
  #
2336
- def asin (value)
2485
+ def asin(value)
2337
2486
  Math.asin value
2338
2487
  end
2339
2488
 
@@ -2343,7 +2492,7 @@ module RubySketch
2343
2492
  #
2344
2493
  # @return [Numeric] the arc cosine
2345
2494
  #
2346
- def acos (value)
2495
+ def acos(value)
2347
2496
  Math.acos value
2348
2497
  end
2349
2498
 
@@ -2353,7 +2502,7 @@ module RubySketch
2353
2502
  #
2354
2503
  # @return [Numeric] the arc tangent
2355
2504
  #
2356
- def atan (value)
2505
+ def atan(value)
2357
2506
  Math.atan value
2358
2507
  end
2359
2508
 
@@ -2364,7 +2513,7 @@ module RubySketch
2364
2513
  #
2365
2514
  # @return [Numeric] the angle in radians
2366
2515
  #
2367
- def atan2 (y, x)
2516
+ def atan2(y, x)
2368
2517
  Math.atan2 y, x
2369
2518
  end
2370
2519
 
@@ -2380,7 +2529,7 @@ module RubySketch
2380
2529
  #
2381
2530
  # @return [Numeric] noise value (0.0..1.0)
2382
2531
  #
2383
- def noise (x, y = 0, z = 0)
2532
+ def noise(x, y = 0, z = 0)
2384
2533
  Rays.perlin(x, y, z) / 2.0 + 0.5
2385
2534
  end
2386
2535
 
@@ -2397,7 +2546,7 @@ module RubySketch
2397
2546
  #
2398
2547
  # @return [Float] random number
2399
2548
  #
2400
- def random (*args)
2549
+ def random(*args)
2401
2550
  return args.first.sample if args.first.kind_of? Array
2402
2551
  high, low = args.reverse
2403
2552
  rand (low || 0).to_f...(high || 1).to_f
@@ -2415,16 +2564,16 @@ module RubySketch
2415
2564
  #
2416
2565
  # @return [Vector] new vector
2417
2566
  #
2418
- def createVector (*args)
2419
- Vector.new *args
2567
+ def createVector(*args)
2568
+ Vector.new(*args, context: self)
2420
2569
  end
2421
2570
 
2422
2571
  # Creates a camera object as a video input device.
2423
2572
  #
2424
2573
  # @return [Capture] camera object
2425
2574
  #
2426
- def createCapture (*args)
2427
- Capture.new *args
2575
+ def createCapture(*args)
2576
+ Capture.new(*args)
2428
2577
  end
2429
2578
 
2430
2579
  # Creates a new off-screen graphics context object.
@@ -2434,7 +2583,7 @@ module RubySketch
2434
2583
  #
2435
2584
  # @return [Graphics] graphics object
2436
2585
  #
2437
- def createGraphics (width, height)
2586
+ def createGraphics(width, height)
2438
2587
  Graphics.new width, height
2439
2588
  end
2440
2589
 
@@ -2445,13 +2594,13 @@ module RubySketch
2445
2594
  #
2446
2595
  # @return [Image] loaded image object
2447
2596
  #
2448
- def loadImage (filename, extension = nil)
2597
+ def loadImage(filename, extension = nil)
2449
2598
  filename = getImage__ filename, extension if filename =~ %r|^https?://|
2450
2599
  Image.new Rays::Image.load filename
2451
2600
  end
2452
2601
 
2453
2602
  # @private
2454
- private def getImage__ (uri, ext)
2603
+ private def getImage__(uri, ext)
2455
2604
  ext ||= File.extname uri
2456
2605
  raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
2457
2606
 
@@ -2475,7 +2624,7 @@ module RubySketch
2475
2624
  end
2476
2625
 
2477
2626
  # @private
2478
- private def tmpdir__ ()
2627
+ private def tmpdir__()
2479
2628
  Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2480
2629
  end
2481
2630