rubysketch 0.3.18 → 0.3.21

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a179e1170c79df6daea6d79f66eddc3aa7b50d22b2c685bcb36864f0644212e
4
- data.tar.gz: 39565ddbcc31a7b6ef472c1fed40c91be91958531246b1673698afcd68ae621e
3
+ metadata.gz: acfaff485263c8e6557c8610df2a4643e53d1046e55cf813c79b648492e84d52
4
+ data.tar.gz: d7beb5170988d554771d50d5a726b1f51c961cded8a3cf3370dc55ccc91059fb
5
5
  SHA512:
6
- metadata.gz: 435879c3538ece61aaecee0c19e6ece104934316fb113b2f23d9dc57394ded709417e30e32a7697ba1c465927b9d615fa945d1d4a4aa5068629509a33fcdbfff
7
- data.tar.gz: b310a39f0c5e5fc55130d8c2cdd71c7620d17a42976d51fc73d7b09d41eeac15854482280bb213a937eda3474185935f27a1dcb6fd2983fad9a144c140fb337a
6
+ metadata.gz: 95ce0e61b8db2f0046bfd43070dd9d10f24b48f7838b267509b98e9a4b6930aa8958c065ae7af253c3877c754a5187ce970b80722421c32e6fbb9de4d1ebc05a
7
+ data.tar.gz: a810edf5f210a8a505fc3830697a46a76a743860a90118d1de38e99192d0cf43639dd5410409baf040c4b6feb0275a90d2cb330230b7457890d8555b9f257d5e
@@ -12,10 +12,10 @@ jobs:
12
12
  - name: checkout
13
13
  uses: actions/checkout@v2
14
14
 
15
- - name: ruby 2.7
15
+ - name: ruby 3.0
16
16
  uses: actions/setup-ruby@v1
17
17
  with:
18
- ruby-version: 2.7.x
18
+ ruby-version: 3.0.x
19
19
 
20
20
  - name: install gems
21
21
  run: |
@@ -13,10 +13,10 @@ jobs:
13
13
  steps:
14
14
  - uses: actions/checkout@v2
15
15
 
16
- - name: ruby 2.7
16
+ - name: ruby 3.0
17
17
  uses: actions/setup-ruby@v1
18
18
  with:
19
- ruby-version: 2.7.x
19
+ ruby-version: 3.0.x
20
20
 
21
21
  - name: install gems
22
22
  run: |
data/ChangeLog.md CHANGED
@@ -1,12 +1,41 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
- ## [0.3.17] - 2021-12-5
4
+ ## [0.3.21] - 2022-x-x
5
+
6
+ - add rubysketch-glsl.rb
7
+ - add blend(), createImage(), setTitle(), tint() and noTint()
8
+ - add save() that saves screen image to file
9
+ - circle() function is affected by ellipseMode()
10
+ - point() draws by line(x, y, x, y)
11
+ - change initial values for strokeCap and strokeJoin to ROUND and MITER
12
+
13
+
14
+ ## [0.3.20] - 2022-07-24
15
+
16
+ - add mouseClicked()
17
+ - add blendMode()
18
+ - add clip() and noClip()
19
+ - translate() can take 'z' parameter
20
+ - fix that resizing canvas consumes too much memory
21
+
22
+
23
+ ## [0.3.19] - 2021-12-5
24
+
25
+ - fix runtime error
26
+
27
+
28
+ ## [0.3.18] - 2021-12-5
5
29
 
6
30
  - add 'mouseButton'
7
31
  - pointer cancel event calls pointer_up block
8
32
 
9
33
 
34
+ ## [0.3.17] - 2021-12-5
35
+
36
+ - add Touch#id
37
+
38
+
10
39
  ## [0.3.16] - 2021-2-14
11
40
 
12
41
  - add key, keyCode and keyPressed system values
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'yard'
4
- gem 'reflexion'
3
+ gemspec
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ test_ruby_extension
22
22
  generate_documents
23
23
  build_ruby_gem
24
24
 
25
- task :default => :test
25
+ task :default
26
26
 
27
27
 
28
28
  namespace :version do
@@ -0,0 +1,28 @@
1
+ # -*- mode: ruby -*-
2
+
3
+
4
+ Pod::Spec.new do |s|
5
+ s.name = "RubySketch"
6
+ s.version = File.readlines(File.expand_path 'VERSION', __dir__)[0].chomp
7
+ s.summary = "Yet Another Processing implementation for Ruby"
8
+ s.description = "Yet Another Processing implementation for Ruby"
9
+ s.license = "MIT"
10
+ s.source = {:git => "https://github.com/xord/rubysketch.git"}
11
+ s.author = {"xordog" => "xordog@gmail.com"}
12
+ s.homepage = "https://github.com/xord/rubysketch"
13
+
14
+ s.osx.deployment_target = "10.10"
15
+ s.ios.deployment_target = "10.0"
16
+
17
+ incdirs = %W[
18
+ #{s.name}/src
19
+ CRuby/CRuby/include
20
+ Reflexion/reflex/include
21
+ ].map {|s| "${PODS_ROOT}/#{s}"}
22
+
23
+ s.preserve_paths = "src"
24
+ s.source_files = "src/*.mm"
25
+ s.xcconfig = {"HEADER_SEARCH_PATHS" => incdirs.join(' ')}
26
+
27
+ s.resource_bundles = {'RubySketch' => %w[lib VERSION]}
28
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.18
1
+ 0.3.21
data/examples/glsl.rb CHANGED
@@ -2,16 +2,13 @@
2
2
  .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
3
  .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
4
 
5
- require 'rubysketch'
5
+ require 'rubysketch-glsl'
6
6
 
7
-
8
- RubySketch::GLSL.start <<END
9
- varying vec4 v_TexCoord;
7
+ RubySketch::GLSL.run <<END
10
8
  uniform vec2 resolution;
11
9
  uniform float time;
