reight 0.1.6 → 0.1.8

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.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/.github/PULL_REQUEST_TEMPLATE.md +12 -0
  3. data/CONTRIBUTING.md +7 -0
  4. data/ChangeLog.md +76 -0
  5. data/README.md +6 -5
  6. data/Rakefile +5 -2
  7. data/VERSION +1 -1
  8. data/bin/r8 +18 -8
  9. data/lib/reight/all.rb +7 -2
  10. data/lib/reight/app/chips.rb +124 -0
  11. data/lib/reight/app/map/brush.rb +1 -1
  12. data/lib/reight/app/map/brush_base.rb +3 -3
  13. data/lib/reight/app/map/canvas.rb +10 -15
  14. data/lib/reight/app/map/editor.rb +44 -17
  15. data/lib/reight/app/map/line.rb +5 -5
  16. data/lib/reight/app/map/rect.rb +2 -2
  17. data/lib/reight/app/map.rb +0 -1
  18. data/lib/reight/app/navigator.rb +33 -47
  19. data/lib/reight/app/runner.rb +123 -86
  20. data/lib/reight/app/sound/brush.rb +32 -0
  21. data/lib/reight/app/sound/canvas.rb +190 -0
  22. data/lib/reight/app/sound/editor.rb +170 -10
  23. data/lib/reight/app/sound/eraser.rb +28 -0
  24. data/lib/reight/app/sound/tool.rb +29 -0
  25. data/lib/reight/app/sound.rb +4 -0
  26. data/lib/reight/app/sprite/canvas.rb +23 -18
  27. data/lib/reight/app/sprite/color.rb +3 -1
  28. data/lib/reight/app/sprite/editor.rb +58 -47
  29. data/lib/reight/app/sprite/line.rb +1 -1
  30. data/lib/reight/app/sprite/shape.rb +1 -1
  31. data/lib/reight/app/sprite.rb +0 -1
  32. data/lib/reight/app.rb +24 -5
  33. data/lib/reight/button.rb +10 -7
  34. data/lib/reight/chip.rb +32 -6
  35. data/lib/reight/context.rb +168 -0
  36. data/lib/reight/helpers.rb +2 -2
  37. data/lib/reight/index.rb +87 -0
  38. data/lib/reight/map.rb +141 -11
  39. data/lib/reight/project.rb +45 -6
  40. data/lib/reight/reight.rb +11 -15
  41. data/lib/reight/sound.rb +238 -0
  42. data/lib/reight/sprite.rb +42 -0
  43. data/lib/reight/text.rb +124 -0
  44. data/lib/reight.rb +7 -3
  45. data/reight.gemspec +7 -7
  46. data/res/icons.png +0 -0
  47. data/test/helper.rb +16 -0
  48. data/test/test_map.rb +7 -7
  49. data/test/test_map_chunk.rb +6 -6
  50. data/test/test_sprite.rb +28 -0
  51. metadata +42 -32
  52. data/lib/reight/app/map/chips.rb +0 -84
  53. data/lib/reight/app/music/editor.rb +0 -25
  54. data/lib/reight/app/music.rb +0 -1
  55. data/lib/reight/app/sprite/chips.rb +0 -92
@@ -9,12 +9,16 @@ class Reight::SpriteEditor < Reight::App
9
9
  project.chips_image,
10
10
  project.chips_image_path
11
11
  ).tap do |canvas|
12
- canvas. tool_changed {update_active_tool}
13
- canvas.color_changed {update_active_color}
12
+ canvas.frame_changed do |old, new|
13
+ history.append [:frame, old, new]
14
+ end
15
+ canvas.selection_changed do |old, new|
16
+ history.append [new ? :select : :deselect, old, new]
17
+ end
14
18
  end
15
19
  end
16
20
 
17
- def activated()
21
+ def setup()
18
22
  super
19
23
  history.disable do
20
24
  colors[7].click
@@ -32,7 +36,7 @@ class Reight::SpriteEditor < Reight::App
32
36
 
33
37
  def key_pressed()
34
38
  super
35
- shift, ctrl, cmd = %i[shift control command].map {pressing? _1}
39
+ shift, ctrl, cmd = [SHIFT, CONTROL, COMMAND].map {pressing? _1}
36
40
  ch = chips
