Class: MarsBase10::Viewport
- Inherits:
-
Object
- Object
- MarsBase10::Viewport
- Defined in:
- lib/mars_base_10/viewport.rb
Constant Summary collapse
- CURSOR_INVISIBLE =
0- CURSOR_VISIBLE =
1
Instance Attribute Summary collapse
-
#controller ⇒ Object
Returns the value of attribute controller.
-
#panes ⇒ Object
readonly
Returns the value of attribute panes.
-
#win ⇒ Object
readonly
Returns the value of attribute win.
Instance Method Summary collapse
- #action_bar ⇒ Object
- #action_bar=(an_action_bar) ⇒ Object
- #activate(pane:) ⇒ Object
-
#active_pane ⇒ Object
This is the pane in the Viewport which is actively accepting keyboard input.
-
#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.
-
#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.
- #add_variable_height_pane(at_row:, width_pct:) ⇒ Object
- #add_variable_width_pane(at_row: self.min_row, at_col: self.min_col, height_pct:) ⇒ Object
- #close ⇒ Object
- #dispose_panes ⇒ Object
-
#initialize ⇒ Viewport
constructor
A new instance of Viewport.
- #max_cols ⇒ Object
- #max_rows ⇒ Object
- #min_col ⇒ Object
- #min_row ⇒ Object
- #open ⇒ Object
Constructor Details
#initialize ⇒ Viewport
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 = 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
#controller ⇒ Object
Returns the value of attribute controller.
9 10 11 |
# File 'lib/mars_base_10/viewport.rb', line 9 def controller @controller end |
#panes ⇒ Object (readonly)
Returns the value of attribute panes.
10 11 12 |
# File 'lib/mars_base_10/viewport.rb', line 10 def panes @panes end |
#win ⇒ Object (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_bar ⇒ Object
35 36 37 38 39 |
# File 'lib/mars_base_10/viewport.rb', line 35 def return unless .nil? # Make a default action bar. Only movement for now. self. = 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 () = .display_on viewport: self end |
#activate(pane:) ⇒ Object
47 48 49 |
# File 'lib/mars_base_10/viewport.rb', line 47 def activate(pane:) @active_pane = pane end |
#active_pane ⇒ Object
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 .
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 |
#close ⇒ Object
107 108 109 |
# File 'lib/mars_base_10/viewport.rb', line 107 def close Curses.close_screen end |
#dispose_panes ⇒ Object
111 112 113 114 |
# File 'lib/mars_base_10/viewport.rb', line 111 def dispose_panes @active_pane = nil @panes = [] end |
#max_cols ⇒ Object
116 117 118 |
# File 'lib/mars_base_10/viewport.rb', line 116 def max_cols self.win.maxx end |
#max_rows ⇒ Object
120 121 122 |
# File 'lib/mars_base_10/viewport.rb', line 120 def max_rows self.win.maxy - 1 end |
#min_col ⇒ Object
124 125 126 |
# File 'lib/mars_base_10/viewport.rb', line 124 def min_col 0 end |
#min_row ⇒ Object
128 129 130 |
# File 'lib/mars_base_10/viewport.rb', line 128 def min_row 0 end |
#open ⇒ Object
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..draw self..window.refresh self.active_pane.process end end |