12
- void main ()
13
- {
14
- vec2 pos = v_TexCoord.xy / resolution;
15
- gl_FragColor = vec4(pos, float(mod(time, 1.0)), 1);
10
+ void main() {
11
+ vec2 pos = gl_FragCoord.xy / resolution;
12
+ gl_FragColor = vec4(pos * abs(sin(time)), 0, 1);
16
13
  }
17
14
  END
@@ -1,29 +1,36 @@
1
1
  module RubySketch
2
2
 
3
3
 
4
- # @private
5
- class GLSL
4
+ # OpenGL Shader Language
5
+ #
6
+ module GLSL
6
7
 
7
- def initialize(glsl)
8
- @shader = Reflex::Shader.new glsl
9
- end
10
8
 
11
9
  # @private
12
- def on_start__(window)
13
- start = Time.now.to_f
14
-
15
- window.draw = proc do |e, painter|
16
- painter.paint do |p|
17
- c = window.canvas
18
- w = c.width
19
- h = c.height
20
- t = Time.now.to_f - start
21
- p.shader @shader, resolution: [w, h], time: t if @shader
10
+ class Context
11
+
12
+ # @private
13
+ def initialize(window, shader_source)
14
+ shader = Rays::Shader.new shader_source
15
+ start = now__
16
+ window.draw = proc do |e|
17
+ i, p = window.canvas_image, window.canvas_painter
18
+ w, h = i.width, i.height
19
+ p.shader shader, resolution: [w, h], time: now__ - start
22
20
  p.fill 1
23
21
  p.rect 0, 0, w, h
24
22
  end
25
23
  end
26
- end
24
+
25
+ private
26
+
27
+ # @private
28
+ def now__()
29
+ Time.now.to_f
30
+ end
31
+
32
+ end# Context
33
+
27
34
 
28
35
  end# GLSL
29
36
 
@@ -624,9 +624,34 @@ module RubySketch
624
624
  # @return [nil] nil
625
625
  #
626
626
  def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
627
+ blend img, sx, sy, sw, sh, dx, dy, dw, dh, :normal
628
+ end
629
+
630
+ # Blends image.
631
+ #
632
+ # @overload blend(sx, sy, sw, sh, dx, dy, dw, dh, mode)
633
+ # @overload blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode)
634
+ #
635
+ # @param img [Image] image for blend source
636
+ # @param sx [Numrtic] x position of source region
637
+ # @param sy [Numrtic] y position of source region
638
+ # @param sw [Numrtic] width of source region
639
+ # @param sh [Numrtic] height of source region
640
+ # @param dx [Numrtic] x position of destination region
641
+ # @param dy [Numrtic] y position of destination region
642
+ # @param dw [Numrtic] width of destination region
643
+ # @param dh [Numrtic] height of destination region
644
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE] blend mode
645
+ #
646
+ # @return [nil] nil
647
+ #
648
+ def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
627
649
  img ||= self
628
650
  @image.paint do |painter|
651
+ current = painter.blend_mode
652
+ painter.blend_mode = mode
629
653
  painter.image img.getInternal__, sx, sy, sw, sh, dx, dy, dw, dh
654
+ painter.blend_mode = current
630
655
  end
631
656
  end
632
657
 
@@ -850,13 +875,17 @@ module RubySketch
850
875
  #
851
876
  TAU = PI * 2
852
877
 
853
- # RGB mode for colorMode().
878
+ # RGBA format for createImage().
854
879
  #
855
- RGB = :rgb
880
+ RGBA = :rgba
881
+
882
+ # RGB format for createImage, or RGB mode for colorMode().
883
+ #
884
+ RGB = :rgb
856
885
 
857
886
  # HSB mode for colorMode().
858
887
  #
859
- HSB = :hsb
888
+ HSB = :hsb
860
889
 
861
890
  # Radian mode for angleMode().
862
891
  #
@@ -882,6 +911,73 @@ module RubySketch
882
911
  #
883
912
  RADIUS = :radius
884
913
 
914
+ # Mode for strokeCap().
915
+ #
916
+ BUTT = :butt
917
+
918
+ # Mode for strokeJoin().
919
+ #
920
+ MITER = :miter
921
+
922
+ # Mode for strokeCap() and strokeJoin().
923
+ #
924
+ ROUND = :round
925
+
926
+ # Mode for strokeCap() and strokeJoin().
927
+ #
928
+ SQUARE = :square
929
+
930
+ # Mode for blendMode().
931
+ #
932
+ BLEND = :normal
933
+
934
+ # Mode for blendMode().
935
+ #
936
+ ADD = :add
937
+
938
+ # Mode for blendMode().
939
+ #
940
+ SUBTRACT = :subtract
941
+
942
+ # Mode for blendMode().
943
+ #
944
+ LIGHTEST = :lightest
945
+
946
+ # Mode for blendMode().
947
+ #
948
+ DARKEST = :darkest
949
+
950
+ # Mode for blendMode().
951
+ #
952
+ EXCLUSION = :exclusion
953
+
954
+ # Mode for blendMode().
955
+ #
956
+ MULTIPLY = :multiply
957
+
958
+ # Mode for blendMode().
959
+ #
960
+ SCREEN = :screen
961
+
962
+ # Mode for blendMode().
963
+ #
964
+ REPLACE = :replace
965
+
966
+ # Key code or Mode for textAlign().
967
+ LEFT = :left
968
+
969
+ # Key code or Mode for textAlign().
970
+ RIGHT = :right
971
+
972
+ # Mode for textAlign().
973
+ TOP = :top
974
+
975
+ # Mode for textAlign().
976
+ BOTTOM = :bottom
977
+
978
+ # Mode for textAlign().
979
+ BASELINE = :baseline
980
+
885
981
  # Key codes.
886
982
  ENTER = :enter
887
983
  SPACE = :space
@@ -931,37 +1027,7 @@ module RubySketch
931
1027
  UP = :up
932
1028
  DOWN = :down
933
1029
 
