Summary
Make pointer interaction detection ergonomic by introducing a cleaner PointerState API inside a dedicated pointer module.
Motivation
This is the current logic to check if the cursor is pressed while hovering:
on_hover(|id, pointer| {
let is_pressed =
pointer.state == ply_engine::engine::PointerDataInteractionState::Pressed ||
pointer.state == ply_engine::engine::PointerDataInteractionState::PressedThisFrame;
if is_pressed {
// activate
}
})
You need:
- Fully qualified paths to internal engine types
- Manual state enumeration
- Knowledge that internal
engine:: types are being exposed, which violates the principle that engine internals should not be user-facing
This was pointed out in #55.
Proposed API
New pointer module:
#[derive(Clone, Copy, Debug)]
pub struct PointerData {
pub position: Vector2,
pub state: PointerState,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PointerState {
#[default]
Idle,
PressedThisFrame,
Pressed,
ReleasedThisFrame,
}
impl PointerData {
/// Check if pointer is currently pressed
pub fn pressed(&self) -> bool {
matches!(self.state, PointerState::Pressed | PointerState::PressedThisFrame)
}
/// Check if pointer is currently released
pub fn released(&self) -> bool {
matches!(self.state, PointerState::Idle | PointerState::ReleasedThisFrame)
}
/// Check if pointer was just pressed this frame
pub fn just_pressed(&self) -> bool {
self.state == PointerState::PressedThisFrame
}
/// Check if pointer is just released this frame
pub fn just_released(&self) -> bool {
self.state == PointerState::ReleasedThisFrame
}
}
Behavior
- The old
engine::PointerData and PointerDataInteractionState types are removed
- The new
pointer::PointerData and pointer::PointerState types are used everywhere
crate::pointer::* is put in the prelude
Summary
Make pointer interaction detection ergonomic by introducing a cleaner
PointerStateAPI inside a dedicatedpointermodule.Motivation
This is the current logic to check if the cursor is pressed while hovering:
You need:
engine::types are being exposed, which violates the principle that engine internals should not be user-facingThis was pointed out in #55.
Proposed API
New
pointermodule:Behavior
engine::PointerDataandPointerDataInteractionStatetypes are removedpointer::PointerDataandpointer::PointerStatetypes are used everywherecrate::pointer::*is put in the prelude