37
41
  case key_code
38
42
  when LEFT then ch.set_frame ch.x - ch.size, ch.y, ch.size, ch.size
@@ -54,39 +58,43 @@ class Reight::SpriteEditor < Reight::App
54
58
 
55
59
  def window_resized()
56
60
  super
57
- [colors, tools, chip_sizes, brush_sizes].flatten.map(&:sprite)
61
+ [chip_sizes, tools, colors, brush_sizes].flatten.map(&:sprite)
58
62
  .each {|sp| sp.w = sp.h = BUTTON_SIZE}
59
63
 
64
+ chips_index.sprite.tap do |sp|
65
+ sp.w, sp.h = INDEX_SIZE, BUTTON_SIZE
66
+ sp.x = SPACE
67
+ sp.y = NAVIGATOR_HEIGHT + SPACE
68
+ end
69
+ chip_sizes.reverse.map {_1.sprite}.each.with_index do |sp, index|
70
+ sp.x = SPACE + CHIPS_WIDTH - (sp.w + (sp.w + 1) * index)
71
+ sp.y = chips_index.sprite.y
72
+ end
60
73
  chips.sprite.tap do |sp|
61
74
  sp.x = SPACE
62
- sp.y = NAVIGATOR_HEIGHT + SPACE
63
- sp.w = CHIPS_WIDTH
75
+ sp.y = chip_sizes.last.sprite.bottom + SPACE
76
+ sp.right = chip_sizes.last.sprite.right
64
77
  sp.bottom = height - SPACE
65
78
  end
66
79
  colors.map {_1.sprite}.each.with_index do |sp, index|
67
- sp.x = chips.sprite.right + SPACE + sp.w * (index % 4)
68
- sp.y = height - (SPACE + sp.h * (4 - index / 4))
80
+ sp.h *= 0.8
81
+ sp.x = chips.sprite.right + SPACE + sp.w * (index % 8)
82
+ sp.y = height - (SPACE + sp.h * (4 - index / 8))
69
83
  end
70
84
  tools.map {_1.sprite}.each.with_index do |sp, index|
71
- line = index < 3 ? 0 : 1
72
- index -= 3 if line == 1
73
- sp.x = colors.last.sprite.right + SPACE + (sp.w + 1) * index
74
- sp.y = colors.first.sprite.y + (sp.h + 1) * line
85
+ sp.x = colors.first.sprite.x + (sp.w + 1) * index
86
+ sp.y = colors.first.sprite.y - (SPACE / 2 + sp.h)
75
87
  end
76
88
  canvas.sprite.tap do |sp|
77
- sp.x = chips.sprite.right + SPACE
78
- sp.y = chips.sprite.y
79
- sp.bottom = colors.first.sprite.top - SPACE
89
+ sp.x = chip_sizes.last.sprite.right + SPACE
90
+ sp.y = chip_sizes.last.sprite.y
91
+ sp.bottom = tools.first.sprite.top - SPACE / 2
80
92
  sp.w = sp.h
81
93
  end
82
- chip_sizes.map {_1.sprite}.each.with_index do |sp, index|
94
+ brush_sizes.map {_1.sprite}.each.with_index do |sp, index|
83
95
  sp.x = canvas.sprite.right + SPACE + (sp.w + 1) * index
84
96
  sp.y = canvas.sprite.y
85
97
  end
86
- brush_sizes.map {_1.sprite}.each.with_index do |sp, index|
87
- sp.x = chip_sizes.first.sprite.x + (sp.w + 1) * index
88
- sp.y = chip_sizes.last.sprite.bottom + SPACE
89
- end
90
98
  shapes.map {_1.sprite}.each.with_index do |sp, index|
91
99
  sp.w = 30
92
100
  sp.h = BUTTON_SIZE
@@ -149,7 +157,7 @@ class Reight::SpriteEditor < Reight::App
149
157
  canvas.deselect
150
158
  canvas.begin_editing do
151
159
  canvas.paint do |g|
152
- g.copy image, 0, 0, w, h, x, y, w, h
160
+ g.blend image, 0, 0, w, h, x, y, w, h, REPLACE
153
161
  end
154
162
  end
155
163
  canvas.select canvas.x + x, canvas.y + y, w, h
