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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rubysketch/window.rb', line 15

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(name: :canvas) {|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}
  }

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

Instance Attribute Details

#after_drawObject

Returns the value of attribute after_draw.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def after_draw
  @after_draw
end

#auto_resizeObject

Returns the value of attribute auto_resize.



11
12
13
# File 'lib/rubysketch/window.rb', line 11

def auto_resize
  @auto_resize
end

#before_drawObject

Returns the value of attribute before_draw.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def before_draw
  @before_draw
end

#canvas_imageObject (readonly)

Returns the value of attribute canvas_image.



13
14
15
# File 'lib/rubysketch/window.rb', line 13

def canvas_image
  @canvas_image
end

#canvas_painterObject (readonly)

Returns the value of attribute canvas_painter.



13
14
15
# File 'lib/rubysketch/window.rb', line 13

def canvas_painter
  @canvas_painter
end

#drawObject

Returns the value of attribute draw.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def draw
  @draw
end

#key_downObject

Returns the value of attribute key_down.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def key_down
  @key_down
end

#key_upObject

Returns the value of attribute key_up.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def key_up
  @key_up
end

#motionObject

Returns the value of attribute motion.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def motion
  @motion
end

#pointer_downObject

Returns the value of attribute pointer_down.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def pointer_down
  @pointer_down
end

#pointer_dragObject

Returns the value of attribute pointer_drag.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def pointer_drag
  @pointer_drag
end

#pointer_moveObject

Returns the value of attribute pointer_move.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def pointer_move
  @pointer_move
end

#pointer_upObject

Returns the value of attribute pointer_up.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def pointer_up
  @pointer_up
end

#resizeObject

Returns the value of attribute resize.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def resize
  @resize
end

#setupObject

Returns the value of attribute setup.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def setup
  @setup
end

#updateObject

Returns the value of attribute update.



6
7
8
# File 'lib/rubysketch/window.rb', line 6

def update
  @update
end

Instance Method Details

#eventObject



44
45
46
# File 'lib/rubysketch/window.rb', line 44

def event()
  @events.last
end

#on_canvas_draw(e) ⇒ Object



77
78
79
80
# File 'lib/rubysketch/window.rb', line 77

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

#on_canvas_pointer(e) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/rubysketch/window.rb', line 82

def on_canvas_pointer(e)
  block = case e.action
    when :down        then @pointer_down
    when :up, :cancel 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



91
92
93
94
# File 'lib/rubysketch/window.rb', line 91

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



72
73
74
75
# File 'lib/rubysketch/window.rb', line 72

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

#on_draw(e) ⇒ Object



56
57
58
# File 'lib/rubysketch/window.rb', line 56

def on_draw(e)
  update_canvas_view
end

#on_key(e) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/rubysketch/window.rb', line 60

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

#on_motion(e) ⇒ Object



68
69
70
# File 'lib/rubysketch/window.rb', line 68

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

#on_resize(e) ⇒ Object



52
53
54
# File 'lib/rubysketch/window.rb', line 52

def on_resize(e)
  on_canvas_resize e
end

#on_setupObject



48
49
50
# File 'lib/rubysketch/window.rb', line 48

def on_setup()
  call_block @setup, nil
end

#start(&block) ⇒ Object



37
38
39
40
41
42
# File 'lib/rubysketch/window.rb', line 37

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