Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Types: select, int, u_int, options, bool, text
- --Meta: value, max_length, position (xy), fg, bg,
- default = {
- bg = colors.black,
- fg = colors.white
- }
- default.width,default.height = term.getSize()
- function createField(t)
- setmetatable(t,{__index={typ='int', cursurPos={x=1,y=1},maxLength=7,fg=colors.white,bg=colors.gray}})
- local ptr, typ, options, func, cursurPos, maxLength, fg, bg =
- t[1] or t.ptr,
- t[2] or t.typ,
- t[3] or t.options,
- t[4] or t.func,
- t[5] or t.cursurPos,
- t[6] or t.maxLength,
- t[7] or t.fg,
- t[8] or t.bg
- if ptr == nil then return nil end
- local active = false
- local negative = typ == 'int' and ptr.val < 0
- return {update = function(key)
- if typ == 'int' then
- local edit = string.len(tostring(ptr.val))
- if ptr.val == 0 then edit = 0 end
- if key <= 11 and (edit < maxLength-1 or (edit < maxLength and ptr.val<0)) then
- val = (key-1)%10
- if edit == 0 then
- ptr.val = val
- if negative and ptr.val > 0 then
- ptr.val = ptr.val * -1
- end
- else
- ptr.val = ptr.val*10
- if not negative then ptr.val = ptr.val + val
- else ptr.val = ptr.val - val end
- end
- return true
- end
- if keys.getName(key) == 'minus' and edit == 0 then
- negative = true
- ptr.val = 0
- return true
- end
- if keys.getName(key) == 'backspace' then
- if ptr.val > 0 then ptr.val = math.floor(ptr.val/10)
- else ptr.val = math.ceil(ptr.val/10) end
- if edit == 0 and negative then
- negative = false
- end
- return true
- end
- elseif typ == 'bool' then
- if keys.getName(key) == 'left' or keys.getName(key) == 'right' then
- ptr.val = not ptr.val
- return true
- end
- elseif typ == 'string' then
- if string.len(keys.getName(key)) == 1 then b = string.byte(keys.getName(key))
- else b = 0 end
- if keys.getName(key) == 'backspace' then
- ptr.val = string.sub(ptr.val,1,-2)
- return true
- elseif b >= string.byte('a') and b <= string.byte('z') and string.len(ptr.val)<maxLength then
- ptr.val = ptr.val..keys.getName(key)
- return true
- elseif key>=1 and key <= 11 and string.len(ptr.val)<maxLength then
- ptr.val = ptr.val..(key-1)%10
- return true
- end
- elseif typ == 'options' then
- if keys.getName(key) == 'left' then
- ptr.val = (ptr.val - 1)%table.getn(options)
- return true
- elseif keys.getName(key) == 'right' then
- ptr.val = (ptr.val + 1)%table.getn(options)
- return true
- end
- elseif typ == 'select' then
- if keys.getName(key) == 'enter' then
- func()
- end
- end
- return false
- end,
- render = function()
- if active then term.setBackgroundColor( bg )
- else term.setBackgroundColor( default.bg ) end
- term.setCursorPos(cursurPos.x,cursurPos.y)
- for i=1,maxLength do write(' ') end
- -- term.setBackgroundColor( default.bg )
- term.setCursorPos(cursurPos.x,cursurPos.y)
- if active and typ == 'int' and negative then write('-') if ptr.val~= 0 then print(math.abs(ptr.val)) end
- elseif typ == 'options' then print(options[ptr.val+1])
- else print(ptr.val) end
- term.setBackgroundColor( default.bg )
- return
- end,
- getVal = function() return ptr.val end,
- setVal = function(_val) ptr.val = _val end,
- isActive = function() return active end,
- setActive = function(_active) active=_active end,
- getMaxLength = function() return maxLength end,
- getCursurPos = function() return cursurPos end}
- end
- function drawBox(t)
- setmetatable(t,{__index={bg=default.bg}})
- local bg, pos1, pos2 =
- t[1] or t.bg,
- t[2] or t.pos1,
- t[3] or t.pos2
- term.setBackgroundColor(bg)
- for i=0,pos2[2]-pos1[2] do
- term.setCursorPos(pos1[1],pos1[2]+i)
- for i=1,pos2[1]-pos1[1]+1 do write(' ') end
- end
- term.setBackgroundColor(default.bg)
- end
- function writeText(t)
- setmetatable(t,{__index={fg=default.fg, bg=default.bg}})
- local text, cursurPos, fg, bg =
- t[1] or t.text,
- t[2] or t.cursurPos,
- t[3] or t.fg,
- t[4] or t.bg
- if cursurPos then
- term.setCursorPos(cursurPos.x,cursurPos.y)
- end
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- write(text)
- term.setBackgroundColor(default.bg)
- term.setTextColor(default.fg)
- end
- function createMenuListener(t)
- setmetatable(t,{__index={run_ptr={val=true},sel={val=0}}})
- local fields, run_ptr, onChange, onScroll, sel =
- t[1] or t.fields,
- t[2] or t.run_ptr,
- t[3] or t.onChange,
- t[4] or t.onScroll,
- t[5] or t.sel
- fields[sel.val+1].setActive(true)
- return function()
- renderFields(fields)
- while run_ptr.val do
- local event,key,isHeld = os.pullEvent('key')
- if keys.getName(key) == 'down' then
- fields[sel.val+1].setActive(false)
- sel.val = (sel.val + 1)%table.getn(fields)
- fields[sel.val+1].setActive(true)
- if onScroll then onScroll() end
- renderFields(fields)
- elseif keys.getName(key) == 'up' then
- fields[sel.val+1].setActive(false)
- sel.val = (sel.val - 1)%table.getn(fields)
- fields[sel.val+1].setActive(true)
- if onScroll then onScroll() end
- renderFields(fields)
- else
- if fields[sel.val+1].update(key) and onChange then onChange() end
- -- fields[sel+1].render()
- renderFields(fields)
- end
- -- break
- end
- end
- end
- function renderFields(fields)
- for i=1,table.getn(fields) do
- fields[i].render()
- end
- end
- function createMenuClickListener(t)
- setmetatable(t,{__index={run_ptr={val=true},sel={val=0}}})
- local fields, run_ptr, sel =
- t[1] or t.fields,
- t[2] or t.run_ptr,
- t[3] or t.sel
- return function()
- while run_ptr.val do
- local event,btn,x,y = os.pullEvent('mouse_click')
- if btn == 1 then
- for i=1,table.getn(fields) do
- pos = fields[i].getCursurPos()
- for j=1,fields[i].getMaxLength() do
- if y == pos.y and x == pos.x + j-1 then
- fields[sel.val+1].setActive(false)
- sel.val = i-1
- fields[sel.val+1].setActive(true)
- fields[sel.val+1].update(28) -- Enter Key
- renderFields(fields)
- end
- end
- end
- end
- end
- end
- end
- function reset()
- term.clear()
- term.setCursorPos(1,1)
- term.setBackgroundColor(default.bg)
- term.setTextColor(default.fg)
- end
Add Comment
Please, Sign In to add comment