From 7fd94fc4c640a38584b40a7132f622104004c38b Mon Sep 17 00:00:00 2001 From: Egor00f Date: Fri, 25 Apr 2025 23:30:55 +0500 Subject: [PATCH] add calc example && update readme --- README.md | 4 ++ tests/calc.lua | 145 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 tests/calc.lua diff --git a/README.md b/README.md index c7f8587..995bfd3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,7 @@ # Syscalls Syscalls library for Lua + +## Examples + +you can find it in tests folder diff --git a/tests/calc.lua b/tests/calc.lua new file mode 100644 index 0000000..f67a413 --- /dev/null +++ b/tests/calc.lua @@ -0,0 +1,145 @@ +local syscalls = require("syscalls") + +local systemColors = syscalls.GetSystemColors() +local skinHeight = syscalls.GetSkinHeight() + +local display = "0" +local ans = "0" + +local buttons = { + {"1", "2", "3", "+", "-"}, + {"4", "5", "6", "*", "/"}, + {"7", "8", "9", "%", ""}, + {"0", ".", "Ans", "="} +} + +local function DrawDisplay() + syscalls.DrawText( + display, + 20, + 20 + skinHeight, + systemColors.workText + ) +end + +local function redraw() + syscalls.StartRedraw() + + syscalls.CreateWindow( + 100, + 100, + 200, + 256, + "lua example Calc", + systemColors.workArea, + syscalls.windowStyle.WithSkinFixSizes + ) + + DrawDisplay() + + --local t = syscalls.ThreadInfo() + + local i = 2 + local h = math.floor(150 / #buttons) + + for rowN, row in pairs(buttons) do + + local w = math.floor(200 / #row) + + for col, v in pairs(row) do + + local x = math.floor((col-1) * w) + local y = math.floor((rowN-1) * h + 50 + skinHeight) + + syscalls.DefineButton( + x, + y, + w, + h, + i, + systemColors.workButton + ) + + syscalls.DrawText( + v, + x, + y, + systemColors.workButtonText + ) + + i=i+1 + end + end + + + syscalls.EndRedraw() +end + +local function CheckButton(ButtonID) + local i = 2 + for k, v in pairs(buttons) do + for j, s in pairs(v) do + if ButtonID ~= i then + i = i + 1 + else + return s + end + end + end + + return nil +end + +local function checkInput(s) + if s == "=" then + display = assert(load( "return " .. display))() + ans = display + elseif s == "Ans" then + display = display .. ans + elseif s then + if display == "0" then + display = s + else + display = display .. s + end + end + + syscalls.DrawRectangle(20, 20 + skinHeight, 200, 30, systemColors.workArea) + DrawDisplay() +end + +-- main + +redraw() + +local exit = false +while not exit do + local event = syscalls.WaitEvent() + + if event == syscalls.Event.Redraw then + redraw() + elseif event == syscalls.Event.Button then + local ButtonID = syscalls.GetButton() + + if ButtonID == syscalls.buttons.close then + exit = true + else + local s = CheckButton(ButtonID) + + checkInput(s) + end + elseif event == syscalls.Event.Key then + local key, a = syscalls.GetKey() + + key = key:sub(1, 1) + + for _, row in pairs(buttons) do + for _, v in pairs(row) do + if v == key then + checkInput(key) + break + end + end + end + end +end