SHOW:
|
|
- or go back to the newest paste.
| 1 | -- Basic Functions -- | |
| 2 | ||
| 3 | local mSel = 1 | |
| 4 | local l = 1 | |
| 5 | local flag = false | |
| 6 | local rsType,rtData | |
| 7 | local w,h = term.getSize() | |
| 8 | ||
| 9 | local defaultColourSet = {
| |
| 10 | overlayHF = colours.grey; | |
| 11 | overlayBack = colours.black; | |
| 12 | overlayText = colours.white; | |
| 13 | menuItem = colours.white; | |
| 14 | msgBoxText = colours.white; | |
| 15 | msgBoxBack = colours.grey; | |
| 16 | inpBoxText = colours.white; | |
| 17 | inpBoxBack = colours.grey; | |
| 18 | inpBoxInput = colours.orange; | |
| 19 | } | |
| 20 | local defaultTextSet ={
| |
| 21 | overlayHeaderText = {"Menu"};
| |
| 22 | - | overlayFooterText = {"GUIAPI by KyleRGS"};
|
| 22 | + | overlayFooterText = {"Footer1"};
|
| 23 | itemName = "Default"; | |
| 24 | } | |
| 25 | ||
| 26 | function defineDefaultColour(overlayHF,overlayBack,overlayText,menuItem,msgBoxText,msgBoxBack,inpBoxText,inpBoxBack,inpBoxInput) | |
| 27 | defaultColourSet.overlayHF = overlayHF | |
| 28 | defaultColourSet.overlayBack = overlayBack | |
| 29 | defaultColourSet.overlayText = overlayText | |
| 30 | defaultColourSet.menuItem = menuItem | |
| 31 | defaultColourSet.msgBoxText = msgBoxText | |
| 32 | defaultColourSet.msgBoxBack = msgBoxBack | |
| 33 | defaultColourSet.inpBoxText = inpBoxText | |
| 34 | defaultColourSet.inpBoxBack = inpBoxBack | |
| 35 | defaultColourSet.inpBoxInput = inpBoxInput | |
| 36 | end | |
| 37 | function defineDefaultColourTable(tColours) | |
| 38 | defaultColourSet = tColours | |
| 39 | end | |
| 40 | function defineDefaultValues(HeaderText, FooterText, MenuItem) | |
| 41 | defaultTextSet.overlayHeaderText = HeaderText | |
| 42 | defaultTextSet.overlayFooterText = FooterText | |
| 43 | defaultTextSet.itemName = MenuItem | |
| 44 | end | |
| 45 | ||
| 46 | function isInteger(nIn) | |
| 47 | if math.floor(nIn) == nIn then return true | |
| 48 | else return false end | |
| 49 | end | |
| 50 | function printCentred(str , h) | |
| 51 | term.setCursorPos(w/2 - #str/2, h) | |
| 52 | term.write(str) | |
| 53 | end | |
| 54 | function printRight(str , h) | |
| 55 | term.setCursorPos(w - #str, h) | |
| 56 | term.write(str) | |
| 57 | end | |
| 58 | function printLeft(str , h) | |
| 59 | term.setCursorPos(1, h) | |
| 60 | term.write(str) | |
| 61 | end | |
| 62 | ||
| 63 | function reduceMenu(height, head, foot) | |
| 64 | return math.floor(math.floor(height - head - foot -1)/2) | |
| 65 | end | |
| 66 | ||
| 67 | --Presenting the Menu-- | |
| 68 | ||
| 69 | function drawMenu(tMenu) | |
| 70 | l = 0 | |
| 71 | if #tMenu.tItems > tMenu.iMenuLen then l = tMenu.iMenuLen | |
| 72 | else l = #tMenu.tItems end | |
| 73 | local j = 4 | |
| 74 | term.setBackgroundColour(tMenu.tOverlay.cOverlayBackground) | |
| 75 | for i = tMenu.iFItem, l+tMenu.iFItem-1 do | |
| 76 | if tMenu.tItems[i] then | |
| 77 | term.setTextColour(tMenu.tItems[i].nColour) | |
| 78 | printCentred(tMenu.tItems[i].sDisp, j) | |
| 79 | j = j +2 | |
| 80 | end | |
| 81 | end | |
| 82 | ||
| 83 | term.setTextColour(tMenu.tItems[tMenu.iCursorPos].nColour) | |
| 84 | local selpos = tMenu.iRSel*2 + 2 | |
| 85 | printCentred("[ "..tMenu.tItems[tMenu.iCursorPos].sDisp.." ]", selpos)
| |
| 86 | term.setBackgroundColour(tMenu.tOverlay.cOverlayColour) | |
| 87 | ||
| 88 | ||
| 89 | ||
| 90 | end | |
| 91 | ||
| 92 | function reDraw(tMenu) | |
| 93 | tMenu:draw() | |
| 94 | end | |
| 95 | ||
| 96 | -- Display functions -- | |
| 97 | ||
| 98 | ||
| 99 | function msgBox(tMsg) | |
| 100 | term.setTextColour(defaultColourSet.msgBoxText) | |
| 101 | term.setBackgroundColour(defaultColourSet.msgBoxBack) | |
| 102 | assert(type(tMsg)=="table", "Message Box must contain a table") | |
| 103 | local tStrLen = {}
| |
| 104 | for i,v in ipairs(tMsg) do | |
| 105 | tStrLen[i] = #v | |
| 106 | end | |
| 107 | local boxHeight = #tMsg | |
| 108 | local boxWidth = math.max(unpack(tStrLen)) +2 | |
| 109 | printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 -boxHeight/2))
| |
| 110 | for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2) do | |
| 111 | printCentred("I"..string.rep(" ",boxWidth).."I", i)
| |
| 112 | end | |
| 113 | printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 +boxHeight/2)+1)
| |
| 114 | printCentred("{[ENTER]}", math.floor(h/2 +boxHeight/2)+1)
| |
| 115 | local j = 1 | |
| 116 | for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2) do | |
| 117 | printCentred(tMsg[j], i) | |
| 118 | j = j+1 | |
| 119 | end | |
| 120 | do sleep(0.1); event, id, param2 = os.pullEvent() repeat until (event =="key" and id==28) or (event=="mouse_click") end | |
| 121 | end | |
| 122 | ||
| 123 | ||
| 124 | function inpBox(tMsg) | |
| 125 | term.setTextColour(defaultColourSet.inpBoxText) | |
| 126 | term.setBackgroundColour(defaultColourSet.inpBoxBack) | |
| 127 | assert(type(tMsg)=="table", "Message Box must contain a table") | |
| 128 | local tStrLen = {}
| |
| 129 | for i,v in ipairs(tMsg) do | |
| 130 | tStrLen[i] = #v | |
| 131 | end | |
| 132 | local boxHeight = #tMsg | |
| 133 | local boxWidth = math.max(unpack(tStrLen)) +2 | |
| 134 | printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 -boxHeight/2))
| |
| 135 | for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2)+1 do | |
| 136 | printCentred("I"..string.rep(" ",boxWidth).."I", i)
| |
| 137 | end | |
| 138 | printCentred("+"..string.rep("-",boxWidth).."+", math.floor(h/2 +boxHeight/2)+2)
| |
| 139 | printCentred("{[ENTER]}", math.floor(h/2 +boxHeight/2)+2)
| |
| 140 | local j = 1 | |
| 141 | for i = math.floor(h/2 -boxHeight/2)+1, math.floor(h/2 +boxHeight/2) do | |
| 142 | printCentred(tMsg[j], i) | |
| 143 | j = j+1 | |
| 144 | end | |
| 145 | term.setCursorPos((w-boxWidth)/2+1, (h+boxHeight)/2+1) | |
| 146 | term.setTextColour(defaultColourSet.inpBoxInput) | |
| 147 | return read() | |
| 148 | end | |
| 149 | ||
| 150 | -- The API functions -- | |
| 151 | ||
| 152 | function newOverlay() | |
| 153 | local tOverlay = {
| |
| 154 | nHeadLen = 2; | |
| 155 | nFootLen = 2; | |
| 156 | cOverlayColour = defaultColourSet.overlayText; | |
| 157 | cOverlayBackground = defaultColourSet.overlayBack; | |
| 158 | cOverlayHF = defaultColourSet.overlayHF; | |
| 159 | tTitle = defaultTextSet.overlayHeaderText; | |
| 160 | tFoot = defaultTextSet.overlayFooterText; | |
| 161 | setHeaderTitle = function(self,...) | |
| 162 | if arg.n < h then | |
| 163 | self.tTitle = {}
| |
| 164 | for i,v in ipairs(arg) do | |
| 165 | table.insert(self.tTitle, v) | |
| 166 | end | |
| 167 | self.nHeadLen = arg.n+1 | |
| 168 | else error("Too Many Lines to set Header Title") end
| |
| 169 | end; | |
| 170 | setFooterText = function(self,...) | |
| 171 | if arg.n < h then | |
| 172 | self.tFoot = {}
| |
| 173 | for i,v in ipairs(arg) do | |
| 174 | table.insert(self.tFoot, v) | |
| 175 | end | |
| 176 | self.nFootLen = arg.n +1 | |
| 177 | else error("Too Many Lines to set Footer text") end
| |
| 178 | end; | |
| 179 | setOverlayColour = function(self, colour) | |
| 180 | self.cOverlayColour = colour | |
| 181 | end; | |
| 182 | setBackgroundColour = function(self, colour) | |
| 183 | self.cOverlayBackground = colour | |
| 184 | end; | |
| 185 | setBackgroundHF = function(self, colour) | |
| 186 | self.cOverlayHF = colour | |
| 187 | end; | |
| 188 | draw = function(self) | |
| 189 | term.setBackgroundColour(self.cOverlayBackground) | |
| 190 | term.clear() | |
| 191 | term.setBackgroundColour(self.cOverlayHF) | |
| 192 | term.setTextColour(self.cOverlayColour) | |
| 193 | ||
| 194 | for i,v in ipairs(self.tTitle) do | |
| 195 | term.setCursorPos(1,i) | |
| 196 | term.clearLine() | |
| 197 | printCentred(v, i) | |
| 198 | end | |
| 199 | printLeft(string.rep("-",w),self.nHeadLen)
| |
| 200 | printLeft(string.rep("-",w),h-self.nFootLen+1)
| |
| 201 | for i,v in ipairs(self.tFoot) do | |
| 202 | term.setCursorPos(1,h-i+1) | |
| 203 | term.clearLine() | |
| 204 | printRight(v, h - i +1) | |
| 205 | end | |
| 206 | term.setTextColour(1) | |
| 207 | term.setBackgroundColour(colours.black) | |
| 208 | end; | |
| 209 | } | |
| 210 | return tOverlay | |
| 211 | end | |
| 212 | ||
| 213 | function newItem() | |
| 214 | local tItem = {
| |
| 215 | sDisp = defaultTextSet.itemName; | |
| 216 | sType = "text"; | |
| 217 | nColour = defaultColourSet.menuItem; | |
| 218 | tData = {};
| |
| 219 | newName = function(self, sName) | |
| 220 | self.sDisp = sName | |
| 221 | end; | |
| 222 | newType = function(self, sType) | |
| 223 | self.sType = sType | |
| 224 | end; | |
| 225 | newColour = function(self, nColour) | |
| 226 | self.nColour = nColour | |
| 227 | end; | |
| 228 | newColor = function(self, nColour) | |
| 229 | self.nColour = nColour | |
| 230 | end; | |
| 231 | setData = function(self, tData) | |
| 232 | self.tData = tData | |
| 233 | end; | |
| 234 | addData = function(self, aData) | |
| 235 | table.insert(self.tData, aData) | |
| 236 | end; | |
| 237 | } | |
| 238 | return tItem | |
| 239 | end | |
| 240 | ||
| 241 | function newMenu() | |
| 242 | local tMenu = {
| |
| 243 | tOverlay = newOverlay(); | |
| 244 | tItems = {};
| |
| 245 | iCursorPos = 1; | |
| 246 | iFItem = 1; | |
| 247 | iRSel = 1; | |
| 248 | iMenuLen = 0; | |
| 249 | newOverlay = function(self, tOver) | |
| 250 | self.tOverlay = tOver | |
| 251 | end; | |
| 252 | resetItems = function(self) self.tItems = {} end;
| |
| 253 | newItem = function(self, tItem) table.insert(self.tItems, tItem) end; | |
| 254 | checkSel = function(self) | |
| 255 | if self.iRSel > self.iMenuLen -1 and self.iCursorPos<#self.tItems then | |
| 256 | self.iRSel = self.iRSel -1 | |
| 257 | self.iFItem = self.iFItem+1 | |
| 258 | elseif self.iRSel == 1 and self.iCursorPos > 1 then | |
| 259 | self.iRSel = self.iRSel +1 | |
| 260 | self.iFItem = self.iFItem -1 | |
| 261 | end | |
| 262 | end; | |
| 263 | draw = function(self) | |
| 264 | local id | |
| 265 | local key | |
| 266 | local param2 | |
| 267 | local param3 | |
| 268 | flag = false | |
| 269 | while true do | |
| 270 | if not flag then | |
| 271 | self.tOverlay:draw() | |
| 272 | drawMenu(self) | |
| 273 | do sleep(0.02); id, key, param2, param3 = os.pullEvent(); repeat until id == "key" or id =="mouse_click" or id == "mouse_scroll" end | |
| 274 | if id == "key" then | |
| 275 | if key == 200 and self.iCursorPos > 1 then self.iCursorPos = self.iCursorPos-1 ; self.iRSel=self.iRSel-1 ; self:checkSel(); | |
| 276 | elseif key == 208 and self.iCursorPos < #self.tItems then self.iCursorPos = self.iCursorPos + 1 ; self.iRSel = self.iRSel +1 ; self:checkSel(); | |
| 277 | elseif key == 28 then | |
| 278 | if self.tItems[self.iCursorPos].sType == "menu" then reDraw(self.tItems[self.iCursorPos].tData[1]) | |
| 279 | elseif self.tItems[self.iCursorPos].sType == "text" then | |
| 280 | elseif self.tItems[self.iCursorPos].sType == "msgbox" then msgBox(self.tItems[self.iCursorPos].tData); | |
| 281 | else rsType,rtData = self.tItems[self.iCursorPos].sType,self.tItems[self.iCursorPos].tData flag = true; break end | |
| 282 | end | |
| 283 | elseif id =="mouse_click" then | |
| 284 | local mSel = param3/2 -1 | |
| 285 | if isInteger(mSel) and key == 1 and mSel <= l then self.iCursorPos = mSel+self.iFItem -1; self.iRSel=mSel; | |
| 286 | if self.tItems[self.iCursorPos].sType == "menu" then reDraw(self.tItems[self.iCursorPos].tData[1]) | |
| 287 | elseif self.tItems[self.iCursorPos].sType == "text" then | |
| 288 | elseif self.tItems[self.iCursorPos].sType == "msgbox" then msgBox(self.tItems[self.iCursorPos].tData); | |
| 289 | else rsType,rtData = self.tItems[self.iCursorPos].sType,self.tItems[self.iCursorPos].tData flag = true; break end | |
| 290 | end | |
| 291 | elseif id =="mouse_scroll" then | |
| 292 | if key == -1 and self.iFItem > 1 then self.iFItem = self.iFItem-1 ; self.iCursorPos=self.iCursorPos-1 ; self:checkSel() ; | |
| 293 | elseif key == -1 and self.iFItem == 1 and self.iCursorPos > 1 then self.iCursorPos = self.iCursorPos - 1 ; self.iRSel = self.iRSel -1 ; | |
| 294 | elseif key == 1 and self.iFItem +l -1< #self.tItems then self.iCursorPos = self.iCursorPos + 1 ; self.iFItem = self.iFItem+1 ; self:checkSel() ; | |
| 295 | 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; | |
| 296 | end | |
| 297 | end | |
| 298 | else break end | |
| 299 | end | |
| 300 | return rsType, rtData | |
| 301 | end; | |
| 302 | } | |
| 303 | tMenu.iMenuLen = reduceMenu(h, tMenu.tOverlay.nHeadLen, tMenu.tOverlay.nFootLen) | |
| 304 | return tMenu | |
| 305 | end |