spenk

CCGUI

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