rubysketch 0.1.5 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ab78b7cd8bbbeedb31234160054509bd9c55de56bce0f7a9251f75fbe60d22b
4
- data.tar.gz: 555fff0c98f8d55cc71fcc6923a63ffa521a591a169abd2a8b71901e8dd7401c
3
+ metadata.gz: 9603661dbdced67794a2d3f89a4e18e1314fc84daeb8721d5d05c47b5a935cd1
4
+ data.tar.gz: 8793bbe339b07ed9d0af7b82e87fc62a92470ce17ff7c9432215adc38c31f74e
5
5
  SHA512:
6
- metadata.gz: 0a7d3a9f9b3b229eae77484d7a9b59813fee6eb5f8225264e515f5dd131914c642fa9fcf1a3eb28839a65f395b24afe1c7acefc46a36e877fd3babc6fd9c6d6e
7
- data.tar.gz: e7239c1aa04a9ee8ecec79df21efd4c9dbc5004c7814b58a35799abb33ce253f10279a5c899a6442ec3a7553956c13497e452f2da36ad50ad20265f0d1ac7bf5
6
+ metadata.gz: 6ea4ed4be93eb7a1e2c2b5f187c59b8a7ea035e7ab80f2f3ec00ecd5c7f14861d55a76188e80251dc8efd3b48c8eb2d91217ff3da8e625885a4e55dcee5d76d9
7
+ data.tar.gz: 71b9f02e1e7a1bcba29ebbbff0d10a4ce0af9f7e12231014300ec23c5f26b15df288975e27ae683f033ba71f09d82ff39db1a0aef72700f34d87bf9474b2d0ba
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.2.4
@@ -0,0 +1,13 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ icon = loadImage 'https://xord.org/rubysketch/rubysketch.png'
9
+
10
+ draw do
11
+ background 0, 10
12
+ image icon, mouseX, mouseY, icon.width / 10, icon.height / 10
13
+ end
@@ -1,3 +1,8 @@
1
+ require 'digest/sha1'
2
+ require 'pathname'
3
+ require 'tmpdir'
4
+ require 'open-uri'
5
+
1
6
  require 'reflex'
2
7
  require 'rubysketch/module'
3
8
  require 'rubysketch/starter'
@@ -2,7 +2,7 @@ module RubySketch
2
2
 
3
3
 
4
4
  # @private
5
- class GLSL1
5
+ class GLSL
6
6
 
7
7
  extend Starter
8
8
 
@@ -27,10 +27,7 @@ module RubySketch
27
27
  end
28
28
  end
29
29
 
30
- end# GLSL1
31
-
32
-
33
- GLSL = GLSL1
30
+ end# GLSL
34
31
 
35
32
 
36
33
  end# RubySketch
@@ -1,9 +1,9 @@
1
1
  module RubySketch
2
2
 
3
3
 
4
- # Processing compatible API v1
4
+ # Processing compatible API
5
5
  #
6
- class Processing1
6
+ class Processing
7
7
 
8
8
  include Math
9
9
 
@@ -43,11 +43,12 @@ module RubySketch
43
43
 
44
44
  # @private
45
45
  def initialize ()
46
+ @matrixStack__ = []
47
+ @styleStack__ = []
46
48
  @frameCount__ = 0
47
49
  @hsbColor__ = false
48
50
  @colorMaxes__ = [1.0] * 4
49
51
  @angleScale__ = 1.0
50
- @styleStack = []
51
52
  @mouseX__ =
52
53
  @mouseY__ =
53
54
  @mousePrevX__ =
@@ -293,12 +294,17 @@ module RubySketch
293
294
  end
294
295
 
295
296
  # @private
296
- def setupDrawBlock__ ()
297
+ private def setupDrawBlock__ ()
297
298
  @window__.draw = proc do |e, painter|
298
299
  @painter__ = painter
299
- pushStyle
300
- @drawBlock__.call e if @drawBlock__
301
- popStyle
300
+ @matrixStack__.clear
301
+ @styleStack__.clear
302
+ begin
303
+ push
304
+ @drawBlock__.call e if @drawBlock__
305
+ ensure
306
+ pop
307
+ end
302
308
  @frameCount__ += 1
303
309
  updateMousePrevPos__
304
310
  end
@@ -317,7 +323,7 @@ module RubySketch
317
323
  end
318
324
 
319
325
  # @private
320
- def setupMousePressedBlock__ ()
326
+ private def setupMousePressedBlock__ ()
321
327
  @window__.pointer_down = proc do |e|
322
328
  updateMouseState__ e.x, e.y, true
