class MarsBase10::ActionBar

Attributes

actions[RW]

Public Class Methods

Default() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 13
def self.Default
  ActionBar.new actions: {'j': 'Move Down', 'k': 'Move Up', 'q': 'Quit'}
end
new(actions:) click to toggle source
# File lib/mars_base_10/action_bar.rb, line 7
def initialize(actions:)
  @actions  = actions
  @viewport = nil
  @win      = nil
end

Public Instance Methods

actions_first_col() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 17
def actions_first_col
  (self.width - self.actions_width)/2
end
actions_width() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 21
def actions_width
  self.actions.values.inject(0) {|acc, item| acc += (3 + 2 + item.length + 2)}
end
add_action(a_hash) click to toggle source
# File lib/mars_base_10/action_bar.rb, line 25
def add_action(a_hash)
  self.actions = Hash[@actions.merge!(a_hash).sort]
  self
end
display_on(viewport:) click to toggle source
# File lib/mars_base_10/action_bar.rb, line 47
def display_on(viewport:)
  @viewport = viewport
end
draw() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 30
def draw
  self.window.attron(Curses.color_pair(2))
  self.window.setpos(0, 0)
  self.window.addstr("Actions:")
  self.window.addstr(" " * (self.actions_first_col - 8))

  self.actions.each do |key, value|
    self.window.attron(Curses::A_REVERSE)
    self.window.addstr(" #{key} ")
    self.window.attroff(Curses::A_REVERSE) # if item == self.index
    self.window.addstr("  #{value}  ")
  end

  self.window.addstr(" " * (self.width - (self.actions_first_col + self.actions_width)))
  self.window.attroff(Curses.color_pair(2))
end
first_col() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 51
def first_col
  0
end
first_row() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 55
def first_row
  @viewport.max_rows
end
height() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 59
def height
  1
end
remove_action(key) click to toggle source
# File lib/mars_base_10/action_bar.rb, line 63
def remove_action(key)
  self.actions.delete_if {|k, v| k == key}
  self
end
width() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 68
def width
  @viewport.max_cols
end
window() click to toggle source
# File lib/mars_base_10/action_bar.rb, line 72
def window
  return @win if @win
  @win = Curses::Window.new(self.height, self.width, self.first_row, self.first_col)
end