934
- # Key code or Mode for textAlign().
935
- LEFT = :left
936
-
937
- # Key code or Mode for textAlign().
938
- RIGHT = :right
939
-
940
- # Mode for textAlign().
941
- TOP = :top
942
-
943
- # Mode for textAlign().
944
- BOTTOM = :bottom
945
-
946
- # Mode for textAlign().
947
- BASELINE = :baseline
948
-
949
- # Mode for strokeCap().
950
- #
951
- BUTT = :butt
952
-
953
- # Mode for strokeJoin().
954
- #
955
- MITER = :miter
956
-
957
- # Mode for strokeCap() and strokeJoin().
958
- #
959
- ROUND = :round
960
-
961
- # Mode for strokeCap() and strokeJoin().
962
- #
963
- SQUARE = :square
964
-
1030
+ # @private
965
1031
  def init__(image, painter)
966
1032
  @drawing__ = false
967
1033
  @hsbColor__ = false
@@ -970,10 +1036,12 @@ module RubySketch
970
1036
  @rectMode__ = nil
971
1037
  @ellipseMode__ = nil
972
1038
  @imageMode__ = nil
1039
+ @tint__ = nil
973
1040
  @textAlignH__ = nil
974
1041
  @textAlignV__ = nil
975
1042
  @matrixStack__ = []
976
1043
  @styleStack__ = []
1044
+ @fontCache__ = {}
977
1045
 
978
1046
  updateCanvas__ image, painter
979
1047
 
@@ -982,13 +1050,18 @@ module RubySketch
982
1050
  rectMode CORNER
983
1051
  ellipseMode CENTER
984
1052
  imageMode CORNER
1053
+ blendMode BLEND
1054
+ strokeCap ROUND
1055
+ strokeJoin MITER
985
1056
  textAlign LEFT
986
1057
 
987
1058
  fill 255
988
1059
  stroke 0
989
1060
  strokeWeight 1
1061
+ noTint
990
1062
  end
991
1063
 
1064
+ # @private
992
1065
  def updateCanvas__(image, painter)
993
1066
  @image__, @painter__ = image, painter
994
1067
  end
@@ -1146,6 +1219,17 @@ module RubySketch
1146
1219
  end
1147
1220
  end
1148
1221
 
1222
+ # Sets blend mode. Default is BLEND.
1223
+ #
1224
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE]
1225
+ #
1226
+ # @return [nil] nil
1227
+ #
1228
+ def blendMode(mode)
1229
+ @painter__.blend_mode = mode
1230
+ nil
1231
+ end
1232
+
1149
1233
  # Sets fill color.
1150
1234
  #
1151
1235
  # @overload fill(rgb)
@@ -1169,6 +1253,15 @@ module RubySketch
1169
1253
  nil
1170
1254
  end
1171
1255
 
1256
+ # Disables filling.
1257
+ #
1258
+ # @return [nil] nil
1259
+ #
1260
+ def noFill()
1261
+ @painter__.fill nil
1262
+ nil
1263
+ end
1264
+
1172
1265
  # Sets stroke color.
1173
1266
  #
1174
1267
  # @overload stroke(rgb)
@@ -1192,6 +1285,15 @@ module RubySketch
1192
1285
  nil
1193
1286
  end
1194
1287
 
1288
+ # Disables drawing stroke.
1289
+ #
1290
+ # @return [nil] nil
1291
+ #
1292
+ def noStroke()
1293
+ @painter__.stroke nil
1294
+ nil
1295
+ end
1296
+
1195
1297
  # Sets stroke weight.
1196
1298
  #
1197
1299
  # @param weight [Numeric] width of stroke
@@ -1225,33 +1327,78 @@ module RubySketch
1225
1327
  nil
1226
1328
  end
1227
1329
 
1228
- # Disables filling.
1330
+ # Sets fill color for drawing images.
1331
+ #
1332
+ # @overload tint(rgb)
1333
+ # @overload tint(rgb, alpha)
1334
+ # @overload tint(gray)
1335
+ # @overload tint(gray, alpha)
1336
+ # @overload tint(r, g, b)
1337
+ # @overload tint(r, g, b, alpha)
1338
+ #
1339
+ # @param rgb [String] color code like '#00AAFF'
1340
+ # @param gray [Integer] gray value (0..255)
1341
+ # @param r [Integer] red value (0..255)
1342
+ # @param g [Integer] green value (0..255)
1343
+ # @param b [Integer] blue value (0..255)
1344
+ # @param alpha [Integer] alpha value (0..255)
1229
1345
  #
1230
1346
  # @return [nil] nil
1231
1347
  #
1232
- def noFill()
1233
- @painter__.fill nil
1348
+ def tint(*args)
1349
+ @tint__ = args
1234
1350
  nil
1235
1351
  end
1236
1352
 
1237
- # Disables drawing stroke.
1353
+ # Resets tint color.
1238
1354
  #
1239
1355
  # @return [nil] nil
1240
1356
  #
1241
- def noStroke()
1242
- @painter__.stroke nil
1357
+ def noTint()
1358
+ @tint__ = nil
1359
+ end
1360
+
1361
+ # Limits the drawable rectangle.
1362
+ #
1363
+ # The parameters a, b, c, and d are determined by rectMode().
1364
+ #
1365
+ # @param a [Numeric] horizontal position of the drawable area, by default
1366
+ # @param b [Numeric] vertical position of the drawable area, by default
1367
+ # @param c [Numeric] width of the drawable area, by default
1368
+ # @param d [Numeric] height of the drawable area, by default
1369
+ #
1370
+ # @return [nil] nil
1371
+ #
1372
+ def clip(a, b, c, d)
1373
+ x, y, w, h = toXYWH__ @imageMode__, a, b, c, d
1374
+ @painter__.clip x, y, w, h
1375
+ nil
1376
+ end
1377
+
1378
+ # Disables clipping.
1379
+ #
1380
+ # @return [nil] nil
1381
+ #
1382
+ def noClip()
1383
+ @painter__.no_clip
1243
1384
  nil
1244
1385
  end
1245
1386
 
1246
1387
  # Sets font.
1247
1388
  #
