init.lua (6113B)
1 -- LuaJIT OS Calculator App - GUI Mode 2 -- Standard calculator with button interface 3 4 -- Calculator state 5 local equation = "" -- The full equation being built 6 local currentNumber = "0" -- Current number being entered 7 local result = nil -- Result after equals is pressed 8 9 -- Button layout (like Windows Calculator) 10 local buttons = { 11 {"7", "8", "9", "/"}, 12 {"4", "5", "6", "*"}, 13 {"1", "2", "3", "-"}, 14 {"C", "0", "=", "+"} 15 } 16 17 -- Window dimensions 18 local windowWidth = 280 19 local windowHeight = 400 20 local window = app:newWindow("Calculator", windowWidth, windowHeight) 21 22 -- Button dimensions 23 local buttonWidth = 60 24 local buttonHeight = 50 25 local buttonMargin = 10 26 local displayHeight = 60 27 28 -- Helper: Check if click is inside a rectangle 29 local function isInside(x, y, rx, ry, rw, rh) 30 return x >= rx and x < rx + rw and y >= ry and y < ry + rh 31 end 32 33 -- Evaluate the equation string 34 local function evaluateEquation(expr) 35 -- Simple left-to-right evaluation (no operator precedence) 36 local nums = {} 37 local ops = {} 38 local current = "" 39 40 -- Parse the expression 41 for i = 1, #expr do 42 local c = expr:sub(i, i) 43 if c == "+" or c == "-" or c == "*" or c == "/" then 44 if current ~= "" then 45 table.insert(nums, tonumber(current) or 0) 46 current = "" 47 end 48 table.insert(ops, c) 49 else 50 current = current .. c 51 end 52 end 53 54 -- Add last number 55 if current ~= "" then 56 table.insert(nums, tonumber(current) or 0) 57 end 58 59 -- Calculate left to right 60 if #nums == 0 then return 0 end 61 local result = nums[1] 62 63 for i = 1, #ops do 64 local nextNum = nums[i + 1] or 0 65 if ops[i] == "+" then 66 result = result + nextNum 67 elseif ops[i] == "-" then 68 result = result - nextNum 69 elseif ops[i] == "*" then 70 result = result * nextNum 71 elseif ops[i] == "/" then 72 if nextNum ~= 0 then 73 result = result / nextNum 74 else 75 return "Error" 76 end 77 end 78 end 79 80 return result 81 end 82 83 local function handleButton(btn) 84 if btn == "C" then 85 -- Clear everything 86 equation = "" 87 currentNumber = "0" 88 result = nil 89 elseif btn == "=" then 90 -- Calculate result 91 if equation ~= "" or currentNumber ~= "0" then 92 local fullEquation = equation .. currentNumber 93 local calcResult = evaluateEquation(fullEquation) 94 95 if type(calcResult) == "number" then 96 result = tostring(calcResult) 97 -- Truncate long decimals 98 if #result > 15 then 99 result = result:sub(1, 15) 100 end 101 else 102 result = calcResult -- "Error" 103 end 104 end 105 elseif btn == "+" or btn == "-" or btn == "*" or btn == "/" then 106 -- Add operation to equation 107 if currentNumber ~= "0" or equation ~= "" then 108 equation = equation .. currentNumber .. btn 109 currentNumber = "0" 110 result = nil 111 end 112 else 113 -- Number input 114 if result then 115 -- Start new calculation after showing result 116 equation = "" 117 currentNumber = btn 118 result = nil 119 elseif currentNumber == "0" then 120 currentNumber = btn 121 else 122 currentNumber = currentNumber .. btn 123 -- Limit number length 124 if #currentNumber > 15 then 125 currentNumber = currentNumber:sub(1, 15) 126 end 127 end 128 end 129 end 130 131 -- Mouse click handling 132 window.onClick = function(mx, my) 133 -- Check which button was clicked 134 for row = 1, 4 do 135 for col = 1, 4 do 136 local btnX = buttonMargin + (col - 1) * (buttonWidth + buttonMargin) 137 local btnY = displayHeight + 20 + (row - 1) * (buttonHeight + buttonMargin) 138 139 if isInside(mx, my, btnX, btnY, buttonWidth, buttonHeight) then 140 local btn = buttons[row][col] 141 handleButton(btn) 142 window:markDirty() -- Redraw after button press 143 return 144 end 145 end 146 end 147 end 148 149 -- Draw the calculator 150 window.onDraw = function(gfx) 151 -- Background 152 gfx:fillRect(0, 0, windowWidth, windowHeight, 0x2C2C2C) 153 154 -- Display area 155 gfx:fillRect(10, 10, windowWidth - 20, displayHeight, 0x1E1E1E) 156 157 -- Show result if calculated, otherwise show equation being built 158 local displayText 159 if result then 160 displayText = result 161 else 162 displayText = equation .. currentNumber 163 if displayText == "" then 164 displayText = "0" 165 end 166 end 167 168 gfx:drawText(20, 25, displayText, 0xFFFFFF) 169 170 -- Draw buttons 171 for row = 1, 4 do 172 for col = 1, 4 do 173 local btn = buttons[row][col] 174 local x = buttonMargin + (col - 1) * (buttonWidth + buttonMargin) 175 local y = displayHeight + 20 + (row - 1) * (buttonHeight + buttonMargin) 176 177 -- Button color based on type 178 local btnColor = 0x505050 179 if btn == "=" then 180 btnColor = 0x0066CC 181 elseif btn == "C" then 182 btnColor = 0xCC3333 183 elseif btn == "+" or btn == "-" or btn == "*" or btn == "/" then 184 btnColor = 0x666666 185 end 186 187 -- Draw button 188 gfx:fillRect(x, y, buttonWidth, buttonHeight, btnColor) 189 190 -- Draw button border (lighter edge) 191 gfx:drawRect(x, y, buttonWidth, buttonHeight, 0x888888) 192 193 -- Draw button text (centered) 194 local textX = x + buttonWidth / 2 - 4 195 local textY = y + buttonHeight / 2 - 6 196 gfx:drawText(textX, textY, btn, 0xFFFFFF) 197 end 198 end 199 200 -- Draw instructions at bottom 201 gfx:drawText(10, windowHeight - 20, "Click buttons to calculate", 0x888888) 202 end 203 204 print("Calculator GUI loaded") 205 print("Window dimensions: " .. windowWidth .. "x" .. windowHeight) 206 print("Use mouse to click buttons")