323
329
  @mousePressedBlock__.call e if @mousePressedBlock__
@@ -330,7 +336,7 @@ module RubySketch
330
336
  end
331
337
 
332
338
  # @private
333
- def setupMouseReleasedBlock__ ()
339
+ private def setupMouseReleasedBlock__ ()
334
340
  @window__.pointer_up = proc do |e|
335
341
  updateMouseState__ e.x, e.y, false
336
342
  @mouseReleasedBlock__.call e if @mouseReleasedBlock__
@@ -343,7 +349,7 @@ module RubySketch
343
349
  end
344
350
 
345
351
  # @private
346
- def setupMouseMovedBlock__ ()
352
+ private def setupMouseMovedBlock__ ()
347
353
  @window__.pointer_move = proc do |e|
348
354
  updateMouseState__ e.x, e.y
349
355
  @mouseMovedBlock__.call e if @mouseMovedBlock__
@@ -356,7 +362,7 @@ module RubySketch
356
362
  end
357
363
 
358
364
  # @private
359
- def setupMouseDraggedBlock__ ()
365
+ private def setupMouseDraggedBlock__ ()
360
366
  @window__.pointer_drag = proc do |e|
361
367
  updateMouseState__ e.x, e.y
362
368
  @mouseDraggedBlock__.call e if @mouseDraggedBlock__
@@ -369,20 +375,20 @@ module RubySketch
369
375
  end
370
376
 
371
377
  # @private
372
- def updateMouseState__ (x, y, pressed = nil)
378
+ private def updateMouseState__ (x, y, pressed = nil)
373
379
  @mouseX__ = x
374
380
  @mouseY__ = y
375
381
  @mousePressed__ = pressed if pressed != nil
376
382
  end
377
383
 
378
384
  # @private
379
- def updateMousePrevPos__ ()
385
+ private def updateMousePrevPos__ ()
380
386
  @mousePrevX__ = @mouseX__
381
387
  @mousePrevY__ = @mouseY__
382
388
  end
383
389
 
384
390
  # @private
385
- def size__ (width, height)
391
+ private def size__ (width, height)
386
392
  raise 'size() must be called on startup or setup block' if @started__
387
393
 
388
394
  @painter__.send :end_paint
@@ -481,7 +487,7 @@ module RubySketch
481
487
  # @return [nil] nil
482
488
  #
483
489
  def colorMode (mode, *maxes)
484
- raise ArgumentError, "Invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
490
+ raise ArgumentError, "invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
485
491
  raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)
486
492
 
487
493
  @hsbColor__ = mode.upcase == HSB
@@ -493,9 +499,9 @@ module RubySketch
493
499
  end
494
500
 
495
501
  # @private
496
- def to_rgba__ (*args)
502
+ private def toRGBA__ (*args)
497
503
  a, b, c, d = args
498
- return parse_color__(a, b || alphaMax__) if a.kind_of?(String)
504
+ return parseColor__(a, b || alphaMax__) if a.kind_of?(String)
499
505
 
500
506
  rgba = case args.size
501
507
  when 1, 2 then [a, a, a, b || alphaMax__]
@@ -508,16 +514,16 @@ module RubySketch
508
514
  end
509
515
 
510
516
  # @private
511
- def parse_color__ (str, alpha)
517
+ private def parseColor__ (str, alpha)
512
518
  result = str.match /^\s*##{'([0-9a-f]{2})' * 3}\s*$/i
513
- raise ArgumentError, "Invalid color code: '#{str}'" unless result
519
+ raise ArgumentError, "invalid color code: '#{str}'" unless result
514
520
 
515
521
  rgb = result[1..3].map.with_index {|hex, i| hex.to_i(16) / 255.0}
516
522
  return *rgb, (alpha / alphaMax__)
517
523
  end
518
524
 
519
525
  # @private
520
- def alphaMax__ ()
526
+ private def alphaMax__ ()
521
527
  @colorMaxes__[3]
522
528
  end
523
529
 
@@ -531,13 +537,13 @@ module RubySketch
531
537
  @angleScale__ = case mode
532
538
  when RADIANS then RAD2DEG__
533
539
  when DEGREES then 1.0
534
- else raise ArgumentError, "Invalid angle mode: #{mode}"
540
+ else raise ArgumentError, "invalid angle mode: #{mode}"
535
541
  end
536
542
  nil
537
543
  end
538
544
 
539
545
  # @private
540
- def to_angle__ (angle)
546
+ private def toAngle__ (angle)
541
547
  angle * @angleScale__
542
548
  end
543
549
 
@@ -572,7 +578,7 @@ module RubySketch
572
578
  end
