File size: 4,718 Bytes
dcbeeb2 | 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | extern crate alloc;
use alloc::format;
use alloc::string::String;
use crate::graphics::color::Color;
use crate::graphics::surface::Surface;
pub struct CalculatorApp {
pub display: String,
pub operand: i64,
pub pending_op: Option<char>,
pub clear_on_next: bool,
}
impl CalculatorApp {
pub fn new() -> Self {
Self {
display: String::from("0"),
operand: 0,
pending_op: None,
clear_on_next: false,
}
}
pub fn handle_click(&mut self, rel_x: i32, rel_y: i32) {
let btn_w = 45;
let btn_h = 35;
let start_x = 15;
let start_y = 60;
let col = (rel_x - start_x) / (btn_w + 8);
let row = (rel_y - start_y) / (btn_h + 8);
if col >= 0 && col < 4 && row >= 0 && row < 4 {
let key = match (row, col) {
(0, 0) => '7',
(0, 1) => '8',
(0, 2) => '9',
(0, 3) => '/',
(1, 0) => '4',
(1, 1) => '5',
(1, 2) => '6',
(1, 3) => '*',
(2, 0) => '1',
(2, 1) => '2',
(2, 2) => '3',
(2, 3) => '-',
(3, 0) => 'C',
(3, 1) => '0',
(3, 2) => '=',
(3, 3) => '+',
_ => return,
};
self.input_char(key);
}
}
pub fn input_char(&mut self, c: char) {
match c {
'0'..='9' => {
if self.clear_on_next || self.display == "0" {
self.display.clear();
self.clear_on_next = false;
}
if self.display.len() < 12 {
self.display.push(c);
}
}
'+' | '-' | '*' | '/' => {
let current_val: i64 = self.display.parse().unwrap_or(0);
self.operand = current_val;
self.pending_op = Some(c);
self.clear_on_next = true;
}
'=' => {
if let Some(op) = self.pending_op {
let second_val: i64 = self.display.parse().unwrap_or(0);
let result = match op {
'+' => self.operand.saturating_add(second_val),
'-' => self.operand.saturating_sub(second_val),
'*' => self.operand.saturating_mul(second_val),
'/' => {
if second_val != 0 {
self.operand / second_val
} else {
0
}
}
_ => second_val,
};
self.display = format!("{}", result);
self.pending_op = None;
self.clear_on_next = true;
}
}
'C' => {
self.display = String::from("0");
self.operand = 0;
self.pending_op = None;
self.clear_on_next = false;
}
_ => {}
}
}
pub fn render(&self, surface: &mut Surface, x: i32, y: i32, w: usize, h: usize) {
surface.draw_rect(x, y, w, h, Color::rgb(26, 28, 36));
// Display screen
surface.draw_rounded_rect(x + 15, y + 15, w.saturating_sub(30), 35, 6, Color::rgb(18, 20, 26));
surface.draw_rect_outline(x + 15, y + 15, w.saturating_sub(30), 35, Color::rgba(255, 255, 255, 20), 1);
let disp_x = x + w as i32 - 30 - (self.display.len() as i32 * 8);
surface.draw_text(&self.display, disp_x.max(x + 25), y + 24, Color::WHITE);
// Keypad buttons
let btn_labels = [
["7", "8", "9", "/"],
["4", "5", "6", "*"],
["1", "2", "3", "-"],
["C", "0", "=", "+"],
];
let btn_w = 45;
let btn_h = 35;
let start_x = x + 15;
let start_y = y + 60;
for (row_idx, row) in btn_labels.iter().enumerate() {
for (col_idx, label) in row.iter().enumerate() {
let bx = start_x + (col_idx as i32 * (btn_w + 8));
let by = start_y + (row_idx as i32 * (btn_h + 8));
let is_op = col_idx == 3 || *label == "=";
let bg_color = if is_op { Color::ACCENT_BLUE } else { Color::rgb(38, 42, 54) };
surface.draw_rounded_rect(bx, by, btn_w as usize, btn_h as usize, 6, bg_color);
surface.draw_text(label, bx + 18, by + 10, Color::WHITE);
}
}
}
}
|