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
use crate::input::virtual_source::VirtualSource;
use crate::input::{GamepadButton, Key, Keyboard};
use std::cell::Cell;
use std::cmp::Ordering;
use std::rc::Rc;
/// Handle to a virtual button.
///
/// This can be used to simultaneously listen to the state of a key and gamepad button, and
/// can be cloned and passed around to give objects access to it.
#[derive(Clone)]
pub struct VirtualButton(Rc<Inner>);
impl PartialEq for VirtualButton {
#[inline]
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.0, &other.0)
}
}
impl PartialOrd for VirtualButton {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Rc::as_ptr(&self.0).partial_cmp(&Rc::as_ptr(&other.0))
}
}
struct Inner {
source: VirtualSource,
btn: Cell<Option<GamepadButton>>,
key: Cell<Option<Key>>,
}
impl VirtualButton {
/// Create a button with no mappings.
pub fn empty(source: &VirtualSource) -> Self {
Self::new(source, None, None)
}
/// Create a button that listens to the provided key and button.
pub fn new(
source: &VirtualSource,
key: impl Into<Option<Key>>,
btn: impl Into<Option<GamepadButton>>,
) -> Self {
Self(Rc::new(Inner {
source: source.clone(),
btn: Cell::new(btn.into()),
key: Cell::new(key.into()),
}))
}
#[inline]
fn keyboard(&self) -> &Keyboard {
self.0.source.keyboard()
}
/// Set the gamepad button to listen to.
#[inline]
pub fn set_button(&self, btn: impl Into<Option<GamepadButton>>) {
self.0.btn.set(btn.into());
}
/// Set the key to listen to.
#[inline]
pub fn set_key(&self, key: impl Into<Option<Key>>) {
self.0.key.set(key.into());
}
/// If the button's key or gamepad button is down.
#[inline]
pub fn down(&self) -> bool {
self.0
.btn
.get()
.is_some_and(|btn| self.0.source.read(|pad| pad.down(btn)).unwrap_or(false))
|| self
.0
.key
.get()
.is_some_and(|key| self.keyboard().down(key))
}
/// If the button's key or gamepad button was pressed this frame.
///
/// This treats the mappings as if they were one button, meaning if the gamepad button is held
/// down and you press the key, it will not count as a press because the virtual button is
/// already considered down.
#[inline]
pub fn pressed(&self) -> bool {
let mut pressed = false;
if let Some(btn) = self.0.btn.get() {
if let Some((press, down)) = self.0.source.read(|pad| (pad.pressed(btn), pad.down(btn)))
{
if press {
pressed = true;
} else if down {
return false;
}
}
}
if let Some(key) = self.0.key.get() {
if self.keyboard().pressed(key) {
pressed = true;
} else if self.keyboard().down(key) {
return false;
}
}
pressed
}
/// If the button's key or gamepad button was pressed this frame.
///
/// This treats the mappings as if they were one button, meaning if the gamepad button is
/// released but the key is still held down, this will not return true until the key is also
/// released.
#[inline]
pub fn released(&self) -> bool {
let mut released = false;
if let Some(btn) = self.0.btn.get() {
if let Some((release, down)) =
self.0.source.read(|pad| (pad.released(btn), pad.down(btn)))
{
if release {
released = true;
} else if !down {
return false;
}
}
}
if let Some(key) = self.0.key.get() {
if self.keyboard().released(key) {
released = true;
} else if !self.keyboard().down(key) {
return false;
}
}
released
}
/// If the button state changed this frame.
#[inline]
pub fn changed(&self) -> bool {
self.0.btn.get().is_some_and(|btn| {
self.0
.source
.read(|pad| pad.btn_changed(btn))
.unwrap_or(false)
}) || self
.0
.key
.get()
.is_some_and(|key| self.keyboard().pressed(key) || self.keyboard().released(key))
}
/// Value of the button.
#[inline]
pub fn value(&self) -> f32 {
if self
.0
.key
.get()
.is_some_and(|key| self.keyboard().down(key))
{
return 1.0;
}
self.0
.btn
.get()
.and_then(|btn| self.0.source.read(|pad| pad.value(btn)))
.unwrap_or(0.0)
}
}