Class: MarsBase10::ActionBar

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions:) ⇒ ActionBar

Returns a new instance of ActionBar.



7
8
9
10
11
# File 'lib/mars_base_10/action_bar.rb', line 7

def initialize(actions:)
  @actions  = actions
  @viewport = nil
  @win      = nil
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



5
6
7
# File 'lib/mars_base_10/action_bar.rb', line 5

def actions
  @actions
end

Class Method Details

.DefaultObject



13
14
15
# File 'lib/mars_base_10/action_bar.rb', line 13

def self.Default
  ActionBar.new actions: {'j': 'Move Down', 'k': 'Move Up', 'J': 'Page Down', 'K': 'Page Up', 'X': 'Switch App', 'q': 'Quit'}
end

Instance Method Details

#actions_first_colObject



17
18
19
# File 'lib/mars_base_10/action_bar.rb', line 17

def actions_first_col
  (self.width - self.actions_width)/2
end

#actions_widthObject



21
22
23
# 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) ⇒ Object



25
26
27
28
# 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:) ⇒ Object



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

def display_on(viewport:)
  @viewport = viewport
end

#drawObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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_colObject



51
52
53
# File 'lib/mars_base_10/action_bar.rb', line 51

def first_col
  0
end

#first_rowObject



55
56
57
# File 'lib/mars_base_10/action_bar.rb', line 55

def first_row
  @viewport.max_rows
end

#heightObject



59
60
61
# File 'lib/mars_base_10/action_bar.rb', line 59

def height
  1
end

#remove_actions(keys) ⇒ Object



63
64
65
66
# File 'lib/mars_base_10/action_bar.rb', line 63

def remove_actions(keys)
  self.actions.delete_if {|k, v| keys.include? k}
  self
end

#widthObject



68
69
70
# File 'lib/mars_base_10/action_bar.rb', line 68

def width
  @viewport.max_cols
end

#windowObject



72
73
74
75
# 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