rubysketch 0.3.13 → 0.3.17

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
884
-
885
- # Mode for textAlign().
886
- LEFT = :LEFT
887
-
888
- # Mode for textAlign().
889
- RIGHT = :RIGHT
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
890
939
 
891
940
  # Mode for textAlign().
892
- TOP = :TOP
941
+ TOP = :top
893
942
 
894
943
  # Mode for textAlign().
895
- BOTTOM = :BOTTOM
944
+ BOTTOM = :bottom
896
945
 
897
946
  # 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,30 +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
- @window__ = window
1768
- init__ @window__.canvas, @window__.canvas_painter.paint {background 0.8}
1817
+ tmpdir__.tap {|dir| FileUtils.rm_r dir.to_s if dir.directory?}
1769
1818
 
1770
- @loop__ = true
1771
- @redraw__ = false
1772
- @frameCount__ = 0
1773
- @mousePos__ =
1774
- @mousePrevPos__ = [0, 0]
1775
- @mousePressed__ = false
1776
- @touches__ = []
1819
+ @window__ = window
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__ = Set.new
1833
+ @touches__ = []
1834
+ @motionGravity__ = createVector 0, 0
1777
1835
 
1778
1836
  @window__.before_draw = proc {beginDraw__}
1779
1837
  @window__.after_draw = proc {endDraw__}
1780
1838
 
1781
1839
  drawFrame = -> {
1782
- updateCanvas__ @window__.canvas, @window__.canvas_painter
1840
+ updateCanvas__ @window__.canvas_image, @window__.canvas_painter
1783
1841
  begin
1784
1842
  push
1785
1843
  @drawBlock__.call if @drawBlock__
@@ -1794,17 +1852,38 @@ module RubySketch
1794
1852
  @redraw__ = false
1795
1853
  drawFrame.call
1796
1854
  end
1797
- @mousePrevPos__ = @mousePos__
1855
+ @pointerPrevPos__ = @pointerPos__
1798
1856
  end
1799
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
+
1800
1867
  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
- }
1868
+ @pointerPos__ = event.pos.to_a
1869
+ @touches__ = event.pointers.map {|p| Touch.new(p.id, *p.pos.to_a)}
1870
+ if pressed != nil
1871
+ set, type = @pointersPressed__, event.type
1872
+ pressed ? set.add(type) : set.delete(type)
1873
+ end
1806
1874
  }
1807
1875
 
1876
+ @window__.key_down = proc do |e|
1877
+ updateKeyStates.call e, true
1878
+ @keyPressedBlock__&.call
1879
+ @keyTypedBlock__&.call if @key__ && !@key__.empty?
1880
+ end
1881
+
1882
+ @window__.key_up = proc do |e|
1883
+ updateKeyStates.call e, false
1884
+ @keyReleasedBlock__&.call
1885
+ end
1886
+
1808
1887
  @window__.pointer_down = proc do |e|
1809
1888
  updatePointerStates.call e, true
1810
1889
  (@touchStartedBlock__ || @mousePressedBlock__)&.call
@@ -1824,62 +1903,108 @@ module RubySketch
1824
1903
  updatePointerStates.call e
1825
1904
  (@touchMovedBlock__ || @mouseDraggedBlock__)&.call
1826
1905
  end
1906
+
1907
+ @window__.motion = proc do |e|
1908
+ @motionGravity__ = createVector(*e.gravity.to_a(3))
1909
+ @motionBlock__&.call
1910
+ end
1827
1911
  end
1828
1912
 
1829
- # Define setup block.
1913
+ # Defines setup block.
1830
1914
  #
1831
- def setup (&block)
1915
+ def setup(&block)
1832
1916
  @window__.setup = block
1833
1917
  nil
1834
1918
  end
1835
1919
 
1836
- # Define draw block.
1920
+ # Defines draw block.
1837
1921
  #
1838
- def draw (&block)
1922
+ def draw(&block)
1839
1923
  @drawBlock__ = block if block
1840
1924
  nil
1841
1925
  end
1842
1926
 
