Advertisement
HandieAndy

Graphix API

Jan 16th, 2018
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | None | 0 0
  1. --2D Graphics API for Computercraft Computers
  2. --Author: Andrew Lalis, andrewlalisofficial@gmail.com
  3.  
  4. --Current peripheral to draw to
  5. local m
  6. --Determines if the peripheral is a monitor or computer screen.
  7. local isMonitor
  8. local buttonLoopActive = false
  9.  
  10. --Holds all registered buttons.
  11. local buttons = {}
  12.  
  13. --Clears the screen.
  14. function clear(bc)
  15.     m.setBackgroundColor(bc)
  16.     m.clear()
  17. end
  18.  
  19. --Sets the current peripheral.
  20. function setTarget(target,isMon)
  21.     m = target
  22.     isMonitor = isMon
  23. end
  24.  
  25. --Wraps text
  26. function wrap(str, limit)
  27.   limit = limit or 72
  28.   local here = 1
  29.   local buf = ""
  30.   local t = {}
  31.   str:gsub("(%s*)()(%S+)()",
  32.   function(sp, st, word, fi)
  33.         if fi-here > limit then
  34.            --# Break the line
  35.            here = st
  36.            table.insert(t, buf)
  37.            buf = word
  38.         else
  39.            buf = buf..sp..word  --# Append
  40.         end
  41.   end)
  42.   --# Tack on any leftovers
  43.   if(buf ~= "") then
  44.         table.insert(t, buf)
  45.   end
  46.   return t
  47. end
  48.  
  49. --Fills a box with a color
  50. function fillBox(x,y,width,height,bc)
  51.     m.setBackgroundColor(bc)
  52.     for i=x,width+x-1 do
  53.         for k=y,height+y-1 do
  54.             m.setCursorPos(i,k)
  55.             m.write(" ")
  56.         end
  57.     end
  58. end
  59.  
  60. --Draws a horizontal line.
  61. function drawLineH(x1,width,y,bc)
  62.     m.setBackgroundColor(bc)
  63.     for x=x1,width+x1-1 do
  64.         m.setCursorPos(x,y)
  65.         m.write(" ")
  66.     end
  67. end
  68.  
  69. --Draws a vertical line.
  70. function drawLineV(x,y1,height,bc)
  71.     m.setBackgroundColor(bc)
  72.     for y=y1,height+y1-1 do
  73.         m.setCursorPos(x,y)
  74.         m.write(" ")
  75.     end
  76. end
  77.  
  78. --Draws border box.
  79. function drawBorder(x1,y1,width,height,bc,fillColor)
  80.     fillBox(x1+1,y1+1,width-2,height-2,fillColor)
  81.     drawLineH(x1,width,y1,bc)
  82.     drawLineH(x1,width,y1+height-1,bc)
  83.     drawLineV(x1,y1,height,bc)
  84.     drawLineV(x1+width-1,y1,height,bc)
  85. end
  86.  
  87. --Creates a text box with a title and border.
  88. function drawTextBox(title,title_tc,title_bc,body,body_tc,body_bc,x,y,width,height)
  89.     if (height == nil) then
  90.         height = #(wrap(body,width)) + 2
  91.     end
  92.     drawBorder(x,y,width,height,title_bc,body_bc)
  93.     writeText(title,x+1,y,title_tc,title_bc)
  94.     writeTextWrapped(body,x+1,y+1,width-2,body_tc,body_bc)
  95. end
  96.  
  97. --Writes a block of text wrapped in an area.
  98. function writeTextWrapped(text,x,y,width,tc,bc)
  99.     local wrapped = wrap(text, width)
  100.     fillBox(x,y,width,#wrapped,bc)
  101.     m.setTextColor(tc)
  102.     for i=1,#wrapped do
  103.         m.setCursorPos(x,y+i-1)
  104.         m.write(wrapped[i])
  105.     end
  106. end
  107.  
  108. --Writes text starting at given coordinates.
  109. function writeText(text,x,y,tc,bc)
  110.     m.setCursorPos(x,y)
  111.     m.setTextColor(tc)
  112.     m.setBackgroundColor(bc)
  113.     m.write(text)
  114. end
  115.  
  116. --Writes text centered at a given point.
  117. function writeTextCentered(text,x,y,tc,bc)
  118.     m.setCursorPos(x-((#text)/2),y)
  119.     m.setTextColor(tc)
  120.     m.setBackgroundColor(bc)
  121.     m.write(text)
  122. end
  123.  
  124. --Gets the horizontal and vertical size of the drawing area.
  125. function getSize()
  126.     return m.getSize()
  127. end
  128.  
  129. --Creates a button with a name, location, text, and function.
  130. function createButton(name,x,y,width,height,tc,bc,bcPressed,func)
  131.     buttons[name] = {name=name,x=x,y=y,width=width,height=height,tc=tc,bc=bc,bcPressed=bcPressed,func=func}
  132. end
  133.  
  134. --Removes a button with a specific name.
  135. function removeButton(name)
  136.     table.remove(buttons, name)
  137. end
  138.  
  139. --Draws a button on the target screen.
  140. local function drawButton(bTable)
  141.     local effectiveBc = bTable.bc
  142.     if (bTable.timer ~= nil) then
  143.         effectiveBc = bTable.bcPressed
  144.     end
  145.     fillBox(bTable.x,bTable.y,bTable.width,bTable.height,effectiveBc)
  146.     writeTextCentered(bTable.name,bTable.x + bTable.width/2,bTable.y + bTable.height/2,bTable.tc,effectiveBc)
  147. end
  148.  
  149. --Checks if a coordinate is within a button's x and y values.
  150. local function buttonContains(bTable,x,y)
  151.     return (x >= bTable.x and x < bTable.x+bTable.width and y >= bTable.y and y < bTable.y+bTable.height)
  152. end
  153.  
  154. --Draws all buttons onto the screen.
  155. function drawButtons()
  156.     for key,button in pairs(buttons) do
  157.         drawButton(button)
  158.     end
  159. end
  160.  
  161. --What to do when the user touches the monitor.
  162. local function onMonitorTouch(side,x,y)
  163.     for key,button in pairs(buttons) do
  164.         if (buttonContains(button,x,y)) then
  165.             button.timer = os.startTimer(0.25)
  166.             drawButton(button)
  167.             button.func(side,x,y)
  168.         end
  169.     end
  170. end
  171.  
  172. --What to do when a timer is up. Set the timer to nil and redraw.
  173. local function onTimerEvent(tId)
  174.     for key,button in pairs(buttons) do
  175.         if (button.timer ~= nil and button.timer == tId) then
  176.             button.timer = nil
  177.             drawButton(button)
  178.         end
  179.     end
  180. end
  181.  
  182. --Begins an event handler for monitor and terminal touch events, and executes any buttons the user clicks on.
  183. function buttonLoop()
  184.     buttonLoopActive = true
  185.     drawButtons()
  186.     while (buttonLoopActive) do
  187.         local event,p1,p2,p3,p4,p5 = os.pullEvent()
  188.         if (isMonitor and event == "monitor_touch") then
  189.             onMonitorTouch(p1,p2,p3)
  190.         elseif (event == "mouse_click") then
  191.             --Do the same thing for computer screens. The user changes the behaviour.
  192.             onMonitorTouch(p1,p2,p3)
  193.         elseif (event == "timer") then
  194.             onTimerEvent(p1)
  195.         end
  196.     end
  197. end
  198.  
  199. --Ends the button loop.
  200. function endButtonLoop()
  201.     buttonLoopActive = false
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement