Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Basic Functions --
- local mSel = 1
- local l = 1
- local flag = false
- local rsType,rtData
- local w,h = term.getSize()
- local defaultColourSet = {
- overlayHF = colours.grey;
- overlayBack = colours.black;
- overlayText = colours.white;
- menuItem = colours.white;
- msgBoxText = colours.white;
- msgBoxBack = colours.grey;
- inpBoxText = colours.white;
- inpBoxBack = colours.grey;
- inpBoxInput = colours.orange;
- }
- local defaultTextSet ={
- overlayHeaderText = {"Menu"};
- overlayFooterText = {"GUIAPI by KyleRGS"};
- itemName = "Default";
- }
- function defineDefaultColour(overlayHF,overlayBack,overlayText,menuItem,msgBoxText,msgBoxBack,inpBoxText,inpBoxBack,inpBoxInput)
- defaultColourSet.overlayHF = overlayHF
- defaultColourSet.overlayBack = overlayBack
- defaultColourSet.overlayText = overlayText
- defaultColourSet.menuItem = menuItem
- defaultColourSet.msgBoxText = msgBoxText
- defaultColourSet.msgBoxBack = msgBoxBack
- defaultColourSet.inpBoxText = inpBoxText
- defaultColourSet.inpBoxBack = inpBoxBack
- defaultColourSet.inpBoxInput = inpBoxInput
- end
- function defineDefaultColourTable(tColours)
- defaultColourSet = tColours
- end
- function defineDefaultValues(HeaderText, FooterText, MenuItem)
- defaultTextSet.overlayHeaderText = HeaderText
- defaultTextSet.overlayFooterText = FooterText
- defaultTextSet.itemName = MenuItem
- end
- function isInteger(nIn)
- if math.floor(nIn) == nIn then return true
- else return false end
- end
- function printCentred(str , h)
- term.setCursorPos(w/2 - #str/2, h)
- term.write(str)
- end
- function printRight(str , h)
- term.setCursorPos(w - #str, h)
- term.write(str)
- end
- function printLeft(str , h)
- term.setCursorPos(1, h)
- term.write(str)
- end
- function reduceMenu(height, head, foot)
- return math.floor(math.floor(height - head - foot -1)/2)
- end
- --Presenting the Menu--
- function drawMenu(tMenu)
- l = 0
- if #tMenu.tItems > tMenu.iMenuLen then l = tMenu.iMenuLen
- else l = #tMenu.tItems end
- local j = 4
- term.setBackgroundColour(tMenu.tOverlay.cOverlayBackground)
- for i = tMenu.iFItem, l+tMenu.iFItem-1 do
- if tMenu.tItems[i] then
- term.setTextColour(tMenu.tItems[i].nColour)
- printCentred(tMenu.tItems[i].sDisp, j)
- j = j +2
- end
- end
- term.setTextColour(tMenu.tItems[tMenu.iCursorPos].nColour)
- local selpos = tMenu.iRSel*2 + 2
- printCentred("[ "..tMenu.tItems[tMenu.iCursorPos].sDisp.." ]", selpos)
- term.setBackgroundColour(tMenu.tOverlay.cOverlayColour)
- end
- function reDraw(tMenu)
- tMenu:draw()
- end
- -- Display functions --
- function msgBox(tMsg)
- term.setTextColour(defaultColourSet.msgBoxText)
- term.setBackgroundColour(defaultColourSet.msgBoxBack)
- assert(type(tMsg)=="table", "Message Box must contain a table")
- local tStrLen = {}
- for i,v in ipairs(tMsg) do
- tStrLen[i] = #v
- end
- local boxHeight = #tMsg
- local boxWidth = math.max(unpack(tStrLen)) +2
- printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 -boxHeight/2))
- for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2) do
- printCentred("I"..string.rep(" ",boxWidth).."I", i)
- end
- printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 +boxHeight/2)+1)
- printCentred("{[ENTER]}", math.floor(h/2 +boxHeight/2)+1)
- local j = 1
- for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2) do
- printCentred(tMsg[j], i)
- j = j+1
- end
- do sleep(0.1); event, id, param2 = os.pullEvent() repeat until (event =="key" and id==28) or (event=="mouse_click") end
- end
- function inpBox(tMsg)
- term.setTextColour(defaultColourSet.inpBoxText)
- term.setBackgroundColour(defaultColourSet.inpBoxBack)
- assert(type(tMsg)=="table", "Message Box must contain a table")
- local tStrLen = {}
- for i,v in ipairs(tMsg) do
- tStrLen[i] = #v
- end
- local boxHeight = #tMsg
- local boxWidth = math.max(unpack(tStrLen)) +2
- printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 -boxHeight/2))
- for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2)+1 do
- printCentred("I"..string.rep(" ",boxWidth).."I", i)
- end
- printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 +boxHeight/2)+2)
- printCentred("{[ENTER]}", math.floor(h/2 +boxHeight/2)+2)
- local j = 1
- for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2) do
- printCentred(tMsg[j], i)
- j = j+1
- end
- term.setCursorPos((w-boxWidth)/2+1, (h+boxHeight)/2+1)
- term.setTextColour(defaultColourSet.inpBoxInput)
- return read()
- end
- -- The API functions --
- function newOverlay()
- local tOverlay = {
- nHeadLen = 2;
- nFootLen = 2;
- cOverlayColour = defaultColourSet.overlayText;
- cOverlayBackground = defaultColourSet.overlayBack;
- cOverlayHF = defaultColourSet.overlayHF;
- tTitle = defaultTextSet.overlayHeaderText;
- tFoot = defaultTextSet.overlayFooterText;
- setHeaderTitle = function(self,...)
- if arg.n < h then
- self.tTitle = {}
- for i,v in ipairs(arg) do
- table.insert(self.tTitle, v)
- end
- self.nHeadLen = arg.n+1
- else error("Too Many Lines to set Header Title") end
- end;
- setFooterText = function(self,...)
- if arg.n < h then
- self.tFoot = {}
- for i,v in ipairs(arg) do
- table.insert(self.tFoot, v)
- end
- self.nFootLen = arg.n +1
- else error("Too Many Lines to set Footer text") end
- end;
- setOverlayColour = function(self, colour)
- self.cOverlayColour = colour
- end;
- setBackgroundColour = function(self, colour)
- self.cOverlayBackground = colour
- end;
- setBackgroundHF = function(self, colour)
- self.cOverlayHF = colour
- end;
- draw = function(self)
- term.setBackgroundColour(self.cOverlayBackground)
- term.clear()
- term.setBackgroundColour(self.cOverlayHF)
- term.setTextColour(self.cOverlayColour)
- for i,v in ipairs(self.tTitle) do
- term.setCursorPos(1,i)
- term.clearLine()
- printCentred(v, i)
- end
- printLeft(string.rep("-",w),self.nHeadLen)
- printLeft(string.rep("-",w),h-self.nFootLen+1)
- for i,v in ipairs(self.tFoot) do
- term.setCursorPos(1,h-i+1)
- term.clearLine()
- printRight(v, h - i +1)
- end
- term.setTextColour(1)
- term.setBackgroundColour(colours.black)
- end;
- }
- return tOverlay
- end
- function newItem()
- local tItem = {
- sDisp = defaultTextSet.itemName;
- sType = "text";
- nColour = defaultColourSet.menuItem;
- tData = {};
- newName = function(self, sName)
- self.sDisp = sName
- end;
- newType = function(self, sType)
- self.sType = sType
- end;
- newColour = function(self, nColour)
- self.nColour = nColour
- end;
- newColor = function(self, nColour)
- self.nColour = nColour
- end;
- setData = function(self, tData)
- self.tData = tData
- end;
- addData = function(self, aData)
- table.insert(self.tData, aData)
- end;
- }
- return tItem
- end
- function newMenu()
- local tMenu = {
- tOverlay = newOverlay();
- tItems = {};
- iCursorPos = 1;
- iFItem = 1;
- iRSel = 1;
- iMenuLen = 0;
- newOverlay = function(self, tOver)
- self.tOverlay = tOver
- end;
- resetItems = function(self) self.tItems = {} end;
- newItem = function(self, tItem) table.insert(self.tItems, tItem) end;
- checkSel = function(self)
- if self.iRSel > self.iMenuLen -1 and self.iCursorPos<#self.tItems then
- self.iRSel = self.iRSel -1
- self.iFItem = self.iFItem+1
- elseif self.iRSel == 1 and self.iCursorPos > 1 then
- self.iRSel = self.iRSel +1
- self.iFItem = self.iFItem -1
- end
- end;
- draw = function(self)
- local id
- local key
- local param2
- local param3
- flag = false
- while true do
- if not flag then
- self.tOverlay:draw()
- drawMenu(self)
- do sleep(0.02); id, key, param2, param3 = os.pullEvent(); repeat until id == "key" or id =="mouse_click" or id == "mouse_scroll" end
- if id == "key" then
- if key == 200 and self.iCursorPos > 1 then self.iCursorPos = self.iCursorPos-1 ; self.iRSel=self.iRSel-1 ; self:checkSel();
- elseif key == 208 and self.iCursorPos < #self.tItems then self.iCursorPos = self.iCursorPos + 1 ; self.iRSel = self.iRSel +1 ; self:checkSel();
- elseif key == 28 then
- if self.tItems[self.iCursorPos].sType == "menu" then reDraw(self.tItems[self.iCursorPos].tData[1])
- elseif self.tItems[self.iCursorPos].sType == "text" then
- elseif self.tItems[self.iCursorPos].sType == "msgbox" then msgBox(self.tItems[self.iCursorPos].tData);
- else rsType,rtData = self.tItems[self.iCursorPos].sType,self.tItems[self.iCursorPos].tData flag = true; break end
- end
- elseif id =="mouse_click" then
- local mSel = param3/2 -1
- if isInteger(mSel) and key == 1 and mSel <= l then self.iCursorPos = mSel+self.iFItem -1; self.iRSel=mSel;
- if self.tItems[self.iCursorPos].sType == "menu" then reDraw(self.tItems[self.iCursorPos].tData[1])
- elseif self.tItems[self.iCursorPos].sType == "text" then
- elseif self.tItems[self.iCursorPos].sType == "msgbox" then msgBox(self.tItems[self.iCursorPos].tData);
- else rsType,rtData = self.tItems[self.iCursorPos].sType,self.tItems[self.iCursorPos].tData flag = true; break end
- end
- elseif id =="mouse_scroll" then
- if key == -1 and self.iFItem > 1 then self.iFItem = self.iFItem-1 ; self.iCursorPos=self.iCursorPos-1 ; self:checkSel() ;
- elseif key == -1 and self.iFItem == 1 and self.iCursorPos > 1 then self.iCursorPos = self.iCursorPos - 1 ; self.iRSel = self.iRSel -1 ;
- elseif key == 1 and self.iFItem +l -1< #self.tItems then self.iCursorPos = self.iCursorPos + 1 ; self.iFItem = self.iFItem+1 ; self:checkSel() ;
- elseif key == 1 and self.iFItem +l-1 >= #self.tItems and self.iCursorPos < #self.tItems then self.iCursorPos = self.iCursorPos + 1 ; self.iRSel = self.iRSel +1;
- end
- end
- else break end
- end
- return rsType, rtData
- end;
- }
- tMenu.iMenuLen = reduceMenu(h, tMenu.tOverlay.nHeadLen, tMenu.tOverlay.nFootLen)
- return tMenu
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement