179 lines
5.9 KiB
Rust
179 lines
5.9 KiB
Rust
use iced::widget::{button, column, text};
|
|
use iced::executor;
|
|
use iced::keyboard;
|
|
use iced::event;
|
|
use iced::subscription;
|
|
use iced::{Alignment, Element, Application, Settings, Subscription, Theme, Command};
|
|
|
|
pub fn main() -> iced::Result {
|
|
Main::run(Settings::default())
|
|
}
|
|
|
|
struct Main {
|
|
ntfy: String,
|
|
configuring: Option<Message>,
|
|
inputs: Inputs,
|
|
}
|
|
|
|
struct Inputs {
|
|
stick: Stick,
|
|
a: char,
|
|
b: char,
|
|
x: char,
|
|
y: char,
|
|
l: char,
|
|
r: char,
|
|
}
|
|
|
|
struct Stick {
|
|
up: char,
|
|
down: char,
|
|
left: char,
|
|
right: char,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
enum Message {
|
|
EventOccurred(iced_native::Event),
|
|
Up,
|
|
Down,
|
|
Left,
|
|
Right,
|
|
A,
|
|
B,
|
|
X,
|
|
Y,
|
|
L,
|
|
R,
|
|
}
|
|
|
|
fn controller_button_to_string(msg: Message) -> String {
|
|
return format!("{:?}", msg);
|
|
}
|
|
|
|
impl Application for Main {
|
|
type Message = Message;
|
|
type Flags = ();
|
|
type Theme = Theme;
|
|
type Executor = executor::Default;
|
|
|
|
fn new(_flags: ()) -> (Self, Command<Message>) {
|
|
return (Self {
|
|
ntfy: String::from(":wave:"),
|
|
configuring: None,
|
|
inputs: Inputs{
|
|
stick: Stick {
|
|
up: 'w',
|
|
down: 's',
|
|
left: 'a',
|
|
right: 'd',
|
|
},
|
|
a: 'k',
|
|
b: 'j',
|
|
x: 'i',
|
|
y: 'u',
|
|
l: 'q',
|
|
r: 'e',
|
|
},
|
|
}, Command::none())
|
|
}
|
|
|
|
fn title(&self) -> String {
|
|
return String::from("Rusty Pipe")
|
|
}
|
|
|
|
fn update(&mut self, msg: Message) -> Command<Message> {
|
|
match msg.clone() {
|
|
Message::EventOccurred(event) if self.configuring.is_some() => {
|
|
match event {
|
|
iced::event::Event::Keyboard(keyboard::Event::KeyPressed{
|
|
key_code,
|
|
modifiers: _,
|
|
..
|
|
}) => {
|
|
let key_char = match key_code {
|
|
iced::keyboard::KeyCode::Key1 => '1',
|
|
iced::keyboard::KeyCode::Key2 => '2',
|
|
iced::keyboard::KeyCode::Key3 => '3',
|
|
iced::keyboard::KeyCode::Key4 => '4',
|
|
iced::keyboard::KeyCode::Key5 => '5',
|
|
iced::keyboard::KeyCode::Key6 => '6',
|
|
iced::keyboard::KeyCode::Key7 => '7',
|
|
iced::keyboard::KeyCode::Key8 => '8',
|
|
iced::keyboard::KeyCode::Key9 => '9',
|
|
iced::keyboard::KeyCode::Key0 => '0',
|
|
iced::keyboard::KeyCode::A => 'a',
|
|
iced::keyboard::KeyCode::B => 'b',
|
|
iced::keyboard::KeyCode::C => 'c',
|
|
iced::keyboard::KeyCode::D => 'd',
|
|
iced::keyboard::KeyCode::E => 'e',
|
|
iced::keyboard::KeyCode::F => 'f',
|
|
iced::keyboard::KeyCode::G => 'g',
|
|
iced::keyboard::KeyCode::H => 'h',
|
|
iced::keyboard::KeyCode::I => 'i',
|
|
iced::keyboard::KeyCode::J => 'j',
|
|
iced::keyboard::KeyCode::K => 'k',
|
|
iced::keyboard::KeyCode::L => 'l',
|
|
iced::keyboard::KeyCode::M => 'm',
|
|
iced::keyboard::KeyCode::N => 'n',
|
|
iced::keyboard::KeyCode::O => 'o',
|
|
iced::keyboard::KeyCode::P => 'p',
|
|
iced::keyboard::KeyCode::Q => 'q',
|
|
iced::keyboard::KeyCode::R => 'r',
|
|
iced::keyboard::KeyCode::S => 's',
|
|
iced::keyboard::KeyCode::T => 't',
|
|
iced::keyboard::KeyCode::U => 'u',
|
|
iced::keyboard::KeyCode::V => 'v',
|
|
iced::keyboard::KeyCode::W => 'w',
|
|
iced::keyboard::KeyCode::X => 'x',
|
|
iced::keyboard::KeyCode::Y => 'y',
|
|
iced::keyboard::KeyCode::Z => 'z',
|
|
iced::keyboard::KeyCode::Up => 'a',
|
|
_ => '`',
|
|
};
|
|
self.ntfy = format!("set {:?}", key_char.clone());
|
|
match self.configuring.as_ref().unwrap() {
|
|
Message::Up => { self.inputs.stick.up = key_char },
|
|
_ => {},
|
|
};
|
|
self.configuring = None;
|
|
},
|
|
_ => {},
|
|
}
|
|
},
|
|
Message::EventOccurred(_) => {},
|
|
_ => {
|
|
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::events_with(|event, _| match event {
|
|
iced::Event::Keyboard(_) => Some(Message::EventOccurred(event)),
|
|
_ => None,
|
|
});
|
|
}
|
|
|
|
fn view(&self) -> Element<Message> {
|
|
return column![
|
|
button(text(controller_button_to_string(Message::Up))).on_press(Message::Up),
|
|
button(text(controller_button_to_string(Message::Down))).on_press(Message::Down),
|
|
button(text(controller_button_to_string(Message::Left))).on_press(Message::Left),
|
|
button(text(controller_button_to_string(Message::Right))).on_press(Message::Right),
|
|
button(text(controller_button_to_string(Message::A))).on_press(Message::A),
|
|
button(text(controller_button_to_string(Message::B))).on_press(Message::B),
|
|
button(text(controller_button_to_string(Message::X))).on_press(Message::X),
|
|
button(text(controller_button_to_string(Message::Y))).on_press(Message::Y),
|
|
button(text(controller_button_to_string(Message::L))).on_press(Message::L),
|
|
button(text(controller_button_to_string(Message::R))).on_press(Message::R),
|
|
text(self.ntfy.clone()).size(50),
|
|
]
|
|
.padding(20)
|
|
.align_items(Alignment::Center)
|
|
.into();
|
|
}
|
|
}
|