1843
- def key (&block)
1844
- @window__.key = block
1927
+ # Defines keyPressed block.
1928
+ #
1929
+ # @return [Boolean] is any key pressed or not
1930
+ #
1931
+ def keyPressed(&block)
1932
+ @keyPressedBlock__ = block if block
1933
+ not @keysPressed__.empty?
1934
+ end
1935
+
1936
+ # Defines keyReleased block.
1937
+ #
1938
+ def keyReleased(&block)
1939
+ @keyReleasedBlock__ = block if block
1845
1940
  nil
1846
1941
  end
1847
1942
 
1848
- def mousePressed (&block)
1943
+ # Defines keyTyped block.
1944
+ #
1945
+ def keyTyped(&block)
1946
+ @keyTypedBlock__ = block if block
1947
+ nil
1948
+ end
1949
+
1950
+ # Defines mousePressed block.
1951
+ #
1952
+ # @return [Boolean] is any mouse button pressed or not
1953
+ #
1954
+ def mousePressed(&block)
1849
1955
  @mousePressedBlock__ = block if block
1850
- @mousePressed__
1956
+ not @pointersPressed__.empty?
1851
1957
  end
1852
1958
 
1853
- def mouseReleased (&block)
1959
+ # Defines mouseReleased block.
1960
+ #
1961
+ def mouseReleased(&block)
1854
1962
  @mouseReleasedBlock__ = block if block
1855
1963
  nil
1856
1964
  end
1857
1965
 
1858
- def mouseMoved (&block)
1966
+ # Defines mouseMoved block.
1967
+ #
1968
+ def mouseMoved(&block)
1859
1969
  @mouseMovedBlock__ = block if block
1860
1970
  nil
1861
1971
  end
1862
1972
 
1863
- def mouseDragged (&block)
1973
+ # Defines mouseDragged block.
1974
+ #
1975
+ def mouseDragged(&block)
1864
1976
  @mouseDraggedBlock__ = block if block
1865
1977
  nil
1866
1978
  end
1867
1979
 
1868
- def touchStarted (&block)
1980
+ # Defines touchStarted block.
1981
+ #
1982
+ def touchStarted(&block)
1869
1983
  @touchStartedBlock__ = block if block
1870
1984
  nil
1871
1985
  end
1872
1986
 
1873
- def touchEnded (&block)
1987
+ # Defines touchEnded block.
1988
+ #
1989
+ def touchEnded(&block)
1874
1990
  @touchEndedBlock__ = block if block
1875
1991
  nil
1876
1992
  end
1877
1993
 
1878
- def touchMoved (&block)
1994
+ # Defines touchMoved block.
1995
+ #
1996
+ def touchMoved(&block)
1879
1997
  @touchMovedBlock__ = block if block
1880
1998
  nil
1881
1999
  end
1882
2000
 
2001
+ # Defines motion block.
2002
+ #
2003
+ def motion(&block)
2004
+ @motionBlock__ = block if block
2005
+ nil
2006
+ end
2007
+
1883
2008
  # Changes canvas size.
1884
2009
  #
1885
2010
  # @param width [Integer] new width
@@ -1888,7 +2013,7 @@ module RubySketch
1888
2013
  #
1889
2014
  # @return [nil] nil
1890
2015
  #
1891
- def size (width, height, pixelDensity: self.pixelDensity)
2016
+ def size(width, height, pixelDensity: self.pixelDensity)
1892
2017
  resizeCanvas__ :size, width, height, pixelDensity
1893
2018
  nil
1894
2019
  end
@@ -1901,7 +2026,7 @@ module RubySketch
1901
2026
  #
1902
2027
  # @return [nil] nil
1903
2028
  #
1904
- def createCanvas (width, height, pixelDensity: self.pixelDensity)
2029
+ def createCanvas(width, height, pixelDensity: self.pixelDensity)
1905
2030
  resizeCanvas__ :createCanvas, width, height, pixelDensity
1906
2031
  nil
1907
2032
  end
@@ -1912,18 +2037,18 @@ module RubySketch
1912
2037
  #
1913
2038
  # @return [Numeric] current pixel density
1914
2039
  #
1915
- def pixelDensity (density = nil)
2040
+ def pixelDensity(density = nil)
1916
2041
  resizeCanvas__ :pixelDensity, width, height, density if density
1917
2042
  @painter__.pixel_density
1918
2043
  end
1919
2044
 
1920
2045
  # @private
