use calc::CalculatorState;
use calc_common::{Button, Character, MetaButton};
use wwind::{Color, WWindState, Window};
const PIXEL_SIZE: u16 = 10;
fn map_to_button(key: u32) -> Option<Button> {
const ENTER: u32 = 65293;
const BACKSPACE: u32 = 65288;
const DELETE: u32 = 65535;
const LEFT: u32 = 65361;
const RIGHT: u32 = 65363;
const UP: u32 = 65362;
const DOWN: u32 = 65364;
const ALT: u32 = 65514;
const HOME: u32 = 65360;
const ZERO: u32 = b'0' as _;
const NINE: u32 = b'9' as _;
const PLUS: u32 = b'+' as _;
const MINUS: u32 = b'-' as _;
const MULTIPLY: u32 = b'*' as _;
const SLASH: u32 = b'/' as _;
const CARET: u32 = b'^' as _;
const CLOSE_PAREN: u32 = b')' as _;
const OPEN_PAREN: u32 = b'(' as _;
dbg!(&key);
match key {
ZERO..=NINE => Character::from_digit((key - ZERO) as u8).map(|char| char.into()),
PLUS => Some(Character::Plus.into()),
MINUS => Some(Character::Minus.into()),
MULTIPLY => Some(Character::Multiply.into()),
SLASH => Some(Character::Slash.into()),
CARET => Some(Character::Caret.into()),
OPEN_PAREN => Some(Character::OpenParen.into()),
CLOSE_PAREN => Some(Character::CloseParen.into()),
ENTER => Some(MetaButton::Enter.into()),
BACKSPACE => Some(MetaButton::Back.into()),
DELETE => Some(MetaButton::Clear.into()),
LEFT => Some(MetaButton::Left.into()),
RIGHT => Some(MetaButton::Right.into()),
UP => Some(MetaButton::Up.into()),
DOWN => Some(MetaButton::Down.into()),
ALT => Some(MetaButton::Alt.into()),
HOME => Some(MetaButton::Menu.into()),
_ => None,
}
}
fn draw(state: &mut WWindState<CalculatorState>, window: &mut Window<CalculatorState>) {
let mut dc = window.get_drawing_context();
dc.set_draw_color(Color::from_hex(0xffffff));
dc.draw_rectangle(wwind::RectRegion {
x: 0,
y: 0,
width: 128 * PIXEL_SIZE,
height: 64 * PIXEL_SIZE,
});
dc.set_draw_color(Color::from_hex(0x000000));
for idx in state.userdata().screen.ones() {
let column = idx as u16 / 64;
let row = idx as u16 % 64;
dc.draw_rectangle(wwind::RectRegion {
x: column * PIXEL_SIZE,
y: row * PIXEL_SIZE,
width: PIXEL_SIZE,
height: PIXEL_SIZE,
});
}
}
fn main() {
let instance = wwind::WWindInstance::<_, CalculatorState>::new(|state| {
let mut window = state.add_window(
0,
0,
64 * PIXEL_SIZE,
128 * PIXEL_SIZE,
"Calculator Simulation",
);
window.on_redraw(|state, window, _| draw(state, window));
window.on_keydown(|state, window, key| {
let calc_state = state.userdata_mut();
if let Some(button) = map_to_button(key) {
calc_state.on_button_press(button);
if !calc_state.msg.is_empty() {
calc_state.msg.clear();
}
draw(state, window);
} else {
println!("Unknown key {key}");
}
});
return CalculatorState::default();
});
instance.unwrap().run();
}