Class: RubySketch::Window

Inherits:
Reflex::Window
  • Object
show all
Defined in:
lib/rubysketch/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width = 500, height = 500, *args, **kwargs, &block) ⇒ Window

Returns a new instance of Window.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rubysketch/window.rb', line 165

def initialize(width = 500, height = 500, *args, **kwargs, &block)
  RubySketch.instance_variable_set :@window, self

  @canvas_image   =
  @canvas_painter = nil
  @events         = []
  @auto_resize    = true
  @error          = nil

  painter.miter_limit = 10
  resize_canvas 1, 1

  @canvas_view = add Reflex::View.new {|v|
    v.on(:update)  {|e| on_canvas_update e}
    v.on(:draw)    {|e| on_canvas_draw e}
    v.on(:pointer) {|e| on_canvas_pointer e}
    v.on(:resize)  {|e| on_canvas_resize e}
  }

  add VirtualKey::View.new

  super(*args, size: [width, height], **kwargs, &block)
end

Instance Attribute Details

#after_drawObject

Returns the value of attribute after_draw.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def after_draw
  @after_draw
end

#auto_resizeObject

Returns the value of attribute auto_resize.



161
162
163
# File 'lib/rubysketch/window.rb', line 161

def auto_resize
  @auto_resize
end

#before_drawObject

Returns the value of attribute before_draw.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def before_draw
  @before_draw
end

#canvas_imageObject (readonly)

Returns the value of attribute canvas_image.



163
164
165
# File 'lib/rubysketch/window.rb', line 163

def canvas_image
  @canvas_image
end

#canvas_painterObject (readonly)

Returns the value of attribute canvas_painter.



163
164
165
# File 'lib/rubysketch/window.rb', line 163

def canvas_painter
  @canvas_painter
end

#drawObject

Returns the value of attribute draw.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def draw
  @draw
end

#key_downObject

Returns the value of attribute key_down.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def key_down
  @key_down
end

#key_upObject

Returns the value of attribute key_up.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def key_up
  @key_up
end

#motionObject

Returns the value of attribute motion.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def motion
  @motion
end

#pointer_downObject

Returns the value of attribute pointer_down.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def pointer_down
  @pointer_down
end

#pointer_dragObject

Returns the value of attribute pointer_drag.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def pointer_drag
  @pointer_drag
end

#pointer_moveObject

Returns the value of attribute pointer_move.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def pointer_move
  @pointer_move
end

#pointer_upObject

Returns the value of attribute pointer_up.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def pointer_up
  @pointer_up
end

#resizeObject

Returns the value of attribute resize.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def resize
  @resize
end

#setupObject

Returns the value of attribute setup.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def setup
  @setup
end

#updateObject

Returns the value of attribute update.



156
157
158
# File 'lib/rubysketch/window.rb', line 156

def update
  @update
end

Instance Method Details

#eventObject



196
197
198
# File 'lib/rubysketch/window.rb', line 196

def event()
  @events.last
end

#on_canvas_draw(e) ⇒ Object



229
230
231
232
# File 'lib/rubysketch/window.rb', line 229

def on_canvas_draw(e)
  draw_canvas {call_block @draw, e} if @draw
  e.painter.image @canvas_image
end

#on_canvas_pointer(e) ⇒ Object



234
235
236
237
238
239
240
241
# File 'lib/rubysketch/window.rb', line 234

def on_canvas_pointer(e)
  block = case e.type
    when :down then @pointer_down
    when :up   then @pointer_up
    when :move then e.drag? ? @pointer_drag : @pointer_move
  end
  draw_canvas {call_block block, e} if block
end

#on_canvas_resize(e) ⇒ Object



243
244
245
246
# File 'lib/rubysketch/window.rb', line 243

def on_canvas_resize(e)
  resize_canvas e.width, e.height if @auto_resize
  draw_canvas {call_block @resize, e} if @resize
end

#on_canvas_update(e) ⇒ Object



224
225
226
227
# File 'lib/rubysketch/window.rb', line 224

def on_canvas_update(e)
  call_block @update, e
  @canvas_view.redraw
end

#on_draw(e) ⇒ Object



208
209
210
# File 'lib/rubysketch/window.rb', line 208

def on_draw(e)
  update_canvas_view
end

#on_key(e) ⇒ Object



212
213
214
215
216
217
218
# File 'lib/rubysketch/window.rb', line 212

def on_key(e)
  block = case e.type
    when :down then @key_down
    when :up   then @key_up
  end
  draw_canvas {call_block block, e} if block
end

#on_motion(e) ⇒ Object



220
221
222
# File 'lib/rubysketch/window.rb', line 220

def on_motion(e)
  draw_canvas {call_block @motion, e} if @motion
end

#on_resize(e) ⇒ Object



204
205
206
# File 'lib/rubysketch/window.rb', line 204

def on_resize(e)
  on_canvas_resize e
end

#on_setupObject



200
201
202
# File 'lib/rubysketch/window.rb', line 200

def on_setup()
  call_block @setup, nil
end

#start(&block) ⇒ Object



189
190
191
192
193
194
# File 'lib/rubysketch/window.rb', line 189

def start(&block)
  draw_canvas do
    block.call if block
    on_setup
  end
end