1921
- def resizeCanvas__ (name, width, height, pixelDensity)
2046
+ def resizeCanvas__(name, width, height, pixelDensity)
1922
2047
  raise '#{name}() must be called on startup or setup block' if @started__
1923
2048
 
1924
2049
  @painter__.__send__ :end_paint
1925
2050
  @window__.__send__ :resize_canvas, width, height, pixelDensity
1926
- updateCanvas__ @window__.canvas, @window__.canvas_painter
2051
+ updateCanvas__ @window__.canvas_image, @window__.canvas_painter
1927
2052
  @painter__.__send__ :begin_paint
1928
2053
 
1929
2054
  @window__.auto_resize = false
@@ -1933,15 +2058,23 @@ module RubySketch
1933
2058
  #
1934
2059
  # @return [Numeric] pixel density
1935
2060
  #
1936
- def displayDensity ()
2061
+ def displayDensity()
1937
2062
  @window__.painter.pixel_density
1938
2063
  end
1939
2064
 
1940
- def windowWidth ()
2065
+ # Returns window width.
2066
+ #
2067
+ # @return [Numeric] window width
2068
+ #
2069
+ def windowWidth()
1941
2070
  @window__.width
1942
2071
  end
1943
2072
 
1944
- def windowHeight ()
2073
+ # Returns window height.
2074
+ #
2075
+ # @return [Numeric] window height
2076
+ #
2077
+ def windowHeight()
1945
2078
  @window__.height
1946
2079
  end
1947
2080
 
@@ -1949,7 +2082,7 @@ module RubySketch
1949
2082
  #
1950
2083
  # @return [Integer] total number of frames
1951
2084
  #
1952
- def frameCount ()
2085
+ def frameCount()
1953
2086
  @frameCount__
1954
2087
  end
1955
2088
 
@@ -1957,55 +2090,79 @@ module RubySketch
1957
2090
  #
1958
2091
  # @return [Float] frames per second
1959
2092
  #
1960
- def frameRate ()
2093
+ def frameRate()
1961
2094
  @window__.event.fps
1962
2095
  end
1963
2096
 
2097
+ # Returns the last key that was pressed or released.
2098
+ #
2099
+ # @return [String] last key
2100
+ #
2101
+ def key()
2102
+ @key__
2103
+ end
2104
+
2105
+ # Returns the last key code that was pressed or released.
2106
+ #
2107
+ # @return [Symbol] last key code
2108
+ #
2109
+ def keyCode()
2110
+ @keyCode__
2111
+ end
2112
+
1964
2113
  # Returns mouse x position
1965
2114
  #
1966
2115
  # @return [Numeric] horizontal position of mouse
1967
2116
  #
1968
- def mouseX ()
1969
- @mousePos__[0]
2117
+ def mouseX()
2118
+ @pointerPos__[0]
1970
2119
  end
1971
2120
 
1972
2121
  # Returns mouse y position
1973
2122
  #
1974
2123
  # @return [Numeric] vertical position of mouse
1975
2124
  #
1976
- def mouseY ()
1977
- @mousePos__[1]
2125
+ def mouseY()
2126
+ @pointerPos__[1]
1978
2127
  end
1979
2128
 
1980
2129
  # Returns mouse x position in previous frame
1981
2130
  #
1982
2131
  # @return [Numeric] horizontal position of mouse
1983
2132
  #
1984
- def pmouseX ()
1985
- @mousePrevPos__[0]
2133
+ def pmouseX()
2134
+ @pointerPrevPos__[0]
1986
2135
  end
1987
2136
 
1988
2137
  # Returns mouse y position in previous frame
1989
2138
  #
1990
2139
  # @return [Numeric] vertical position of mouse
1991
2140
  #
1992
- def pmouseY ()
1993
- @mousePrevPos__[1]
2141
+ def pmouseY()
2142
+ @pointerPrevPos__[1]
1994
2143
  end
1995
2144
 
1996
2145
  # Returns array of touches
1997
2146
  #
1998
2147
  # @return [Array] Touch objects
1999
2148
  #
2000
- def touches ()
2149
+ def touches()
2001
2150
  @touches__
2002
2151
  end
2003
2152
 
