core: move scaling of input positions into core
The core may need to control the scale of the input positions to layout positions, such as when the layout is changed. Handle the scaling of input positions in the button of the keyboard.
This commit is contained in:
parent
0fa318ca7e
commit
0b985cefdb
2 changed files with 22 additions and 16 deletions
|
|
@ -132,6 +132,9 @@ pub struct Button<D: Display, K: Keyboard> {
|
|||
longpress: Duration,
|
||||
repeat: Duration,
|
||||
r_square: f64,
|
||||
|
||||
x_scale: f64,
|
||||
y_scale: f64,
|
||||
}
|
||||
|
||||
const PART_TL: usize = 1;
|
||||
|
|
@ -185,6 +188,9 @@ impl<D: Display, K: Keyboard> Button<D, K> {
|
|||
longpress: Duration::from_millis(cfg.longpress_ms()),
|
||||
repeat: Duration::from_millis(cfg.repeat_ms()),
|
||||
r_square: 0.25,
|
||||
|
||||
x_scale: 0.0,
|
||||
y_scale: 0.0,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,6 +230,12 @@ impl<D: Display, K: Keyboard> Button<D, K> {
|
|||
self.layout.update_keys_supported(&self.kbd)
|
||||
}
|
||||
|
||||
pub fn set_scale(&mut self, x_scale: f64, y_scale: f64)
|
||||
{
|
||||
self.x_scale = x_scale;
|
||||
self.y_scale = y_scale;
|
||||
}
|
||||
|
||||
pub fn next_time(&self) -> Option<Instant>
|
||||
{
|
||||
let id = *self.timers.front()?;
|
||||
|
|
@ -326,6 +338,8 @@ impl<D: Display, K: Keyboard> Button<D, K> {
|
|||
return;
|
||||
}
|
||||
|
||||
let (x, y) = (x * self.x_scale, y * self.y_scale);
|
||||
|
||||
let key = match self.layout.locate_key_mut(x, y) {
|
||||
Some(k) => k,
|
||||
// Ignore the press if it is not for a key.
|
||||
|
|
@ -428,6 +442,8 @@ impl<D: Display, K: Keyboard> Button<D, K> {
|
|||
None => return,
|
||||
};
|
||||
|
||||
let (x, y) = (x * self.x_scale, y * self.y_scale);
|
||||
|
||||
let (dx, dy) = (x - press.x1, y - press.y1);
|
||||
if dx * dx + dy * dy < self.r_square {
|
||||
// We only need to make changes when the key is being dragged.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue