1use crate::BindTarget;
2use egui::{Context, Key, Modifiers, PointerButton};
3
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum KeyOrPointer {
8 Key(Key),
10 Pointer(PointerButton),
12}
13
14impl BindTarget for KeyOrPointer {
15 const IS_KEY: bool = true;
16 const IS_POINTER: bool = true;
17 const CLEARABLE: bool = false;
18
19 fn set_key(&mut self, key: Key, _: Modifiers) {
20 *self = Self::Key(key);
21 }
22
23 fn set_pointer(&mut self, button: PointerButton, _: Modifiers) {
24 *self = Self::Pointer(button);
25 }
26
27 fn clear(&mut self) {
28 unimplemented!()
29 }
30
31 fn format(&self) -> String {
32 match self {
33 Self::Key(k) => k.format(),
34 Self::Pointer(p) => p.format(),
35 }
36 }
37
38 fn down(&self, ctx: &Context) -> bool {
39 match self {
40 Self::Key(k) => k.down(ctx),
41 Self::Pointer(p) => p.down(ctx),
42 }
43 }
44
45 fn pressed(&self, ctx: &Context) -> bool {
46 match self {
47 Self::Key(k) => k.pressed(ctx),
48 Self::Pointer(p) => p.pressed(ctx),
49 }
50 }
51
52 fn released(&self, ctx: &Context) -> bool {
53 match self {
54 Self::Key(k) => k.released(ctx),
55 Self::Pointer(p) => p.released(ctx),
56 }
57 }
58}
59
60impl BindTarget for Option<KeyOrPointer> {
61 const IS_KEY: bool = true;
62 const IS_POINTER: bool = true;
63 const CLEARABLE: bool = true;
64
65 fn set_key(&mut self, key: Key, _: Modifiers) {
66 *self = Some(KeyOrPointer::Key(key));
67 }
68
69 fn set_pointer(&mut self, button: PointerButton, _: Modifiers) {
70 *self = Some(KeyOrPointer::Pointer(button));
71 }
72
73 fn clear(&mut self) {
74 *self = None
75 }
76
77 fn format(&self) -> String {
78 match self {
79 Some(KeyOrPointer::Key(k)) => k.format(),
80 Some(KeyOrPointer::Pointer(p)) => p.format(),
81 None => "None".into(),
82 }
83 }
84
85 fn down(&self, ctx: &Context) -> bool {
86 self.as_ref().map(|v| v.down(ctx)).unwrap_or(false)
87 }
88
89 fn pressed(&self, ctx: &Context) -> bool {
90 self.as_ref().map(|v| v.pressed(ctx)).unwrap_or(false)
91 }
92
93 fn released(&self, ctx: &Context) -> bool {
94 self.as_ref().map(|v| v.released(ctx)).unwrap_or(false)
95 }
96}