2153
+ # Returns vector for real world gravity
2154
+ #
2155
+ # @return [Vector] gravity vector
2156
+ #
2157
+ def motionGravity()
2158
+ @motionGravity__
2159
+ end
2160
+
2004
2161
  # Enables calling draw block on every frame.
2005
2162
  #
2006
2163
  # @return [nil] nil
2007
2164
  #
2008
- def loop ()
2165
+ def loop()
2009
2166
  @loop__ = true
2010
2167
  end
2011
2168
 
@@ -2013,7 +2170,7 @@ module RubySketch
2013
2170
  #
2014
2171
  # @return [nil] nil
2015
2172
  #
2016
- def noLoop ()
2173
+ def noLoop()
2017
2174
  @loop__ = false
2018
2175
  end
2019
2176
 
@@ -2021,7 +2178,7 @@ module RubySketch
2021
2178
  #
2022
2179
  # @return [nil] nil
2023
2180
  #
2024
- def redraw ()
2181
+ def redraw()
2025
2182
  @redraw__ = true
2026
2183
  end
2027
2184
 
@@ -2035,7 +2192,7 @@ module RubySketch
2035
2192
  #
2036
2193
  # @return [Numeric] absolute number
2037
2194
  #
2038
- def abs (value)
2195
+ def abs(value)
2039
2196
  value.abs
2040
2197
  end
2041
2198
 
@@ -2045,7 +2202,7 @@ module RubySketch
2045
2202
  #
2046
2203
  # @return [Numeric] rounded up number
2047
2204
  #
2048
- def ceil (value)
2205
+ def ceil(value)
2049
2206
  value.ceil
2050
2207
  end
2051
2208
 
@@ -2055,7 +2212,7 @@ module RubySketch
2055
2212
  #
2056
2213
  # @return [Numeric] rounded down number
2057
2214
  #
2058
- def floor (value)
2215
+ def floor(value)
2059
2216
  value.floor
2060
2217
  end
2061
2218
 
@@ -2065,7 +2222,7 @@ module RubySketch
2065
2222
  #
2066
2223
  # @return [Numeric] rounded number
2067
2224
  #
2068
- def round (value)
2225
+ def round(value)
2069
2226
  value.round
2070
2227
  end
2071
2228
 
@@ -2075,7 +2232,7 @@ module RubySketch
2075
2232
  #
2076
2233
  # @return [Numeric] result number
2077
2234
  #
2078
- def log (n)
2235
+ def log(n)
2079
2236
  Math.log n
2080
2237
  end
2081
2238
 
@@ -2085,7 +2242,7 @@ module RubySketch
2085
2242
  #
2086
2243
  # @return [Numeric] result number
2087
2244
  #
2088
- def exp (n)
2245
+ def exp(n)
2089
2246
  Math.exp n
2090
2247
  end
2091
2248
 
@@ -2096,7 +2253,7 @@ module RubySketch
2096
2253
  #
2097
2254
  # @return [Numeric] value ** exponent
2098
2255
  #
2099
- def pow (value, exponent)
2256
+ def pow(value, exponent)
2100
2257
  value ** exponent
2101
2258
  end
2102
2259
 
@@ -2106,7 +2263,7 @@ module RubySketch
2106
2263
  #
2107
2264
  # @return [Numeric] squared value
2108
2265
  #
2109
- def sq (value)
2266
+ def sq(value)
2110
2267
  value * value
2111
2268
  end
2112
2269
 
@@ -2116,7 +2273,7 @@ module RubySketch
2116
2273
  #
2117
2274
  # @return [Numeric] squared value
2118
2275
  #
2119
- def sqrt (value)
2276
+ def sqrt(value)
2120
2277
  Math.sqrt value
2121
2278
  end
2122
2279
 
@@ -2131,7 +2288,7 @@ module RubySketch
2131
2288
  #
2132
2289
  # @return [Numeric] magnitude
2133
2290
  #
2134
- def mag (*args)
2291
+ def mag(*args)
2135
2292
  x, y, z = *args
2136
2293
  case args.size
2137
2294
  when 2 then Math.sqrt x * x + y * y
@@ -2154,7 +2311,7 @@ module RubySketch
2154
2311
  #
2155
2312
  # @return [Numeric] distance between 2 points
2156
2313
  #
2157
- def dist (*args)
2314
+ def dist(*args)
2158
2315
  case args.size
2159
2316
  when 4
