use embedded_graphics_simulator::{
BinaryColorTheme, OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use embedded_graphics::{fonts::Font6x8, pixelcolor::BinaryColor, prelude::*};
use embedded_text::prelude::*;
use sdl2::keyboard::{Keycode, Mod};
use std::{collections::HashMap, thread, time::Duration};
trait Selector {
fn select_modified(&self, keymod: Mod) -> &str;
}
impl Selector for (&str, &str, &str, &str) {
#[inline]
fn select_modified(&self, keymod: Mod) -> &str {
if keymod.contains(Mod::RALTMOD) {
self.3
} else if keymod.intersects(Mod::LSHIFTMOD | Mod::RSHIFTMOD) {
self.1
} else if keymod.contains(Mod::CAPSMOD) {
self.2
} else {
self.0
}
}
}
fn main() {
let inputs: HashMap<_, _> = [
(Keycode::A, ("a", "A", "A", "ä")),
(Keycode::B, ("b", "B", "B", "{")),
(Keycode::C, ("c", "C", "C", "&")),
(Keycode::D, ("d", "D", "D", "Đ")),
(Keycode::E, ("e", "E", "E", "Ä")),
(Keycode::F, ("f", "F", "F", "[")),
(Keycode::G, ("g", "G", "G", "]")),
(Keycode::H, ("h", "H", "H", "")),
(Keycode::I, ("i", "I", "I", "Í")),
(Keycode::J, ("j", "J", "J", "í")),
(Keycode::K, ("k", "K", "K", "ł")),
(Keycode::L, ("l", "L", "L", "Ł")),
(Keycode::M, ("m", "M", "M", "<")),
(Keycode::N, ("n", "N", "N", "}")),
(Keycode::O, ("o", "O", "O", "")),
(Keycode::P, ("p", "P", "P", "")),
(Keycode::Q, ("q", "Q", "Q", "\\")),
(Keycode::R, ("r", "R", "R", "")),
(Keycode::S, ("s", "S", "S", "đ")),
(Keycode::T, ("t", "T", "T", "")),
(Keycode::U, ("u", "U", "U", "€")),
(Keycode::V, ("v", "V", "V", "@")),
(Keycode::W, ("w", "W", "W", "|")),
(Keycode::X, ("x", "X", "X", "#")),
(Keycode::Y, ("y", "Y", "Y", ">")),
(Keycode::Z, ("z", "Z", "Z", "")),
(Keycode::Num0, ("0", "§", "0", "")),
(Keycode::Num1, ("1", "'", "1", "~")),
(Keycode::Num2, ("2", "\"", "2", "ˇ")),
(Keycode::Num3, ("3", "+", "3", "^")),
(Keycode::Num4, ("4", "!", "4", "˘")),
(Keycode::Num5, ("5", "%", "5", "°")),
(Keycode::Num6, ("6", "/", "6", "˛")),
(Keycode::Num7, ("7", "=", "7", "`")),
(Keycode::Num8, ("8", "(", "8", "˙")),
(Keycode::Num9, ("9", ")", "9", "´")),
(Keycode::Space, (" ", " ", " ", " ")),
(Keycode::Comma, (",", "?", ",", " ")),
(Keycode::Period, (".", ":", ".", ">")),
(Keycode::Minus, ("-", "_", "-", "*")),
(Keycode::Return, ("\n", "\n", "\n", "\n")),
(Keycode::KpEnter, ("\n", "\n", "\n", "\n")),
]
.iter()
.cloned()
.collect();
let bounds = Rectangle::new(Point::new(0, 0), Point::new(127, 63));
let textbox_style = TextBoxStyleBuilder::new(Font6x8)
.vertical_alignment(Scrolling)
.text_color(BinaryColor::On)
.build();
let output_settings = OutputSettingsBuilder::new()
.theme(BinaryColorTheme::OledBlue)
.build();
let mut window = Window::new("TextBox input demonstration", &output_settings);
let mut text = String::from("Hello, world!");
'running: loop {
let text_and_cursor = format!("{}_", text);
let text_box = TextBox::new(&text_and_cursor, bounds).into_styled(textbox_style);
let mut display = SimulatorDisplay::new(text_box.size());
text_box.draw(&mut display).unwrap();
window.update(&display);
for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
SimulatorEvent::KeyDown {
keycode, keymod, ..
} => match keycode {
Keycode::Escape => break 'running,
Keycode::Backspace => {
text.pop();
}
_ => {
inputs.get(&keycode).map(|k| {
text += k.select_modified(keymod);
});
}
},
_ => {}
}
}
thread::sleep(Duration::from_millis(10));
}
}