Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local charMap =
- {
- [2] = "1",
- [3] = "2",
- [4] = "3",
- [5] = "4",
- [6] = "5",
- [7] = "6",
- [8] = "7",
- [9] = "8",
- [10] = "9",
- [11] = "0",
- [12] = "-",
- [13] = "=",
- [16] = "q",
- [17] = "w",
- [18] = "e",
- [19] = "r",
- [20] = "t",
- [21] = "y",
- [22] = "u",
- [23] = "i",
- [24] = "o",
- [25] = "p",
- [26] = "[",
- [27] = "]",
- [30] = "a",
- [31] = "s",
- [32] = "d",
- [33] = "f",
- [34] = "g",
- [35] = "h",
- [36] = "j",
- [37] = "k",
- [38] = "l",
- [39] = ";",
- [40] = "'",
- [44] = "z",
- [45] = "x",
- [46] = "c",
- [47] = "v",
- [48] = "b",
- [49] = "n",
- [50] = "m",
- [51] = ",",
- [52] = ".",
- [53] = "/",
- [57] = " ",
- [71] = "7",
- [72] = "8",
- [73] = "9",
- [75] = "4",
- [76] = "5",
- [77] = "6",
- [79] = "1",
- [81] = "2",
- [82] = "3",
- [181] = "/",
- [55] = "*",
- [74] = "-",
- [78] = "+",
- [41] = "`"
- }
- function newTermFrame(t)
- local handle = {}
- handle.term = t
- handle.frame = {}
- handle.put = function(frame)
- handle.frame = frame
- return handle
- end
- handle.render = function()
- term.clear()
- local render = {}
- render.write = function(x,y,text)
- handle.term.setCursorPos(x,y)
- handle.term.write(text)
- end
- if handle.frame then
- handle.frame.render.render(render)
- end
- return handle
- end
- handle.setFocused = function(frame)
- handle.activeFrame = frame
- end
- handle.event = {}
- handle.event.click = function(x,y)
- if handle.frame then
- handle.frame.event.click(x,y,handle)
- end
- end
- handle.event.key = function(c)
- if handle.activeFrame then
- if handle.activeFrame.event.key then
- handle.activeFrame.event.key(c,handle)
- end
- end
- end
- return handle
- end
- function newVframe(size)
- local handle = {}
- handle.frames = {}
- handle.size = size
- handle.put = function(n, frame)
- handle.frames[n] = frame
- return handle
- end
- handle.render = {}
- handle.render.getWidth = function()
- local max = 0
- for n=1, handle.size do
- if handle.frames[n] then
- local w = handle.frames[n].render.getWidth()
- if w > max then
- max = w
- end
- end
- end
- return max
- end
- handle.render.getHeight = function()
- local height = 0
- for n=1, handle.size do
- if handle.frames[n] then
- height = height + handle.frames[n].render.getHeight()
- end
- end
- return height
- end
- handle.render.render = function(ctx)
- local h = 0
- for n=1, handle.size do
- if handle.frames[n] then
- local render = {}
- render.write = function(x,y,txt)
- ctx.write(x,y+h,txt)
- end
- handle.frames[n].render.render(render)
- h = h + handle.frames[n].render.getHeight()
- end
- end
- end
- handle.event = {}
- handle.event.click = function(x,y, master)
- local top = 1
- for n=1, handle.size do
- if handle.frames[n] then
- local bottom = top + handle.frames[n].render.getHeight() - 1
- local width = handle.frames[n].render.getWidth()
- if x <= width and y >= top and y <= bottom then
- handle.frames[n].event.click(x, y - top + 1, master)
- break
- end
- top = bottom + 1
- end
- end
- end
- return handle
- end
- function newHframe(size)
- local handle = {}
- handle.frames = {}
- handle.size = size
- handle.put = function(n, frame)
- handle.frames[n] = frame
- return handle
- end
- handle.render = {}
- handle.render.getWidth = function()
- local width = 0
- for n=1, handle.size do
- if handle.frames[n] then
- width = width + handle.frames[n].render.getWidth()
- end
- end
- return width
- end
- handle.render.getHeight = function()
- local max = 0
- for n=1, handle.size do
- if handle.frames[n] then
- local h = handle.frames[n].render.getHeight()
- if h > max then
- max = h
- end
- end
- end
- return max
- end
- handle.render.render = function(ctx)
- local w = 0
- for n=1, handle.size do
- if handle.frames[n] then
- local render = {}
- render.write = function(x,y,txt)
- ctx.write(x+w,y,txt)
- end
- handle.frames[n].render.render(render)
- w = w + handle.frames[n].render.getWidth()
- end
- end
- end
- handle.event = {}
- handle.event.click = function(x,y,master)
- local left = 1
- for n=1, handle.size do
- if handle.frames[n] then
- local right = left + handle.frames[n].render.getWidth() - 1
- local height = handle.frames[n].render.getHeight()
- if y <= height and x >= left and x <= right then
- handle.frames[n].event.click(x - left + 1, y, master)
- break
- end
- left = right + 1
- end
- end
- end
- return handle
- end
- function newBorderFrame(p1,p2,p3,p4)
- local handle = {}
- if p1 and not(p2 or p3 or p4) then
- handle.top = p1
- handle.bottom = p1
- handle.left = p1
- handle.right = p1
- elseif (p1 and p2) and not(p3 or p4) then
- handle.top = p1
- handle.bottom = p1
- handle.left = p2
- handle.right = p2
- elseif p1 and p2 and p3 and p4 then
- handle.top = p1
- handle.bottom = p2
- handle.left = p3
- handle.right = p4
- else
- error("MGTK:newBorderFrame: Invalid parameters")
- end
- handle.put = function(frame)
- handle.frame = frame
- return handle
- end
- handle.render = {}
- handle.render.getWidth = function()
- local w = handle.left + handle.right
- if handle.frame then
- w = w + handle.frame.render.getWidth()
- end
- return w
- end
- handle.render.getHeight = function()
- local h = handle.top + handle.bottom
- if handle.frame then
- h = h + handle.frame.render.getHeight()
- end
- return h
- end
- handle.render.render = function(ctx)
- if handle.frame then
- local render = {}
- render.write = function(x,y,txt)
- ctx.write(x + handle.left, y + handle.top, txt)
- end
- handle.frame.render.render(render)
- end
- end
- handle.event = {}
- handle.event.click = function(x,y,master)
- if handle.frame then
- handle.frame.event.click(x-handle.left,y-handle.top,master)
- end
- end
- return handle
- end
- function newClickableFrame(action)
- local handle = {}
- handle.action = action
- handle.render = {}
- handle.put = function(frame)
- handle.frame = frame
- handle.render = handle.frame.render
- return handle
- end
- handle.event = {}
- handle.event.click = function(x,y,master)
- handle.action(handle,x,y)
- if handle.frame then
- handle.frame.event.click(x,y,master)
- end
- end
- return handle
- end
- function newTextLabel(text)
- local handle = {}
- handle.text = text
- handle.render = {}
- handle.render.getWidth = function()
- return #handle.text
- end
- handle.render.getHeight = function()
- return 1
- end
- handle.setText = function(text)
- handle.text = text
- return handle
- end
- handle.getText = function(text)
- return handle.text
- end
- handle.render.render = function(ctx)
- ctx.write(1,1,handle.text)
- end
- handle.event = {}
- handle.event.click = function()end
- return handle
- end
- function newTextInput(width, default)
- local handle = {}
- handle.width = width
- handle.render = {}
- handle.text = default or ""
- handle.render.getWidth = function()
- return handle.width+1
- end
- handle.render.getHeight = function()
- return 1
- end
- handle.setText = function(text)
- handle.text = text
- return handle
- end
- handle.getText = function(text)
- return handle.text
- end
- handle.render.render = function(ctx)
- ctx.write(1,1,handle.text:sub(#handle.text-handle.width > 0 and #handle.text-handle.width+1 or 0,#handle.text))
- end
- handle.event = {}
- handle.event.click = function(x,y,master)master.setFocused(handle)end
- handle.event.key = function(key, master)
- if charMap[key] then
- handle.text = handle.text .. charMap[key]
- elseif key == 14 then
- handle.text = handle.text:sub(1,#handle.text-1)
- end
- master.render()
- end
- return handle
- end
- function newPasswordInput(width, default)
- local handle = {}
- handle.width = width
- handle.render = {}
- handle.text = default or ""
- handle.rendered = default and string.rep("*",#default) or ""
- handle.render.getWidth = function()
- return handle.width+1
- end
- handle.render.getHeight = function()
- return 1
- end
- handle.setText = function(text)
- handle.text = text
- handle.rendered = string.rep("*",#text)
- return handle
- end
- handle.getText = function(text)
- return handle.text
- end
- handle.render.render = function(ctx)
- ctx.write(1,1,handle.rendered:sub(#handle.rendered-handle.width > 0 and #handle.rendered-handle.width+1 or 0,#handle.rendered))
- end
- handle.event = {}
- handle.event.click = function(x,y,master)master.setFocused(handle)end
- handle.event.key = function(key, master)
- if charMap[key] then
- handle.text = handle.text .. charMap[key]
- handle.rendered = handle.rendered .. "*"
- elseif key == 14 then
- handle.text = handle.text:sub(1,#handle.text-1)
- handle.rendered = handle.rendered:sub(1,#handle.rendered-1)
- end
- master.render()
- end
- return handle
- end
- function newTextButton(text, action)
- local handle = {}
- handle.text = text
- handle.action = action
- handle.render = {}
- handle.render.getWidth = function()
- return #handle.text
- end
- handle.render.getHeight = function()
- return 1
- end
- handle.setText = function(text)
- handle.text = text
- return handle
- end
- handle.getText = function(text)
- return handle.text
- end
- handle.render.render = function(ctx)
- ctx.write(1,1,handle.text)
- end
- handle.event = {}
- handle.event.click = function(x,y,master)
- master.setFocused(handle)
- if handle.action then
- handle.action(handle,x,y)
- end
- end
- return handle
- end
- function newToggleButton(text1, text2, action)
- local handle = {}
- handle.text1 = text1
- handle.text2 = text2
- handle.state = true
- handle.action = action
- handle.render = {}
- handle.render.getWidth = function()
- return #(handle.state and handle.text1 or handle.text2)
- end
- handle.render.getHeight = function()
- return 1
- end
- handle.setText1 = function(text1)
- handle.text1 = text1
- return handle
- end
- handle.setText2 = function(text2)
- handle.text2 = text2
- return handle
- end
- handle.getText = function()
- return handle.text1, handle.text1
- end
- handle.getState = function()
- return handle.state
- end
- handle.setState = function(state)
- handle.state = state
- return handle
- end
- handle.render.render = function(ctx)
- ctx.write(1,1,handle.state and handle.text1 or handle.text2)
- end
- handle.event = {}
- handle.event.click = function(x,y,master)
- master.setFocused(handle)
- if handle.action then
- handle.state = not handle.state
- handle.action(handle,handle.state,x,y)
- end
- end
- return handle
- end
- function newEventLoop(handler)
- local handle = {}
- handle.handler = handler
- handle.running = true
- handle.click = {}
- handle.key = {}
- handle.setClickEvent = function(name, processor)
- handle.click.name = name
- handle.click.processor = processor
- return handle
- end
- handle.setKeyEvent = function(name, processor)
- handle.key.name = name
- handle.key.processor = processor
- return handle
- end
- handle.stop = function()
- handle.running = false
- return handle
- end
- handle.start = function()
- while handle.running do
- local e = {os.pullEvent()}
- if handle.click.name and handle.click.name == e[1] then
- local x,y
- if handle.click.processor then
- x,y = handle.click.processor(unpack(e))
- else
- x = e[3]
- y = e[4]
- end
- if x and y then
- handle.handler.event.click(x,y)
- end
- elseif handle.key.name and handle.key.name == e[1] then
- local c
- if handle.click.processor then
- c = handle.key.processor(unpack(e))
- else
- c = e[2]
- end
- handle.handler.event.key(c)
- end
- end
- end
- return handle
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement