@@ -67,6 +67,9 @@ def on_render(self, console: tcod.console.Console) -> None:
6767 def ev_quit (self , event : tcod .event .Quit ) -> Optional [game .actions .Action ]:
6868 raise SystemExit ()
6969
70+ def ev_mousemotion (self , event : tcod .event .MouseMotion ) -> None :
71+ pass
72+
7073
7174class EventHandler (BaseEventHandler ):
7275 def __init__ (self , engine : game .engine .Engine ):
@@ -104,6 +107,10 @@ def handle_action(self, action: Optional[game.actions.Action]) -> bool:
104107 def on_render (self , console : tcod .console .Console ) -> None :
105108 self .engine .render (console )
106109
110+ def ev_mousemotion (self , event : tcod .event .MouseMotion ) -> None :
111+ if self .engine .game_map .in_bounds (int (event .position .x ), int (event .position .y )):
112+ self .engine .mouse_location = int (event .position .x ), int (event .position .y )
113+
107114
108115class MainGameEventHandler (EventHandler ):
109116 def ev_keydown (self , event : tcod .event .KeyDown ) -> Optional [ActionOrHandler ]:
@@ -119,6 +126,8 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> Optional[ActionOrHandler]:
119126 action = game .actions .BumpAction (player , dx , dy )
120127 elif key == tcod .event .KeySym .ESCAPE :
121128 action = game .actions .EscapeAction (player )
129+ elif key == tcod .event .KeySym .V :
130+ return HistoryViewer (self .engine )
122131 elif key == tcod .event .KeySym .PERIOD and modifiers & (tcod .event .KMOD_LSHIFT | tcod .event .KMOD_RSHIFT ):
123132 # Wait if user presses '>' (shift + period)
124133 action = game .actions .WaitAction (player )
@@ -133,3 +142,46 @@ def handle_events(self, event: tcod.event.Event) -> BaseEventHandler:
133142 if isinstance (action_or_state , BaseEventHandler ):
134143 return action_or_state
135144 return self # Keep this handler active
145+
146+
147+ class HistoryViewer (EventHandler ):
148+ """Print the history on a larger window which can be navigated."""
149+
150+ def __init__ (self , engine : game .engine .Engine ):
151+ super ().__init__ (engine )
152+ self .log_length = len (engine .message_log .messages )
153+ self .cursor = self .log_length - 1
154+
155+ def on_render (self , console : tcod .console .Console ) -> None :
156+ super ().on_render (console ) # Draw the main state as the background.
157+
158+ log_console = tcod .console .Console (console .width - 6 , console .height - 6 )
159+
160+ # Draw a frame with a custom banner title.
161+ log_console .draw_frame (0 , 0 , log_console .width , log_console .height )
162+ log_console .print_box (0 , 0 , log_console .width , 1 , "┤Message history├" , alignment = tcod .CENTER )
163+
164+ # Render the message log using the cursor parameter.
165+ self .engine .message_log .render_messages (
166+ log_console ,
167+ 1 ,
168+ 1 ,
169+ log_console .width - 2 ,
170+ log_console .height - 2 ,
171+ self .engine .message_log .messages [: self .cursor + 1 ],
172+ )
173+ log_console .blit (console , 3 , 3 )
174+
175+ def ev_keydown (self , event : tcod .event .KeyDown ) -> Optional [ActionOrHandler ]:
176+ # Fancy conditional movement to make it feel right.
177+ if event .sym in (tcod .event .KeySym .UP , tcod .event .KeySym .K ):
178+ self .cursor = max (0 , self .cursor - 1 )
179+ elif event .sym in (tcod .event .KeySym .DOWN , tcod .event .KeySym .J ):
180+ self .cursor = min (self .log_length - 1 , self .cursor + 1 )
181+ elif event .sym == tcod .event .KeySym .HOME :
182+ self .cursor = 0
183+ elif event .sym == tcod .event .KeySym .END :
184+ self .cursor = self .log_length - 1
185+ else : # Any other key moves back to the main game state.
186+ return MainGameEventHandler (self .engine )
187+ return None
0 commit comments