Advertisement
Pinkishu

gui_main

Jan 20th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1. guis["main"] = {events = {}}
  2.  
  3. function guis.main:init()
  4.     term.clear()
  5.     guiTools.printBorder(colors.gray)
  6.     local w,h = term.getSize()
  7.     guiTools.drawRect(1,3,w,1,colors.gray)
  8.     guiTools.drawRect(30,3,1,h-3,colors.gray)
  9.     guiTools.drawRect(30,12,w-30,1,colors.gray)
  10.     self.orderCount = 0
  11.     self.searchList = {}
  12.     term.setCursorPos(2,4)
  13.     term.setTextColor(colors.white)
  14.     term.setBackgroundColor(colors.black)
  15.     term.setCursorPos(31,4)
  16.     term.write(" +     Stacks     - ")
  17.     term.setCursorPos(31,5)
  18.     term.write(" +  Half Stacks   - ")
  19.     term.setCursorPos(31,6)
  20.     term.write(" +     Single     - ")
  21.  
  22.     term.setCursorPos(45,8)
  23.     term.write("Stacks")
  24.     term.setCursorPos(45,9)
  25.     term.write("Items")
  26.  
  27.     term.setCursorPos(44,7)
  28.     term.setBackgroundColor(colors.red)
  29.     term.setTextColor(colors.white)
  30.     term.write("<Reset>")
  31.     term.setCursorPos(37,11)
  32.     term.setBackgroundColor(colors.lime)
  33.     term.setTextColor(colors.black)
  34.     term.write("<Order>")
  35.     term.setTextColor(colors.white)
  36.     term.setBackgroundColor(colors.gray)
  37.     term.setCursorPos(2,1)
  38.     term.write("/                    Search                     \\")
  39.     term.setCursorPos(2,3)
  40.     term.write("/         Results          \\")
  41.     term.setCursorPos(31,3)
  42.     term.write("/      Order       \\")
  43.     term.setCursorPos(31,12)
  44.     term.write("/  Active Orders   \\")
  45.     self.targetIndex = -1
  46.     self.searchStr = ""
  47.     self:fixPos()
  48.     self:updateOrderCount()
  49.     self:updateRetrievalList()
  50. end
  51.  
  52. function guis.main:fixPos()
  53.     term.setCursorPos(2+string.len(self.searchStr),2)
  54.     term.setCursorBlink(true)
  55. end
  56.  
  57. function guis.main:updateList()
  58.  
  59.     local w,h = term.getSize()
  60.     guiTools.clearArea(2,4,30-2,h-4)
  61.    -- local y=4
  62.     for k,v in pairs(self.searchList) do
  63.         term.setCursorPos(2,k+3)
  64.         term.write(v)
  65.         if self.targetIndex == k then
  66.             term.setCursorPos(29,k+3)
  67.             term.setBackgroundColor(colors.lime)
  68.             term.write(" ")
  69.             term.setBackgroundColor(colors.black)
  70.         end
  71.        
  72.     end
  73.     self:fixPos()
  74. end
  75.  
  76. function guis.main:updateRetrievalList()
  77.     local w,h = term.getSize()
  78.     term.setCursorPos(31,13)
  79.     guiTools.clearArea(31,13,w-31,h-13)
  80.     term.setTextColor(colors.white)
  81.     term.setBackgroundColor(colors.black)
  82.     term.setCursorPos(31,13)
  83.        
  84.     for dI = 1,math.min(3,#retrievalQueue),1 do
  85.         local dY = (dI-1)*2
  86.         term.setCursorPos(31,13+dY)
  87.         term.write(string.sub(retrievalQueue[dI].name,1,w-31))
  88.         term.setCursorPos(31,13+dY+1)
  89.         local stacks = math.floor( retrievalQueue[dI].count / 64 )
  90.         local items = retrievalQueue[dI].count - stacks*64
  91.         if stacks > 0 then
  92.             term.write(string.format("%d*64+%d",stacks,items))
  93.         else
  94.             term.write(string.format("%d",items))
  95.         end
  96.         term.write()
  97.     end
  98.     self:fixPos()
  99. end
  100.  
  101. function guis.main:doSearch()
  102.     self.targetIndex = -1
  103.     self.searchList = findEntries(self.searchStr)
  104.     self:updateList()
  105. end
  106.  
  107. function guis.main.events:char(charIn)
  108.     self:fixPos()
  109.     term.setTextColor(colors.white)
  110.     term.setBackgroundColor(colors.black)
  111.     term.write(charIn)
  112.     self.searchStr = self.searchStr .. charIn
  113.     self:fixPos()
  114.     self:startSearchTimer()
  115. end
  116.  
  117. function guis.main:startSearchTimer()
  118.     self.searchTimer = os.startTimer(1)
  119. end
  120.  
  121.  
  122. function guis.main.events:key(keyIn)
  123.     if keyIn == keys.backspace then
  124.         if string.len(self.searchStr) > 0 then
  125.             self:fixPos()
  126.             local x = term.getCursorPos()
  127.             term.setCursorPos(x-1,2)
  128.             term.setBackgroundColor(colors.black)
  129.             term.write(" ")
  130.             self.searchStr = string.sub(self.searchStr,0,string.len(self.searchStr)-1)
  131.             self:fixPos()
  132.             self:startSearchTimer()
  133.         end
  134.  
  135.     elseif keyIn == keys.f4 then
  136.         error("")
  137.     end
  138. end
  139.  
  140. function guis.main.events:timer(timerKey)
  141.     if self.searchTimer == timerKey then
  142.         self:doSearch()
  143.     end
  144. end
  145.  
  146. function guis.main:updateOrderCount()
  147.     local w,h = term.getSize()
  148.     term.setCursorPos(31,8)
  149.     term.setTextColor(colors.white)
  150.     term.setBackgroundColor(colors.black)
  151.     local stacks = math.floor(self.orderCount/64)
  152.     term.write(guiTools.padString(string.format("%d",stacks),w-31-6).."Stacks")
  153.     term.setCursorPos(31,9)
  154.     local items = self.orderCount-stacks*64
  155.     term.write(guiTools.padString(string.format("%d",items),w-31-6).."Items ")
  156.     self:fixPos()
  157. end
  158.  
  159. function guis.main:handleCountClick(dX,dY)
  160.     local vMod = 0
  161.     if dX == 18 then vMod = -1 end
  162.     if dX == 1 then vMod = 1 end
  163.     if vMod == 0 then return end
  164.  
  165.     local mods = {64,32,1}
  166.     local mod = mods[dY+1]*vMod
  167.  
  168.     self.orderCount = math.max(self.orderCount+mod,0)
  169.     self:updateOrderCount()
  170.  
  171. end
  172.  
  173. function guis.main.events:mouse_click(button,clickX,clickY)
  174.     local w,h = term.getSize()
  175.     if button == 1 then
  176.         if clickX >= 2 and clickX <= 29 and clickY >= 4 and clickY <= h-1 then
  177.             local index = clickY - 3
  178.             if self.searchList[index] ~= nil then
  179.                 self.targetIndex = index
  180.                 self:updateList()
  181.             end
  182.         elseif clickX >= 31 and clickX <= w-1 and clickY >= 4 and clickY <= 11 then
  183.             if clickY >= 4 and clickY <= 6 then
  184.                 self:handleCountClick(clickX-31,clickY-4)
  185.             elseif clickY == 7 and clickX > w-1-7 and clickX <= w-1 then
  186.                 self.orderCount = 0
  187.                 self:updateOrderCount()
  188.             elseif clickY == 11 and clickX >= 37 and clickX <= w-8 and self.targetIndex ~= -1 and self.orderCount > 0 then
  189.                 addRetrievalRequest(self.searchList[self.targetIndex],self.orderCount)
  190.             end
  191.         end
  192.     end
  193. end
  194.  
  195. function guis.main.events:retrieval_update()
  196.     self:updateRetrievalList()
  197. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement