Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("utils")
- --constant
- local MAXCHAR = 20
- local MAXITEMS = 5
- local BLINKTIME = 0.2
- --buffer variables
- local dy = 0
- local dx = 0
- local bf = {}
- local rClickedPos = {0, 0}
- --Private functions
- local CloseMenu = function()
- local _clsstr = utils.FillString("", MAXCHAR)
- for i = 0, #bf-1, 1 do
- term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+i-dy)
- print(_clsstr)
- end
- end
- local BLINKTIMEItemMenu = function(num)
- utils.SetPalette("select")
- term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+num-dy)
- print(bf[num+1]["text"])
- os.sleep(BLINKTIME)
- utils.SetPalette("os")
- term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+num-dy)
- print(bf[num+1]["text"])
- end
- local DrawMenu = function()
- local _bflen = #bf
- dx = 0
- dy = 0
- _ww, _wh = term.getSize();
- if (rClickedPos[1]+MAXCHAR > _ww) then dx = MAXCHAR end
- if (rClickedPos[2]+_bflen > _wh-1) then dy = _bflen end
- utils.SetPalette("standart")
- for i = 0, _bflen-1, 1 do
- term.setCursorPos(rClickedPos[1]-dx, rClickedPos[2]+i-dy)
- print(bf[i+1]["text"])
- end
- utils.SetPalette("os")
- end
- --MainFunction
- Update = function()
- if (#bf == 0) then error("Error: expected at least one element.") end
- local event, btn, xPos, yPos = os.pullEvent()
- if (event ~= "mouse_click") then return end
- if (btn == 2) then
- CloseMenu()
- rClickedPos[1] = xPos
- rClickedPos[2] = yPos
- DrawMenu()
- elseif (btn == 1) then
- for i = 0, #bf-1, 1 do
- if ((xPos >= rClickedPos[1] and xPos <= rClickedPos[1]+MAXCHAR) and yPos == rClickedPos[2]+i-dy) then
- BLINKTIMEItemMenu(i)
- CloseMenu()
- bf[i+1]["func"]()
- break
- end
- end
- CloseMenu()
- end
- end
- AddItem = function(id, text, func)
- if (type(id) ~= "number") then
- error("Error: Expected number, got " .. type(text) .. ".")
- elseif (type(text) ~= "string") then
- error("Error: Expected string, got " .. type(text) .. ".")
- elseif (type(func) ~= "function") then
- error("Error: Expected function, got " .. type(text) .. ".")
- elseif (id <= 0) then
- error("Error: Id <= 0.")
- elseif (id > MAXITEMS) then
- error("Error: Id > " .. MAXITEMS .. ".")
- elseif (#text > MAXCHAR) then
- error("Error: The maximum length of this string " .. MAXCHAR .. ".")
- elseif (#bf == MAXITEMS) then
- error("Error: The maximum number of elements " .. MAXITEMS .. ".")
- end
- bf[id] = {}
- bf[id]["text"] = utils.FillString(text, MAXCHAR)
- bf[id]["func"] = func
- end
- RemoveItem = function(id)
- if (type(id) ~= "number") then
- error("Error: Expected number, got " .. type(text) .. ".")
- elseif (id <= 0) then
- error("Error: Id <= 0.")
- elseif (id > MAXITEMS) then
- error("Error: Id > " .. MAXITEMS .. ".")
- end
- table.remove(bf, id)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement