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