196 lines
6.9 KiB
Rust
196 lines
6.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};
|
|
|
|
use crate::stream::OutputStream;
|
|
|
|
pub fn main(outputStream: Box<dyn OutputStream>) -> iced::Result {
|
|
let def: iced::Settings<()> = Settings::default();
|
|
let settings = Settings{
|
|
flags: Flags {
|
|
outputStream: outputStream,
|
|
},
|
|
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,
|
|
flags: Flags,
|
|
}
|
|
|
|
struct Flags {
|
|
outputStream: Box<dyn OutputStream>,
|
|
}
|
|
|
|
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),
|
|
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 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::K,
|
|
b: iced::keyboard::KeyCode::J,
|
|
x: iced::keyboard::KeyCode::I,
|
|
y: iced::keyboard::KeyCode::U,
|
|
l: iced::keyboard::KeyCode::Q,
|
|
r: iced::keyboard::KeyCode::E,
|
|
},
|
|
flags: flags,
|
|
}, 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: _,
|
|
..
|
|
}) => {
|
|
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: _,
|
|
..
|
|
}) => {
|
|
let s = match key_code {
|
|
_ if key_code == self.inputs.stick.up => String::from("up"),
|
|
_ if key_code == self.inputs.stick.down => String::from("down"),
|
|
_ if key_code == self.inputs.stick.left => String::from("left"),
|
|
_ if key_code == self.inputs.stick.right => String::from("right"),
|
|
_ if key_code == self.inputs.a => String::from("a"),
|
|
_ if key_code == self.inputs.b => String::from("b"),
|
|
_ if key_code == self.inputs.x => String::from("x"),
|
|
_ if key_code == self.inputs.y => String::from("y"),
|
|
_ if key_code == self.inputs.l => String::from("l"),
|
|
_ if key_code == self.inputs.r => String::from("r"),
|
|
_ => String::from(""),
|
|
};
|
|
if s.len() > 0 {
|
|
self.flags.outputStream.put(s.chars().collect());
|
|
}
|
|
},
|
|
_ => {},
|
|
}
|
|
},
|
|
_ => {
|
|
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, 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();
|
|
}
|
|
}
|