272 lines
9.4 KiB
Rust
272 lines
9.4 KiB
Rust
use iced::widget::{button, column, text};
|
|
use iced::executor;
|
|
use iced::keyboard;
|
|
use iced::subscription;
|
|
use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command};
|
|
use iced_futures::backend::native::async_std::time::every;
|
|
|
|
use crate::stream::OutputStream;
|
|
use crate::config::GUI;
|
|
|
|
pub fn main(cfg: GUI, output_stream: Box<dyn OutputStream>) -> iced::Result {
|
|
let def: iced::Settings<()> = Settings::default();
|
|
let settings = Settings{
|
|
flags: Flags {
|
|
output_stream: output_stream,
|
|
cfg: cfg,
|
|
},
|
|
antialiasing: def.antialiasing,
|
|
default_font: def.default_font,
|
|
default_text_size: def.default_text_size,
|
|
exit_on_close_request: def.exit_on_close_request,
|
|
id: def.id,
|
|
text_multithreading: def.text_multithreading,
|
|
try_opengles_first: def.try_opengles_first,
|
|
window: def.window,
|
|
};
|
|
Main::run(settings)
|
|
}
|
|
|
|
struct Main {
|
|
ntfy: String,
|
|
configuring: Option<Message>,
|
|
inputs: Inputs,
|
|
keys_newly_down: Vec<iced::keyboard::KeyCode>,
|
|
keys_already_down: Vec<iced::keyboard::KeyCode>,
|
|
keys_up: Vec<iced::keyboard::KeyCode>,
|
|
flags: Flags,
|
|
}
|
|
|
|
struct Flags {
|
|
output_stream: Box<dyn OutputStream>,
|
|
cfg: GUI,
|
|
}
|
|
|
|
struct Inputs {
|
|
stick: Stick,
|
|
a: iced::keyboard::KeyCode,
|
|
b: iced::keyboard::KeyCode,
|
|
x: iced::keyboard::KeyCode,
|
|
y: iced::keyboard::KeyCode,
|
|
l: iced::keyboard::KeyCode,
|
|
r: iced::keyboard::KeyCode,
|
|
}
|
|
|
|
struct Stick {
|
|
up: iced::keyboard::KeyCode,
|
|
down: iced::keyboard::KeyCode,
|
|
left: iced::keyboard::KeyCode,
|
|
right: iced::keyboard::KeyCode,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum Message {
|
|
EventOccurred(iced_native::Event),
|
|
Tick,
|
|
Up,
|
|
Down,
|
|
Left,
|
|
Right,
|
|
A,
|
|
B,
|
|
X,
|
|
Y,
|
|
L,
|
|
R,
|
|
}
|
|
|
|
fn controller_button_to_string(btn: Message, cur: iced::keyboard::KeyCode) -> String {
|
|
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;
|
|
type Theme = Theme;
|
|
type Executor = executor::Default;
|
|
|
|
fn new(flags: Self::Flags) -> (Self, Command<Message>) {
|
|
return (Self {
|
|
ntfy: String::from(":wave:"),
|
|
configuring: None,
|
|
inputs: Inputs{
|
|
stick: Stick {
|
|
up: iced::keyboard::KeyCode::W,
|
|
down: iced::keyboard::KeyCode::S,
|
|
left: iced::keyboard::KeyCode::A,
|
|
right: iced::keyboard::KeyCode::D,
|
|
},
|
|
a: iced::keyboard::KeyCode::Key1,
|
|
b: iced::keyboard::KeyCode::Key2,
|
|
x: iced::keyboard::KeyCode::Key3,
|
|
y: iced::keyboard::KeyCode::Key4,
|
|
l: iced::keyboard::KeyCode::Q,
|
|
r: iced::keyboard::KeyCode::E,
|
|
},
|
|
flags: flags,
|
|
keys_newly_down: vec![],
|
|
keys_already_down: vec![],
|
|
keys_up: vec![],
|
|
}, Command::none())
|
|
}
|
|
|
|
fn title(&self) -> String {
|
|
return String::from("Rusty Pipe")
|
|
}
|
|
|
|
fn update(&mut self, msg: Message) -> Command<Message> {
|
|
match msg.clone() {
|
|
Message::Tick => {
|
|
let mut s = vec![];
|
|
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 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 {
|
|
iced::event::Event::Keyboard(keyboard::Event::KeyPressed{
|
|
key_code,
|
|
modifiers: _,
|
|
..
|
|
}) => {
|
|
match self.configuring.as_ref().unwrap() {
|
|
Message::Up => { self.inputs.stick.up = key_code },
|
|
Message::Down => { self.inputs.stick.down = key_code },
|
|
Message::Left => { self.inputs.stick.left = key_code },
|
|
Message::Right => { self.inputs.stick.right = key_code },
|
|
Message::A => { self.inputs.a = key_code },
|
|
Message::B => { self.inputs.b = key_code },
|
|
Message::X => { self.inputs.x = key_code },
|
|
Message::Y => { self.inputs.y = key_code },
|
|
Message::L => { self.inputs.l = key_code },
|
|
Message::R => { self.inputs.r = key_code },
|
|
_ => {},
|
|
};
|
|
self.ntfy = format!("{:?} => {:?}", key_code.clone(), self.configuring.as_ref().unwrap());
|
|
self.configuring = None;
|
|
},
|
|
_ => {},
|
|
}
|
|
},
|
|
Message::EventOccurred(event) => {
|
|
match event {
|
|
iced::event::Event::Keyboard(keyboard::Event::KeyPressed{
|
|
key_code,
|
|
modifiers: _,
|
|
..
|
|
}) => {
|
|
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_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 => {},
|
|
};
|
|
},
|
|
_ => {},
|
|
}
|
|
},
|
|
_ => {
|
|
self.configuring = Some(msg.clone());
|
|
self.ntfy = format!("push a key to bind to {:?}", msg.clone());
|
|
},
|
|
}
|
|
return Command::none();
|
|
}
|
|
|
|
fn subscription(&self) -> Subscription<Message> {
|
|
return subscription::Subscription::batch(vec![
|
|
subscription::events_with(|event, _| match event {
|
|
iced::Event::Keyboard(_) => Some(Message::EventOccurred(event)),
|
|
_ => None,
|
|
}),
|
|
every(std::time::Duration::from_millis(25)).map(|_| Message::Tick),
|
|
]);
|
|
}
|
|
|
|
fn view(&self) -> Element<Message> {
|
|
return column![
|
|
button(text(controller_button_to_string(Message::Up, self.inputs.stick.up))).on_press(Message::Up),
|
|
button(text(controller_button_to_string(Message::Down, self.inputs.stick.down))).on_press(Message::Down),
|
|
button(text(controller_button_to_string(Message::Left, self.inputs.stick.left))).on_press(Message::Left),
|
|
button(text(controller_button_to_string(Message::Right, self.inputs.stick.right))).on_press(Message::Right),
|
|
button(text(controller_button_to_string(Message::A, self.inputs.a))).on_press(Message::A),
|
|
button(text(controller_button_to_string(Message::B, self.inputs.b))).on_press(Message::B),
|
|
button(text(controller_button_to_string(Message::X, self.inputs.x))).on_press(Message::X),
|
|
button(text(controller_button_to_string(Message::Y, self.inputs.y))).on_press(Message::Y),
|
|
button(text(controller_button_to_string(Message::L, self.inputs.l))).on_press(Message::L),
|
|
button(text(controller_button_to_string(Message::R, self.inputs.r))).on_press(Message::R),
|
|
text(self.ntfy.clone()).size(50),
|
|
]
|
|
.padding(20)
|
|
.align_items(Alignment::Center)
|
|
.into();
|
|
}
|
|
}
|