Restructure event handling
Events start in the Interface. The interface gets first crack at any incoming events. If the interface doesn't handle the event, it is given to the engine. The engine has an EngineEventHandler that yields actions just like the event handler prior to this change. The interface's event handler passes events to each window in the interface. Windows can choose to handle events however they like, and they return a bool indicating whether the event was fully handled.
This commit is contained in:
parent
ee1c6f2222
commit
003aedf30e
6 changed files with 197 additions and 116 deletions
|
@ -1,8 +1,9 @@
|
|||
# Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
import numpy as np
|
||||
from tcod import event as tev
|
||||
from tcod.console import Console
|
||||
|
||||
from .. import log
|
||||
|
@ -12,11 +13,46 @@ from ..object import Entity, Hero
|
|||
|
||||
|
||||
class Window:
|
||||
'''A user interface window. It can be framed.'''
|
||||
'''A user interface window. It can be framed and it can handle events.'''
|
||||
|
||||
def __init__(self, bounds: Rect, *, framed: bool = True):
|
||||
class EventHandler(tev.EventDispatch[bool]):
|
||||
'''
|
||||
Handles events for a Window. Event dispatch methods return True if the event
|
||||
was handled and no further action is needed.
|
||||
'''
|
||||
|
||||
def __init__(self, window: 'Window'):
|
||||
super().__init__()
|
||||
self.window = window
|
||||
|
||||
def mouse_point_for_event(self, event: tev.MouseState) -> Point:
|
||||
'''
|
||||
Return the mouse point in tiles for a window event. Raises a ValueError
|
||||
if the event is not a mouse event.
|
||||
'''
|
||||
if not isinstance(event, tev.MouseState):
|
||||
raise ValueError("Can't get mouse point for non-mouse event")
|
||||
|
||||
return Point(event.tile.x, event.tile.y)
|
||||
|
||||
def ev_keydown(self, event: tev.KeyDown) -> bool:
|
||||
return False
|
||||
|
||||
def ev_keyup(self, event: tev.KeyUp) -> bool:
|
||||
return False
|
||||
|
||||
def ev_mousemotion(self, event: tev.MouseMotion) -> bool:
|
||||
mouse_point = self.mouse_point_for_event(event)
|
||||
|
||||
if mouse_point not in self.window.bounds:
|
||||
return False
|
||||
|
||||
return False
|
||||
|
||||
def __init__(self, bounds: Rect, *, framed: bool = True, event_handler: Optional['EventHandler'] = None):
|
||||
self.bounds = bounds
|
||||
self.is_framed = framed
|
||||
self.event_handler = event_handler or self.__class__.EventHandler(self)
|
||||
|
||||
@property
|
||||
def drawable_bounds(self) -> Rect:
|
||||
|
@ -28,6 +64,14 @@ class Window:
|
|||
return self.bounds.inset_rect(1, 1, 1, 1)
|
||||
return self.bounds
|
||||
|
||||
def convert_console_point(self, point: Point) -> Optional[Point]:
|
||||
'''
|
||||
Converts a point in console coordinates to window-relative coordinates.
|
||||
If the point is out of bounds of the window, return None.
|
||||
'''
|
||||
converted_point = point - Vector.from_point(self.bounds.origin)
|
||||
return converted_point if converted_point in self.bounds else None
|
||||
|
||||
def draw(self, console: Console):
|
||||
'''Draw the window to the conole'''
|
||||
if self.is_framed:
|
||||
|
@ -46,6 +90,20 @@ class Window:
|
|||
class MapWindow(Window):
|
||||
'''A Window that displays a game map'''
|
||||
|
||||
class EventHandler(Window.EventHandler):
|
||||
'''An event handler for the MapWindow.'''
|
||||
|
||||
def ev_mousemotion(self, event: tev.MouseMotion) -> bool:
|
||||
mouse_point = self.window.convert_console_point(self.mouse_point_for_event(event))
|
||||
if not mouse_point:
|
||||
return False
|
||||
|
||||
# TODO: Convert window point to map point
|
||||
# TODO: Perform a path finding operation from the hero to the mouse point
|
||||
# TODO: Highlight those points on the map
|
||||
|
||||
return False
|
||||
|
||||
# pylint: disable=redefined-builtin
|
||||
def __init__(self, bounds: Rect, map: Map, **kwargs):
|
||||
super().__init__(bounds, **kwargs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue