Class: RubySketch::Context

Inherits:
Processing::Context
  • Object
show all
Defined in:
lib/rubysketch/context.rb

Constant Summary collapse

Sprite =
RubySketch::Sprite
Circle =
RubySketch::Circle
Sound =
RubySketch::Sound

Instance Method Summary collapse

Instance Method Details

#addSprite(sprite) ⇒ Sprite

Adds the sprite to the physics engine.

Parameters:

  • sprite (Sprite)

    sprite object

Returns:



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

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



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

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



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

Parameters:

  • id (Object)

    The identifier of timeout or interval timer to stop

Returns:

  • (nil)

    nil



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.

Overloads:

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

    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) ⇒ Object

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

    Parameters:

    • img (Image)

      sprite image

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

    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) ⇒ Object

    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) ⇒ Object

    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) ⇒ Object

    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) ⇒ Object

    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



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.

Overloads:

  • #gravity(vec) ⇒ Object

    Parameters:

    • vec (Vector)

      gracity vector

  • #gravity(ary) ⇒ Object

    Parameters:

    • ary (Array<Numeric>)

      gravityX, gravityY

  • #gravity(x, y) ⇒ Object

    Parameters:

    • x (Numeric)

      x of gravity vector

    • y (Numeric)

      y of gracity vector



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.

Parameters:

  • path (String)

    path for sound file

Returns:

  • (Sound)

    sound object



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.

Parameters:

  • sprite (Sprite)

    sprite object

Returns:



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

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



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

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



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.

Parameters:

Returns:

  • (nil)

    nil



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

#vibratenil

Generates haptic feedback

Returns:

  • (nil)

    nil



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

def vibrate ()
  Reflex.vibrate
end