Module: Reight::Context

Includes:
Processing::GraphicsContext, RubySketch::GraphicsContext
Defined in:
lib/reight/context.rb

Constant Summary collapse

TIMER_PREFIX__ =
'__r8__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#canvasFrame__Object (readonly)



166
167
168
# File 'lib/reight/context.rb', line 166

def canvasFrame__
  @canvasFrame__
end

Instance Method Details

#addSpriteObject



82
83
84
# File 'lib/reight/context.rb', line 82

def addSprite(...)
  spriteWorld__.addSprite(...)
end

#beginDraw__Object



124
125
126
127
# File 'lib/reight/context.rb', line 124

def beginDraw__()
  super
  @painter__.__send__ :begin_paint
end

#clearInterval(id) ⇒ Object



119
120
121
# File 'lib/reight/context.rb', line 119

def clearInterval(id)
  @rootContext__.clearInterval [TIMER_PREFIX__, id]
end

#clearTimeout(id) ⇒ Object



114
115
116
# File 'lib/reight/context.rb', line 114

def clearTimeout(id)
  @rootContext__.clearTimeout [TIMER_PREFIX__, id]
end

#clearTimer(id) ⇒ Object



109
110
111
# File 'lib/reight/context.rb', line 109

def clearTimer(id)
  @rootContext__.clearTimer [TIMER_PREFIX__, id]
end

#createSprite(*args, klass: nil, **kwargs, &block) ⇒ Object



76
77
78
79
# File 'lib/reight/context.rb', line 76

def createSprite(*args, klass: nil, **kwargs, &block)
  klass ||= Reight::Sprite
  spriteWorld__.createSprite(*args, klass: klass, **kwargs, &block)
end

#endDraw__Object



130
131
132
133
134
# File 'lib/reight/context.rb', line 130

def endDraw__()
  @painter__.__send__ :end_paint
  super
  resizeCanvas__
end

#gravityObject



92
93
94
# File 'lib/reight/context.rb', line 92

def gravity(...)
  spriteWorld__.gravity(...)
end

#initialize(rootContext, project) ⇒ Object



9
10
11
12
# File 'lib/reight/context.rb', line 9

def initialize(rootContext, project)
  @rootContext__, @project__ = rootContext, project
  init__ Rays::Image.new(@rootContext__.width, @rootContext__.height)
end

#mouseXObject



52
53
54
55
# File 'lib/reight/context.rb', line 52

def mouseX()
  x, (cx, _) = @rootContext__.mouseX,  @canvasFrame__
  cx ? (x - cx) / spriteWorld__.zoom : x
end

#mouseYObject



58
59
60
61
# File 'lib/reight/context.rb', line 58

def mouseY()
  y, (_, cy) = @rootContext__.mouseY,  @canvasFrame__
  cy ? (y - cy) / spriteWorld__.zoom : y
end

#pmouseXObject



64
65
66
67
# File 'lib/reight/context.rb', line 64

def pmouseX()
  x, (cx, _) = @rootContext__.pmouseX, @canvasFrame__
  cx ? (x - cx) / spriteWorld__.zoom : x
end

#pmouseYObject



70
71
72
73
# File 'lib/reight/context.rb', line 70

def pmouseY()
  y, (_, cy) = @rootContext__.pmouseY, @canvasFrame__
  cy ? (y - cy) / spriteWorld__.zoom : y
end

#projectObject

Returns the project object.



16
17
18
# File 'lib/reight/context.rb', line 16

def project()
  @project__
end

#removeSpriteObject



87
88
89
# File 'lib/reight/context.rb', line 87

def removeSprite(...)
  spriteWorld__.removeSprite(...)
end

#resizeCanvas__Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/reight/context.rb', line 142

def resizeCanvas__()
  w, h            = @resizeCanvas__ || return
  @resizeCanvas__ = nil
  updateCanvas__ Rays::Image.new(w.to_i, h.to_i)

  rootw, rooth = @rootContext__.width, @rootContext__.height
  if w == rootw && h == rooth
    @canvasFrame__ = nil
  else
    wide           = w.to_f / h >= rootw.to_f / rooth
    canvasw        = wide ? rootw                : rooth * (w.to_f / h)
    canvash        = wide ? rootw * (h.to_f / w) : rooth
    @canvasFrame__ = [
      (rootw - canvasw) / 2,
      (rooth - canvash) / 2,
      canvasw,
      canvash
    ].map(&:to_i)
  end

  spriteWorld__.zoom = wide ? rootw.to_f / w : rooth.to_f / h
end

#screenOffset(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/reight/context.rb', line 21

def screenOffset(*args)
  unless args.empty?
    args    = args.flatten
    x, y, z =
      case arg = args.first
      when Vector  then [arg.x,   arg.y,        arg.z]
      when Numeric then [args[0], args[1] || 0, args[2] || 0]
      when nil     then [0,       0,            0]
      else raise ArgumentError
      end
    cx, cy, = @canvasFrame__
    if cx && cy
      zoom = spriteWorld__.zoom
      x   += cx / zoom
      y   += cy / zoom
    end
    spriteWorld__.offset = [x, y, z]
  end
  spriteWorld__.offset
end

#setInterval(*a, id: @rootContext__.nextTimerID__, **k, &b) ⇒ Object



103
104
105
106
# File 'lib/reight/context.rb', line 103

def setInterval(*a, id: @rootContext__.nextTimerID__, **k, &b)
  @rootContext__.setInterval(*a, id: [TIMER_PREFIX__, id], **k, &b)
  id
end

#setTimeout(*a, id: @rootContext__.nextTimerID__, **k, &b) ⇒ Object



97
98
99
100
# File 'lib/reight/context.rb', line 97

def setTimeout( *a, id: @rootContext__.nextTimerID__, **k, &b)
  @rootContext__.setTimeout(*a, id: [TIMER_PREFIX__, id], **k, &b)
  id
end

#size(width, height) ⇒ Object Also known as: createCanvas



43
44
45
46
# File 'lib/reight/context.rb', line 43

def size(width, height, **)
  return if width == self.width || height == self.height
  @resizeCanvas__ = [width, height]
end

#spriteWorld__Object



137
138
139
# File 'lib/reight/context.rb', line 137

def spriteWorld__()
  @spriteWorld__ ||= SpriteWorld.new(pixelsPerMeter: 8)
end