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, 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); } } } }