Class: RubySketch::Context
- Inherits:
-
Processing::Context
- Object
- Processing::Context
- RubySketch::Context
- Includes:
- GraphicsContext
- Defined in:
- lib/rubysketch/context.rb
Constant Summary collapse
- Sprite =
RubySketch::Sprite
- SpriteWorld =
RubySketch::SpriteWorld
- Circle =
RubySketch::Circle
- Sound =
RubySketch::Sound
- MML =
RubySketch::MML
Instance Method Summary collapse
-
#addSprite(array = nil, sprite) ⇒ Sprite
Adds sprite 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.
-
#controlChange(&block) ⇒ nil
Defines controlChange block.
-
#controllerIndex ⇒ Numeric
Returns the last controller index that was changed.
-
#controllerValue ⇒ Numeric
Returns the last controller value that was changed.
-
#createSprite(*args, **kwargs) ⇒ Sprite
Creates a new sprite and add it to physics engine.
-
#createWorld(pixelsPerMeter: 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.
-
#noteFrequency ⇒ Numeric
Returns the last note frequency that was pressed or released.
-
#noteNumber ⇒ Numeric
Returns the last note number that was pressed or released.
-
#notePressed(&block) ⇒ nil
Defines notePressed block.
-
#noteReleased(&block) ⇒ nil
Defines noteReleased block.
-
#noteVelocity ⇒ Numeric
Returns the last note velocity that was pressed or released.
-
#removeSprite(array = nil, sprite) ⇒ Sprite
Removes sprite 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.
-
#vibrate ⇒ nil
Generates haptic feedback.
Methods included from GraphicsContext
Instance Method Details
#addSprite(array = nil, sprite) ⇒ Sprite
Adds sprite to the physics engine.
278 279 280 |
# File 'lib/rubysketch/context.rb', line 278 def addSprite(array = nil, sprite) @world__.addSprite array, sprite end |
#addWorld(*worlds) ⇒ SpriteWorld
Adds worlds
309 310 311 312 |
# File 'lib/rubysketch/context.rb', line 309 def addWorld(*worlds) worlds.each {@window__. _1.getInternal__} worlds.first end |
#animate(seconds, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object
Animate with easing functions
182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rubysketch/context.rb', line 182 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
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/rubysketch/context.rb', line 207 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
150 151 152 153 |
# File 'lib/rubysketch/context.rb', line 150 def clearTimer(id) [@timers__, @firingTimers__].each {|timers| timers.delete id} nil end |
#controlChange(&block) ⇒ nil
Defines controlChange block.
94 95 96 97 |
# File 'lib/rubysketch/context.rb', line 94 def controlChange(&block) @controlChangeBlock__ = block if block nil end |
#controllerIndex ⇒ Numeric
Returns the last controller index that was changed.
363 364 365 |
# File 'lib/rubysketch/context.rb', line 363 def controllerIndex() @controllerIndex__ end |
#controllerValue ⇒ Numeric
Returns the last controller value that was changed.
371 372 373 |
# File 'lib/rubysketch/context.rb', line 371 def controllerValue() @controllerValue__ 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.
267 268 269 |
# File 'lib/rubysketch/context.rb', line 267 def createSprite(*args, **kwargs) @world__.createSprite(*args, context: self, **kwargs) end |
#createWorld(pixelsPerMeter: 0) ⇒ SpriteWorld
Creates a new world to calculate the physics of sprites.
299 300 301 |
# File 'lib/rubysketch/context.rb', line 299 def createWorld(pixelsPerMeter: 0) addWorld SpriteWorld.new pixelsPerMeter: pixelsPerMeter end |
#gravity(vec) ⇒ nil #gravity(ary) ⇒ nil #gravity(x, y) ⇒ nil
Sets gravity for the physics engine.
389 390 391 |
# File 'lib/rubysketch/context.rb', line 389 def gravity(...) @world__.gravity(...) end |
#loadSound(path) ⇒ Sound
Loads sound file.
331 332 333 |
# File 'lib/rubysketch/context.rb', line 331 def loadSound(path) Sound.load path end |
#noteFrequency ⇒ Numeric
Returns the last note frequency that was pressed or released.
347 348 349 |
# File 'lib/rubysketch/context.rb', line 347 def noteFrequency() @noteFrequency__ end |
#noteNumber ⇒ Numeric
Returns the last note number that was pressed or released.
339 340 341 |
# File 'lib/rubysketch/context.rb', line 339 def noteNumber() @noteNumber__ end |
#notePressed(&block) ⇒ nil
Defines notePressed block.
76 77 78 79 |
# File 'lib/rubysketch/context.rb', line 76 def notePressed(&block) @notePressedBlock__ = block if block nil end |
#noteReleased(&block) ⇒ nil
Defines noteReleased block.
85 86 87 88 |
# File 'lib/rubysketch/context.rb', line 85 def noteReleased(&block) @noteReleasedBlock__ = block if block nil end |
#noteVelocity ⇒ Numeric
Returns the last note velocity that was pressed or released.
355 356 357 |
# File 'lib/rubysketch/context.rb', line 355 def noteVelocity() @noteVelocity__ end |
#removeSprite(array = nil, sprite) ⇒ Sprite
Removes sprite from the physics engine.
289 290 291 |
# File 'lib/rubysketch/context.rb', line 289 def removeSprite(array = nil, sprite) @world__.removeSprite array, sprite end |
#removeWorld(*worlds) ⇒ SpriteWorld
Removes worlds
320 321 322 323 |
# File 'lib/rubysketch/context.rb', line 320 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
121 122 123 124 125 126 |
# File 'lib/rubysketch/context.rb', line 121 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
107 108 109 110 |
# File 'lib/rubysketch/context.rb', line 107 def setTimeout(seconds = 0, *args, id: nextTimerID__, &block) return unless block setTimeout__ id, Time.now.to_f + seconds, args, &block end |
#vibrate ⇒ nil
Generates haptic feedback
397 398 399 |
# File 'lib/rubysketch/context.rb', line 397 def vibrate() Reflex.vibrate end |