Files
Fyrox/src/gui/event.rs
T
2019-10-24 22:23:51 +03:00

129 lines
3.2 KiB
Rust

use crate::{
gui::{UserInterface, node::UINode},
MouseButton, VirtualKeyCode,
};
use rg3d_core::{math::vec2::Vec2, pool::Handle};
use std::any::Any;
pub type UIEventHandler = dyn FnMut(&mut UserInterface, Handle<UINode>, &mut UIEvent);
pub enum UIEventKind {
/// Generated when some mouse button was pressed.
MouseDown {
pos: Vec2,
button: MouseButton,
},
/// Generated when some mouse button was released.
MouseUp {
pos: Vec2,
button: MouseButton,
},
/// Generated when mouse cursor was moved in bounds of widget.
MouseMove {
pos: Vec2
},
/// Generated when some text was entered.
Text {
symbol: char
},
/// Generated when some key was pressed.
KeyDown {
code: VirtualKeyCode
},
/// Generated when some key was released.
KeyUp {
code: VirtualKeyCode
},
/// Generated when mouse wheel was rolled while cursor was in bounds of widget.
MouseWheel {
pos: Vec2,
amount: f32,
},
/// Generated once when mouse leaves bounds of widget.
MouseLeave,
/// Generated once when mouse enters bounds of widget.
MouseEnter,
/// Generated by clickable widgets such as buttons.
///
/// # Notes
/// This event differs from [`MouseDown`] event! [`Click`] event will be generated only
/// if button (or any other "clickable" widget) was previously pressed and mouse button
/// was released right inside widget bounds.
Click,
/// Generated by widgets that has some numeric value that can change.
NumericValueChanged {
old_value: f32,
new_value: f32,
},
MaxValueChanged(f32),
MinValueChanged(f32),
SelectionChanged(Option<usize>),
/// Any kind of user-defined event.
User(Box<dyn Any>),
}
/// Event is basic communication element that is used to deliver information to UI nodes
/// or some other places.
pub struct UIEvent {
/// Flag which allows to mark event as handled. This can be useful if multiple listeners
/// can handle event but event should be handled only once.
///
/// # Notes
///
/// This value does not have effect on event dispatcher.
pub handled: bool,
pub kind: UIEventKind,
/// Handle of node for which this event was produced. Can be NONE if target is undefined,
/// this is the case when user click a button, button produces Click event but it does
/// not case who will handle it. Targeted events are useful to send some data to specific
/// nodes.
///
/// # Notes
///
/// Even if event has `target` it still will be available to all other event handlers and
/// listeners.
pub target: Handle<UINode>,
/// Source of event.
pub(in crate::gui) source: Handle<UINode>,
}
impl UIEvent {
pub fn targeted(target: Handle<UINode>, kind: UIEventKind) -> Self {
Self {
kind,
handled: false,
source: Handle::NONE,
target
}
}
pub fn new(kind: UIEventKind) -> Self {
Self {
kind,
handled: false,
source: Handle::NONE,
target: Handle::NONE
}
}
pub fn source(&self) -> Handle<UINode> {
self.source
}
}