add examples #9

Open
Egor00f wants to merge 1 commits from add-excamples into main
2 changed files with 149 additions and 0 deletions

View File

@@ -1,3 +1,7 @@
# Syscalls
Syscalls library for Lua
## Examples
you can find it in tests folder

145
tests/calc.lua Normal file
View File

@@ -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