-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_menu_controller.py
129 lines (105 loc) · 4.45 KB
/
main_menu_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from types import ModuleType
from typing import Set, Tuple
import key
import key_binding_manager
import main_menu_model
import main_menu_view
import pygame
class MainMenuController:
_KEY_SELECT_CURRENT_MENU_ITEM = key.ModifiedKey(pygame.K_RETURN)
_KEY_UP = key.ModifiedKey(pygame.K_UP)
_KEY_DOWN = key.ModifiedKey(pygame.K_DOWN)
def __init__(self, game_key_binding_manager, new_game_func, quit_func):
self._game_key_binding_manager = game_key_binding_manager
self._is_enabled = True
entries = [
main_menu_model.Entry('New Game',
on_select_func=self._get_toggle_then_call_func_func(new_game_func),
on_hover_start_func=None,
on_hover_end_func=None,
does_close_menu=True),
main_menu_model.Entry('Controls',
on_select_func=None,
on_hover_start_func=lambda: self._view.set_right_area_enabled(True),
on_hover_end_func=lambda: self._view.set_right_area_enabled(False),
does_close_menu=False),
main_menu_model.Entry('Quit',
on_select_func=quit_func,
on_hover_start_func=None,
on_hover_end_func=None,
does_close_menu=False),
]
self._model = main_menu_model.MainMenuModel(entries, self._game_key_binding_manager)
self._view = main_menu_view.MainMenuView(self._model)
self._on_entry_index_changed()
self._is_dirty = False
def is_enabled(self):
return self._is_enabled
def toggle(self):
self._is_enabled = not self._is_enabled
self._is_dirty = True
def _get_toggle_then_call_func_func(self, func):
def toggle_then_call_func_func():
self.toggle()
func()
return toggle_then_call_func_func
def draw(self, surface):
self._view.draw(surface, self.is_enabled())
self._is_dirty = False
def is_dirty(self):
return self._is_dirty or self._view.is_dirty()
def tick(self):
self._view.tick()
def handle_event_key_down(self, modified_key):
"""
@param modified_key a key.ModifiedKey
@return True if the event is consumed by the handler and should not be further processed.
"""
self._is_dirty = False
if not self.is_enabled():
return False
# Pass through TOGGLE_MAIN_MENU and QUIT.
game_action = self._get_game_action(modified_key)
if (game_action == key_binding_manager.Action.TOGGLE_MAIN_MENU
or game_action == key_binding_manager.Action.QUIT):
self._is_dirty = True
return False
if modified_key == self._KEY_SELECT_CURRENT_MENU_ITEM:
self._select_current_menu_item()
elif modified_key == self._KEY_UP:
self._change_entry_index(-1)
elif modified_key == self._KEY_DOWN:
self._change_entry_index(1)
return True
def _select_current_menu_item(self):
entry = self._model.get_current_entry()
entry.select()
self._view.on_menu_item_selected()
def _change_entry_index(self, delta_index):
old_entry = self._model.get_current_entry()
old_entry.end_hover()
self._model.change_current_index(delta_index)
self._on_entry_index_changed()
def _on_entry_index_changed(self):
entry = self._model.get_current_entry()
entry.start_hover()
self._is_dirty = True
def _get_game_action(self, modified_key):
"""
@param modified_key a key.ModifiedKey
"""
return self._game_key_binding_manager.get_action(modified_key)
def run_tests(headless: bool) -> Tuple[Tuple[int, int], Set[ModuleType]]:
"""
@return ((failure_count, test_count), tested_modules)
"""
import sys
import test
return test.run_doctests(sys.modules[__name__],
module_dependencies=[key,
key_binding_manager,
main_menu_model,
main_menu_view],
headless=headless)
if __name__ == '__main__':
run_tests(headless=False)