gui.py 4.9KB

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