Class: RichEngine::Canvas::Slot

Inherits:
RichEngine::Canvas show all
Defined in:
lib/rich_engine/canvas/slot.rb

Overview

A sub-section of a parent Canvas. All drawing operations are translated by the slot's origin (x, y).

Instance Attribute Summary collapse

Attributes inherited from RichEngine::Canvas

#canvas, #height, #width

Instance Method Summary collapse

Methods inherited from RichEngine::Canvas

#dimensions, #draw_circle, #draw_rect, #draw_sprite, #each_coord, #out_of_bounds?, #rows, #slot, #write_string

Constructor Details

#initialize(parent, x, y, width, height, bg: nil) ⇒ Slot

Returns a new instance of Slot.

Parameters:

  • parent (RichEngine::Canvas)

    the canvas this slot draws onto

  • x (Integer)

    the slot's left column on the parent canvas

  • y (Integer)

    the slot's top row on the parent canvas

  • width (Integer)

    the slot width

  • height (Integer)

    the slot height

  • bg (String, nil) (defaults to: nil)

    the slot's background fill, or nil to inherit the parent's



19
20
21
22
23
24
25
26
# File 'lib/rich_engine/canvas/slot.rb', line 19

def initialize(parent, x, y, width, height, bg: nil)
  @parent = parent
  @offset_x = x
  @offset_y = y
  @width = width
  @height = height
  @bg = bg
end

Instance Attribute Details

#bgString?

Returns the slot's background fill, or nil to inherit the parent's.

Returns:

  • (String, nil)

    the slot's background fill, or nil to inherit the parent's



10
11
12
# File 'lib/rich_engine/canvas/slot.rb', line 10

def bg
  @bg
end

Instance Method Details

#[](x, y) ⇒ String?

Read the cell at the slot-local (x, y), translated to parent space.

Parameters:

  • x (Integer)

    the slot-local column

  • y (Integer)

    the slot-local row

Returns:

  • (String, nil)

    the cell contents, or nil if out of the slot's bounds



34
35
36
37
# File 'lib/rich_engine/canvas/slot.rb', line 34

def [](x, y)
  return nil if out_of_bounds?(x, y)
  @parent[@offset_x + x, @offset_y + y]
end

#[]=(x, y, value) ⇒ void

This method returns an undefined value.

Write the cell at the slot-local (x, y), translated to parent space. Writes outside the slot are clipped.

Parameters:

  • x (Integer)

    the slot-local column

  • y (Integer)

    the slot-local row

  • value (String)

    the cell contents to write



46
47
48
49
# File 'lib/rich_engine/canvas/slot.rb', line 46

def []=(x, y, value)
  return if out_of_bounds?(x, y)
  @parent[@offset_x + x, @offset_y + y] = value
end

#clearvoid

This method returns an undefined value.

Clear the slot, filling it with its background (or the parent's if the slot has none).



55
56
57
58
59
60
# File 'lib/rich_engine/canvas/slot.rb', line 55

def clear
  fill_char = @bg || @parent.bg
  each_coord do |x, y|
    self[x, y] = fill_char
  end
end