From 77b9d05823b6f334f16cbba06a457aebe3c393ed Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Sat, 8 Feb 2020 20:11:47 -0600 Subject: [PATCH] customize main widget to allow for customization this includes two test commands to move items up and down the initiative with < and > --- npytabletracker/ui.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/npytabletracker/ui.py b/npytabletracker/ui.py index e68e301..81b94d6 100644 --- a/npytabletracker/ui.py +++ b/npytabletracker/ui.py @@ -5,8 +5,43 @@ 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()