1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//! A crate for running/simulating the `calc-rs` project on windows or linux.

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() {
                    // println!("msg: {}", &calc_state.msg);
                    calc_state.msg.clear();
                }
                draw(state, window);
            } else {
                println!("Unknown key {key}");
            }
        });

        return CalculatorState::default();
    });

    instance.unwrap().run();
}