Skip to content

text: Implement double/triple click selection #16946

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

Merged
merged 7 commits into from
Jul 6, 2024
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
57 changes: 53 additions & 4 deletions core/src/backend/ui.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::backend::navigator::OwnedFuture;
use crate::events::{KeyCode, PlayerEvent, TextControlCode};
pub use crate::loader::Error as DialogLoaderError;
use chrono::{DateTime, Utc};
use chrono::{DateTime, TimeDelta, Utc};
use downcast_rs::Downcast;
use fluent_templates::loader::langid;
pub use fluent_templates::LanguageIdentifier;
Expand Down Expand Up @@ -140,7 +140,7 @@ pub enum MouseCursor {
/// Equivalent to AS3 `MouseCursor.ARROW`.
Arrow,

/// The hand icon incdicating a button or link.
/// The hand icon indicating a button or link.
/// Equivalent to AS3 `MouseCursor.BUTTON`.
Hand,

Expand All @@ -153,12 +153,28 @@ pub enum MouseCursor {
Grab,
}

struct ClickEventData {
x: f64,
y: f64,
time: DateTime<Utc>,
index: usize,
}

impl ClickEventData {
fn distance_squared_to(&self, x: f64, y: f64) -> f64 {
let dx = x - self.x;
let dy = y - self.y;
dx * dx + dy * dy
}
}

pub struct InputManager {
keys_down: HashSet<KeyCode>,
keys_toggled: HashSet<KeyCode>,
last_key: KeyCode,
last_char: Option<char>,
last_text_control: Option<TextControlCode>,
last_click: Option<ClickEventData>,
}

impl InputManager {
Expand All @@ -169,6 +185,7 @@ impl InputManager {
last_key: KeyCode::Unknown,
last_char: None,
last_text_control: None,
last_click: None,
}
}

Expand Down Expand Up @@ -212,15 +229,40 @@ impl InputManager {
PlayerEvent::TextControl { code } => {
self.last_text_control = Some(code);
}
PlayerEvent::MouseDown { button, .. } => {
PlayerEvent::MouseDown {
x,
y,
button,
index,
} => {
self.toggle_key(button.into());
self.add_key(button.into())
self.add_key(button.into());
self.update_last_click(x, y, index);
}
PlayerEvent::MouseUp { button, .. } => self.remove_key(button.into()),
_ => {}
}
}

fn update_last_click(&mut self, x: f64, y: f64, index: Option<usize>) {
let time = Utc::now();
let index = index.unwrap_or_else(|| {
let Some(last_click) = self.last_click.as_ref() else {
return 0;
};

// TODO Make this configurable as "double click delay" and "double click distance"
if (time - last_click.time).abs() < TimeDelta::milliseconds(500)
&& last_click.distance_squared_to(x, y) < 4.0
{
last_click.index + 1
} else {
0
}
});
self.last_click = Some(ClickEventData { x, y, time, index });
}

pub fn is_key_down(&self, key: KeyCode) -> bool {
self.keys_down.contains(&key)
}
Expand All @@ -241,6 +283,13 @@ impl InputManager {
self.last_text_control
}

pub fn last_click_index(&self) -> usize {
self.last_click
.as_ref()
.map(|lc| lc.index)
.unwrap_or_default()
}

pub fn is_mouse_down(&self) -> bool {
self.is_key_down(KeyCode::MouseLeft)
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/display_object/avm1_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl<'gc> TInteractiveObject<'gc> for Avm1Button<'gc> {
Some(ButtonActionCondition::OUT_DOWN_TO_OVER_DOWN),
None,
),
ClipEvent::Press => (
ClipEvent::Press { .. } => (
ButtonState::Down,
Some(ButtonActionCondition::OVER_UP_TO_OVER_DOWN),
static_data.over_to_down_sound.as_ref(),
Expand Down
2 changes: 1 addition & 1 deletion core/src/display_object/avm2_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ impl<'gc> TInteractiveObject<'gc> for Avm2Button<'gc> {
let (new_state, sound) = match event {
ClipEvent::DragOut { .. } => (ButtonState::Over, None),
ClipEvent::DragOver { .. } => (ButtonState::Down, None),
ClipEvent::Press => (ButtonState::Down, static_data.over_to_down_sound.as_ref()),
ClipEvent::Press { .. } => (ButtonState::Down, static_data.over_to_down_sound.as_ref()),
ClipEvent::Release => (ButtonState::Over, static_data.down_to_over_sound.as_ref()),
ClipEvent::ReleaseOutside => (ButtonState::Up, static_data.over_to_up_sound.as_ref()),
ClipEvent::MouseUpInside => (ButtonState::Up, static_data.over_to_up_sound.as_ref()),
Expand Down
Loading
Loading