Class: MarsBase10::Viewport

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

Constant Summary collapse

CURSOR_INVISIBLE =
0
CURSOR_VISIBLE =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeViewport

Returns a new instance of Viewport.



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

def initialize
  Curses.init_screen
  Curses.curs_set(CURSOR_INVISIBLE)
  Curses.noecho   # Do not echo characters typed by the user.

  Curses.start_color if Curses.has_colors?
  Curses.init_pair(1, Curses::COLOR_RED, Curses::COLOR_BLACK)
  Curses.init_pair(2, Curses::COLOR_BLACK, Curses::COLOR_CYAN)

  @active_pane = nil
  @controller = nil

  @action_bar = nil
  @panes = []

  # this is the whole visible drawing surface.
  # we don't ever draw on this, but we need it for reference.
  @win = Curses::Window.new 0, 0, 0, 0
end

Instance Attribute Details

#controllerObject

Returns the value of attribute controller.



9
10
11
# File 'lib/mars_base_10/viewport.rb', line 9

def controller
  @controller
end

#panesObject (readonly)

Returns the value of attribute panes.



10
11
12
# File 'lib/mars_base_10/viewport.rb', line 10

def panes
  @panes
end

#winObject (readonly)

Returns the value of attribute win.



10
11
12
# File 'lib/mars_base_10/viewport.rb', line 10

def win
  @win
end

Instance Method Details

#action_barObject



35
36
37
38
39
# File 'lib/mars_base_10/viewport.rb', line 35

def action_bar
  return @action_bar unless @action_bar.nil?
  # Make a default action bar. Only movement for now.
  self.action_bar = ActionBar.new actions: {'j': 'Move Down', 'k': 'Move Up', 'q': 'Quit'}
end

#action_bar=(an_action_bar) ⇒ Object



41
42
43
44
45
# File 'lib/mars_base_10/viewport.rb', line 41

def action_bar=(an_action_bar)
  @action_bar = an_action_bar
  @action_bar.display_on viewport: self
  @action_bar
end

#activate(pane:) ⇒ Object



47
48
49
# File 'lib/mars_base_10/viewport.rb', line 47

def activate(pane:)
  @active_pane = pane
end

#active_paneObject

This is the pane in the Viewport which is actively accepting keyboard input.



54
55
56
# File 'lib/mars_base_10/viewport.rb', line 54

def active_pane
  @active_pane
end

#add_pane(at_row: self.min_row, at_col: self.min_col, height_pct: 1, width_pct: 1) ⇒ Object

Adds a new drawable area (Pane) to the viewport. By default it is anchored to the top left. (min_row, min_col)

and full screen. (height and width 100%)


63
64
65
66
67
68
69
70
71
# File 'lib/mars_base_10/viewport.rb', line 63

def add_pane(at_row: self.min_row, at_col: self.min_col, height_pct: 1, width_pct: 1)
  p = MarsBase10::Pane.new viewport:   self,
                           at_row:     at_row,
                           at_col:     at_col,
                           height_pct: height_pct,
                           width_pct:  width_pct
  @panes << p
  @active_pane = p
end

#add_variable_both_pane(at_row: self.min_row, at_col: self.min_col) ⇒ Object

Adds a new variable width drawable area (VariableBothPane) to the

right-hand side of the viewport.

The caller must specify the upper left corner (at_row, at_col) but

after that it will automatically adjust its width based upon how
many columns the left pane(s) use.


81
82
83
84
85
86
87
# File 'lib/mars_base_10/viewport.rb', line 81

def add_variable_both_pane(at_row: self.min_row, at_col: self.min_col)
  p = VariableBothPane.new viewport: self,
                             at_row: at_row,
                             at_col: at_col
  @panes << p
  p
end

#add_variable_height_pane(at_row:, width_pct:) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/mars_base_10/viewport.rb', line 89

def add_variable_height_pane(at_row:, width_pct:)
  p = VariableHeightPane.new viewport: self,
                              at_row: at_row,
                              at_col: self.min_col,
                           width_pct: width_pct
  @panes << p
  p
end

#add_variable_width_pane(at_row: self.min_row, at_col: self.min_col, height_pct:) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/mars_base_10/viewport.rb', line 98

def add_variable_width_pane(at_row: self.min_row, at_col: self.min_col, height_pct:)
  p = VariableWidthPane.new viewport: self,
                              at_row: at_row,
                              at_col: at_col,
                          height_pct: height_pct
  @panes << p
  p
end

#closeObject



107
108
109
# File 'lib/mars_base_10/viewport.rb', line 107

def close
  Curses.close_screen
end

#dispose_panesObject



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

def dispose_panes
  @active_pane = nil
  @panes = []
end

#max_colsObject



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

def max_cols
  self.win.maxx
end

#max_rowsObject



120
121
122
# File 'lib/mars_base_10/viewport.rb', line 120

def max_rows
  self.win.maxy - 1
end

#min_colObject



124
125
126
# File 'lib/mars_base_10/viewport.rb', line 124

def min_col
  0
end

#min_rowObject



128
129
130
# File 'lib/mars_base_10/viewport.rb', line 128

def min_row
  0
end

#openObject



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/mars_base_10/viewport.rb', line 132

def open
  loop do
    self.panes.each do |pane|
      pane.draw
      pane.window.refresh
    end
    self.action_bar.draw
    self.action_bar.window.refresh
    self.active_pane.process
  end
end