Class: RubySketch::Context

Inherits:
Processing::Context
  • Object
show all
Includes:
GraphicsContext
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

Methods included from GraphicsContext

#sprite

Instance Method Details

#addSprite(array = nil, sprite) ⇒ Sprite

Adds sprite to the physics engine.

Parameters:

  • array (Array) (defaults to: nil)

    user array to store the sprite

  • sprite (Sprite)

    sprite object

Returns:

  • (Sprite)

    the added sprite



277
278
279
# File 'lib/rubysketch/context.rb', line 277

def addSprite(array = nil, sprite)
  @world__.addSprite array, sprite
end

#addWorld(*worlds) ⇒ SpriteWorld

Adds worlds

Parameters:

Returns:



308
309
310
311
# File 'lib/rubysketch/context.rb', line 308

def addWorld(*worlds)
  worlds.each {@window__.add_overlay _1.getInternal__}
  worlds.first
end

#animate(seconds, id: nextTimerID__, easing: :expoOut, &block) ⇒ Object

Animate with easing functions

Parameters:

  • seconds (Numeric)

    Animation duration

  • id (Object) (defaults to: nextTimerID__)

    Timer object identifier

  • easing (Symbol) (defaults to: :expoOut)

    Easing function name

Returns:

  • (Object)

    Timer object identifier



181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rubysketch/context.rb', line 181

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

Parameters:

  • seconds (Numeric)

    Animation duration

  • from (Numeric, Vector)

    Value at the beggining of the animation

  • to (Numeric, Vector)

    Value at the end of the animation

  • id (Object) (defaults to: nextTimerID__)

    Timer object identifier

  • easing (Symbol) (defaults to: :expoOut)

    Easing function name

Returns:

  • (Object)

    Timer object identifier



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rubysketch/context.rb', line 206

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

Parameters:

  • id (Object)

    The identifier of timeout or interval timer to stop

Returns:

  • (nil)

    nil



149
150
151
152
# File 'lib/rubysketch/context.rb', line 149

def clearTimer(id)
  [@timers__, @firingTimers__].each {|timers| timers.delete id}
  nil
end

#controlChange(&block) ⇒ nil

Defines controlChange block.

Returns:

  • (nil)

    nil



93
94
95
96
# File 'lib/rubysketch/context.rb', line 93

def controlChange(&block)
  @controlChangeBlock__ = block if block
  nil
end

#controllerIndexNumeric

Returns the last controller index that was changed.

Returns:

  • (Numeric)

    last controller index



362
363
364
# File 'lib/rubysketch/context.rb', line 362

def controllerIndex()
  @controllerIndex__
end

#controllerValueNumeric

Returns the last controller value that was changed.

Returns:

  • (Numeric)

    last controller value



370
371
372
# File 'lib/rubysketch/context.rb', line 370

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.

Overloads:

  • #createSprite(x, y, w, h) ⇒ Sprite

    pos(x, y), size: [w, h]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • w (Numeric)

      width of the sprite

    • h (Numeric)

      height of the sprite

  • #createSprite(image: img) ⇒ Sprite

    pos: [0, 0], size: [image.width, image.height]

    Parameters:

    • img (Image)

      sprite image

  • #createSprite(x, y, image: img) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

  • #createSprite(x, y, image: img, offset: off) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height], offset: [offset.x, offset.x]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

    • off (Vector)

      offset of the sprite image

  • #createSprite(x, y, image: img, shape: shp) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

  • #createSprite(x, y, image: img, offset: off, shape: shp) ⇒ Sprite

    pos: [x, y], size: [image.width, image.height], offset: [offset.x, offset.x]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • img (Image)

      sprite image

    • off (Vector)

      offset of the sprite image

    • shp (Shape)

      shape of the sprite for physics calculations

  • #createSprite(x, y, shape: shp) ⇒ Sprite

    pos: [x, y], size: [shape.width, shape.height]

    Parameters:

    • x (Numeric)

      x of the sprite position

    • y (Numeric)

      y of the sprite position

    • shp (Shape)

      shape of the sprite for physics calculations

Returns:

  • (Sprite)

    the new sprite object



266
267
268
# File 'lib/rubysketch/context.rb', line 266

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.

Returns:



