local currentInput = ""
local resultDisplayed = false
function onCreate()
createSprite('calculatorBackground', 300, 400, 'FF5733', 0, 0)
createSprite('display', 280, 50, '000000', 0, -100)
local buttons = {
{'7', 0, 0}, {'8', 60, 0}, {'9', 120, 0}, {'/', 180, 0},
{'4', 0, 60}, {'5', 60, 60}, {'6', 120, 60}, {'*', 180, 60},
{'1', 0, 120}, {'2', 60, 120}, {'3', 120, 120}, {'-', 180, 120},
{'0', 60, 180}, {'+', 180, 180}, {'=', 120, 180}
}
local buttonContainerX = (screenWidth / 2) - 100
local buttonContainerY = (screenHeight / 2) - 60
for _, button in ipairs(buttons) do
createButton('button' .. button[1], button[1], buttonContainerX + button[2], buttonContainerY + button[3])
end
makeLuaText('displayText', ' ', 280, (screenWidth / 2) - 140, (screenHeight / 2) - 115)
setTextSize('displayText', 30)
setTextColor('displayText', 'FFFFFF')
setObjectCamera('displayText', 'other')
addLuaText('displayText')
end
function createSprite(name, width, height, color, offsetX, offsetY)
makeLuaSprite(name, '', 0, 0)
makeGraphic(name, width, height, color)
setObjectCamera(name, 'other')
setProperty(name .. '.x', (screenWidth / 2) - (width / 2) + offsetX)
setProperty(name .. '.y', (screenHeight / 2) - (height / 2) + offsetY)
addLuaSprite(name, false)
end
function createButton(name, label, x, y)
makeLuaSprite(name, '', x, y)
makeGraphic(name, 50, 50, '000000')
setObjectCamera(name, 'other')
addLuaSprite(name, false)
makeLuaText(name .. 'Text', label, 50, x + 15, y + 10)
setTextSize(name .. 'Text', 20)
setTextColor(name .. 'Text', 'FFFFFF')
setObjectCamera(name .. 'Text', 'other')
addLuaText(name .. 'Text')
end
function onSongStart()
setPropertyFromClass('flixel.FlxG', 'mouse.visible', true)
end
function onUpdate(elapsed)
local buttons = {
'7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '+', '='
}
for _, button in ipairs(buttons) do
if mouseOverlaps('button' .. button) and mouseClicked('left') then
handleButtonClick(button)
break
end
end
end
function handleButtonClick(button)
print("Button clicked: " .. button)
if button == '=' then
local success, result = pcall(function() return load("return " .. currentInput)() end)
if success then
currentInput = tostring(result)
else
currentInput = "Error"
end
resultDisplayed = true
else
if resultDisplayed then
currentInput = button
resultDisplayed = false
else
currentInput = currentInput .. button
end
end
updateDisplay()
end
function updateDisplay()
setTextString('displayText', currentInput)
end
function mouseOverlaps(tag)
addHaxeLibrary('Reflect')
return runHaxeCode([[
var obj = game.getLuaObject(']]..tag..[[');
if (obj == null) obj = Reflect.getProperty(game, ']]..tag..[[');
if (obj == null) return false;
return obj.getScreenBounds(null, obj.cameras [0]).containsPoint(FlxG.mouse.getScreenPosition(obj.cameras[0]));
]])
end