Class: RubySketch::Context
- Inherits:
-
Processing::Context
- Object
- Processing::Context
- RubySketch::Context
- Defined in:
- lib/rubysketch/context.rb
Constant Summary collapse
- Sprite =
RubySketch::Sprite
- Circle =
RubySketch::Circle
- Sound =
RubySketch::Sound
Instance Method Summary collapse
-
#addSprite(sprite) ⇒ Sprite
Adds the sprite to the physics engine.
-
#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) ⇒ Object
Creates a new sprite and add it to physics engine.
-
#gravity(*args) ⇒ Object
Sets gravity for the physics engine.
-
#loadSound(path) ⇒ Sound
Loads sound file.
-
#removeSprite(sprite) ⇒ Sprite
Removes the sprite from the physics engine.
-
#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(sprite) ⇒ Sprite
Adds the sprite to the physics engine.
199 200 201 202 |
# File 'lib/rubysketch/context.rb', line 199 def addSprite(sprite) @layer__.add sprite.getInternal__ if sprite sprite end |
#animate(seconds, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate with easing functions
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rubysketch/context.rb', line 106 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 0, id: id, &eachDrawBlock end end setTimeout 0, id: id, &eachDrawBlock end |
#animateValue(seconds, from:, to:, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate value with easing functions
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rubysketch/context.rb', line 131 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
74 75 76 77 |
# File 'lib/rubysketch/context.rb', line 74 def clearTimer(id) [@timers__, @firingTimers__].each {|timers| timers.delete id} nil end |
#createSprite(x, y, w, h) ⇒ Object #createSprite(image: img) ⇒ Object #createSprite(x, y, image: img) ⇒ Object #createSprite(x, y, image: img, offset: off) ⇒ Object #createSprite(x, y, image: img, shape: shp) ⇒ Object #createSprite(x, y, image: img, offset: off, shape: shp) ⇒ Object #createSprite(x, y, shape: shp) ⇒ Object
Creates a new sprite and add it to physics engine.
189 190 191 |
# File 'lib/rubysketch/context.rb', line 189 def createSprite(*args, **kwargs) addSprite Sprite.new(*args, **kwargs, context: self) end |
#gravity(vec) ⇒ Object #gravity(ary) ⇒ Object #gravity(x, y) ⇒ Object
Sets gravity for the physics engine.
272 273 274 275 276 277 278 279 280 |
# File 'lib/rubysketch/context.rb', line 272 def gravity(*args) x, y = case arg = args.first when Vector then arg.array when Array then arg else args end @layer__.gravity x, y end |
#loadSound(path) ⇒ Sound
Loads sound file.
256 257 258 |
# File 'lib/rubysketch/context.rb', line 256 def loadSound(path) Sound.load path end |
#removeSprite(sprite) ⇒ Sprite
Removes the sprite from the physics engine.
210 211 212 213 |
# File 'lib/rubysketch/context.rb', line 210 def removeSprite(sprite) @layer__.remove sprite.getInternal__ if sprite sprite end |
#setInterval(seconds = 0, *args, id: nextTimerID__, now: false, &block) ⇒ Object
Repeats block call at each interval
45 46 47 48 49 50 |
# File 'lib/rubysketch/context.rb', line 45 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
31 32 33 34 |
# File 'lib/rubysketch/context.rb', line 31 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.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/rubysketch/context.rb', line 221 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
286 287 288 |
# File 'lib/rubysketch/context.rb', line 286 def vibrate () Reflex.vibrate end |