-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbind.rs
240 lines (209 loc) · 7.09 KB
/
bind.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
use egui::{InputState, Key, KeyboardShortcut, ModifierNames, PointerButton};
/// A trait can can be used for keybindings.
///
/// Must have a function to update the keybinding with a given [Key] and
/// [Modifiers], aswell as a method that formats the keybinding as a [String].
///
/// Must implement [Clone].
pub trait Bind: Clone {
/// Set the keybind with a given [KeyboardShortcut] and/or [PointerButton].
///
/// # Arguments
/// * `keyboard` - The keyboard shortcut to set ([KeyboardShortcut]), or [None].
/// * `pointer` - The pointer button to set ([PointerButton]), or [None].
fn set(&mut self, keyboard: Option<KeyboardShortcut>, pointer: Option<PointerButton>);
/// Format the current keybind as a [String].
///
/// # Arguments
/// * `names` - The [ModifierNames] to use.
/// * `is_mac` - Whether to use MacOS symbols.
///
/// # Returns
/// The formatted keybind as a [String].
fn format(&self, names: &ModifierNames<'_>, is_mac: bool) -> String;
/// Check if the keybind is pressed.
///
/// # Arguments
/// * `input` - The [InputState] to check with.
///
/// # Returns
/// Whether the keybind is pressed.
fn pressed(&self, input: &mut InputState) -> bool;
}
/// A [Bind] implementation for [egui]'s [KeyboardShortcut].
impl Bind for KeyboardShortcut {
fn set(&mut self, keyboard: Option<KeyboardShortcut>, _pointer: Option<PointerButton>) {
if let Some(keyboard) = keyboard {
*self = keyboard
}
}
fn format(&self, names: &ModifierNames<'_>, is_mac: bool) -> String {
self.format(names, is_mac)
}
fn pressed(&self, input: &mut InputState) -> bool {
input.consume_shortcut(self)
}
}
impl Bind for Option<KeyboardShortcut> {
fn set(&mut self, keyboard: Option<KeyboardShortcut>, _pointer: Option<PointerButton>) {
*self = keyboard;
}
fn format(&self, names: &ModifierNames<'_>, is_mac: bool) -> String {
self.as_ref().map_or_else(
|| "None".to_string(),
|shortcut| shortcut.format(names, is_mac),
)
}
fn pressed(&self, input: &mut InputState) -> bool {
if let Some(shortcut) = self {
input.consume_shortcut(shortcut)
} else {
false
}
}
}
/// A [Bind] implementation for [egui]'s [Key]. Ignores modifiers.
impl Bind for Key {
fn set(&mut self, keyboard: Option<KeyboardShortcut>, _pointer: Option<PointerButton>) {
if let Some(keyboard) = keyboard {
*self = keyboard.logical_key
}
}
fn format(&self, _names: &ModifierNames<'_>, _is_mac: bool) -> String {
self.name().to_string()
}
fn pressed(&self, input: &mut InputState) -> bool {
input.key_pressed(*self)
}
}
impl Bind for Option<Key> {
fn set(&mut self, keyboard: Option<KeyboardShortcut>, _pointer: Option<PointerButton>) {
if let Some(keyboard) = keyboard {
*self = Some(keyboard.logical_key)
}
}
fn format(&self, _names: &ModifierNames<'_>, _is_mac: bool) -> String {
self.as_ref()
.map_or_else(|| "None".to_string(), |key| key.name().to_string())
}
fn pressed(&self, input: &mut InputState) -> bool {
if let Some(key) = self {
input.key_pressed(*key)
} else {
false
}
}
}
/// A [Bind] implementation for [egui]'s [PointerButton]. Ignores keys and modifiers.
impl Bind for PointerButton {
fn set(&mut self, _keyboard: Option<KeyboardShortcut>, pointer: Option<PointerButton>) {
if let Some(pointer) = pointer {
*self = pointer
}
}
fn format(&self, _names: &ModifierNames<'_>, _is_mac: bool) -> String {
format!("{:?}", self)
}
fn pressed(&self, input: &mut InputState) -> bool {
input.pointer.button_pressed(*self)
}
}
impl Bind for Option<PointerButton> {
fn set(&mut self, _keyboard: Option<KeyboardShortcut>, pointer: Option<PointerButton>) {
*self = pointer;
}
fn format(&self, _names: &ModifierNames<'_>, _is_mac: bool) -> String {
self.as_ref()
.map_or_else(|| "None".to_string(), |button| format!("{:?}", button))
}
fn pressed(&self, input: &mut InputState) -> bool {
if let Some(button) = self {
input.pointer.button_pressed(*button)
} else {
false
}
}
}
/// A keybind that can be set with either the keyboard or a mouse.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Shortcut {
/// Keyboard shortcut, if any. This can be set along with the mouse shortcut.
keyboard: Option<KeyboardShortcut>,
/// Mouse button, if any. This can be set along with the keyboard shortcut.
pointer: Option<PointerButton>,
}
impl Shortcut {
/// No keybind.
pub const NONE: Self = Self {
keyboard: None,
pointer: None,
};
/// Create a new [Shortcut].
///
/// # Arguments
///
/// * `keyboard` - The keyboard shortcut to set ([KeyboardShortcut]), or [None].
/// * `pointer` - The pointer button to set ([PointerButton]), or [None].
pub fn new(keyboard: Option<KeyboardShortcut>, pointer: Option<PointerButton>) -> Self {
Self {
keyboard,
pointer,
}
}
/// Keyboard shortcut, if any. This can be set along with the mouse shortcut.
#[inline]
pub fn keyboard(&self) -> Option<KeyboardShortcut> {
self.keyboard
}
/// Mouse button, if any. This can be set along with the keyboard shortcut.
#[inline]
pub const fn pointer(&self) -> Option<PointerButton> {
self.pointer
}
}
impl Bind for Shortcut {
fn set(&mut self, keyboard: Option<KeyboardShortcut>, pointer: Option<PointerButton>) {
self.keyboard = keyboard;
self.pointer = pointer;
}
fn format(&self, names: &ModifierNames<'_>, is_mac: bool) -> String {
let mut string = self.keyboard.map_or_else(
|| String::with_capacity(9),
|kb| Into::<KeyboardShortcut>::into(kb).format(names, is_mac),
);
if let Some(pointer) = self.pointer {
if !string.is_empty() {
string.push('+');
}
string.push_str(&pointer.format(names, is_mac));
}
if string.is_empty() {
string.push_str("None");
}
string
}
fn pressed(&self, input: &mut InputState) -> bool {
let mut pressed = false;
if let Some(kb) = &self.keyboard {
pressed = input.consume_shortcut(kb);
}
if let Some(button) = self.pointer {
if self.keyboard.is_none() {
return input.pointer.button_clicked(button);
}
pressed &= input.pointer.button_clicked(button);
}
pressed
}
}
impl From<Shortcut> for Option<KeyboardShortcut> {
fn from(value: Shortcut) -> Self {
value.keyboard
}
}
impl From<Shortcut> for Option<PointerButton> {
fn from(value: Shortcut) -> Self {
value.pointer
}
}