From 4bb0e9250aebe17c29b2ca60d21b16f078efa894 Mon Sep 17 00:00:00 2001 From: rr- Date: Fri, 9 Nov 2018 22:53:07 +0100 Subject: [PATCH] ui: change paddings in game view --- pqcli/ui/data_table.py | 20 ++++++++++++++++++++ pqcli/ui/game_view.py | 14 +++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/pqcli/ui/data_table.py b/pqcli/ui/data_table.py index b124928..a05995c 100644 --- a/pqcli/ui/data_table.py +++ b/pqcli/ui/data_table.py @@ -33,12 +33,32 @@ class DataTable(NPile): (urwid.PACK, 1), ) ) + + self.resize_columns() self._rows.append(widgets) def delete_row(self, idx: int) -> None: del self.contents[idx] del self._rows[idx] + self.resize_columns() def delete_rows(self, start_idx: int, end_idx: int) -> None: del self.contents[start_idx:end_idx] del self._rows[start_idx:end_idx] + self.resize_columns() + + def resize_columns(self) -> None: + if not self._rows: + return + + for i, sizing in enumerate(self._columns): + if isinstance(sizing, tuple) and sizing[0] == urwid.PACK: + width = max( + row_widgets[i].pack()[0] for row_widgets in self._rows + ) + for item in self.contents: + columns = item[0] + columns.contents[i] = ( + columns.contents[i][0], + (urwid.GIVEN, width, None), + ) diff --git a/pqcli/ui/game_view.py b/pqcli/ui/game_view.py index c2e6ad4..bc43ecf 100644 --- a/pqcli/ui/game_view.py +++ b/pqcli/ui/game_view.py @@ -31,7 +31,7 @@ class CharacterSheetView(DoubleLineBox): self.stat_texts = {stat: urwid.Text("") for stat in list(StatType)} self.exp_bar = CustomProgressBar() - data_table = DataTable(columns=[(urwid.WEIGHT, 1), (urwid.WEIGHT, 1)]) + data_table = DataTable(columns=[(urwid.WEIGHT, 1), (urwid.WEIGHT, 2)]) data_table.add_row(urwid.Text("Name"), urwid.Text(player.name)) data_table.add_row(urwid.Text("Race"), urwid.Text(player.race.name)) data_table.add_row(urwid.Text("Class"), urwid.Text(player.class_.name)) @@ -82,9 +82,7 @@ class SpellBookView(CustomLineBox): self.player.spell_book.connect("add", self.sync_spell_add) self.player.spell_book.connect("change", self.sync_spell_change) - super().__init__( - ScrollBar(self.scrollable, padding=1), title="Spell Book" - ) + super().__init__(ScrollBar(self.scrollable), title="Spell Book") self.sync() def sync(self) -> None: @@ -102,6 +100,7 @@ class SpellBookView(CustomLineBox): for row_widgets in self.data_table.data_rows: if row_widgets[0].text == spell.name: row_widgets[1].set_text(to_roman(spell.level)) + self.data_table.resize_columns() class EquipmentView(CustomLineBox): @@ -139,7 +138,7 @@ class InventoryView(DoubleLineBox): def __init__(self, player: Player) -> None: self.player = player - self.gold_text = urwid.Text("") + self.gold_text = urwid.Text("", align=urwid.RIGHT) self.data_table = DataTable(columns=[(urwid.WEIGHT, 3), (urwid.PACK,)]) self.data_table.add_row(urwid.Text("Gold"), self.gold_text) self.scrollable = Scrollable(self.data_table) @@ -155,7 +154,7 @@ class InventoryView(DoubleLineBox): ) super().__init__( - top_widget=ScrollBar(self.scrollable, padding=1), + top_widget=ScrollBar(self.scrollable), top_title="Inventory", bottom_widget=self.encumbrance_bar, bottom_title="Encumbrance", @@ -184,7 +183,8 @@ class InventoryView(DoubleLineBox): def sync_item_add(self, item: InventoryItem) -> None: self.data_table.add_row( - urwid.Text(item.name), urwid.Text(str(item.quantity)) + urwid.Text(item.name), + urwid.Text(str(item.quantity), align=urwid.RIGHT), ) self.scrollable.set_scrollpos(self.scrollable.rows_max() - 1)