2160
2317
  x1, y1, x2, y2 = *args
@@ -2176,7 +2333,7 @@ module RubySketch
2176
2333
  #
2177
2334
  # @return [Numeric] normalized value between 0..1
2178
2335
  #
2179
- def norm (value, start, stop)
2336
+ def norm(value, start, stop)
2180
2337
  (value.to_f - start.to_f) / (stop.to_f - start.to_f)
2181
2338
  end
2182
2339
 
@@ -2188,7 +2345,7 @@ module RubySketch
2188
2345
  #
2189
2346
  # @return [Numeric] interporated number
2190
2347
  #
2191
- def lerp (start, stop, amount)
2348
+ def lerp(start, stop, amount)
2192
2349
  start + (stop - start) * amount
2193
2350
  end
2194
2351
 
@@ -2202,7 +2359,7 @@ module RubySketch
2202
2359
  #
2203
2360
  # @return [Numeric] mapped number
2204
2361
  #
2205
- def map (value, start1, stop1, start2, stop2)
2362
+ def map(value, start1, stop1, start2, stop2)
2206
2363
  lerp start2, stop2, norm(value, start1, stop1)
2207
2364
  end
2208
2365
 
@@ -2219,7 +2376,7 @@ module RubySketch
2219
2376
  #
2220
2377
  # @return [Numeric] minimum value
2221
2378
  #
2222
- def min (*args)
2379
+ def min(*args)
2223
2380
  args.flatten.min
2224
2381
  end
2225
2382
 
@@ -2236,7 +2393,7 @@ module RubySketch
2236
2393
  #
2237
2394
  # @return [Numeric] maximum value
2238
2395
  #
2239
- def max (*args)
2396
+ def max(*args)
2240
2397
  args.flatten.max
2241
2398
  end
2242
2399
 
@@ -2248,7 +2405,7 @@ module RubySketch
2248
2405
  #
2249
2406
  # @return [Numeric] constrained number
2250
2407
  #
2251
- def constrain (value, min, max)
2408
+ def constrain(value, min, max)
2252
2409
  value < min ? min : (value > max ? max : value)
2253
2410
  end
2254
2411
 
@@ -2258,7 +2415,7 @@ module RubySketch
2258
2415
  #
2259
2416
  # @return [Numeric] radian
2260
2417
  #
2261
- def radians (degree)
2418
+ def radians(degree)
2262
2419
  degree * DEG2RAD__
2263
2420
  end
2264
2421
 
@@ -2268,7 +2425,7 @@ module RubySketch
2268
2425
  #
2269
2426
  # @return [Numeric] degree
2270
2427
  #
2271
- def degrees (radian)
2428
+ def degrees(radian)
2272
2429
  radian * RAD2DEG__
2273
2430
  end
2274
2431
 
@@ -2278,7 +2435,7 @@ module RubySketch
2278
2435
  #
2279
2436
  # @return [Numeric] the sine
2280
2437
  #
2281
- def sin (angle)
2438
+ def sin(angle)
2282
2439
  Math.sin angle
2283
2440
  end
2284
2441
 
@@ -2288,7 +2445,7 @@ module RubySketch
2288
2445
  #
2289
2446
  # @return [Numeric] the cosine
2290
2447
  #
2291
- def cos (angle)
2448
+ def cos(angle)
2292
2449
  Math.cos angle
2293
2450
  end
2294
2451
 
@@ -2298,7 +2455,7 @@ module RubySketch
2298
2455
  #
2299
2456
  # @return [Numeric] the tangent
2300
2457
  #
2301
- def tan (angle)
2458
+ def tan(angle)
2302
2459
  Math.tan angle
2303
2460
  end
2304
2461
 
@@ -2308,7 +2465,7 @@ module RubySketch
2308
2465
  #
2309
2466
  # @return [Numeric] the arc sine
2310
2467
  #
2311
- def asin (value)
2468
+ def asin(value)
2312
2469
  Math.asin value
2313
2470
  end
2314
2471
 
@@ -2318,7 +2475,7 @@ module RubySketch
2318
2475
  #
2319
2476
  # @return [Numeric] the arc cosine
2320
2477
  #
2321
- def acos (value)
2478
+ def acos(value)
2322
2479
  Math.acos value
