Class: Alchemist::Curses::ItemWindow

Inherits:
Object
  • Object
show all
Includes:
FFI::NCurses
Defined in:
lib/alchemist-server/curses/item_window.rb

Instance Method Summary collapse

Constructor Details

#initialize(label, size, line, column) ⇒ ItemWindow

Returns a new instance of ItemWindow.



6
7
8
9
10
11
# File 'lib/alchemist-server/curses/item_window.rb', line 6

def initialize(label, size, line, column)
  @label = label
  @width = label.length + size * 2
  @win = newwin 1, @width, line, column
  @items = ""
end

Instance Method Details

#drawObject



18
19
20
21
22
# File 'lib/alchemist-server/curses/item_window.rb', line 18

def draw
  wclear @win
  wprintw @win, "#{@label}#{@items.pad_to_unicode_monospace}"
  wrefresh @win
end

#have_user_selectObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/alchemist-server/curses/item_window.rb', line 24

def have_user_select
  wmove @win, 0, @label.length
  wrefresh @win

  while (c = getch) != KEY_RETURN
    case c
    when KEY_LEFT
      y,x = getyx @win
      new_x = [x - 2, @label.length].max
      wmove @win, 0, new_x
      wrefresh @win

    when KEY_RIGHT
      y,x = getyx @win
      new_x = [x + 2, @width - 1].min
      wmove @win, 0, new_x
      wrefresh @win

    when KEY_ESCAPE
      return nil
    end
  end

  y,x = getyx @win

  item_at x
end

#item_at(x) ⇒ Object



52
53
54
55
# File 'lib/alchemist-server/curses/item_window.rb', line 52

def item_at(x)
  inv_index = (x - @label.length) / 2
  @items[inv_index]
end

#update(items) ⇒ Object



13
14
15
16
# File 'lib/alchemist-server/curses/item_window.rb', line 13

def update(items)
  @items = items.force_encoding Encoding::UTF_8
  draw
end