1389
+ # @overload textFont(font)
1390
+ # @overload textFont(name)
1391
+ # @overload textFont(font, size)
1392
+ # @overload textFont(name, size)
1393
+ #
1394
+ # @param font [Font] font
1248
1395
  # @param name [String] font name
1249
1396
  # @param size [Numeric] font size (max 256)
1250
1397
  #
1251
1398
  # @return [Font] current font
1252
1399
  #
1253
- def textFont(name = nil, size = nil)
1254
- setFont__ name, size if name || size
1400
+ def textFont(font = nil, size = nil)
1401
+ setFont__ font, size if font || size
1255
1402
  Font.new @painter__.font
1256
1403
  end
1257
1404
 
@@ -1262,7 +1409,7 @@ module RubySketch
1262
1409
  # @return [nil] nil
1263
1410
  #
1264
1411
  def textSize(size)
1265
- setFont__ @painter__.font.name, size
1412
+ setFont__ nil, size
1266
1413
  nil
1267
1414
  end
1268
1415
 
@@ -1284,9 +1431,15 @@ module RubySketch
1284
1431
  end
1285
1432
 
1286
1433
  # @private
1287
- def setFont__(name, size)
1288
- size = 256 if size && size > 256
1289
- @painter__.font name, size
1434
+ def setFont__(fontOrName, size)
1435
+ name = case fontOrName
1436
+ when Font then fontOrName.name
1437
+ else fontOrName || @painter__.font.name
1438
+ end
1439
+ size ||= @painter__.font.size
1440
+ size = 256 if size > 256
1441
+ font = @fontCache__[[name, size]] ||= Rays::Font.new name, size
1442
+ @painter__.font = font
1290
1443
  end
1291
1444
 
1292
1445
  # Clears screen.
@@ -1313,7 +1466,7 @@ module RubySketch
1313
1466
  if rgba[3] == 1
1314
1467
  @painter__.background(*rgba)
1315
1468
  else
1316
- @painter__.push fill: rgba, stroke: nil do |_|
1469
+ @painter__.push fill: rgba, stroke: :none do |_|
1317
1470
  @painter__.rect 0, 0, width, height
1318
1471
  end
1319
1472
  end
@@ -1329,9 +1482,7 @@ module RubySketch
1329
1482
  #
1330
1483
  def point(x, y)
1331
1484
  assertDrawing__
1332
- w = @painter__.stroke_width
1333
- w = 1 if w == 0
1334
- @painter__.ellipse x - (w / 2.0), y - (w / 2.0), w, w
1485
+ @painter__.line x, y, x, y
1335
1486
  nil
1336
1487
  end
1337
1488
 
@@ -1352,14 +1503,16 @@ module RubySketch
1352
1503
 
1353
1504
  # Draws a rectangle.
1354
1505
  #
1506
+ # The parameters a, b, c, and d are determined by rectMode().
1507
+ #
1355
1508
  # @overload rect(a, b, c, d)
1356
1509
  # @overload rect(a, b, c, d, r)
1357
1510
  # @overload rect(a, b, c, d, tl, tr, br, bl)
1358
1511
  #
1359
- # @param a [Numeric] horizontal position of the shape by default
1360
- # @param b [Numeric] vertical position of the shape by default
1361
- # @param c [Numeric] width of the shape by default
1362
- # @param d [Numeric] height of the shape by default
1512
+ # @param a [Numeric] horizontal position of the shape, by default
1513
+ # @param b [Numeric] vertical position of the shape, by default
1514
+ # @param c [Numeric] width of the shape, by default
1515
+ # @param d [Numeric] height of the shape, by default
1363
1516
  # @param r [Numeric] radius for all corners
1364
1517
  # @param tl [Numeric] radius for top-left corner
1365
1518
  # @param tr [Numeric] radius for top-right corner
@@ -1382,10 +1535,12 @@ module RubySketch
1382
1535
 
1383
1536
  # Draws an ellipse.
1384
1537
  #
1385
- # @param a [Numeric] horizontal position of the shape
1386
- # @param b [Numeric] vertical position of the shape
1387
- # @param c [Numeric] width of the shape
1388
- # @param d [Numeric] height of the shape
1538
+ # The parameters a, b, c, and d are determined by ellipseMode().
1539
+ #
1540
+ # @param a [Numeric] horizontal position of the shape, by default
1541
+ # @param b [Numeric] vertical position of the shape, by default
1542
+ # @param c [Numeric] width of the shape, by default
1543
+ # @param d [Numeric] height of the shape, by default
1389
1544
  #
1390
1545
  # @return [nil] nil
1391
1546
  #
@@ -1410,10 +1565,12 @@ module RubySketch
1410
1565
 
1411
1566
  # Draws an arc.
1412
1567
  #
1413
- # @param a [Numeric] horizontal position of the shape
1414
- # @param b [Numeric] vertical position of the shape
1415
- # @param c [Numeric] width of the shape
1416
- # @param d [Numeric] height of the shape
1568
+ # The parameters a, b, c, and d are determined by ellipseMode().
1569
+ #
1570
+ # @param a [Numeric] horizontal position of the shape, by default
1571
+ # @param b [Numeric] vertical position of the shape, by default
1572
+ # @param c [Numeric] width of the shape, by default
1573
+ # @param d [Numeric] height of the shape, by default
1417
1574
  # @param start [Numeric] angle to start the arc
1418
1575
  # @param stop [Numeric] angle to stop the arc
1419
1576
  #
@@ -1516,6 +1673,8 @@ module RubySketch
1516
1673
 
1517
1674
  # Draws a text.
1518
1675
  #
1676
+ # The parameters a, b, c, and d are determined by rectMode().
1677
+ #
1519
1678
  # @overload text(str)
1520
1679
  # @overload text(str, x, y)
1521
1680
  # @overload text(str, a, b, c, d)
@@ -1523,10 +1682,10 @@ module RubySketch
1523
1682
  # @param str [String] text to draw
1524
1683
  # @param x [Numeric] horizontal position of the text
1525
1684
  # @param y [Numeric] vertical position of the text