573
579
 
574
580
  # @private
575
- def to_xywh__ (mode, a, b, c, d)
581
+ private def toXYWH__ (mode, a, b, c, d)
576
582
  case mode
577
583
  when CORNER then [a, b, c, d]
578
584
  when CORNERS then [a, b, c - a, d - b]
@@ -601,7 +607,7 @@ module RubySketch
601
607
  # @return [nil] nil
602
608
  #
603
609
  def background (*args)
604
- rgba = to_rgba__ *args
610
+ rgba = toRGBA__ *args
605
611
  if rgba[3] == 1
606
612
  @painter__.background *rgba
607
613
  else
@@ -631,7 +637,7 @@ module RubySketch
631
637
  # @return [nil] nil
632
638
  #
633
639
  def fill (*args)
634
- @painter__.fill(*to_rgba__(*args))
640
+ @painter__.fill(*toRGBA__(*args))
635
641
  nil
636
642
  end
637
643
 
@@ -654,7 +660,7 @@ module RubySketch
654
660
  # @return [nil] nil
655
661
  #
656
662
  def stroke (*args)
657
- @painter__.stroke(*to_rgba__(*args))
663
+ @painter__.stroke(*toRGBA__(*args))
658
664
  nil
659
665
  end
660
666
 
@@ -757,7 +763,7 @@ module RubySketch
757
763
  # @return [nil] nil
758
764
  #
759
765
  def rect (a, b, c, d, *args)
760
- x, y, w, h = to_xywh__ @rectMode__, a, b, c, d
766
+ x, y, w, h = toXYWH__ @rectMode__, a, b, c, d
761
767
  case args.size
762
768
  when 0 then @painter__.rect x, y, w, h
763
769
  when 1 then @painter__.rect x, y, w, h, round: args[0]
@@ -777,7 +783,7 @@ module RubySketch
777
783
  # @return [nil] nil
778
784
  #
779
785
  def ellipse (a, b, c, d)
780
- x, y, w, h = to_xywh__ @ellipseMode__, a, b, c, d
786
+ x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
781
787
  @painter__.ellipse x, y, w, h
782
788
  nil
783
789
  end
@@ -806,9 +812,9 @@ module RubySketch
806
812
  # @return [nil] nil
807
813
  #
808
814
  def arc (a, b, c, d, start, stop)
809
- x, y, w, h = to_xywh__ @ellipseMode__, a, b, c, d
810
- start = to_angle__ start
811
- stop = to_angle__ stop
815
+ x, y, w, h = toXYWH__ @ellipseMode__, a, b, c, d
816
+ start = toAngle__ start
817
+ stop = toAngle__ stop
812
818
  @painter__.ellipse x, y, w, h, from: start, to: stop
813
819
  nil
814
820
  end
@@ -875,6 +881,26 @@ module RubySketch
875
881
  nil
876
882
  end
877
883
 
884
+ # Draws an image.
885
+ #
886
+ # @overload image(img, x, y)
887
+ # @overload image(img, x, y, w, h)
888
+ #
889
+ # @param img [Image] image to draw
890
+ # @param x [Numeric] horizontal position of the image
891
+ # @param y [Numeric] vertical position of the image
892
+ # @param w [Numeric] width of the image
893
+ # @param h [Numeric] height of the image
894
+ #
895
+ # @return [nil] nil
896
+ #
897
+ def image (img, x, y, w = nil, h = nil)
898
+ w ||= img.width
899
+ h ||= img.height
900
+ @painter__.image img.internal, x, y, w, h
901
+ nil
902
+ end
903
+
878
904
  # Applies translation matrix to current transformation matrix.
879
905
  #
880
906
  # @param x [Numeric] horizontal transformation
@@ -910,7 +936,7 @@ module RubySketch
910
936
  # @return [nil] nil
911
937
  #
912
938
  def rotate (angle)
913
- @painter__.rotate to_angle__ angle
939
+ @painter__.rotate toAngle__ angle
914
940
  nil
915
941
  end
916
942
 
@@ -919,7 +945,7 @@ module RubySketch
919
945
  # @return [nil] nil
920
946
  #
921
947
  def pushMatrix ()
922
- @painter__.push_matrix
948
+ @matrixStack__.push @painter__.matrix
923
949
  nil
924
950
  end
925
951
 
@@ -928,7 +954,8 @@ module RubySketch
928
954
  # @return [nil] nil
929
955
  #
930
956
  def popMatrix ()
931
- @painter__.pop_matrix
957
+ raise "matrix stack underflow" if @matrixStack__.empty?
958
+ @painter__.matrix = @matrixStack__.pop
932
959
  nil
