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)



180
181
182
# File 'lib/reight/context.rb', line 180

def canvasFrame__
  @canvasFrame__
end

Instance Method Details

#addSpriteObject



90
91
92
# File 'lib/reight/context.rb', line 90

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

#beginDraw__Object



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

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

#clearInterval(id) ⇒ Object



127
128
129
# File 'lib/reight/context.rb', line 127

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

#clearTimeout(id) ⇒ Object



122
123
124
# File 'lib/reight/context.rb', line 122

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

#clearTimer(id) ⇒ Object



117
118
119
# File 'lib/reight/context.rb', line 117

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

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



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

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

#endDraw__Object



138
139
140
141
142
# File 'lib/reight/context.rb', line 138

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

#gravityObject



100
101
102
# File 'lib/reight/context.rb', line 100

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

#initialize(rootContext, project, pixelDensity: 1) ⇒ Object



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

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

#mouseXObject



60
61
62
63
# File 'lib/reight/context.rb', line 60

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

#mouseYObject



66
67
68
69
# File 'lib/reight/context.rb', line 66

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

#pixelDensity(density = nil) ⇒ Object



54
55
56
57
# File 'lib/reight/context.rb', line 54

def pixelDensity(density = nil)
  size width, height, pixelDensity: density if density
  super()
end

#pmouseXObject



72
73
74
75
# File 'lib/reight/context.rb', line 72

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

#pmouseYObject



78
79
80
81
# File 'lib/reight/context.rb', line 78

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

#projectObject

Returns the project object.



19
20
21
# File 'lib/reight/context.rb', line 19

def project()
  @project__
end

#removeSpriteObject



95
96
97
# File 'lib/reight/context.rb', line 95

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

#resizeCanvas__Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/reight/context.rb', line 150

def resizeCanvas__()
  w, h, pd        = @resizeCanvas__ || return
  @resizeCanvas__ = nil

  @pixelDensity__ = pd if pd
  pd              = @pixelDensity__ == :auto ? nil : @pixelDensity__
  pd            ||= @rootContext__.pixelDensity
  return if w == width && h == height && pd == pixelDensity

  updateCanvas__ Rays::Image.new(w.to_i, h.to_i, pixel_density: pd)

  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



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

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



111
112
113
114
# File 'lib/reight/context.rb', line 111

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



105
106
107
108
# File 'lib/reight/context.rb', line 105

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

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



46
47
48
# File 'lib/reight/context.rb', line 46

def size(width, height, pixelDensity: nil)
  @resizeCanvas__ = [width, height, pixelDensity]
end

#spriteWorld__Object



145
146
147
# File 'lib/reight/context.rb', line 145

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