1526
- # @param a [Numeric] equivalent to parameters of the rect(), see rectMode()
1527
- # @param b [Numeric] equivalent to parameters of the rect(), see rectMode()
1528
- # @param c [Numeric] equivalent to parameters of the rect(), see rectMode()
1529
- # @param d [Numeric] equivalent to parameters of the rect(), see rectMode()
1685
+ # @param a [Numeric] horizontal position of the text, by default
1686
+ # @param b [Numeric] vertical position of the text, by default
1687
+ # @param c [Numeric] width of the text, by default
1688
+ # @param d [Numeric] height of the text, by default
1530
1689
  #
1531
1690
  # @return [nil] nil
1532
1691
  #
@@ -1553,22 +1712,27 @@ module RubySketch
1553
1712
 
1554
1713
  # Draws an image.
1555
1714
  #
1715
+ # The parameters a, b, c, and d are determined by imageMode().
1716
+ #
1556
1717
  # @overload image(img, a, b)
1557
1718
  # @overload image(img, a, b, c, d)
1558
1719
  #
1559
1720
  # @param img [Image] image to draw
1560
- # @param a [Numeric] horizontal position of the image
1561
- # @param b [Numeric] vertical position of the image
1562
- # @param c [Numeric] width of the image
1563
- # @param d [Numeric] height of the image
1721
+ # @param a [Numeric] horizontal position of the image, by default
1722
+ # @param b [Numeric] vertical position of the image, by default
1723
+ # @param c [Numeric] width of the image, by default
1724
+ # @param d [Numeric] height of the image, by default
1564
1725
  #
1565
1726
  # @return [nil] nil
1566
1727
  #
1567
1728
  def image(img, a, b, c = nil, d = nil)
1568
1729
  assertDrawing__
1569
- i = img.getInternal__
1730
+ i = img.getInternal__
1570
1731
  x, y, w, h = toXYWH__ @imageMode__, a, b, c || i.width, d || i.height
1571
- @painter__.image i, x, y, w, h
1732
+ tint = @tint__ ? toRGBA__(*@tint__) : 1
1733
+ @painter__.push fill: tint, stroke: :none do |_|
1734
+ @painter__.image i, x, y, w, h
1735
+ end
1572
1736
  nil
1573
1737
  end
1574
1738
 
@@ -1590,21 +1754,59 @@ module RubySketch
1590
1754
  # @return [nil] nil
1591
1755
  #
1592
1756
  def copy(img = nil, sx, sy, sw, sh, dx, dy, dw, dh)
1757
+ blend img, sx, sy, sw, sh, dx, dy, dw, dh, BLEND
1758
+ end
1759
+
1760
+ # Blends image.
1761
+ #
1762
+ # @overload blend(sx, sy, sw, sh, dx, dy, dw, dh, mode)
1763
+ # @overload blend(img, sx, sy, sw, sh, dx, dy, dw, dh, mode)
1764
+ #
1765
+ # @param img [Image] image for blend source
1766
+ # @param sx [Numrtic] x position of source region
1767
+ # @param sy [Numrtic] y position of source region
1768
+ # @param sw [Numrtic] width of source region
1769
+ # @param sh [Numrtic] height of source region
1770
+ # @param dx [Numrtic] x position of destination region
1771
+ # @param dy [Numrtic] y position of destination region
1772
+ # @param dw [Numrtic] width of destination region
1773
+ # @param dh [Numrtic] height of destination region
1774
+ # @param mode [BLEND, ADD, SUBTRACT, LIGHTEST, DARKEST, EXCLUSION, MULTIPLY, SCREEN, REPLACE] blend mode
1775
+ #
1776
+ # @return [nil] nil
1777
+ #
1778
+ def blend(img = nil, sx, sy, sw, sh, dx, dy, dw, dh, mode)
1593
1779
  assertDrawing__
1594
- src = img&.getInternal__ || @window__.canvas_image
1780
+ src = img&.getInternal__ || @window__.canvas_image
1781
+ current = @painter__.blend_mode
1782
+
1783
+ @painter__.blend_mode = mode
1595
1784
  @painter__.image src, sx, sy, sw, sh, dx, dy, dw, dh
1785
+ @painter__.blend_mode = current
1786
+ end
1787
+
1788
+ # Saves screen image to file.
1789
+ #
1790
+ # @param filename [String] file name to save image
1791
+ #
1792
+ def save(filename)
1793
+ @window__.canvas_image.save filename
1596
1794
  end
1597
1795
 
1598
1796
  # Applies translation matrix to current transformation matrix.
1599
1797
  #
1600
- # @param x [Numeric] horizontal transformation
1601
- # @param y [Numeric] vertical transformation
1798
+ # @overload translate(x, y)
1799
+ # @overload translate(x, y, z)
1800
+ #
1801
+ # @param x [Numeric] left/right translation
1802
+ # @param y [Numeric] up/down translation
1803
+ # @param y [Numeric] forward/backward translation
1602
1804
  #
1603
1805
  # @return [nil] nil
1604
1806
  #
1605
- def translate(x, y)
1807
+ def translate(x, y, z = 0)
1606
1808
  assertDrawing__
1607
- @painter__.translate x, y
1809
+ @painter__.translate x, y, z
1608
1810
  nil
1609
1811
  end
1610
1812
 
@@ -1684,6 +1886,8 @@ module RubySketch
1684
1886
  @painter__.stroke_width,
1685
1887
  @painter__.stroke_cap,
1686
1888
  @painter__.stroke_join,
1889
+ @painter__.clip,
1890
+ @painter__.blend_mode,
1687
1891
  @painter__.font,
1688
1892
  @hsbColor__,
1689
1893
  @colorMaxes__,
@@ -1711,6 +1915,8 @@ module RubySketch
1711
1915
  @painter__.stroke_width,
1712
1916
  @painter__.stroke_cap,
1713
1917
  @painter__.stroke_join,
1918
+ @painter__.clip,
1919
+ @painter__.blend_mode,
1714
1920
  @painter__.font,
