Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add *_released & *_clicked PointerState methods #1582

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui-w


## Unreleased

* Add `*_released` & `*_clicked` methods for `PointerState`.

## 0.18.1 - 2022-05-01
* Change `Shape::Callback` from `&dyn Any` to `&mut dyn Any` to support more backends.
Expand Down
2 changes: 1 addition & 1 deletion egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ impl Context {
for pointer_event in &input.pointer.pointer_events {
match pointer_event {
PointerEvent::Moved(_) => {}
PointerEvent::Pressed(_) => {
PointerEvent::Pressed { .. } => {
if hovered {
if sense.click && memory.interaction.click_id.is_none() {
// potential start of a click
Expand Down
46 changes: 43 additions & 3 deletions egui/src/input_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,16 @@ impl Click {
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PointerEvent {
Moved(Pos2),
Pressed(Pos2),
Pressed {
position: Pos2,
button: PointerButton,
},
Released(Option<Click>),
}

impl PointerEvent {
pub fn is_press(&self) -> bool {
matches!(self, PointerEvent::Pressed(_))
matches!(self, PointerEvent::Pressed { .. })
}
pub fn is_release(&self) -> bool {
matches!(self, PointerEvent::Released(_))
Expand Down Expand Up @@ -509,7 +512,10 @@ impl PointerState {
self.press_origin = Some(pos);
self.press_start_time = Some(time);
self.has_moved_too_much_for_a_click = false;
self.pointer_events.push(PointerEvent::Pressed(pos));
self.pointer_events.push(PointerEvent::Pressed {
position: pos,
button,
});
} else {
let clicked = self.could_any_button_be_click();

Expand Down Expand Up @@ -667,6 +673,23 @@ impl PointerState {
self.pointer_events.iter().any(|event| event.is_release())
}

/// Was the button given released this frame?
pub fn button_released(&self, button: PointerButton) -> bool {
self.pointer_events
.iter()
.any(|event| matches!(event, &PointerEvent::Released(Some(Click{button: b, ..})) if button == b))
}

/// Was the primary button released this frame?
pub fn primary_released(&self) -> bool {
self.button_released(PointerButton::Primary)
}

/// Was the secondary button released this frame?
pub fn secondary_released(&self) -> bool {
self.button_released(PointerButton::Secondary)
}

/// Is any pointer button currently down?
pub fn any_down(&self) -> bool {
self.down.iter().any(|&down| down)
Expand All @@ -677,6 +700,23 @@ impl PointerState {
self.pointer_events.iter().any(|event| event.is_click())
}

/// Was the button given clicked this frame?
pub fn button_clicked(&self, button: PointerButton) -> bool {
self.pointer_events
.iter()
.any(|event| matches!(event, &PointerEvent::Pressed { button: b, .. } if button == b))
}

/// Was the primary button clicked this frame?
pub fn primary_clicked(&self) -> bool {
self.button_clicked(PointerButton::Primary)
}

/// Was the secondary button clicked this frame?
pub fn secondary_clicked(&self) -> bool {
self.button_clicked(PointerButton::Secondary)
}

// /// Was this button pressed (`!down -> down`) this frame?
// /// This can sometimes return `true` even if `any_down() == false`
// /// because a press can be shorted than one frame.
Expand Down