ui: finish character confirmation

This commit is contained in:
rr-
2018-11-05 21:04:39 +01:00
parent bec02d2b2c
commit b66b3fca18
2 changed files with 75 additions and 43 deletions
+6 -1
View File
@@ -2,6 +2,7 @@ import typing as T
import urwid import urwid
from pqcli.mechanic import Player
from pqcli.roster import Roster from pqcli.roster import Roster
from pqcli.ui.exit_view import ExitView from pqcli.ui.exit_view import ExitView
from pqcli.ui.new_game_view import NewGameView from pqcli.ui.new_game_view import NewGameView
@@ -72,9 +73,13 @@ class Ui:
def switch_to_new_game_view(self) -> None: def switch_to_new_game_view(self) -> None:
self.loop.widget = NewGameView() self.loop.widget = NewGameView()
self._connect("confirm", self.switch_to_game_view) self._connect("confirm", self.create_player)
self._connect("cancel", self.switch_to_roster_view) self._connect("cancel", self.switch_to_roster_view)
def create_player(self, player: Player) -> None:
self.roster.add_player(player)
self.switch_to_roster_view()
def switch_to_game_view(self, player_name: str) -> None: def switch_to_game_view(self, player_name: str) -> None:
raise NotImplementedError("not implemented") raise NotImplementedError("not implemented")
+69 -42
View File
@@ -5,12 +5,12 @@ import urwid
import urwid_readline import urwid_readline
from pqcli.config import CLASSES, PRIME_STATS, RACES from pqcli.config import CLASSES, PRIME_STATS, RACES
from pqcli.mechanic import StatsBuilder, generate_name from pqcli.mechanic import StatsBuilder, generate_name, create_player
from pqcli.ui.button import MenuButton from pqcli.ui.button import MenuButton
from pqcli.ui.line_box import LineBox from pqcli.ui.line_box import LineBox
class StatsBox(urwid.Filler): class StatsBox(LineBox):
def __init__(self) -> None: def __init__(self) -> None:
self.stats_builder = StatsBuilder() self.stats_builder = StatsBuilder()
self.stats = self.stats_builder.roll() self.stats = self.stats_builder.roll()
@@ -25,37 +25,19 @@ class StatsBox(urwid.Filler):
label_texts = [urwid.Text(f"{stat.value}: ") for stat in PRIME_STATS] label_texts = [urwid.Text(f"{stat.value}: ") for stat in PRIME_STATS]
super().__init__( super().__init__(
urwid.Pile( urwid.ListBox(
[ [
( urwid.Columns(
13, [urwid.Pile(label_texts), urwid.Pile(value_texts)]
LineBox( ),
urwid.ListBox( urwid.Divider(),
[ urwid.Columns([urwid.Text("Total: "), self.total_label]),
urwid.Columns( urwid.Divider(),
[ roll_button,
urwid.Pile(label_texts), unroll_button,
urwid.Pile(value_texts),
]
),
urwid.Divider(),
urwid.Columns(
[
urwid.Text("Total: "),
self.total_label,
]
),
urwid.Divider(),
roll_button,
unroll_button,
]
),
title="Stats",
),
)
] ]
), ),
valign="top", title="Stats",
) )
def on_roll_press(self, _user_data: T.Any) -> None: def on_roll_press(self, _user_data: T.Any) -> None:
@@ -79,37 +61,57 @@ class StatsBox(urwid.Filler):
class RaceBox(LineBox): class RaceBox(LineBox):
def __init__(self) -> None: def __init__(self) -> None:
race_checkboxes: T.List[urwid.RadioButton] = []
self.race = random.choice(RACES) self.race = random.choice(RACES)
radio_buttons: T.List[urwid.RadioButton] = []
for race in RACES: for race in RACES:
urwid.RadioButton( urwid.RadioButton(
group=race_checkboxes, group=radio_buttons,
label=race.name, label=race.name,
state=race.name == self.race.name, state=race.name == self.race.name,
on_state_change=self.on_state_change,
user_data=race,
) )
super().__init__(urwid.ListBox(race_checkboxes), title="Race")
super().__init__(urwid.ListBox(radio_buttons), title="Race")
def on_state_change(
self, _widget: urwid.RadioButton, new_state: bool, user_data: T.Any
) -> None:
if new_state:
self.race = user_data
class ClassBox(LineBox): class ClassBox(LineBox):
def __init__(self) -> None: def __init__(self) -> None:
class_checkboxes: T.List[urwid.RadioButton] = []
self.class_ = random.choice(CLASSES) self.class_ = random.choice(CLASSES)
radio_buttons: T.List[urwid.RadioButton] = []
for class_ in CLASSES: for class_ in CLASSES:
urwid.RadioButton( urwid.RadioButton(
group=class_checkboxes, group=radio_buttons,
label=class_.name, label=class_.name,
state=class_.name == self.class_.name, state=class_.name == self.class_.name,
on_state_change=self.on_state_change,
user_data=class_,
) )
super().__init__(urwid.ListBox(class_checkboxes), title="Class")
super().__init__(urwid.ListBox(radio_buttons), title="Class")
def on_state_change(
self, _widget: urwid.RadioButton, new_state: bool, user_data: T.Any
) -> None:
if new_state:
self.class_ = user_data
class NewGameView(urwid.Pile): class NewGameView(urwid.Pile):
signals = ["confirm", "cancel"] signals = ["confirm", "cancel"]
def __init__(self) -> None: def __init__(self) -> None:
race_box = RaceBox() self.race_box = RaceBox()
class_box = ClassBox() self.class_box = ClassBox()
stats_box = StatsBox() self.stats_box = StatsBox()
self.char_name_edit = urwid_readline.ReadlineEdit( self.char_name_edit = urwid_readline.ReadlineEdit(
"Name: ", generate_name() "Name: ", generate_name()
@@ -118,6 +120,19 @@ class NewGameView(urwid.Pile):
"Generate random name", on_press=self.on_generate_char_name_press "Generate random name", on_press=self.on_generate_char_name_press
) )
buttons_box = urwid.Filler(
urwid.Padding(
urwid.Pile(
[
MenuButton("Sold!", self.on_confirm_press),
MenuButton("Cancel", self.on_cancel_press),
]
),
width=20,
align="center",
)
)
super().__init__( super().__init__(
[ [
(1, urwid.Filler(self.char_name_edit)), (1, urwid.Filler(self.char_name_edit)),
@@ -132,9 +147,9 @@ class NewGameView(urwid.Pile):
), ),
urwid.Columns( urwid.Columns(
[ [
("weight", 2, race_box), ("weight", 2, self.race_box),
("weight", 2, class_box), ("weight", 2, self.class_box),
stats_box, urwid.Pile([(13, self.stats_box), buttons_box]),
] ]
), ),
] ]
@@ -147,6 +162,18 @@ class NewGameView(urwid.Pile):
def on_generate_char_name_press(self, _user_data: T.Any) -> None: def on_generate_char_name_press(self, _user_data: T.Any) -> None:
self.generate_random_char_name() self.generate_random_char_name()
def on_confirm_press(self, _user_data: T.Any) -> None:
player = create_player(
name=self.char_name_edit.edit_text,
race=self.race_box.race,
class_=self.class_box.class_,
stats=self.stats_box.stats,
)
self._emit("confirm", player)
def on_cancel_press(self, _user_data: T.Any) -> None:
self._emit("cancel")
def unhandled_input(self, key: str) -> bool: def unhandled_input(self, key: str) -> bool:
if key == "esc": if key == "esc":
self._emit("cancel") self._emit("cancel")