npytabletracker/npytabletracker/ui.py

48 lines
1.8 KiB
Python
Raw Permalink Normal View History

"""UI elements for the npytabletracker application."""
import npyscreen
from npytabletracker.cmd import ManagementActionController
from npytabletracker.data import TableState
class CharacterList(npyscreen.MultiLineAction):
"""Display characters' initiative order and allow for operations on them."""
def __init__(self, *args, **keywords):
"""Set up some extra handlers when selecting characters in the list."""
super(CharacterList, self).__init__(*args, **keywords)
self.add_handlers({
">": self.when_init_move_down,
"<": self.when_init_move_up
})
def when_init_move_down(self, *args, **keywords):
"""Move the selected character down one spot in the initiative table."""
if self.cursor_line < len(self.parent.value.characters) - 1:
chars = self.parent.value.characters
pos = self.cursor_line
chars[pos], chars[pos+1] = chars[pos+1], chars[pos]
self.parent.update_list()
self.cursor_line += 1
def when_init_move_up(self, *args, **keywords):
"""Move the selected character up one spot in the initiative table."""
if self.cursor_line - 1 >= 0:
chars = self.parent.value.characters
pos = self.cursor_line
chars[pos], chars[pos-1] = chars[pos-1], chars[pos]
self.parent.update_list()
self.cursor_line -= 1
class TableTrackerDisplay(npyscreen.FormMuttActiveTraditional):
"""Create the high level form, which is organized like mutt/irssi."""
ACTION_CONTROLLER = ManagementActionController
DATA_CONTROLER = TableState
MAIN_WIDGET_CLASS = CharacterList
def update_list(self):
"""Update data in the window and represent it."""
self.wMain.display()