Skip to content

PointerData ergonomics #57

@TheRedDeveloper

Description

@TheRedDeveloper

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedA feature that has been approvedfeatNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions