Trait ggez::event::EventHandler
source · pub trait EventHandler<E = GameError>where
E: Debug,{
Show 18 methods
// Required methods
fn update(&mut self, _ctx: &mut Context) -> Result<(), E>;
fn draw(&mut self, _ctx: &mut Context) -> Result<(), E>;
// Provided methods
fn mouse_button_down_event(
&mut self,
_ctx: &mut Context,
_button: MouseButton,
_x: f32,
_y: f32
) -> Result<(), E> { ... }
fn mouse_button_up_event(
&mut self,
_ctx: &mut Context,
_button: MouseButton,
_x: f32,
_y: f32
) -> Result<(), E> { ... }
fn mouse_motion_event(
&mut self,
_ctx: &mut Context,
_x: f32,
_y: f32,
_dx: f32,
_dy: f32
) -> Result<(), E> { ... }
fn mouse_enter_or_leave(
&mut self,
_ctx: &mut Context,
_entered: bool
) -> Result<(), E> { ... }
fn mouse_wheel_event(
&mut self,
_ctx: &mut Context,
_x: f32,
_y: f32
) -> Result<(), E> { ... }
fn key_down_event(
&mut self,
ctx: &mut Context,
input: KeyInput,
_repeated: bool
) -> Result<(), E> { ... }
fn key_up_event(
&mut self,
_ctx: &mut Context,
_input: KeyInput
) -> Result<(), E> { ... }
fn text_input_event(
&mut self,
_ctx: &mut Context,
_character: char
) -> Result<(), E> { ... }
fn touch_event(
&mut self,
ctx: &mut Context,
phase: TouchPhase,
x: f64,
y: f64
) -> Result<(), E> { ... }
fn gamepad_button_down_event(
&mut self,
_ctx: &mut Context,
_btn: Button,
_id: GamepadId
) -> Result<(), E> { ... }
fn gamepad_button_up_event(
&mut self,
_ctx: &mut Context,
_btn: Button,
_id: GamepadId
) -> Result<(), E> { ... }
fn gamepad_axis_event(
&mut self,
_ctx: &mut Context,
_axis: Axis,
_value: f32,
_id: GamepadId
) -> Result<(), E> { ... }
fn focus_event(
&mut self,
_ctx: &mut Context,
_gained: bool
) -> Result<(), E> { ... }
fn quit_event(&mut self, _ctx: &mut Context) -> Result<bool, E> { ... }
fn resize_event(
&mut self,
_ctx: &mut Context,
_width: f32,
_height: f32
) -> Result<(), E> { ... }
fn on_error(
&mut self,
_ctx: &mut Context,
_origin: ErrorOrigin,
_e: E
) -> bool { ... }
}
Expand description
A trait defining event callbacks. This is your primary interface with
ggez
’s event loop. Implement this trait for a type and
override at least the update()
and
draw()
methods, then pass it to
event::run()
to run the game’s mainloop.
The default event handlers do nothing, apart from
key_down_event()
, which will by
default exit the game if the escape key is pressed. Just
override the methods you want to use.
For the error type simply choose the default GameError
,
or something more generic, if your situation requires it.
Required Methods§
sourcefn update(&mut self, _ctx: &mut Context) -> Result<(), E>
fn update(&mut self, _ctx: &mut Context) -> Result<(), E>
Called upon each logic update to the game. This should be where the game’s logic takes place.
sourcefn draw(&mut self, _ctx: &mut Context) -> Result<(), E>
fn draw(&mut self, _ctx: &mut Context) -> Result<(), E>
Called to do the drawing of your game.
You probably want to start this with
Canvas::from_frame
and end it
with Canvas::finish
.
Provided Methods§
A mouse button was pressed
A mouse button was released
sourcefn mouse_motion_event(
&mut self,
_ctx: &mut Context,
_x: f32,
_y: f32,
_dx: f32,
_dy: f32
) -> Result<(), E>
fn mouse_motion_event( &mut self, _ctx: &mut Context, _x: f32, _y: f32, _dx: f32, _dy: f32 ) -> Result<(), E>
The mouse was moved; it provides both absolute x and y coordinates in the window, and relative x and y coordinates compared to its last position.
sourcefn mouse_enter_or_leave(
&mut self,
_ctx: &mut Context,
_entered: bool
) -> Result<(), E>
fn mouse_enter_or_leave( &mut self, _ctx: &mut Context, _entered: bool ) -> Result<(), E>
mouse entered or left window area
sourcefn mouse_wheel_event(
&mut self,
_ctx: &mut Context,
_x: f32,
_y: f32
) -> Result<(), E>
fn mouse_wheel_event( &mut self, _ctx: &mut Context, _x: f32, _y: f32 ) -> Result<(), E>
The mousewheel was scrolled, vertically (y, positive away from and negative toward the user) or horizontally (x, positive to the right and negative to the left).
sourcefn key_down_event(
&mut self,
ctx: &mut Context,
input: KeyInput,
_repeated: bool
) -> Result<(), E>
fn key_down_event( &mut self, ctx: &mut Context, input: KeyInput, _repeated: bool ) -> Result<(), E>
A keyboard button was pressed.
The default implementation of this will call ctx.request_quit()
when the escape key is pressed. If you override this with your own
event handler you have to re-implement that functionality yourself.
sourcefn key_up_event(
&mut self,
_ctx: &mut Context,
_input: KeyInput
) -> Result<(), E>
fn key_up_event( &mut self, _ctx: &mut Context, _input: KeyInput ) -> Result<(), E>
A keyboard button was released.
sourcefn text_input_event(
&mut self,
_ctx: &mut Context,
_character: char
) -> Result<(), E>
fn text_input_event( &mut self, _ctx: &mut Context, _character: char ) -> Result<(), E>
A unicode character was received, usually from keyboard input. This is the intended way of facilitating text input.
sourcefn touch_event(
&mut self,
ctx: &mut Context,
phase: TouchPhase,
x: f64,
y: f64
) -> Result<(), E>
fn touch_event( &mut self, ctx: &mut Context, phase: TouchPhase, x: f64, y: f64 ) -> Result<(), E>
An event from a touchscreen has been triggered; it provides the x and y location inside the window as well as the state of the tap (such as Started, Moved, Ended, etc) By default, touch events will trigger mouse behavior
A gamepad button was pressed; id
identifies which gamepad.
A gamepad button was released; id
identifies which gamepad.
sourcefn gamepad_axis_event(
&mut self,
_ctx: &mut Context,
_axis: Axis,
_value: f32,
_id: GamepadId
) -> Result<(), E>
fn gamepad_axis_event( &mut self, _ctx: &mut Context, _axis: Axis, _value: f32, _id: GamepadId ) -> Result<(), E>
A gamepad axis moved; id
identifies which gamepad.
sourcefn focus_event(&mut self, _ctx: &mut Context, _gained: bool) -> Result<(), E>
fn focus_event(&mut self, _ctx: &mut Context, _gained: bool) -> Result<(), E>
Called when the window is shown or hidden.
sourcefn quit_event(&mut self, _ctx: &mut Context) -> Result<bool, E>
fn quit_event(&mut self, _ctx: &mut Context) -> Result<bool, E>
Called upon a quit event. If it returns true, the game does not exit (the quit event is cancelled).
sourcefn resize_event(
&mut self,
_ctx: &mut Context,
_width: f32,
_height: f32
) -> Result<(), E>
fn resize_event( &mut self, _ctx: &mut Context, _width: f32, _height: f32 ) -> Result<(), E>
Called when the user resizes the window, or when it is resized
via GraphicsContext::set_mode()
.
sourcefn on_error(&mut self, _ctx: &mut Context, _origin: ErrorOrigin, _e: E) -> bool
fn on_error(&mut self, _ctx: &mut Context, _origin: ErrorOrigin, _e: E) -> bool
Something went wrong, causing a GameError
(or some other kind of error, depending on what you specified).
If this returns true, the error was fatal, so the event loop ends, aborting the game.