Class: RubySketch::Context
- Inherits:
-
Processing::Context
- Object
- Processing::Context
- RubySketch::Context
- Defined in:
- lib/rubysketch/context.rb
Constant Summary collapse
- Sprite =
RubySketch::Sprite
- SpriteWorld =
RubySketch::SpriteWorld
- Circle =
RubySketch::Circle
- Sound =
RubySketch::Sound
Instance Method Summary collapse
-
#addSprite(*sprites) ⇒ Sprite
Adds sprites to the physics engine.
-
#addWorld(*worlds) ⇒ SpriteWorld
Adds worlds.
-
#animate(seconds, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate with easing functions.
-
#animateValue(seconds, from:, to:, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate value with easing functions.
-
#clearTimer(id) ⇒ nil
(also: #clearTimeout, #clearInterval)
Stops timeout or interval timer by id.
-
#createSprite(*args, **kwargs) ⇒ Sprite
Creates a new sprite and add it to physics engine.
-
#createWorld(pixels_per_meter: 0) ⇒ SpriteWorld
Creates a new world to calculate the physics of sprites.
-
#gravity ⇒ nil
Sets gravity for the physics engine.
-
#loadSound(path) ⇒ Sound
Loads sound file.
-
#removeSprite(*sprites) ⇒ Sprite
Removes sprites from the physics engine.
-
#removeWorld(*worlds) ⇒ SpriteWorld
Removes worlds.
-
#setInterval(seconds = 0, *args, id: nextTimerID__, now: false, &block) ⇒ Object
Repeats block call at each interval.
-
#setTimeout(seconds = 0, *args, id: nextTimerID__, &block) ⇒ Object
Calls block after specified seconds.
-
#sprite(*sprites) ⇒ nil
(also: #drawSprite)
Draws one or more sprites.
-
#vibrate ⇒ nil
Generates haptic feedback.
Instance Method Details
#addSprite(*sprites) ⇒ Sprite
Adds sprites to the physics engine.
205 206 207 |
# File 'lib/rubysketch/context.rb', line 205 def addSprite(*sprites) @world__.addSprite(*sprites) end |
#addWorld(*worlds) ⇒ SpriteWorld
Adds worlds
268 269 270 271 |
# File 'lib/rubysketch/context.rb', line 268 def addWorld(*worlds) worlds.each {@window__. _1.getInternal__} worlds.first end |
#animate(seconds, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate with easing functions
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/rubysketch/context.rb', line 110 def animate(seconds, id: nextTimerID__, easing: :expoOut, &block) fun = EASINGS[easing] or raise "'#{easing}' easing function not found" start = Time.now.to_f eachDrawBlock = lambda do t = (Time.now.to_f - start) / seconds if t >= 1.0 block.call fun.call(1.0), true else block.call fun.call(t), false setTimeout id: id, &eachDrawBlock end end setTimeout id: id, &eachDrawBlock end |
#animateValue(seconds, from:, to:, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate value with easing functions
135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/rubysketch/context.rb', line 135 def animateValue(seconds, from:, to:, id: nextTimerID__, easing: :expoOut, &block) if from.is_a? Vector animate seconds, id: id, easing: easing do |t, finished| block.call Vector.lerp(from, to, t), finished end else animate seconds, id: id, easing: easing do |t, finished| block.call lerp(from, to, t), finished end end end |
#clearTimer(id) ⇒ nil Also known as: clearTimeout, clearInterval
Stops timeout or interval timer by id
78 79 80 81 |
# File 'lib/rubysketch/context.rb', line 78 def clearTimer(id) [@timers__, @firingTimers__].each {|timers| timers.delete id} nil end |
#createSprite(x, y, w, h) ⇒ Sprite #createSprite(image: img) ⇒ Sprite #createSprite(x, y, image: img) ⇒ Sprite #createSprite(x, y, image: img, offset: off) ⇒ Sprite #createSprite(x, y, image: img, shape: shp) ⇒ Sprite #createSprite(x, y, image: img, offset: off, shape: shp) ⇒ Sprite #createSprite(x, y, shape: shp) ⇒ Sprite
Creates a new sprite and add it to physics engine.
195 196 197 |
# File 'lib/rubysketch/context.rb', line 195 def createSprite(*args, **kwargs) @world__.createSprite(*args, context: self, **kwargs) end |
#createWorld(pixels_per_meter: 0) ⇒ SpriteWorld
Creates a new world to calculate the physics of sprites.
258 259 260 |
# File 'lib/rubysketch/context.rb', line 258 def createWorld(pixels_per_meter: 0) addWorld SpriteWorld.new pixels_per_meter: pixels_per_meter end |
#gravity(vec) ⇒ nil #gravity(ary) ⇒ nil #gravity(x, y) ⇒ nil
Sets gravity for the physics engine.
308 309 310 |
# File 'lib/rubysketch/context.rb', line 308 def gravity(...) @world__.gravity(...) end |
#loadSound(path) ⇒ Sound
Loads sound file.
290 291 292 |
# File 'lib/rubysketch/context.rb', line 290 def loadSound(path) Sound.load path end |
#removeSprite(*sprites) ⇒ Sprite
Removes sprites from the physics engine.
215 216 217 |
# File 'lib/rubysketch/context.rb', line 215 def removeSprite(*sprites) @world__.removeSprite(*sprites) end |
#removeWorld(*worlds) ⇒ SpriteWorld
Removes worlds
279 280 281 282 |
# File 'lib/rubysketch/context.rb', line 279 def removeWorld(*worlds) worlds.each {@window__. _1.getInternal__} worlds.first end |
#setInterval(seconds = 0, *args, id: nextTimerID__, now: false, &block) ⇒ Object
Repeats block call at each interval
49 50 51 52 53 54 |
# File 'lib/rubysketch/context.rb', line 49 def setInterval(seconds = 0, *args, id: nextTimerID__, now: false, &block) return unless block time = Time.now.to_f block.call(*args) if now setInterval__ id, time, seconds, args, &block end |
#setTimeout(seconds = 0, *args, id: nextTimerID__, &block) ⇒ Object
Calls block after specified seconds
35 36 37 38 |
# File 'lib/rubysketch/context.rb', line 35 def setTimeout(seconds = 0, *args, id: nextTimerID__, &block) return unless block setTimeout__ id, Time.now.to_f + seconds, args, &block end |
#sprite(*sprites) ⇒ nil Also known as: drawSprite
Draws one or more sprites.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/rubysketch/context.rb', line 225 def sprite(*sprites) sprites.flatten! if sprites.first&.is_a? Array sprites.each do |sp| next if sp.hidden? view, draw = sp.getInternal__, sp.instance_variable_get(:@drawBlock__) f, degrees, pivot = view.frame, view.angle, view.pivot if draw push do translate f.x + pivot.x * f.w, f.y + pivot.y * f.h rotate fromDegrees__ degrees translate (-pivot.x) * f.w, (-pivot.y) * f.h draw.call {sp.draw__ self, 0, 0, f.w, f.h} end elsif degrees == 0 sp.draw__ self, f.x, f.y, f.w, f.h else pushMatrix do translate f.x + pivot.x * f.w, f.y + pivot.y * f.h rotate fromDegrees__ degrees translate (-pivot.x) * f.w, (-pivot.y) * f.h sp.draw__ self, 0, 0, f.w, f.h end end end nil end |
#vibrate ⇒ nil
Generates haptic feedback
316 317 318 |
# File 'lib/rubysketch/context.rb', line 316 def vibrate() Reflex.vibrate end |