@@ -168,14 +176,13 @@ class Reight::SpriteEditor < Reight::App
168
176
  private
169
177
 
170
178
  def sprites()
171
- [canvas, chips, *chip_sizes, *colors, *tools, *brush_sizes, *shapes, *types]
179
+ [chips_index, *chip_sizes, chips, *tools, *colors, canvas, *brush_sizes, *shapes, *types]
172
180
  .map(&:sprite) + super
173
181
  end
174
182
 
175
- def chips()
176
- @chips ||= Chips.new self, project.chips_image do |x, y, w, h|
177
- canvas.set_frame x, y, w, h
178
- chip_changed x, y, w, h
183
+ def chips_index()
184
+ @chips_index ||= Reight::Index.new max: project.chips_npages - 1 do |index|
185
+ chips.offset = chips.index2offset index if index != chips.offset2index
179
186
  end
180
187
  end
181
188
 
@@ -187,6 +194,17 @@ class Reight::SpriteEditor < Reight::App
187
194
  })
188
195
  end
189
196
 
197
+ def chips()
198
+ @chips ||= Chips.new(self, project.chips).tap do |chips|
199
+ chips.frame_changed do |x, y, w, h|
200
+ chip_changed x, y, w, h
201
+ end
202
+ chips.offset_changed do |offset|
203
+ chips_index.index = chips.offset2index offset
204
+ end
205
+ end
206
+ end
207
+
190
208
  def tools()
