Class: WhirledPeas::Graphics::Canvas
- Inherits:
-
Object
- Object
- WhirledPeas::Graphics::Canvas
- Defined in:
- lib/whirled_peas/graphics/canvas.rb
Overview
Canvas represent the area of the screen a painter can paint on.
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#left ⇒ Object
readonly
Returns the value of attribute left.
-
#top ⇒ Object
readonly
Returns the value of attribute top.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #child(child_left, child_top, child_width, child_height) ⇒ Object
- #hash ⇒ Object
-
#initialize(left, top, width, height) ⇒ Canvas
constructor
A new instance of Canvas.
- #inspect ⇒ Object
-
#stroke(stroke_left, stroke_top, raw, formatting = []) {|stroke_left, stroke_top, fstring| ... } ⇒ Object
Yields a single line of formatted characters positioned on the canvas, verifying only characters within the canvas are included.
- #writable? ⇒ Boolean
Constructor Details
#initialize(left, top, width, height) ⇒ Canvas
Returns a new instance of Canvas.
13 14 15 16 17 18 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 13 def initialize(left, top, width, height) @left = left @top = top @width = width @height = height end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
7 8 9 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 7 def height @height end |
#left ⇒ Object (readonly)
Returns the value of attribute left.
7 8 9 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 7 def left @left end |
#top ⇒ Object (readonly)
Returns the value of attribute top.
7 8 9 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 7 def top @top end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
7 8 9 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 7 def width @width end |
Class Method Details
.unwritable ⇒ Object
9 10 11 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 9 def self.unwritable new(-1, -1, 0, 0) end |
Instance Method Details
#==(other) ⇒ Object
109 110 111 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 109 def ==(other) other.is_a?(self.class) && hash == other.hash end |
#child(child_left, child_top, child_width, child_height) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 24 def child(child_left, child_top, child_width, child_height) Graphics.debugger( proc do "Create child: #{self.inspect}.child(left=#{child_left}, top=#{child_top}, width=#{child_width}, height=#{child_height})" end ) if child_left >= left + width self.class.unwritable elsif child_left + child_width <= left self.class.unwritable elsif child_top >= top + height self.class.unwritable elsif child_top + child_height <= top self.class.unwritable else if child_left < left child_width -= left - child_left child_left = left end child_width = [width - (child_left - left), child_width].min if child_top < top child_height -= top - child_top child_top = top end child_height = [height - (child_top - top), child_height].min child_canvas = self.class.new( child_left, child_top, child_width, child_height, ) Graphics.debugger(proc { " -> #{child_canvas.inspect}" }) child_canvas end end |
#hash ⇒ Object
105 106 107 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 105 def hash [left, top, width, height].hash end |
#inspect ⇒ Object
113 114 115 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 113 def inspect "Canvas(left=#{left}, top=#{top}, width=#{width}, height=#{height})" end |
#stroke(stroke_left, stroke_top, raw, formatting = []) {|stroke_left, stroke_top, fstring| ... } ⇒ Object
Yields a single line of formatted characters positioned on the canvas, verifying only characters within the canvas are included.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 62 def stroke(stroke_left, stroke_top, raw, formatting=[], &block) Graphics.debugger( proc do "Stroke: #{self.inspect}.stroke(left=#{stroke_left}, top=#{stroke_top}, length=#{raw.length})" end ) if stroke_left >= left + width # The stroke starts to the right of the canvas fstring = Utils::FormattedString.blank elsif stroke_left + raw.length <= left # The stroke ends to the left of the canvas fstring = Utils::FormattedString.blank elsif stroke_top < top # The stroke is above the canvas fstring = Utils::FormattedString.blank elsif stroke_top >= top + height # The stroke is below the canvas fstring = Utils::FormattedString.blank else # In this section, we know that at least part of the stroke should be visible # on the canvas. Chop off parts of the raw string that aren't within the # canvas boundary and ensure the stroke start position is also within the # canvas boundary # If the stroke starts to the left of the canvas, set the start index to the # first value that will be on the canvas, then update stroke_left to be on # the canvas start_index = stroke_left < left ? left - stroke_left : 0 stroke_left = left if stroke_left <= left # Determine how many characters from the stroke will fit on the canvas visible_length = [raw.length, width - (stroke_left - left)].min end_index = start_index + visible_length - 1 fstring = Utils::FormattedString.new(raw[start_index..end_index], formatting) end Graphics.debugger( proc do " -> Stroke(left=#{stroke_left}, top=#{stroke_top}, length=#{fstring.length})" end ) yield stroke_left, stroke_top, fstring end |
#writable? ⇒ Boolean
20 21 22 |
# File 'lib/whirled_peas/graphics/canvas.rb', line 20 def writable? width > 0 || height > 0 end |