1715
1921
  @hsbColor__,
1716
1922
  @colorMaxes__,
@@ -1828,7 +2034,7 @@ module RubySketch
1828
2034
  @keyCode__ = nil
1829
2035
  @keysPressed__ = Set.new
1830
2036
  @pointerPos__ =
1831
- @pointerPrevPos__ = [0, 0]
2037
+ @pointerPrevPos__ = Rays::Point.new 0
1832
2038
  @pointersPressed__ = []
1833
2039
  @touches__ = []
1834
2040
  @motionGravity__ = createVector 0, 0
@@ -1852,7 +2058,6 @@ module RubySketch
1852
2058
  @redraw__ = false
1853
2059
  drawFrame.call
1854
2060
  end
1855
- @pointerPrevPos__ = @pointerPos__
1856
2061
  end
1857
2062
 
1858
2063
  updateKeyStates = -> event, pressed {
@@ -1871,11 +2076,12 @@ module RubySketch
1871
2076
  }
1872
2077
 
1873
2078
  updatePointerStates = -> event, pressed = nil {
1874
- @pointerPos__ = event.pos.to_a
2079
+ @pointerPrevPos__ = @pointerPos__
2080
+ @pointerPos__ = event.pos.dup
1875
2081
  @touches__ = event.pointers.map {|p| Touch.new(p.id, *p.pos.to_a)}
1876
2082
  if pressed != nil
1877
2083
  array = @pointersPressed__
1878
- event.type
2084
+ event.types
1879
2085
  .tap {|types| types.delete :mouse}
1880
2086
  .map {|type| mouseButtonMap[type] || type}
1881
2087
  .each {|type| pressed ? array.push(type) : array.delete(type)}
@@ -1895,12 +2101,17 @@ module RubySketch
1895
2101
 
1896
2102
  @window__.pointer_down = proc do |e|
1897
2103
  updatePointerStates.call e, true
2104
+ @pointerDownStartPos__ = @pointerPos__.dup
1898
2105
  (@touchStartedBlock__ || @mousePressedBlock__)&.call
1899
2106
  end
1900
2107
 
1901
2108
  @window__.pointer_up = proc do |e|
1902
2109
  updatePointerStates.call e, false
1903
2110
  (@touchEndedBlock__ || @mouseReleasedBlock__)&.call
2111
+ if startPos = @pointerDownStartPos__
2112
+ @mouseClickedBlock__&.call if (@pointerPos__ - startPos).length < 3
2113
+ @pointerDownStartPos__ = nil
2114
+ end
1904
2115
  end
1905
2116
 
1906
2117
  @window__.pointer_move = proc do |e|
@@ -1921,6 +2132,8 @@ module RubySketch
1921
2132
 
1922
2133
  # Defines setup block.
1923
2134
  #
2135
+ # @return [nil] nil
2136
+ #
1924
2137
  def setup(&block)
1925
2138
  @window__.setup = block
1926
2139
  nil
@@ -1928,6 +2141,8 @@ module RubySketch
1928
2141
 
1929
2142
  # Defines draw block.
1930
2143
  #
2144
+ # @return [nil] nil
2145
+ #
1931
2146
  def draw(&block)
1932
2147
  @drawBlock__ = block if block
1933
2148
  nil
@@ -1944,6 +2159,8 @@ module RubySketch
1944
2159
 
1945
2160
  # Defines keyReleased block.
1946
2161
  #
2162
+ # @return [nil] nil
2163
+ #
1947
2164
  def keyReleased(&block)
1948
2165
  @keyReleasedBlock__ = block if block
1949
2166
  nil
@@ -1951,6 +2168,8 @@ module RubySketch
1951
2168
 
1952
2169
  # Defines keyTyped block.
1953
2170
  #
2171
+ # @return [nil] nil
2172
+ #
1954
2173
  def keyTyped(&block)
1955
2174
  @keyTypedBlock__ = block if block
1956
2175
  nil
@@ -1967,6 +2186,8 @@ module RubySketch
1967
2186
 
1968
2187
  # Defines mouseReleased block.
1969
2188
  #
2189
+ # @return [nil] nil
2190
+ #
1970
2191
  def mouseReleased(&block)
1971
2192
  @mouseReleasedBlock__ = block if block
1972
2193
  nil
@@ -1974,6 +2195,8 @@ module RubySketch
1974
2195
 
1975
2196
  # Defines mouseMoved block.
1976
2197
  #
2198
+ # @return [nil] nil
2199
+ #
1977
2200
  def mouseMoved(&block)
1978
2201
  @mouseMovedBlock__ = block if block
1979
2202
  nil
@@ -1981,13 +2204,26 @@ module RubySketch
1981
2204
 
1982
2205
  # Defines mouseDragged block.
1983
2206
  #
2207
+ # @return [nil] nil
2208
+ #
1984
2209
  def mouseDragged(&block)
1985
2210
  @mouseDraggedBlock__ = block if block
1986
2211
  nil
1987
2212
  end
1988
2213
 
2214
+ # Defines mouseClicked block.
2215
+ #
2216
+ # @return [nil] nil
2217
+ #
2218
+ def mouseClicked(&block)
2219
+ @mouseClickedBlock__ = block if block
2220
+ nil
2221
+ end
2222
+
1989
2223
  # Defines touchStarted block.
1990
2224
  #
2225
+ # @return [nil] nil
2226
+ #
1991
2227
  def touchStarted(&block)
1992
2228
  @touchStartedBlock__ = block if block
1993
2229
  nil
@@ -1995,6 +2231,8 @@ module RubySketch
1995
2231
 
1996
2232
  # Defines touchEnded block.
1997
2233
  #
2234
+ # @return [nil] nil
2235
+ #
1998
2236
  def touchEnded(&block)
1999
2237
  @touchEndedBlock__ = block if block
2000
2238
  nil
@@ -2002,6 +2240,8 @@ module RubySketch
2002
2240
 
2003
2241
  # Defines touchMoved block.
2004
2242
  #
2243
+ # @return [nil] nil
2244
+ #
2005
2245
  def touchMoved(&block)
2006
2246
  @touchMovedBlock__ = block if block
2007
2247
  nil
@@ -2009,6 +2249,8 @@ module RubySketch
2009
2249
 
2010
2250
  # Defines motion block.
2011
2251
  #
2252
+ # @return [nil] nil
2253
+ #
2012
2254
  def motion(&block)
2013
2255
  @motionBlock__ = block if block
2014
2256
  nil
@@ -2040,6 +2282,17 @@ module RubySketch
2040
2282
  nil
2041
2283
  end
2042
2284
 
2285
+ # Changes title of window.
2286
+ #
2287
+ # @param title [String] new title
2288
+ #
2289
+ # @return [nil] nil
2290
+ #
2291
+ def setTitle(title)
2292
+ @window__.title = title
2293
+ nil
2294
+ end
2295
+
2043
2296
  # Changes and returns canvas pixel density.
2044
2297
  #
2045
2298
  # @param density [Numeric] new pixel density
@@ -2056,9 +2309,12 @@ module RubySketch
2056
2309
  raise '#{name}() must be called on startup or setup block' if @started__
2057
2310
 
2058
2311
  @painter__.__send__ :end_paint
2059
- @window__.__send__ :resize_canvas, width, height, pixelDensity
2060
- updateCanvas__ @window__.canvas_image, @window__.canvas_painter
2061
- @painter__.__send__ :begin_paint
2312
+ begin
2313
+ @window__.__send__ :resize_canvas, width, height, pixelDensity
2314
+ updateCanvas__ @window__.canvas_image, @window__.canvas_painter
2315
+ ensure
2316
+ @painter__.__send__ :begin_paint
2317
+ end
2062
2318
 
2063
2319
  @window__.auto_resize = false
2064
2320
  end
@@ -2124,7 +2380,7 @@ module RubySketch
2124
2380
  # @return [Numeric] horizontal position of mouse
2125
2381
  #
2126
2382
  def mouseX()
2127
- @pointerPos__[0]
2383
+ @pointerPos__.x
2128
2384
  end
2129
2385
 
2130
2386
  # Returns mouse y position
@@ -2132,7 +2388,7 @@ module RubySketch
2132
2388
  # @return [Numeric] vertical position of mouse
2133
2389
  #
2134
2390
  def mouseY()
2135
- @pointerPos__[1]
2391
+ @pointerPos__.y
2136
2392
  end
2137
2393
 
2138
2394
  # Returns mouse x position in previous frame
@@ -2140,7 +2396,7 @@ module RubySketch
2140
2396
  # @return [Numeric] horizontal position of mouse
2141
2397
  #
2142
2398
  def pmouseX()
2143
- @pointerPrevPos__[0]
2399
+ @pointerPrevPos__.x
2144
2400
  end
2145
2401
 
2146
2402
  # Returns mouse y position in previous frame
@@ -2148,7 +2404,7 @@ module RubySketch
2148
2404
  # @return [Numeric] vertical position of mouse
2149
2405
  #
2150
2406
  def pmouseY()
2151
- @pointerPrevPos__[1]
2407
+ @pointerPrevPos__.y
2152
2408
  end
2153
2409
 
2154
2410
  # Returns which mouse button was pressed
@@ -2568,6 +2824,23 @@ module RubySketch
2568
2824
  Vector.new(*args, context: self)
2569
2825
  end
2570
2826
 
2827
+ # Creates a new image.
2828
+ #
2829
+ # @overload createImage(w, h)
2830
+ # @overload createImage(w, h, format)
2831
+ #
2832
+ # @param w [Numeric] width of new image
2833
+ # @param h [Numeric] height of new image
2834
+ # @param format [RGB, RGBA] image format
2835
+ #
2836
+ # @return [Image] new image
2837
+ #
2838
+ def createImage(w, h, format = RGBA)
2839
+ colorspace = {RGB => Rays::RGB, RGBA => Rays::RGBA}[format]
2840
+ raise ArgumentError, "Unknown image format" unless colorspace
2841
+ Image.new Rays::Image.new(w, h, colorspace).paint {background 0, 0}
2842
+ end
2843
+
2571
2844
  # Creates a camera object as a video input device.
2572
2845
  #
2573
2846
  # @return [Capture] camera object
@@ -3,6 +3,24 @@ module RubySketch
3
3
 
4
4
  class Window < Reflex::Window
5
5
 
6
+ class CanvasView < Reflex::View
7
+ def on_update(e)
8
+ window.on_canvas_update e
9
+ end
10
+
11
+ def on_draw(e)
12
+ window.on_canvas_draw e
13
+ end
14
+
15
+ def on_pointer(e)
16
+ window.on_canvas_pointer e
17
+ end
18
+
19
+ def on_resize(e)
20
+ window.on_canvas_resize e
21
+ end
22
+ end
23
+
6
24
  attr_accessor :setup, :update, :draw, :before_draw, :after_draw, :resize,
7
25
  :key_down, :key_up,
8
26
  :pointer_down, :pointer_up, :pointer_move, :pointer_drag,
@@ -24,12 +42,7 @@ module RubySketch
24
42
  painter.miter_limit = 10
25
43
  resize_canvas 1, 1
26
44
 
27
- @canvas_view = add Reflex::View.new(name: :canvas) {|v|
28
- v.on(:update) {|e| on_canvas_update e}
29
- v.on(:draw) {|e| on_canvas_draw e}
30
- v.on(:pointer) {|e| on_canvas_pointer e}
31
- v.on(:resize) {|e| on_canvas_resize e}
32
- }
45
+ @canvas_view = add CanvasView.new name: :canvas
33
46
 
34
47
  super(*args, size: [width, height], **kwargs, &block)
35
48
  end
@@ -118,6 +131,7 @@ module RubySketch
118
131
  end
119
132
 
120
133
  resize_window width, height
134
+ GC.start
121
135
  end
122
136
 
123
137
  @canvas_painter
@@ -0,0 +1,11 @@
1
+ require 'rubysketch'
2
+
3
+
4
+ def (RubySketch::GLSL).run(
5
+ shader_source,
6
+ title: 'RubySketch',
7
+ width: 500, height: 500)
8
+ w = RubySketch::Window.new(title: title, w: width, h: height) {start}
9
+ RubySketch::GLSL::Context.new w, shader_source
10
+ at_exit {RubySketch::App.new {w.show}.start unless $!}
11
+ end
@@ -6,8 +6,8 @@ begin
6
6
  context = RubySketch::Processing::Context.new window
7
7
 
8
8
  (context.methods - Object.instance_methods).each do |method|
9
- define_method method do |*args, &block|
10
- context.__send__ method, *args, &block
9
+ define_method method do |*args, **kwargs, &block|
10
+ context.__send__ method, *args, **kwargs, &block
11
11
  end
12
12
  end
13
13
 
@@ -20,4 +20,6 @@ begin
20
20
  window.__send__ :end_draw
21
21
  RubySketch::App.new {window.show}.start unless $!
22
22
  end
23
+
24
+ RUBYSKETCH_WINDOW = window
23
25
  end
data/rubysketch.gemspec CHANGED
@@ -28,7 +28,9 @@ Gem::Specification.new do |s|
28
28
  s.platform = Gem::Platform::RUBY
29
29
  s.required_ruby_version = '>= 2.6.0'
30
30
 
31
- s.add_runtime_dependency 'reflexion', '~> 0.1.24'
31
+ s.add_runtime_dependency 'reflexion', '~> 0.1.28'
32
+
33
+ s.add_development_dependency 'yard'
32
34
 
33
35
  s.files = `git ls-files`.split $/
34
36
  s.test_files = s.files.grep %r{^(test|spec|features)/}
data/src/RubySketch.h ADDED
@@ -0,0 +1,15 @@
1
+ // -*- mode: objc -*-
2
+ #import <Foundation/Foundation.h>
3
+
4
+
5
+ @interface RubySketch : NSObject
6
+
7
+ + (void) setup;
8
+
9
+ + (void) start: (NSString*) path;
10
+
11
+ + (void) setActiveReflexViewController: (id) reflexViewController;
12
+
13
+ + (void) resetActiveReflexViewController;
14
+
15
+ @end
data/src/RubySketch.mm ADDED
@@ -0,0 +1,55 @@
1
+ #import <CRuby.h>
2
+ #import "RubySketch.h"
3
+ #include "../src/ios/view_controller.h"
4
+
5
+
6
+ static ReflexViewController* active_reflex_view_controller = nil;
7
+
8
+ static ReflexViewController*
9
+ ReflexViewController_create()
10
+ {
11
+ return active_reflex_view_controller;
12
+ }
13
+
14
+ static void
15
+ ReflexViewController_show (UIViewController*, ReflexViewController*)
16
+ {
17
+ }
18
+
19
+
20
+ @implementation RubySketch
21
+
22
+ + (void) setup
23
+ {
24
+ static BOOL done = NO;
25
+ if (done) return;
26
+ done = YES;
27
+
28
+ [CRuby addLibrary:@"RubySketch" bundle:[NSBundle bundleForClass:RubySketch.class]];
29
+
30
+ ReflexViewController_set_create_fun(ReflexViewController_create);
31
+ ReflexViewController_set_show_fun(ReflexViewController_show);
32
+ }
33
+
34
+ + (void) start: (NSString*) path
35
+ {
36
+ [CRuby evaluate:[NSString stringWithFormat:@
37
+ "raise 'already started' unless require 'rubysketch-processing'\n"
38
+ "load '%@'\n"
39
+ "RUBYSKETCH_WINDOW.__send__ :end_draw\n"
40
+ "RUBYSKETCH_WINDOW.show",
41
+ path
42
+ ]];
43
+ }
44
+
45
+ + (void) setActiveReflexViewController: (id) reflexViewController
46
+ {
47
+ active_reflex_view_controller = reflexViewController;
48
+ }
49
+
50
+ + (void) resetActiveReflexViewController
51
+ {
52
+ active_reflex_view_controller = nil;
53
+ }
54
+
55
+ @end
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.3.18
4
+ version: 0.3.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-12-04 00:00:00.000000000 Z
11
+ date: 2022-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: reflexion
@@ -16,14 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.24
19
+ version: 0.1.28
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.24
26
+ version: 0.1.28
27
+ - !ruby/object:Gem::Dependency
28
+ name: yard
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description: Creative Coding Framework have API compatible to Processing API or p5.js.
28
42
  email: xordog@gmail.com
29
43
  executables: []
@@ -39,6 +53,7 @@ files:
39
53
  - LICENSE
40
54
  - README.md
41
55
  - Rakefile
56
+ - RubySketch.podspec
42
57
  - VERSION
43
58
  - examples/breakout.rb
44
59
  - examples/camera.rb
@@ -48,6 +63,7 @@ files:
48
63
  - examples/hello.rb
49
64
  - examples/image.rb
50
65
  - examples/shapes.rb
66
+ - lib/rubysketch-glsl.rb
51
67
  - lib/rubysketch-processing.rb
52
68
  - lib/rubysketch.rb
53
69
  - lib/rubysketch/app.rb
@@ -56,6 +72,8 @@ files:
56
72
  - lib/rubysketch/processing.rb
57
73
  - lib/rubysketch/window.rb
58
74
  - rubysketch.gemspec
75
+ - src/RubySketch.h
76
+ - src/RubySketch.mm
59
77
  - test/helper.rb
60
78
  - test/processing/test_utility.rb
61
79
  - test/processing/test_vector.rb
@@ -77,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
95
  - !ruby/object:Gem::Version
78
96
  version: '0'
79
97
  requirements: []
80
- rubygems_version: 3.1.6
98
+ rubygems_version: 3.2.33
81
99
  signing_key:
82
100
  specification_version: 4
83
101
  summary: Processing like Creative Coding Framework.