2323
2480
  end
2324
2481
 
@@ -2328,7 +2485,7 @@ module RubySketch
2328
2485
  #
2329
2486
  # @return [Numeric] the arc tangent
2330
2487
  #
2331
- def atan (value)
2488
+ def atan(value)
2332
2489
  Math.atan value
2333
2490
  end
2334
2491
 
@@ -2339,7 +2496,7 @@ module RubySketch
2339
2496
  #
2340
2497
  # @return [Numeric] the angle in radians
2341
2498
  #
2342
- def atan2 (y, x)
2499
+ def atan2(y, x)
2343
2500
  Math.atan2 y, x
2344
2501
  end
2345
2502
 
@@ -2355,7 +2512,7 @@ module RubySketch
2355
2512
  #
2356
2513
  # @return [Numeric] noise value (0.0..1.0)
2357
2514
  #
2358
- def noise (x, y = 0, z = 0)
2515
+ def noise(x, y = 0, z = 0)
2359
2516
  Rays.perlin(x, y, z) / 2.0 + 0.5
2360
2517
  end
2361
2518
 
@@ -2372,7 +2529,7 @@ module RubySketch
2372
2529
  #
2373
2530
  # @return [Float] random number
2374
2531
  #
2375
- def random (*args)
2532
+ def random(*args)
2376
2533
  return args.first.sample if args.first.kind_of? Array
2377
2534
  high, low = args.reverse
2378
2535
  rand (low || 0).to_f...(high || 1).to_f
@@ -2390,16 +2547,16 @@ module RubySketch
2390
2547
  #
2391
2548
  # @return [Vector] new vector
2392
2549
  #
2393
- def createVector (*args)
2394
- Vector.new *args
2550
+ def createVector(*args)
2551
+ Vector.new(*args, context: self)
2395
2552
  end
2396
2553
 
2397
2554
  # Creates a camera object as a video input device.
2398
2555
  #
2399
2556
  # @return [Capture] camera object
2400
2557
  #
2401
- def createCapture (*args)
2402
- Capture.new *args
2558
+ def createCapture(*args)
2559
+ Capture.new(*args)
2403
2560
  end
2404
2561
 
2405
2562
  # Creates a new off-screen graphics context object.
@@ -2409,7 +2566,7 @@ module RubySketch
2409
2566
  #
2410
2567
  # @return [Graphics] graphics object
2411
2568
  #
2412
- def createGraphics (width, height)
2569
+ def createGraphics(width, height)
2413
2570
  Graphics.new width, height
2414
2571
  end
2415
2572
 
@@ -2420,24 +2577,26 @@ module RubySketch
2420
2577
  #
2421
2578
  # @return [Image] loaded image object
2422
2579
  #
2423
- def loadImage (filename, extension = nil)
2580
+ def loadImage(filename, extension = nil)
2424
2581
  filename = getImage__ filename, extension if filename =~ %r|^https?://|
2425
2582
  Image.new Rays::Image.load filename
2426
2583
  end
2427
2584
 
2428
2585
  # @private
2429
- private def getImage__ (uri, ext)
2586
+ private def getImage__(uri, ext)
2430
2587
  ext ||= File.extname uri
2431
2588
  raise "unsupported image type -- #{ext}" unless ext =~ /^\.?(png)$/i
2432
2589
 
2433
- tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2590
+ tmpdir = tmpdir__
2434
2591
  path = tmpdir + Digest::SHA1.hexdigest(uri)
2435
2592
  path = path.sub_ext ext
2436
2593
 
2437
2594
  unless path.file?
2438
2595
  URI.open uri do |input|
2596
+ input.set_encoding nil# disable default_internal
2439
2597
  tmpdir.mkdir unless tmpdir.directory?
2440
2598
  path.open('w') do |output|
2599
+ output.set_encoding Encoding::ASCII_8BIT
2441
2600
  while buf = input.read(2 ** 16)
2442
2601
  output.write buf
2443
2602
  end
@@ -2447,6 +2606,11 @@ module RubySketch
2447
2606
  path.to_s
2448
2607
  end
2449
2608
 
2609
+ # @private
2610
+ private def tmpdir__()
2611
+ Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
2612
+ end
2613
+
2450
2614
  end# Context
2451
2615
 
2452
2616