Class: UT::Viewport

Inherits:
Object
  • Object
show all
Defined in:
lib/ut/viewport.rb

Overview

The viewport stores the *Tile*s that should be drawn onto the screen as well as their location on the screen. ‘Viewport.new` takes an `options` hash as its only parameter. The `options` hash respects three members:

* `:renderer`: The renderer that renders the tiles. _Defaults to `nil`._

* `:left`: Specifies the horizontal offset in _pixels_ from the left side
of the screen. _Defaults to 0_.

* `:top`: Specifies the vertical offset in _pixels_ from the top of the
screen. _Defaults to 0_.

* `:width`: The width of the *Viewport* in _tiles_.

* `:height`: The height of the *Viewport* in _tiles_.

* `:wrap_mode`: The wrap mode for strings. _Defaults to `:block`_.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Viewport

Returns a new instance of Viewport.



44
45
46
47
48
49
50
51
52
53
# File 'lib/ut/viewport.rb', line 44

def initialize options={}
  self.renderer = options[:renderer]
  self.left = options[:left] || 0
  self.top = options[:top] || 0
  self.width = options[:width]
  self.height = options[:height]
  self.wrap_mode = options[:wrap_mode] || :block

  @tiles = {}
end

Instance Attribute Details

#heightObject

Width and height of the viewport measured in tiles.



33
34
35
# File 'lib/ut/viewport.rb', line 33

def height
  @height
end

#leftObject

Coordinates of the top-left corner of the Viewport.



31
32
33
# File 'lib/ut/viewport.rb', line 31

def left
  @left
end

#rendererObject

The renderer used to draw the tiles on screen. Must repond to:

* `tile_size`: Returns the size of the rendered tile in _pixel_.

* `render(tile, left, top)`: Renders the `tile` on screen with the
top-left corner of the tile located at `[left, top]`.


29
30
31
# File 'lib/ut/viewport.rb', line 29

def renderer
  @renderer
end

#topObject

Coordinates of the top-left corner of the Viewport.



31
32
33
# File 'lib/ut/viewport.rb', line 31

def top
  @top
end

#widthObject

Width and height of the viewport measured in tiles.



33
34
35
# File 'lib/ut/viewport.rb', line 33

def width
  @width
end

#wrap_modeObject

A flag which tells the Viewport how to handle strings that are longer than ‘width`. Accepts the following values:

* `:line`: Wraps the string into the next line and starts at the
beginning of the line.
* `:block`: Wraps the string into the next line and starts at the same
x-coordinate.
* `:none`: No wrapping of strings


42
43
44
# File 'lib/ut/viewport.rb', line 42

def wrap_mode
  @wrap_mode
end

Instance Method Details

#center_xObject

The x-coordinate of the center of the Viewport in tiles.



111
112
113
# File 'lib/ut/viewport.rb', line 111

def center_x
  (width/2).floor
end

#center_yObject

The y-coordinate of the center of the Viewport in tiles.



116
117
118
# File 'lib/ut/viewport.rb', line 116

def center_y
  (height/2).floor
end

#clearObject

Clears all tiles in the Viewport



56
57
58
# File 'lib/ut/viewport.rb', line 56

def clear
  @tiles = {}
end

#drawObject

Draws the tiles onto the screen using the ‘renderer`



92
93
94
95
96
97
98
# File 'lib/ut/viewport.rb', line 92

def draw
  @tiles.each do |coords, tile|
    tleft = left + coords[0]*@renderer.tile_size
    ttop = top + coords[1]*@renderer.tile_size
    @renderer.render tile, tleft, ttop
  end
end

#put_string(x, y, string, foreground = nil, background = nil, wrap_mode = nil) ⇒ Object

Puts a ‘string` into the Viewport by creating a tile for each character and storing them in a line starting at `[x,y]`. The tiles colors can be specified by `foreground` and `background`. `wrap_mode` overrides the global `wrap_mode` of the Viewport.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ut/viewport.rb', line 73

def put_string x, y, string, foreground = nil, background = nil, wrap_mode = nil
  counter = 0
  string.each_char do |c|
    tx, ty = case wrap_mode || self.wrap_mode
             when :block
               [x+counter%(width-x), y+(counter/(width-x)).floor]
             when :line
               [(x+counter)%width, ty = y+((x+counter)/width).floor]
             else
               [x + counter, y]
             end
    put_tile tx, ty, (Tile.new :glyph => c,
                      :foreground => foreground,
                      :background => background)
    counter += 1;
  end
end

#put_tile(x, y, tile) ⇒ Object

Stores the ‘tile` at location `[x,y]`



61
62
63
64
65
66
67
# File 'lib/ut/viewport.rb', line 61

def put_tile x, y, tile
  if tile.nil?
    @tiles.delete [x,y]
  else
    @tiles[[x,y]] = tile
  end
end

#render_heightObject

The height of the Viewport on the screen in pixels.



106
107
108
# File 'lib/ut/viewport.rb', line 106

def render_height
  height * @renderer.tile_size
end

#render_widthObject

The width of the Viewport on the screen in pixels.



101
102
103
# File 'lib/ut/viewport.rb', line 101

def render_width
  width * @renderer.tile_size
end