Class: Zyps::WxCanvas

Inherits:
Object
  • Object
show all
Defined in:
lib/zyps/views/canvas/wx.rb

Overview

Called by View objects for use in wxRuby applications. Assign an instance to a View, and the drawing_area will be updated whenever the View is.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWxCanvas

Returns a new instance of WxCanvas.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zyps/views/canvas/wx.rb', line 37

def initialize

  #Will be resized later.
  @width, @height = 1, 1

  #Set to correct size.
  resize
  
  #Arrays of shapes that will be painted when render() is called.
  @rectangle_queue = []
  @line_queue = []
  
  #Hash of Wx::Pens used to draw in various colors and widths.
  @pens = Hash.new {|h, k| h[k] = Hash.new}
  #Hash of Wx::Brushes for various colors.
  @brushes = Hash.new
  
end

Instance Attribute Details

#bufferObject (readonly)

The Wx::Bitmap to draw to.



31
32
33
# File 'lib/zyps/views/canvas/wx.rb', line 31

def buffer
  @buffer
end

#heightObject

Dimensions of the drawing area. Control should normally be left to the owner View object.



34
35
36
# File 'lib/zyps/views/canvas/wx.rb', line 34

def height
  @height
end

#widthObject

Dimensions of the drawing area. Control should normally be left to the owner View object.



34
35
36
# File 'lib/zyps/views/canvas/wx.rb', line 34

def width
  @width
end

Instance Method Details

#draw_line(options = {}) ⇒ Object

Takes a hash with these keys and defaults: :color => nil :width => nil :x1 => nil :y1 => nil :x2 => nil :y2 => nil



92
93
94
# File 'lib/zyps/views/canvas/wx.rb', line 92

def draw_line(options = {})
  @line_queue << options
end

#draw_rectangle(options = {}) ⇒ Object

Takes a hash with these keys and defaults: :color => nil :border_width => 1 :filled => true :x => nil :y => nil :width => nil :height => nil



75
76
77
78
79
80
81
# File 'lib/zyps/views/canvas/wx.rb', line 75

def draw_rectangle(options = {})
  options = {
    :filled => true,
    :border_width => 1
  }.merge(options)
  @rectangle_queue << options
end

#renderObject

Draw all objects to the drawing area.



98
99
100
101
102
103
104
105
# File 'lib/zyps/views/canvas/wx.rb', line 98

def render
  buffer.draw do |surface|
    #Draw all queued rectangles.
    render_rectangles(surface)
    #Draw all queued lines.
    render_lines(surface)
  end
end