def test_update_settings_are_saved(self) -> None: self.load_bindings(self.default_binding()) self.keybindings.update_binding('y', BasicEvents.NONE) # ensure key change persists through saving self.keybindings = Keybindings() self.keybindings.preference_file = self.preference_file.name self.keybindings.load() self.assertEqual(self.keybindings.event_for_key('y'), BasicEvents.NONE)
def test_save_settings(self) -> None: self.load_bindings(self.default_binding()) new_prefs_file = tempfile.NamedTemporaryFile(mode='w') self.keybindings.preference_file = new_prefs_file.name self.keybindings.save() # load from new file self.keybindings = Keybindings() self.keybindings.preference_file = new_prefs_file.name self.keybindings.load() self.assertEqual(self.keybindings.event_for_key('y'), BasicEvents.SETTINGS)
def load_bindings(self, bindings: Dict[str, BasicEvents]) -> None: self.preference_file = tempfile.NamedTemporaryFile(mode='w') self.keybindings = Keybindings() self.keybindings.preference_file = self.preference_file.name BINDING = 'binding' KEY = 'key' with open(self.preference_file.name, 'w') as fake_csv: writer = csv.DictWriter(fake_csv, fieldnames=[BINDING, KEY]) writer.writeheader() for key, binding in bindings.items(): writer.writerow({BINDING: binding, KEY: key}) self.keybindings.load()
class Keyboard(EventListener): def __init__(self) -> None: super(Keyboard, self).__init__() self.bindings = Keybindings() def notify(self, event: EventType) -> None: if event == BasicEvents.TICK: self.handle_inputs() def handle_inputs(self) -> None: # Called for each game tick. We check our keyboard presses here. for pg_event in self.get_pygame_events(): # handle window manager closing our window if self.is_quit_event(pg_event): EventManager.post(BasicEvents.QUIT) # handle key down events elif pg_event.type == pygame.KEYDOWN: self.handle_keypress(pg_event.unicode) elif pg_event.type == pygame.KEYUP: pass elif pg_event.type == pygame.MOUSEBUTTONDOWN: self.handle_mouse_click() def handle_keypress(self, key_name: str) -> None: if self.get_binding(key_name) != BasicEvents.NONE: self.post_bound_event(key=key_name) else: input_event = InputEvent(event_type=BasicEvents.KEYPRESS, key=key_name) EventManager.post(input_event) def handle_mouse_click(self) -> None: mouse_event = self.mouse_event() EventManager.post(mouse_event) def mouse_event(self) -> InputEvent: return InputEvent(event_type=BasicEvents.MOUSE_CLICK, key='', mouse=self.mouse_pos()) def mouse_pos(self) -> Tuple[int, int]: return pygame.mouse.get_pos() def get_binding(self, key: str) -> EventType: return self.bindings.event_for_key(key) def post_bound_event(self, key: str) -> None: binding = self.get_binding(key) EventManager.post(BasicEvents(binding)) def get_pygame_events(self) -> List[pygame.event.EventType]: return pygame.event.get() def is_quit_event(self, pg_event: pygame.event.EventType) -> bool: if pg_event.type == pygame.QUIT: return True return pg_event.type == pygame.KEYDOWN and pg_event.key == pygame.K_ESCAPE
def setup_module(module): # Ensure that working directory is sixth_corp os.chdir(dirname(dirname(dirname(dirname(abspath(__file__)))))) # Turn off game animations combat_scene.ANIMATION = False initialize_pygame(no_UI=True) # Tests in other modules may change the bindings. Keybindings().load() EventManager.listeners.clear()
def render(self, screen: Screen, scene: Scene) -> None: assert isinstance(scene, InventoryScene) # Scene title, exit key, and error message inv_key = Keybindings().keys_for_event(BasicEvents.INVENTORY)[0] x, = rescale_horizontal(20) y_shift, = rescale_vertical(10) y = y_shift rect = screen.render_text('Inventory', _OVERLAY_FONT_SIZE, x, y, WHITE) y = rect.y + rect.h rect = screen.render_text('{} - Return'.format(inv_key), _OVERLAY_FONT_SIZE, x, y, WHITE) if scene.UI_error_message: x = rect.x + rect.w + 50 h = y + rect.h - y_shift y = y_shift screen.render_text(scene.UI_error_message, _ERROR_FONT_SIZE, x, y, RED, h=h) # Inventory slots, mods, and mod information layout = scene.layout scene_objects = layout.all_objects() if scene.selected_mod is not None: selected_mod_slots = scene.selected_mod.valid_slots() selected_mod_slots.append(SlotTypes.GROUND) # ground always valid else: selected_mod_slots = [] for obj in scene_objects: if obj is None: continue rects = layout.get_rects(obj) if isinstance(obj, SlotHeaderInfo): assert len(rects) == 1 _render_slot_header(obj, rects[0], selected_mod_slots, screen) elif isinstance(obj, SlotRowInfo): assert len(rects) == 1 _render_mod_slot(obj, rects[0], screen) elif isinstance(obj, SelectedModInfo): assert len(rects) == 1 _render_selected_mod_info(obj, rects[0], screen)
def __init__(self) -> None: super(Keyboard, self).__init__() self.bindings = Keybindings()
class KeybindingsTest(TestCase): def default_binding(self) -> Dict[str, BasicEvents]: return {'y': BasicEvents.SETTINGS, 'z': BasicEvents.SETTINGS} def load_bindings(self, bindings: Dict[str, BasicEvents]) -> None: self.preference_file = tempfile.NamedTemporaryFile(mode='w') self.keybindings = Keybindings() self.keybindings.preference_file = self.preference_file.name BINDING = 'binding' KEY = 'key' with open(self.preference_file.name, 'w') as fake_csv: writer = csv.DictWriter(fake_csv, fieldnames=[BINDING, KEY]) writer.writeheader() for key, binding in bindings.items(): writer.writerow({BINDING: binding, KEY: key}) self.keybindings.load() def test_load_settings(self) -> None: self.load_bindings(self.default_binding()) self.assertEqual(self.keybindings.event_for_key('y'), BasicEvents.SETTINGS) def test_save_settings(self) -> None: self.load_bindings(self.default_binding()) new_prefs_file = tempfile.NamedTemporaryFile(mode='w') self.keybindings.preference_file = new_prefs_file.name self.keybindings.save() # load from new file self.keybindings = Keybindings() self.keybindings.preference_file = new_prefs_file.name self.keybindings.load() self.assertEqual(self.keybindings.event_for_key('y'), BasicEvents.SETTINGS) def test_update_settings(self) -> None: self.load_bindings(self.default_binding()) self.keybindings.update_binding('y', BasicEvents.NONE) self.assertEqual(self.keybindings.event_for_key('y'), BasicEvents.NONE) def test_inverse_binding(self) -> None: bindings = self.default_binding() event = BasicEvents.SETTINGS self.load_bindings(bindings) actual = tuple(sorted((self.keybindings.keys_for_event(event)))) expected = tuple(sorted(k for k, v in bindings.items() if v == event)) assert actual == expected def test_inverse_binding_no_keys(self) -> None: bindings = self.default_binding() event = BasicEvents.DEBUG self.load_bindings(bindings) actual = tuple(sorted((self.keybindings.keys_for_event(event)))) assert actual == () expected = tuple(sorted(k for k, v in bindings.items() if v == event)) assert actual == expected def test_update_settings_are_saved(self) -> None: self.load_bindings(self.default_binding()) self.keybindings.update_binding('y', BasicEvents.NONE) # ensure key change persists through saving self.keybindings = Keybindings() self.keybindings.preference_file = self.preference_file.name self.keybindings.load() self.assertEqual(self.keybindings.event_for_key('y'), BasicEvents.NONE)