|
@@ -1,14 +1,18 @@
|
1
|
1
|
# coding: utf-8
|
2
|
2
|
import typing
|
3
|
3
|
from tkinter import Tk
|
|
4
|
+from tkinter import Button
|
|
5
|
+from tkinter import YES
|
4
|
6
|
from tkinter import StringVar
|
5
|
7
|
from tkinter import OptionMenu
|
6
|
8
|
from tkinter import W
|
|
9
|
+from tkinter.ttk import Combobox
|
|
10
|
+from tkinter.ttk import Treeview
|
7
|
11
|
|
8
|
12
|
from synergine2.config import Config
|
9
|
13
|
|
10
|
14
|
from opencombat.gui import Gui
|
11
|
|
-from opencombat.strategy.manager import TroopManager
|
|
15
|
+from opencombat.strategy.team.stash import TeamStash
|
12
|
16
|
|
13
|
17
|
|
14
|
18
|
class SelectTroopsGui(Gui):
|
|
@@ -16,26 +20,105 @@ class SelectTroopsGui(Gui):
|
16
|
20
|
self,
|
17
|
21
|
config: Config,
|
18
|
22
|
master: Tk,
|
19
|
|
- troop_manager: TroopManager,
|
|
23
|
+ team_stash: TeamStash,
|
20
|
24
|
countries: typing.List[str],
|
21
|
25
|
) -> None:
|
22
|
26
|
super().__init__(config, master)
|
23
|
27
|
self._master.title('Troops selection')
|
|
28
|
+ self._team_stash = team_stash
|
|
29
|
+ self._countries_troops = {} # type: typing.Dict[str, typing.List[TeamModel]] # nopep8
|
24
|
30
|
|
25
|
31
|
# Widgets
|
26
|
|
- self.selected_country_var = StringVar(self._master)
|
27
|
|
- self.selected_country_var.set(countries[0])
|
28
|
|
- self.selected_country_var.trace('w', self.change_country)
|
29
|
|
- self.select_country_menu = OptionMenu(
|
|
32
|
+ self._selected_country_var = StringVar(self._master)
|
|
33
|
+ self._selected_country_var.set(countries[0])
|
|
34
|
+ self._selected_country_var.trace('w', self._country_changed)
|
|
35
|
+ self._select_country_menu = OptionMenu(
|
30
|
36
|
self._master,
|
31
|
|
- self.selected_country_var,
|
|
37
|
+ self._selected_country_var,
|
32
|
38
|
*countries,
|
33
|
39
|
)
|
34
|
40
|
|
|
41
|
+ self._teams_var = StringVar(self._master)
|
|
42
|
+ self._teams_list = Combobox(
|
|
43
|
+ self._master,
|
|
44
|
+ height=10,
|
|
45
|
+ state='readonly',
|
|
46
|
+ textvariable=self._teams_var,
|
|
47
|
+ )
|
|
48
|
+
|
|
49
|
+ self._add_troop_var = StringVar(self._master)
|
|
50
|
+ self._add_troop_button = Button(
|
|
51
|
+ self._master,
|
|
52
|
+ textvariable=self._add_troop_var,
|
|
53
|
+ command=self._add_troop,
|
|
54
|
+ )
|
|
55
|
+ self._add_troop_var.set('Add troop')
|
|
56
|
+
|
|
57
|
+ self._troops_view = Treeview(
|
|
58
|
+ self._master,
|
|
59
|
+ columns=('Soldiers',),
|
|
60
|
+ height=10,
|
|
61
|
+ )
|
|
62
|
+ self._troops_view.heading('#0', text='Team')
|
|
63
|
+ self._troops_view.heading('#1', text='Soldiers')
|
|
64
|
+ self._troops_view.column('#0', stretch=YES)
|
|
65
|
+ self._troops_view.column('#1', stretch=YES)
|
|
66
|
+
|
35
|
67
|
# Layout
|
36
|
|
- self.select_country_menu.grid(row=0, column=0, sticky=W)
|
|
68
|
+ self._select_country_menu.grid(row=0, column=0, sticky=W)
|
|
69
|
+ self._teams_list.grid(row=1, column=0, sticky=W)
|
|
70
|
+ self._add_troop_button.grid(row=2, column=0, sticky=W)
|
|
71
|
+ self._troops_view.grid(row=3, column=0, sticky=W)
|
|
72
|
+
|
|
73
|
+ # Default behaviours
|
|
74
|
+ self._selected_country_var.set(countries[0])
|
|
75
|
+ self._country_changed()
|
|
76
|
+
|
|
77
|
+ def _country_changed(self, *args, **kwargs) -> None:
|
|
78
|
+ country = self._selected_country_var.get()
|
37
|
79
|
|
38
|
|
- def change_country(self, *args, **kwargs) -> None:
|
39
|
80
|
self._logger.info('Change country to "{}"'.format(
|
40
|
|
- self.selected_country_var.get(),
|
|
81
|
+ country,
|
41
|
82
|
))
|
|
83
|
+ country_team_names = [
|
|
84
|
+ t.name for
|
|
85
|
+ t in self._team_stash.get_team_by_country(
|
|
86
|
+ self._selected_country_var.get(),
|
|
87
|
+ )
|
|
88
|
+ ]
|
|
89
|
+
|
|
90
|
+ self._logger.debug('Change teams for: "{}"'.format(country_team_names))
|
|
91
|
+ self._teams_list['values'] = country_team_names
|
|
92
|
+ self._teams_var.set(country_team_names[0])
|
|
93
|
+ self._update_troops_view(country)
|
|
94
|
+
|
|
95
|
+ def _add_troop(self, *args, **kwargs) -> None:
|
|
96
|
+ if self._teams_var.get():
|
|
97
|
+ country = self._selected_country_var.get()
|
|
98
|
+ team_name = self._teams_var.get()
|
|
99
|
+
|
|
100
|
+ self._logger.info('Add troop "{}" to country "{}" troops'.format(
|
|
101
|
+ team_name,
|
|
102
|
+ team_name,
|
|
103
|
+ ))
|
|
104
|
+
|
|
105
|
+ team_model = self._team_stash.get_team_by_name(
|
|
106
|
+ team_name=team_name,
|
|
107
|
+ team_country=country,
|
|
108
|
+ )
|
|
109
|
+ self._countries_troops.setdefault(country, []).append(
|
|
110
|
+ team_model,
|
|
111
|
+ )
|
|
112
|
+ self._update_troops_view(country)
|
|
113
|
+
|
|
114
|
+ def _update_troops_view(self, country: str) -> None:
|
|
115
|
+ teams = self._countries_troops.get(country, [])
|
|
116
|
+
|
|
117
|
+ self._troops_view.delete(*self._troops_view.get_children())
|
|
118
|
+ for team in teams:
|
|
119
|
+ self._troops_view.insert(
|
|
120
|
+ '',
|
|
121
|
+ 'end',
|
|
122
|
+ text=team.name,
|
|
123
|
+ values=('o' * len(team.units,))
|
|
124
|
+ )
|