From 03fcf4b3654bded8e83c2ac793cf8686ff2d6803 Mon Sep 17 00:00:00 2001 From: rr- Date: Mon, 5 Nov 2018 18:22:39 +0100 Subject: [PATCH] ui: add plot view --- pqcli/mechanic.py | 3 +- pqcli/ui/game_view.py | 52 ++++++++++++++++++++++++++++++++- pqcli/ui/read_only_check_box.py | 16 ++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 pqcli/ui/read_only_check_box.py diff --git a/pqcli/mechanic.py b/pqcli/mechanic.py index 9b03944..913d53b 100644 --- a/pqcli/mechanic.py +++ b/pqcli/mechanic.py @@ -239,7 +239,7 @@ class PlotTask(RegularTask): class Player(SignalMixin): - signals = ["new_task", "level_up"] + signals = ["new_task", "level_up", "complete_act"] def __init__( self, @@ -521,6 +521,7 @@ class Simulation: if self.player.act > 1: self.player.win_item() self.player.win_equipment() + self.player.emit("complete_act") def complete_quest(self) -> None: self.player.quest_book.quest_bar.reset(50 + random.below_low(1000)) diff --git a/pqcli/ui/game_view.py b/pqcli/ui/game_view.py index 8ace103..bff79af 100644 --- a/pqcli/ui/game_view.py +++ b/pqcli/ui/game_view.py @@ -4,10 +4,11 @@ import typing as T import urwid from pqcli.config import EquipmentType, StatType -from pqcli.lingo import to_roman +from pqcli.lingo import act_name, to_roman from pqcli.mechanic import InventoryItem, Player, Simulation, Spell from pqcli.ui.custom_line_box import CustomLineBox from pqcli.ui.custom_progress_bar import CustomProgressBar +from pqcli.ui.read_only_check_box import ReadOnlyCheckBox class CharacterSheetView(CustomLineBox): @@ -202,6 +203,53 @@ class InventoryView(urwid.Pile): return None +class PlotView(urwid.Pile): + cutoff = 100 + + def __init__(self, player: Player) -> None: + self.player = player + + self.list_box = urwid.ListBox([]) + self.plot_bar = CustomProgressBar() + + self.player.connect("complete_act", self.sync_act_add) + self.player.plot_bar.connect("change", self.sync_position) + self.sync_acts() + self.sync_position() + + super().__init__( + [ + CustomLineBox(self.list_box, title="Plot Development"), + (1, urwid.Filler(self.plot_bar)), + ] + ) + + def sync_acts(self) -> None: + del self.list_box.body[:] + for act_number in range( + max(0, self.player.act - self.cutoff), self.player.act + ): + self.list_box.body.append( + ReadOnlyCheckBox(act_name(act_number), state=True) + ) + self.list_box.body.append( + ReadOnlyCheckBox(act_name(self.player.act), state=False) + ) + + def sync_act_add(self): + del self.list_box.body[: -self.cutoff] + if self.list_box.body: + self.list_box.body[-1].set_state(True) + self.list_box.body.append( + ReadOnlyCheckBox(act_name(self.player.act), state=False) + ) + self.list_box.set_focus(len(self.list_box.body) - 1) + + def sync_position(self) -> None: + self.plot_bar.set_completion(self.player.plot_bar.position) + self.plot_bar.set_max(self.player.plot_bar.max_) + + class TaskView(urwid.Pile): def __init__(self, player: Player) -> None: self.player = player @@ -243,6 +291,7 @@ class GameView(urwid.Pile): self.spell_book_view = SpellBookView(player) self.equipment_view = EquipmentView(player) self.inventory_view = InventoryView(player) + self.plot_view = PlotView(player) self.task_view = TaskView(player) super().__init__( @@ -267,6 +316,7 @@ class GameView(urwid.Pile): [self.equipment_view, self.inventory_view] ), ), + ("weight", 2, urwid.Pile([self.plot_view])), ] ), ("pack", self.task_view), diff --git a/pqcli/ui/read_only_check_box.py b/pqcli/ui/read_only_check_box.py new file mode 100644 index 0000000..d6b4d89 --- /dev/null +++ b/pqcli/ui/read_only_check_box.py @@ -0,0 +1,16 @@ +import urwid + + +class ReadOnlyCheckBox(urwid.Text): + def __init__(self, text, state: bool = False) -> None: + self.icons = { + key: widget.text for key, widget in urwid.CheckBox.states.items() + } + self._caption = text + + super().__init__("") + + self.set_state(state) + + def set_state(self, state: bool) -> None: + self.set_text(self.icons[state] + " " + self._caption)