Skip to content
Snippets Groups Projects
Commit d917c988 authored by MrGVSV's avatar MrGVSV
Browse files

Documented event types

This includes InputEvent, EventType, EventCategory, and
InputEventCategory
parent 0be194cf
No related branches found
No related tags found
No related merge requests found
use crate::cursor::CursorEvent;
use crate::{Index, KeyboardEvent};
/// An event type sent to widgets
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Event {
/// The node targeted by this event
......@@ -70,37 +71,54 @@ impl Event {
/// with a custom wrapper.
#[derive(Debug, Clone, Copy)]
pub enum EventType {
/// An event that occurs when the user clicks a widget
Click(CursorEvent),
/// An event that occurs when the user hovers the cursor over a widget
Hover(CursorEvent),
/// An event that occurs when the user moves the cursor into a widget
MouseIn(CursorEvent),
/// An event that occurs when the user moves the cursor out of a widget
MouseOut(CursorEvent),
/// An event that occurs when the user presses down on the cursor over a widget
MouseDown(CursorEvent),
/// An event that occurs when the user releases the cursor over a widget
MouseUp(CursorEvent),
/// An event that occurs when a widget receives focus
Focus,
/// An event that occurs when a widget loses focus
Blur,
/// An event that occurs when the user types in a character within a _focused_ widget
CharInput { c: char },
/// An event that occurs when the user releases a key within a _focused_ widget
KeyUp(KeyboardEvent),
/// An event that occurs when the user presses a key down within a _focused_ widget
KeyDown(KeyboardEvent),
}
impl Eq for EventType {}
/// __Note:__ Only checks if the two event types are the same discriminant
impl PartialEq for EventType {
fn eq(&self, other: &Self) -> bool {
std::mem::discriminant(self) == std::mem::discriminant(other)
}
}
/// __Note:__ Only uses the discriminant for hashing
impl std::hash::Hash for EventType {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::hash::Hash::hash(&std::mem::discriminant(self), state);
}
}
/// The various categories an event can belong to
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum EventCategory {
/// A category for events related to the mouse/cursor
Mouse,
/// A category for events related to the keyboard
Keyboard,
/// A category for events related to focus
Focus,
}
......
use crate::KeyCode;
/// Events sent to [`KayakContext`](crate::KayakContext) containing user input data
#[derive(Debug, PartialEq)]
pub enum InputEvent {
/// An event that occurs when the user moves the mouse
MouseMoved((f32, f32)),
/// An event that occurs when the user presses the left mouse button
MouseLeftPress,
/// An event that occurs when the user releases the left mouse button
MouseLeftRelease,
/// An event that occurs when the user types in a character
CharEvent { c: char },
/// An event that occurs when the user presses or releases a key
Keyboard { key: KeyCode, is_pressed: bool },
}
/// The various categories an input event can belong to
pub enum InputEventCategory {
/// A category for events related to the mouse/cursor
Mouse,
/// A category for events related to the keyboard
Keyboard,
// TODO: Gamepad, etc.
}
impl InputEvent {
/// Get the category of this input event
pub fn category(&self) -> InputEventCategory {
match self {
// Mouse events
......
......@@ -3,6 +3,10 @@ use std::fmt::{Debug, Formatter};
use std::sync::{Arc, RwLock};
/// A container for a function that handles events
///
/// This differs from a standard [`Handler`](crate::Handler) in that it's sent directly
/// from the [`KayakContext`](crate::KayakContext) and gives the [`KayakContextRef`]
/// as a parameter.
#[derive(Clone)]
pub struct OnEvent(
Arc<RwLock<dyn FnMut(&mut KayakContextRef, &mut Event) + Send + Sync + 'static>>,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment