xkonti

monitorXAPI v. 0.2.4

Mar 19th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. -----------------
  2. -- monitorXAPI --
  3. --  by Xkonti  --
  4. -----------------
  5.  
  6. ---- External APIs ----
  7. -- You have to include:
  8. --  - textXAPI
  9. -----------------------
  10.  
  11. -------------------
  12. -- GLOBAL VARIABLES
  13.  
  14. local XApiMonitor   -- peripheral
  15. local XApiBkgColor  -- default color for screen cleaning
  16.  
  17. --
  18. -------------------
  19.  
  20.  
  21.  
  22. --
  23. ------ Utilities ------
  24. --
  25.  
  26. function monitorSetup(side, textScale, backgroundColor)
  27.  
  28.     XApiMonitor = peripheral.wrap(side)
  29.     XApiMonitor.setTextScale(textScale)
  30.     XApiBkgColor = backgroundColor
  31.     clear(XApiBkgColor)
  32.    
  33. end
  34.  
  35. -- Clear screen
  36. function clear(color)
  37.  
  38.     XApiMonitor.setBackgroundColor(color)
  39.     XApiMonitor.clear()
  40.    
  41. end
  42.  
  43.  
  44.  
  45. --
  46. ------ Text ------
  47. --
  48.  
  49. -- Draw text
  50. function drawText(x, y, text, bkgColor, txtColor)
  51.    
  52.     XApiMonitor.setBackgroundColor(bkgColor)
  53.     XApiMonitor.setTextColor(txtColor)
  54.     XApiMonitor.setCursorPos(x, y)
  55.    
  56.     XApiMonitor.write(text)
  57.  
  58. end
  59.  
  60. -- Draw centered text
  61. function drawCenteredText(x, y, text, length, backgroundColor, textColor)
  62.  
  63.     drawText(x + textXAPI.offsetToCenter(text, length), y, text, backgroundColor, textColor)
  64.  
  65. end
  66.  
  67. -- Draw right-aligned text
  68. function drawRightAlignedText(x, y, text, length, backgroundColor, textColor)
  69.  
  70.     drawText(x + textXAPI.offsetToRight(text, length), y, text, backgroundColor, textColor)
  71.  
  72. end
  73.  
  74.  
  75.  
  76. --
  77. ------ Shapes ------
  78. --
  79.  
  80. -- Colored Filled Rectangle with Stroke
  81. function drawCFRectangle(x1, y1, x2, y2, strokeColor, fillColor)
  82.    
  83.     XApiMonitor.setBackgroundColor(strokeColor)
  84.    
  85.     local x, y
  86.    
  87.     -- Top line
  88.     XApiMonitor.setCursorPos(x1, y1)
  89.     for x=x1, x2, 1 do
  90.        
  91.         XApiMonitor.write(" ")
  92.            
  93.     end
  94.    
  95.     -- Bottom line
  96.     XApiMonitor.setCursorPos(x1, y2)
  97.     for x=x1, x2, 1 do
  98.        
  99.         XApiMonitor.write(" ")
  100.            
  101.     end
  102.    
  103.     -- Left line
  104.     for y=y1+1, y2-1, 1 do
  105.        
  106.         XApiMonitor.setCursorPos(x1, y)
  107.         XApiMonitor.write(" ")
  108.            
  109.     end
  110.    
  111.     -- Right line
  112.     for y=y1+1, y2-1, 1 do
  113.        
  114.         XApiMonitor.setCursorPos(x2, y)
  115.         XApiMonitor.write(" ")
  116.            
  117.     end
  118.    
  119.     drawCSRectangle(x1+1, y1+1, x2-1, y2-1, fillColor)
  120.    
  121. end
  122.  
  123. -- Colored Solid Rectangle
  124. function drawCSRectangle(x1, y1, x2, y2, color)
  125.    
  126.     XApiMonitor.setBackgroundColor(color)
  127.    
  128.     local x, y
  129.     for y=y1, y2, 1 do
  130.        
  131.         XApiMonitor.setCursorPos(x1, y)
  132.        
  133.         for x=x1, x2, 1 do
  134.        
  135.             XApiMonitor.write(" ")
  136.            
  137.         end
  138.        
  139.     end
  140.  
  141. end
  142.  
  143. -- Draw edit box
  144. function drawEditBox(x, y, size, length, text, strokeColor, fillColor, textColor)
  145.  
  146.     drawCFRectangle(x, y, x+length+(2*size), y+(2*size), strokeColor, fillColor)
  147.     drawText(x+size, y+size, text, fillColor, textColor)
  148.    
  149. end
  150.  
  151. -- Draw button
  152. function drawButton(x, y, size, length, text, fillColor, textColor)
  153.  
  154.     drawCSRectangle(x, y, x+length+(2*size), y+(2*size), fillColor)
  155.     drawCenteredText(x+size, y+size, text, length, fillColor, textColor)
  156.      
  157. end
  158.    
  159. -- Draw statusbar
  160. function drawStatusBar(pathLength, timeLength, path, pathFillColor, pathTextColor, timeFillColor, timeTextColor)
  161.  
  162.     drawCSRectangle(1, 1, pathLength, 1, pathFillColor)
  163.     drawCSRectangle(pathLength + 1, 1, pathLength + timeLength, 1, timeFillColor)
  164.    
  165.     drawText(1,1, path, pathFillColor, pathTextColor)
  166.     timeHour = textutils.formatTime(os.time(), true)
  167.     timeDay = tostring(os.day())
  168.     drawRightAlignedText(pathLength + 1, 1, timeHour.." of "..timeDay.." MC day", timeLength, timeFillColor, timeTextColor)
  169.    
  170. end
  171.  
  172.  
  173.  
  174. --
  175. ------ Classes ------
  176. --
  177.  
  178.  
  179. -----------------------
  180. --- StatusBar class ---
  181.  
  182. -- StatusBar declaration
  183. StatusBar = {   pathLength = 54,
  184.                 timeLength = 25,
  185.                 path = "path",
  186.                 pathFillColor = colors.gray,
  187.                 pathTextColor = colors.white,
  188.                 timeFillColor = colors.lightGray,
  189.                 timeTextColor = colors.black
  190.             }
  191.  
  192. -- Constructor
  193. function StatusBar:new(object)
  194.  
  195.     object = object or {    pathLength = 54,
  196.                             timeLength = 25,
  197.                             path = "path",
  198.                             pathFillColor = colors.gray,
  199.                             pathTextColor = colors.white,
  200.                             timeFillColor = colors.lightGray,
  201.                             timeTextColor = colors.black
  202.                         }
  203.  
  204.     setmetatable(object, self)
  205.     self.__index = self
  206.     return object
  207. end
  208.  
  209. -- StatusBar draw function
  210. function StatusBar:draw()
  211.     drawStatusBar(self.pathLength, self.timeLength, self.path, self.pathFillColor, self.pathTextColor, self.timeFillColor, self.timeTextColor)
  212. end
  213.  
  214. -- StatusBar touch function
  215. function StatusBar:touch()
  216. end
  217.  
  218.  
  219. --------------------
  220. --- Button class ---
  221.  
  222. -- Button declaration
  223. Button = {  x = 1,
  224.             y = 1,
  225.             size = 1,
  226.             length = 5,
  227.             text = "text",
  228.             enabled = true,
  229.             visible = true,
  230.             enabledFillColor = colors.lightGray,
  231.             enabledTextColor = colors.black,
  232.             disabledFillColor = colors.gray,
  233.             disabledTextColor = colors.black,
  234.             onClickFunction = function() end
  235.          }
  236.  
  237. -- Constructor
  238. function Button:new(object)
  239.  
  240.     object = object or {    x = 1,
  241.                             y = 1,
  242.                             size = 1,
  243.                             length = 5,
  244.                             text = "text",
  245.                             enabled = true,
  246.                             visible = true,
  247.                             enabledFillColor = colors.black,
  248.                             enabledTextColor = colors.lightGray,
  249.                             disabledFillColor = colors.black,
  250.                             disabledTextColor = colors.gray,
  251.                             onClickFunction = function() end
  252.                        }
  253.     setmetatable(object, self)
  254.     self.__index = self
  255.     return object
  256. end
  257.  
  258. -- Button draw function
  259. function Button:draw()
  260.    
  261.     if self.visible then
  262.    
  263.         if self.enabled then
  264.             drawButton(self.x, self.y, self.size, self.length, self.text, self.enabledFillColor, self.enabledTextColor)
  265.         else
  266.             drawButton(self.x, self.y, self.size, self.length, self.text, self.disabledFillColor, self.disabledTextColor)
  267.         end
  268.     end
  269. end
  270.  
  271. -- When clicked function - checks if button was clicked and starts onClickFunction - returns true or false
  272. function Button:touch(x, y)
  273.  
  274.     if self.enabled then
  275.    
  276.         if x >= self.x then
  277.             if x <= (self.x + self.length + (2 * self.size)) then
  278.            
  279.                 if y >= self.y then
  280.                     if y <= (self.y + (2 * self.size)) then
  281.                    
  282.                         self.onClickFunction()
  283.                         return true
  284.                        
  285.                     else
  286.                     return false
  287.                     end
  288.                 else
  289.                 return false
  290.                 end
  291.             else
  292.             return false
  293.             end
  294.         else
  295.         return false
  296.         end
  297.     else
  298.     return false
  299.     end
  300. end
  301.  
  302. -- Set Button position
  303. function Button:setPos(x, y)
  304.     self.x = x
  305.     self.y = y
  306. end
  307.  
  308.  
  309.  
  310. --
  311. ------ UI functions ------
  312. --
  313.  
  314. function uiDraw(components)
  315.  
  316. print("--DRAWING--")    -- debug
  317.    
  318.     for i=1, table.maxn(components) do
  319.         components[i]:draw()
  320.     end
  321. end
  322.  
  323. -- Event handling
  324. function uiEvents(components, refreshRate)
  325.    
  326.     os.startTimer(refreshRate)      -- Start refreshing screen
  327.    
  328.     while true do
  329.    
  330.         local event, param1, param2, param3 = os.pullEvent()        -- event handler
  331.        
  332.         if event == "timer" then            -- time to refresh?
  333.        
  334.             os.startTimer(refreshRate)      -- start new timer
  335.             uiDraw(components)              -- refresh UI
  336.        
  337.         elseif event == "monitor_touch" then    -- touched?
  338.        
  339.             uiTouch(components, param2, param3) -- check what was touched
  340.            
  341.         end
  342.        
  343.     end
  344.    
  345. end
  346.  
  347. -- Check what was clicked
  348. function uiTouch(components, x, y)
  349.    
  350.     print("--TOUCHING--")   -- debug
  351.    
  352.     for i=1, table.maxn(components) do
  353.    
  354.         if components[i]:touch(x, y) then
  355.             break
  356.         end
  357.     end
  358. end
Add Comment
Please, Sign In to add comment