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.
201 202 203 204 |
# File 'lib/rubysketch/context.rb', line 201 def addSprite(sprite) @layer__.add sprite.getInternal__ if sprite sprite end |
#animate(seconds, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate with easing functions
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rubysketch/context.rb', line 108 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
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rubysketch/context.rb', line 133 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
76 77 78 79 |
# File 'lib/rubysketch/context.rb', line 76 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.
191 192 193 |
# File 'lib/rubysketch/context.rb', line 191 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.
274 275 276 277 278 279 280 281 282 |
# File 'lib/rubysketch/context.rb', line 274 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.
258 259 260 |
# File 'lib/rubysketch/context.rb', line 258 def loadSound(path) Sound.load path end |
#removeSprite(sprite) ⇒ Sprite
Removes the sprite from the physics engine.
212 213 214 215 |
# File 'lib/rubysketch/context.rb', line 212 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
47 48 49 50 51 52 |
# File 'lib/rubysketch/context.rb', line 47 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
33 34 35 36 |
# File 'lib/rubysketch/context.rb', line 33 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.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/rubysketch/context.rb', line 223 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
288 289 290 |
# File 'lib/rubysketch/context.rb', line 288 def vibrate () Reflex.vibrate end |