gui emits keydown/keyup as c/cargo run
parent
aaefbde377
commit
2e70942b21
Binary file not shown.
95
src/gui.rs
95
src/gui.rs
|
|
@ -31,7 +31,9 @@ struct Main {
|
|||
ntfy: String,
|
||||
configuring: Option<Message>,
|
||||
inputs: Inputs,
|
||||
keys_down: Vec<iced::keyboard::KeyCode>,
|
||||
keys_newly_down: Vec<iced::keyboard::KeyCode>,
|
||||
keys_already_down: Vec<iced::keyboard::KeyCode>,
|
||||
keys_up: Vec<iced::keyboard::KeyCode>,
|
||||
flags: Flags,
|
||||
}
|
||||
|
||||
|
|
@ -77,6 +79,24 @@ fn controller_button_to_string(btn: Message, cur: iced::keyboard::KeyCode) -> St
|
|||
return format!("{:?} => {:?}", cur, btn);
|
||||
}
|
||||
|
||||
impl Main {
|
||||
fn key_code_to_string(&self, key_code: &iced::keyboard::KeyCode) -> Option<&String> {
|
||||
match key_code {
|
||||
_ if key_code == &self.inputs.stick.up => Some(&self.flags.cfg.up),
|
||||
_ if key_code == &self.inputs.stick.down => Some(&self.flags.cfg.down),
|
||||
_ if key_code == &self.inputs.stick.left => Some(&self.flags.cfg.left),
|
||||
_ if key_code == &self.inputs.stick.right => Some(&self.flags.cfg.right),
|
||||
_ if key_code == &self.inputs.a => Some(&self.flags.cfg.a),
|
||||
_ if key_code == &self.inputs.b => Some(&self.flags.cfg.b),
|
||||
_ if key_code == &self.inputs.x => Some(&self.flags.cfg.x),
|
||||
_ if key_code == &self.inputs.y => Some(&self.flags.cfg.y),
|
||||
_ if key_code == &self.inputs.l => Some(&self.flags.cfg.l),
|
||||
_ if key_code == &self.inputs.r => Some(&self.flags.cfg.r),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Application for Main {
|
||||
type Message = Message;
|
||||
type Flags = Flags;
|
||||
|
|
@ -102,7 +122,9 @@ impl Application for Main {
|
|||
r: iced::keyboard::KeyCode::E,
|
||||
},
|
||||
flags: flags,
|
||||
keys_down: vec![],
|
||||
keys_newly_down: vec![],
|
||||
keys_already_down: vec![],
|
||||
keys_up: vec![],
|
||||
}, Command::none())
|
||||
}
|
||||
|
||||
|
|
@ -114,30 +136,38 @@ impl Application for Main {
|
|||
match msg.clone() {
|
||||
Message::Tick => {
|
||||
let mut s = vec![];
|
||||
for key_code in self.keys_down.iter() {
|
||||
let nope = String::from("");
|
||||
let s2 = match key_code {
|
||||
_ if key_code == &self.inputs.stick.up => &self.flags.cfg.up,
|
||||
_ if key_code == &self.inputs.stick.down => &self.flags.cfg.down,
|
||||
_ if key_code == &self.inputs.stick.left => &self.flags.cfg.left,
|
||||
_ if key_code == &self.inputs.stick.right => &self.flags.cfg.right,
|
||||
_ if key_code == &self.inputs.a => &self.flags.cfg.a,
|
||||
_ if key_code == &self.inputs.b => &self.flags.cfg.b,
|
||||
_ if key_code == &self.inputs.x => &self.flags.cfg.x,
|
||||
_ if key_code == &self.inputs.y => &self.flags.cfg.y,
|
||||
_ if key_code == &self.inputs.l => &self.flags.cfg.l,
|
||||
_ if key_code == &self.inputs.r => &self.flags.cfg.r,
|
||||
_ => &nope,
|
||||
for key_code in self.keys_newly_down.iter() {
|
||||
match self.key_code_to_string(key_code) {
|
||||
Some(x) => {
|
||||
for c in x.chars() {
|
||||
s.push(c);
|
||||
}
|
||||
self.keys_already_down.push(*key_code);
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
if s2.len() > 0 {
|
||||
for c in s2.chars() {
|
||||
s.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if s.len() > 0 {
|
||||
self.flags.output_stream.put(s);
|
||||
}
|
||||
self.keys_newly_down.clear();
|
||||
|
||||
let mut s = vec![];
|
||||
for key_code in self.keys_up.iter() {
|
||||
match self.key_code_to_string(key_code) {
|
||||
Some(x) => {
|
||||
for c in x.chars() {
|
||||
s.push('!');
|
||||
s.push(c);
|
||||
}
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
}
|
||||
if s.len() > 0 {
|
||||
self.flags.output_stream.put(s);
|
||||
}
|
||||
self.keys_up.clear();
|
||||
},
|
||||
Message::EventOccurred(event) if self.configuring.is_some() => {
|
||||
match event {
|
||||
|
|
@ -172,15 +202,30 @@ impl Application for Main {
|
|||
modifiers: _,
|
||||
..
|
||||
}) => {
|
||||
self.keys_down.push(key_code);
|
||||
self.keys_down.dedup();
|
||||
match self.keys_already_down.iter().position(|x| *x == key_code) {
|
||||
Some(_) => {},
|
||||
None => {
|
||||
self.keys_newly_down.push(key_code);
|
||||
self.keys_newly_down.dedup();
|
||||
},
|
||||
};
|
||||
},
|
||||
iced::event::Event::Keyboard(keyboard::Event::KeyReleased{
|
||||
key_code,
|
||||
..
|
||||
}) => {
|
||||
match self.keys_down.iter().position(|x| *x == key_code) {
|
||||
Some(idx) => { self.keys_down.remove(idx); },
|
||||
match self.keys_already_down.iter().position(|x| *x == key_code) {
|
||||
Some(idx) => {
|
||||
self.keys_already_down.remove(idx);
|
||||
self.keys_up.push(key_code);
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
match self.keys_newly_down.iter().position(|x| *x == key_code) {
|
||||
Some(idx) => {
|
||||
self.keys_newly_down.remove(idx);
|
||||
self.keys_up.push(key_code);
|
||||
},
|
||||
None => {},
|
||||
};
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue