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; use crate::config::GUI; pub fn main(cfg: GUI, output_stream: Box) -> 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, inputs: Inputs, // TODO keys down flags: Flags, } struct Flags { output_stream: Box, 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), 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) { 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, }, Command::none()) } fn title(&self) -> String { return String::from("Rusty Pipe") } fn update(&mut self, msg: Message) -> Command { 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) => { eprintln!("{:?}", event); match event { iced::event::Event::Keyboard(keyboard::Event::KeyPressed{ // TODO key released key_code, modifiers: _, .. }) => { let nope = String::from(""); let s = 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, }; if s.len() > 0 { self.flags.output_stream.put(s.chars().collect()); } }, _ => {}, } }, _ => { self.configuring = Some(msg.clone()); self.ntfy = format!("push a key to bind to {:?}", msg.clone()); }, } // TODO self.flags.output_stream.put(s.chars().collect()); for all keys down return Command::none(); } fn subscription(&self) -> Subscription { return subscription::events_with(|event, _| match event { iced::Event::Keyboard(_) => Some(Message::EventOccurred(event)), _ => None, }); } fn view(&self) -> Element { 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(); } }