191
209
  @tools ||= group(
192
210
  select,
@@ -209,6 +227,18 @@ class Reight::SpriteEditor < Reight::App
209
227
  def stroke_ellipse = @stroke_ellipse ||= Shape.new(self, :ellipse, false) {canvas.tool = _1}
210
228
  def fill_ellipse = @fill_ellipse ||= Shape.new(self, :ellipse, true) {canvas.tool = _1}
211
229
 
230
+ def colors()
231
+ @colors ||= project.palette_colors.map {|color|
232
+ rgb = self.color(color)
233
+ .then {[red(_1), green(_1), blue(_1), alpha(_1)]}.map &:to_i
234
+ Color.new(rgb) do
235
+ canvas.color = rgb
236
+ end.tap do |c|
237
+ canvas.color_changed {c.active = _1 == rgb}
238
+ end
239
+ }
240
+ end
241
+
212
242
  def brush_sizes()
213
243
  @brush_sizes ||= group(*[1, 2, 3, 5, 10].map {|size|
214
244
  Reight::Button.new name: "Button Size #{size}", label: size do
@@ -218,14 +248,6 @@ class Reight::SpriteEditor < Reight::App
218
248
  })
219
249
  end
220
250
 
221
- def colors()
222
- @colors ||= project.palette_colors.map {|color|
223
- rgb = self.color(color)
224
- .then {[red(_1), green(_1), blue(_1), alpha(_1)]}.map &:to_i
225
- Color.new(rgb) {canvas.color = rgb}
226
- }
227
- end
228
-
229
251
  def shapes()
230
252
  @shapes ||= group(
231
253
  Reight::Button.new(name: 'No Shape', label: 'None') {
@@ -252,21 +274,10 @@ class Reight::SpriteEditor < Reight::App
252
274
  end
253
275
 
254
276
  def chip_changed(x, y, w, h)
277
+ canvas.set_frame x, y, w, h
255
278
  chip = project.chips.at x, y, w, h
256
279
  shapes[[nil, :rect, :circle].index(chip.shape)].click
257
280
  types[chip.sensor? ? 1 : 0].click
258
281
  end
259
282
 
260
- def update_active_tool()
261
- tools.each do |tool|
262
- tool.active = tool == canvas.tool
263
- end
264
- end
265
-
266
- def update_active_color()
267
- colors.each do |button|
268
- button.active = button.color == canvas.color
269
- end
270
- end
271
-
272
283
  end# SpriteEditor
@@ -4,7 +4,7 @@ using Reight
4
4
  class Reight::SpriteEditor::Line < Reight::SpriteEditor::Tool
5
5
 
6
6
  def initialize(app, &block)
7
- super app, icon: app.icon(3, 2, 8), &block
7
+ super app, icon: app.icon(4, 2, 8), &block
8
8
  set_help left: name, right: 'Pick Color'
9
9
  end
10
10
 
@@ -6,7 +6,7 @@ class Reight::SpriteEditor::Shape < Reight::SpriteEditor::Tool
6
6
  def initialize(app, shape, fill, &block)
7
7
  @shape, @fill = shape, fill
8
8
  icon_index = [:rect, :ellipse].product([false, true]).index([shape, fill])
9
- super app, icon: app.icon(icon_index + 4, 2, 8), &block
9
+ super app, icon: app.icon(icon_index + 5, 2, 8), &block
10
10
  set_help left: name, right: 'Pick Color'
11
11
  end
12
12
 
@@ -1,6 +1,5 @@
1
1
  require 'reight/app/sprite/editor'
2
2
  require 'reight/app/sprite/canvas'
3
- require 'reight/app/sprite/chips'
4
3
  require 'reight/app/sprite/color'
5
4
  require 'reight/app/sprite/tool'
6
5
  require 'reight/app/sprite/select'
data/lib/reight/app.rb CHANGED
@@ -8,15 +8,28 @@ class Reight::App
8
8
 
9
9
  SPACE = 6
10
10
  BUTTON_SIZE = 12
11
+ INDEX_SIZE = 36
11
12
  NAVIGATOR_HEIGHT = BUTTON_SIZE + 2
12
13
  CHIPS_WIDTH = 128
13
14
 
15
+ PALETTE_COLORS = %w[
16
+ #00000000 #742f29 #ab5236 #f18459 #f7cca9 #ee044e #b8023f #7e2553
17
+ #452d32 #5f574f #a28879 #c2c3c7 #fdf1e8 #f6acc5 #f277a8 #e40dab
18
+ #1d2c53 #3363b0 #42a5a1 #56adff #64dff6 #bd9adf #83759c #644788
19
+ #1e5359 #2d8750 #3eb250 #4fe436 #95f041 #f8ec27 #f3a207 #e26b02
20
+ ]
21
+
14
22
  def initialize(project)
15
23
  @project = project
24
+ @active = false
16
25
  end
17
26
 
18
27
  attr_reader :project
19
28
 
29
+ def label()
30
+ self.class.name.split('::').last.gsub(/([a-z])([A-Z])/) {"#{$1} #{$2}"}
31
+ end
32
+
20
33
  def flash(...)
21
34
  navigator.flash(...) if history.enabled?
22
35
  end
@@ -30,12 +43,12 @@ class Reight::App
30
43
  buttons
31
44
  end
32
45
 
33
- def pressing?(key)
34
- pressing_keys.include? key
46
+ def active?()
47
+ @active
35
48
  end
36
49
 
37
- def name()
38
- self.class.name
50
+ def pressing?(key)
51
+ pressing_keys.include? key
39
52
  end
40
53
 
41
54
  def history()
@@ -57,12 +70,18 @@ class Reight::App
57
70
 
58
71
  def activated()
59
72
  add_world world if world
73
+ @setup ||= true.tap {setup}
74
+ @active = true
60
75
  end
61
76
 
62
77
  def deactivated()
78
+ @active = false
63
79
  remove_world world if world
64
80
  end
65
81
 
82
+ def setup()
83
+ end
84
+
66
85
  def draw()
67
86
  navigator.draw
68
87
  end
@@ -112,7 +131,7 @@ class Reight::App
112
131
 
113
132
  def world()
114
133
  @world ||= SpriteWorld.new.tap do |w|
115
- w.add_sprite(*sprites)
134
+ sprites.each {w.add_sprite _1}
116
135
  end
117
136
  end
118
137
 
data/lib/reight/button.rb CHANGED
@@ -13,17 +13,19 @@ class Reight::Button
13
13
  super()
14
14
 
15
15
  hook :clicked
16
- self.clicked(&clicked)
16
+ self.clicked(&clicked) if clicked
17
17
  self.clicked {r8.flash name}
18
18
  end
19
19
 
20
+ attr_accessor :name, :icon, :label
21
+
20
22
  def draw()
21
23
  sp = sprite
22
24
  no_stroke
23
25
 
24
26
  if @label
25
27
  fill 210
26
- rect 0, pressing? ? 2 : 1, sp.w, sp.h - 1, 2
28
+ rect 0, pressing? ? 1 : 0, sp.w, sp.h, 2
27
29
  end
28
30
 
29
31
  if active?
@@ -39,12 +41,13 @@ class Reight::Button
39
41
  end
40
42
 
41
43
  if @label
44
+ y = pressing? ? 1 : 0
42
45
  text_size r8.project.font_size - 1
43
46
  text_align CENTER, CENTER
44
- fill 100, 100, 100
45
- text @label, 0, 1, sp.w, sp.h
46
- fill 255, 255, 255
47
- text @label, 0, 0, sp.w, sp.h
47
+ fill active? ? 250 : 230
48
+ text @label, 0, y + 1, sp.w, sp.h
49
+ fill active? ? 70 : 50
50
+ text @label, 0, y, sp.w, sp.h
48
51
  end
49
52
  end
50
53
 
@@ -72,7 +75,7 @@ class Reight::Button
72
75
  def disabled? = !enabled?
73
76
 
74
77
  def sprite()
75
- @sprite ||= Sprite.new(physics: false).tap do |sp|
78
+ @sprite ||= RubySketch::Sprite.new(physics: false).tap do |sp|
76
79
  sp.draw {draw}
77
80
  sp.mouse_pressed {pressed sp.mouse_x, sp.mouse_y}
78
81
  sp.mouse_released {released sp.mouse_x, sp.mouse_y}
data/lib/reight/chip.rb CHANGED
@@ -7,7 +7,7 @@ class Reight::Chip
7
7
 
8
8
  def initialize(id, image, x, y, w, h, pos: nil, shape: nil, sensor: nil)
9
9
  @id, @image, @x, @y, @w, @h, @pos, @shape, @sensor =
10
- id, image, x, y, w, h, pos, shape, (sensor || false)
10
+ id, image, x, y, w, h, pos, shape, (sensor || false)
11
11
  end
12
12
 
13
13
  attr_accessor :shape
@@ -27,6 +27,7 @@ class Reight::Chip
27
27
  def with(**kwargs)
28
28
  id, image, x, y, w, h, pos, shape, sensor =
29
29
  kwargs.values_at :id, :image, :x, :y, :w, :h, :pos, :shape, :sensor
30
+ #kwargs => {id:, image:, x:, y:, w:, h:, pos:, shape:, sensor:}
30
31
  self.class.new(
31
32
  id || @id,
32
33
  image || @image,
@@ -46,8 +47,9 @@ class Reight::Chip
46
47
  when :circle then [true, RubySketch::Circle.new(0, 0, w)]
47
48
  else [false, nil]
48
49
  end
49
- Sprite.new(
50
- 0, 0, w, h, image: image, offset: [x, y], shape: shape, physics: physics
50
+ Reight::Sprite.new(
51
+ 0, 0, w, h, chip: self,
52
+ image: image, offset: [x, y], shape: shape, physics: physics
51
53
  ).tap do |sp|
52
54
  sp.x, sp.y = pos.x, pos.y if pos
53
55
  if physics
@@ -57,6 +59,14 @@ class Reight::Chip
57
59
  end
58
60
  end
59
61
 
62
+ def sprite()
63
+ @sprite ||= to_sprite
64
+ end
65
+
66
+ def clear_sprite()
67
+ @sprite = nil
68
+ end
69
+
60
70
  def to_hash()
61
71
  {
62
72
  id: id, x: x, y: y, w: w, h: h
@@ -67,7 +77,12 @@ class Reight::Chip
67
77
  end
68
78
  end
69
79
 
70
- def <=>(o)
80
+ def inspect()
81
+ "#<#{self.class.name}:0x#{object_id}>"
82
+ end
83
+
84
+ # @private
85
+ def cmp__(o)
71
86
  a = [@id, @image.object_id, @x, @y, @w, @h, @pos, @shape, @sensor]
72
87
  b = o.instance_eval {[@id, @image.object_id, @x, @y, @w, @h, @pos, @shape, @sensor]}
73
88
  a <=> b
@@ -76,6 +91,7 @@ class Reight::Chip
76
91
  def self.restore(hash, image)
77
92
  id, x, y, w, h, pos, shape, sensor =
78
93
  hash.values_at :id, :x, :y, :w, :h, :pos, :shape, :sensor
94
+ #hash => {id:, x:, y:, w:, h:, pos:, shape:, sensor:}
79
95
  new(
80
96
  id, image, x, y, w, h, pos: pos&.then {create_vector(*_1)},
81
97
  shape: shape&.to_sym, sensor: sensor || false)
@@ -110,6 +126,10 @@ class Reight::ChipList
110
126
  @frame2chip[[x, y, w, h]] ||= create_chip x, y, w, h
111
127
  end
112
128
 
129
+ def each(&block)
130
+ @id2chip.values.each(&block)
131
+ end
132
+
113
133
  def to_hash()
114
134
  {next_id: @next_id, chips: @id2chip.values.map {_1.to_hash}}
115
135
  end
@@ -118,14 +138,20 @@ class Reight::ChipList
118
138
  @id2chip[id]
119
139
  end
120
140
 
121
- def <=>(o)
141
+ def inspect()
142
+ "#<#{self.class.name}:0x#{object_id}>"
143
+ end
144
+
145
+ # @private
146
+ def cmp__(o)
122
147
  a = [@image, @next_id, @id2chip, @frame2chip]
123
148
  b = o.instance_eval {[@image, @next_id, @id2chip, @frame2chip]}
124
149
  a <=> b
125
150
  end
126
151
 
127
152
  def self.restore(hash, image)
128
- hash => {next_id:, chips:}
153
+ next_id, chips = hash.values_at :next_id, :chips
154
+ #hash => {next_id:, chips:}
129
155
  new(image).tap do |obj|
130
156
  obj.instance_eval do
131
157
  @next_id = next_id
@@ -0,0 +1,168 @@
1
+ module Reight::Context
2
+
3
+ include Processing::GraphicsContext
4
+ include RubySketch::GraphicsContext
5
+
6
+ TIMER_PREFIX__ = '__r8__'
7
+
8
+ # @private
9
+ def initialize(rootContext, project)
10
+ @rootContext__, @project__ = rootContext, project
11
+ init__ Rays::Image.new(@rootContext__.width, @rootContext__.height)
12
+ end
13
+
14
+ # Returns the project object.
15
+ #
16
+ def project()
17
+ @project__
18
+ end
19
+
20
+ # @private
21
+ def screenOffset(*args)
22
+ unless args.empty?
23
+ args = args.flatten
24
+ x, y, z =
25
+ case arg = args.first
26
+ when Vector then [arg.x, arg.y, arg.z]
27
+ when Numeric then [args[0], args[1] || 0, args[2] || 0]
28
+ when nil then [0, 0, 0]
29
+ else raise ArgumentError
30
+ end
31
+ cx, cy, = @canvasFrame__
32
+ if cx && cy
33
+ zoom = spriteWorld__.zoom
34
+ x += cx / zoom
35
+ y += cy / zoom
36
+ end
37
+ spriteWorld__.offset = [x, y, z]
38
+ end
39
+ spriteWorld__.offset
40
+ end
41
+
42
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#size-instance_method
43
+ def size(width, height, **)
44
+ return if width == self.width || height == self.height
45
+ @resizeCanvas__ = [width, height]
46
+ end
47
+
48
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#createCanvas-instance_method
49
+ alias createCanvas size
50
+
51
+ # @see https://www.rubydoc.info/gems/processing/Processing/Context#mouseX-instance_method
52
+ def mouseX()
53
+ x, (cx, _) = @rootContext__.mouseX, @canvasFrame__
54
+ cx ? (x - cx) / spriteWorld__.zoom : x
55
+ end
56
+
57
+ # @see https://www.rubydoc.info/gems/processing/Processing/Context#mouseY-instance_method
58
+ def mouseY()
59
+ y, (_, cy) = @rootContext__.mouseY, @canvasFrame__
60
+ cy ? (y - cy) / spriteWorld__.zoom : y
61
+ end
62
+
63
+ # @see https://www.rubydoc.info/gems/processing/Processing/Context#pmouseX-instance_method
64
+ def pmouseX()
65
+ x, (cx, _) = @rootContext__.pmouseX, @canvasFrame__
66
+ cx ? (x - cx) / spriteWorld__.zoom : x
67
+ end
68
+
69
+ # @see https://www.rubydoc.info/gems/processing/Processing/Context#pmouseY-instance_method
70
+ def pmouseY()
71
+ y, (_, cy) = @rootContext__.pmouseY, @canvasFrame__
72
+ cy ? (y - cy) / spriteWorld__.zoom : y
73
+ end
74
+
75
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#createSprite-instance_method
76
+ def createSprite(*args, klass: nil, **kwargs, &block)
77
+ klass ||= Reight::Sprite
78
+ spriteWorld__.createSprite(*args, klass: klass, **kwargs, &block)
79
+ end
80
+
81
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#addSprite-instance_method
82
+ def addSprite(...)
83
+ spriteWorld__.addSprite(...)
84
+ end
85
+
86
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#removeSprite-instance_method
87
+ def removeSprite(...)
88
+ spriteWorld__.removeSprite(...)
89
+ end
90
+
91
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#gravity-instance_method
92
+ def gravity(...)
93
+ spriteWorld__.gravity(...)
94
+ end
95
+
96
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#setTimeout-instance_method
97
+ def setTimeout( *a, id: @rootContext__.nextTimerID__, **k, &b)
98
+ id = [TIMER_PREFIX__, id]
99
+ @rootContext__.setTimeout(*a, id: id, **k, &b)
100
+ end
101
+
102
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#setInterval-instance_method
103
+ def setInterval(*a, id: @rootContext__.nextTimerID__, **k, &b)
104
+ id = [TIMER_PREFIX__, id]
105
+ @rootContext__.setInterval(*a, id: id, **k, &b)
106
+ end
107
+
108
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#clearTimer-instance_method
109
+ def clearTimer(id)
110
+ @rootContext__.clearTimer [TIMER_PREFIX__, id]
111
+ end
112
+
113
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#clearTimeout-instance_method
114
+ def clearTimeout(id)
115
+ @rootContext__.clearTimeout [TIMER_PREFIX__, id]
116
+ end
117
+
118
+ # @see https://rubydoc.info/gems/rubysketch/RubySketch/Context#clearInterval-instance_method
119
+ def clearInterval(id)
120
+ @rootContext__.clearInterval [TIMER_PREFIX__, id]
121
+ end
122
+
123
+ # @private
124
+ def beginDraw__()
125
+ super
126
+ @painter__.__send__ :begin_paint
127
+ end
128
+
129
+ # @private
130
+ def endDraw__()
131
+ @painter__.__send__ :end_paint
132
+ super
133
+ resizeCanvas__
134
+ end
135
+
136
+ # @private
137
+ def spriteWorld__()
138
+ @spriteWorld__ ||= SpriteWorld.new(pixelsPerMeter: 8)
139
+ end
140
+
141
+ # @private
142
+ def resizeCanvas__()
143
+ w, h = @resizeCanvas__ || return
144
+ @resizeCanvas__ = nil
145
+ updateCanvas__ Rays::Image.new(w.to_i, h.to_i)
146
+
147
+ rootw, rooth = @rootContext__.width, @rootContext__.height
148
+ if w == rootw && h == rooth
149
+ @canvasFrame__ = nil
150
+ else
151
+ wide = w.to_f / h >= rootw.to_f / rooth
152
+ canvasw = wide ? rootw : rooth * (w.to_f / h)
153
+ canvash = wide ? rootw * (h.to_f / w) : rooth
154
+ @canvasFrame__ = [
155
+ (rootw - canvasw) / 2,
156
+ (rooth - canvash) / 2,
157
+ canvasw,
158
+ canvash
159
+ ].map(&:to_i)
160
+ end
161
+
162
+ spriteWorld__.zoom = wide ? rootw.to_f / w : rooth.to_f / h
163
+ end
164
+
165
+ # @private
166
+ attr_reader :canvasFrame__
167
+
168
+ end# Context
@@ -33,8 +33,8 @@ module Reight::Hookable
33
33
  @hookable_hooks ||= {}
34
34
  (@hookable_hooks[name] ||= []).push block
35
35
  end
36
- singleton_class.__send__ :define_method, "#{name}!" do |*args|
37
- @hookable_hooks&.[](name)&.each {|block| block.call(*args)}
36
+ singleton_class.__send__ :define_method, "#{name}!" do |*args, &block|
37
+ @hookable_hooks&.[](name)&.each {_1.call(*args, &block)}
38
38
  end
39
39
  end
40
40
  end