298
299
300
# File 'lib/rubysketch/context.rb', line 298

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.

Overloads:

  • #gravity(vec) ⇒ nil

    Parameters:

    • vec (Vector)

      gracity vector

  • #gravity(ary) ⇒ nil

    Parameters:

    • ary (Array<Numeric>)

      gravityX, gravityY

  • #gravity(x, y) ⇒ nil

    Parameters:

    • x (Numeric)

      x of gravity vector

    • y (Numeric)

      y of gracity vector

Returns:

  • (nil)

    nil



388
389
390
# File 'lib/rubysketch/context.rb', line 388

def gravity(...)
  @world__.gravity(...)
end

#loadSound(path) ⇒ Sound

Loads sound file.

Parameters:

  • path (String)

    path for sound file

Returns:

  • (Sound)

    sound object



330
331
332
# File 'lib/rubysketch/context.rb', line 330

def loadSound(path)
  Sound.load path
end

#noteFrequencyNumeric

Returns the last note frequency that was pressed or released.

Returns:

  • (Numeric)

    last note frequency



346
347
348
# File 'lib/rubysketch/context.rb', line 346

def noteFrequency()
  @noteFrequency__
end

#noteNumberNumeric

Returns the last note number that was pressed or released.

Returns:

  • (Numeric)

    last note number



338
339
340
# File 'lib/rubysketch/context.rb', line 338

def noteNumber()
  @noteNumber__
end

#notePressed(&block) ⇒ nil

Defines notePressed block.

Returns:

  • (nil)

    nil



75
76
77
78
# File 'lib/rubysketch/context.rb', line 75

def notePressed(&block)
  @notePressedBlock__ = block if block
  nil
end

#noteReleased(&block) ⇒ nil

Defines noteReleased block.

Returns:

  • (nil)

    nil



84
85
86
87
# File 'lib/rubysketch/context.rb', line 84

def noteReleased(&block)
  @noteReleasedBlock__ = block if block
  nil
end

#noteVelocityNumeric

Returns the last note velocity that was pressed or released.

Returns:

  • (Numeric)

    last note velocity



354
355
356
# File 'lib/rubysketch/context.rb', line 354

def noteVelocity()
  @noteVelocity__
end

#removeSprite(array = nil, sprite) ⇒ Sprite

Removes sprite from the physics engine.

Parameters:

  • array (Array) (defaults to: nil)

    user array to remove the sprite

  • sprite (Sprite)

    sprite object

Returns:

  • (Sprite)

    the removed sprite



288
289
290
# File 'lib/rubysketch/context.rb', line 288

def removeSprite(array = nil, sprite)
  @world__.removeSprite array, sprite
end

#removeWorld(*worlds) ⇒ SpriteWorld

Removes worlds

Parameters:

Returns:



319
320
321
322
# File 'lib/rubysketch/context.rb', line 319

def removeWorld(*worlds)
  worlds.each {@window__.remove_overlay _1.getInternal__}
  worlds.first
end

#setInterval(seconds = 0, *args, id: nextTimerID__, now: false, &block) ⇒ Object

Repeats block call at each interval

Parameters:

  • seconds (Numeric) (defaults to: 0)

    Each interval duration

  • args (Array)

    Arguments passed to block

  • id (Object) (defaults to: nextTimerID__)

    Timer object identifier

  • now (Boolean) (defaults to: false)

    Wheather or not to call the block right now

Returns:

  • (Object)

    Timer object identifier



120
121
122
123
124
125
# File 'lib/rubysketch/context.rb', line 120

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

Parameters:

  • seconds (Numeric) (defaults to: 0)

    Time at which the block is called

  • args (Array)

    Arguments passed to block

  • id (Object) (defaults to: nextTimerID__)

    Timer object identifier

Returns:

  • (Object)

    Timer object identifier



106
107
108
109
# File 'lib/rubysketch/context.rb', line 106

def setTimeout(seconds = 0, *args, id: nextTimerID__, &block)
  return unless block
  setTimeout__ id, Time.now.to_f + seconds, args, &block
end

#vibratenil

Generates haptic feedback

Returns:

  • (nil)

    nil



396
397
398
# File 'lib/rubysketch/context.rb', line 396

def vibrate()
  Reflex.vibrate
end