933
960
  end
934
961
 
@@ -946,8 +973,11 @@ module RubySketch
946
973
  # @return [nil] nil
947
974
  #
948
975
  def pushStyle ()
949
- @painter__.push_state
950
- @styleStack.push [
976
+ @styleStack__.push [
977
+ @painter__.fill,
978
+ @painter__.stroke,
979
+ @painter__.stroke_width,
980
+ @painter__.font,
951
981
  @hsbColor__,
952
982
  @colorMaxes__,
953
983
  @angleScale__,
@@ -962,9 +992,16 @@ module RubySketch
962
992
  # @return [nil] nil
963
993
  #
964
994
  def popStyle ()
965
- @painter__.pop_state
966
- @hsbColor__, @colorMaxes__, @angleScale__, @rectMode__, @ellipseMode__ =
967
- @styleStack.pop
995
+ raise "style stack underflow" if @styleStack__.empty?
996
+ @painter__.fill,
997
+ @painter__.stroke,
998
+ @painter__.stroke_width,
999
+ @painter__.font,
1000
+ @hsbColor__,
1001
+ @colorMaxes__,
1002
+ @angleScale__,
1003
+ @rectMode__,
1004
+ @ellipseMode__ = @styleStack__.pop
968
1005
  nil
969
1006
  end
970
1007
 
@@ -990,85 +1027,163 @@ module RubySketch
990
1027
  #
991
1028
  # @overload noise(x)
992
1029
  # @overload noise(x, y)
1030
+ # @overload noise(x, y, z)
993
1031
  #
994
1032
  # @param x [Numeric] horizontal point in noise space
995
1033
  # @param y [Numeric] vertical point in noise space
1034
+ # @param z [Numeric] depth point in noise space
996
1035
  #
997
1036
  # @return [Numeric] noise value (0.0..1.0)
998
1037
  #
999
- def noise (x, y = 0)
1000
- Rays.perlin(x, y) / 2.0 + 1.0
1038
+ def noise (x, y = 0, z = 0)
1039
+ Rays.perlin(x, y, z) / 2.0 + 0.5
1001
1040
  end
1002
1041
 
1003
- # Font object.
1042
+ # Loads image.
1004
1043
  #
1005
- class Font
1044
+ # @param filename [String] file name to load image
1045
+ # @param extension [String] type of image to load (ex. 'png')
1046
+ #
1047
+ # @return [Image] loaded image object
1048
+ #
1049
+ def loadImage (filename, extension = nil)
1050
+ filename = getImage__ filename, extension if filename =~ %r|^https?://|
1051
+ Image.new Rays::Image.load filename
1052
+ end
1006
1053
 
1007
- # Initialize font.
1008
- #
1009
- # @private
1010
- #
1011
- def initialize (font)
1012
- @font = font
1054
+ # @private
1055
+ private def getImage__ (uri, ext)
1056
+ ext ||= File.extname uri
1057
+ raise "unsupported image type" unless ext =~ /^\.?(png)$/i
1058
+
1059
+ tmpdir = Pathname(Dir.tmpdir) + Digest::SHA1.hexdigest(self.class.name)
1060
+ path = tmpdir + Digest::SHA1.hexdigest(uri)
1061
+ path = path.sub_ext ext
1062
+
1063
+ unless path.file?
1064
+ p "getting #{uri}"
1065
+ URI.open uri do |input|
1066
+ tmpdir.mkdir unless tmpdir.directory?
1067
+ path.open('w') do |output|
1068
+ while buf = input.read(2 ** 16)
1069
+ output.write buf
1070
+ end
1071
+ end
1072
+ end
1013
1073
  end
1074
+ path.to_s
1075
+ end
1014
1076
 
1015
- # Returns bounding box.
1016
- #
1017
- # @overload textBounds(str)
1018
- # @overload textBounds(str, x, y)
1019
- # @overload textBounds(str, x, y, fontSize)
1020
- #
1021
- # @param str [String] text to calculate bounding box
1022
- # @param x [Numeric] horizontal position of bounding box
1023
- # @param y [Numeric] vertical position of bounding box
1024
- # @param fontSize [Numeric] font size
1025
- #
1026
- # @return [TextBounds] bounding box for text
1027
- #
1028
- def textBounds (str, x = 0, y = 0, fontSize = nil)
1029
- f = fontSize ? Rays::Font.new(@font.name, fontSize) : @font
1030
- TextBounds.new x, y, x + f.width(str), y + f.height
1031
- end
1077
+ end# Processing
1032
1078
 
1033
- # Bounding box for text.
1034
- #
1035
- class TextBounds
1036
-
1037
- # Horizontal position
1038
- #
1039
- attr_reader :x
1040
-
1041
- # Vertical position
1042
- #
1043
- attr_reader :y
1044
-
1045
- # Width of bounding box
1046
- #
1047
- attr_reader :w
1048
-
1049
- # Height of bounding box
1050
- #
1051
- attr_reader :h
1052
-
1053
- # Initialize bouding box.
1054
- #
1055
- # @param x [Numeric] horizontal position
1056
- # @param y [Numeric] vertical position
1057
- # @param w [Numeric] width of bounding box
1058
- # @param h [Numeric] height of bounding box
1059
- #
1060
- def initialize (x, y, w, h)
1061
- @x, @y, @w, @h = x, y, w, h
1062
- end
1063
1079
 
1064
- end# TextBounds
1080
+ # Image object.
1081
+ #
1082
+ class Processing::Image
1083
+
1084
+ # Initialize image.
1085
+ #
1086
+ def initialize (image)
1087
+ @image = image
1088
+ end
1089
+
1090
+ # Gets width of image.
1091
+ #
1092
+ # @return [Numeric] width of image
1093
+ #
1094
+ def width ()
1095
+ @image.width
1096
+ end
1097
+
1098
+ # Gets height of image.
1099
+ #
1100
+ # @return [Numeric] height of image
1101
+ #
1102
+ def height ()
1103
+ @image.height
1104
+ end
1105
+
1106
+ # Saves image to file.
1107
+ #
1108
+ # @param filename [String] file name to save image
1109
+ #
1110
+ def save (filename)
1111
+ @image.save filename
1112
+ end
1113
+
1114
+ # @private
1115
+ def internal ()
1116
+ @image
1117
+ end
1118
+
1119
+ end# Processing::Image
1120
+
1121
+
1122
+ # Font object.
1123
+ #
1124
+ class Processing::Font
1125
+
1126
+ # Initialize font.
1127
+ #
1128
+ # @private
1129
+ #
1130
+ def initialize (font)
1131
+ @font = font
1132
+ end
1133
+
1134
+ # Returns bounding box.
1135
+ #
1136
+ # @overload textBounds(str)
1137
+ # @overload textBounds(str, x, y)
1138
+ # @overload textBounds(str, x, y, fontSize)
1139
+ #
1140
+ # @param str [String] text to calculate bounding box
1141
+ # @param x [Numeric] horizontal position of bounding box
1142
+ # @param y [Numeric] vertical position of bounding box
1143
+ # @param fontSize [Numeric] font size
1144
+ #
1145
+ # @return [TextBounds] bounding box for text
1146
+ #
1147
+ def textBounds (str, x = 0, y = 0, fontSize = nil)
1148
+ f = fontSize ? Rays::Font.new(@font.name, fontSize) : @font
1149
+ TextBounds.new x, y, x + f.width(str), y + f.height
1150
+ end
1151
+
1152
+ end# Processing::Font
1065
1153
 
1066
- end# Font
1067
1154
 
1068
- end# Processing1
1155
+ # Bounding box for text.
1156
+ #
1157
+ class Processing::TextBounds
1069
1158
 
1159
+ # Horizontal position
1160
+ #
1161
+ attr_reader :x
1162
+
1163
+ # Vertical position
1164
+ #
1165
+ attr_reader :y
1166
+
1167
+ # Width of bounding box
1168
+ #
1169
+ attr_reader :w
1170
+
1171
+ # Height of bounding box
1172
+ #
1173
+ attr_reader :h
1174
+
1175
+ # Initialize bouding box.
1176
+ #
1177
+ # @param x [Numeric] horizontal position
1178
+ # @param y [Numeric] vertical position
1179
+ # @param w [Numeric] width of bounding box
1180
+ # @param h [Numeric] height of bounding box
1181
+ #
1182
+ def initialize (x, y, w, h)
1183
+ @x, @y, @w, @h = x, y, w, h
1184
+ end
1070
1185
 
1071
- Processing = Processing1
1186
+ end# Processing::TextBounds
1072
1187
 
1073
1188
 
1074
1189
  end# RubySketch
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-18 00:00:00.000000000 Z
11
+ date: 2020-03-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: yard
@@ -110,6 +110,7 @@ files:
110
110
  - examples/clock.rb
111
111
  - examples/glsl.rb
112
112
  - examples/hello.rb
113
+ - examples/image.rb
113
114
  - examples/shapes.rb
114
115
  - lib/rubysketch-processing.rb
115
116
  - lib/rubysketch.rb