Miki_Tellurium

Button API

Dec 22nd, 2022 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.35 KB | Gaming | 0 0
  1. --Button making API by Miki_Tellurium
  2. --Version: 1.0.0
  3. local expect = require("cc.expect")
  4. local expect = expect.expect
  5.  
  6. local defaultTextColor = colors.black
  7. local defaultActiveBackgroundColor = colors.green
  8. local defaultInactiveBackgroundColor = colors.red
  9. local defaultDisabledBackgroundColor = colors.lightGray
  10. local defaultBlinkColor = colors.yellow
  11. active = "active"
  12. inactive = "inactive"
  13. disabled = "disabled"
  14.  
  15. --- This API provide tools to draw and add functions to buttons.
  16. --- Buttons can have different state and change look based on the
  17. --- current state. The background and text color of the button is
  18. --- customizable using the ComputerCraft [colors] API. You can also
  19. --- use [button:waitForClick] to check when a button is clicked and
  20. --- add functions to it.
  21. ---@class button
  22. button = {
  23.     label = "button",  --text displayed on the button
  24.     width = 0,         --can't be smaller then label
  25.     x = 1,
  26.     y = 1,
  27.     activeTextColor = defaultTextColor,
  28.     inactiveTextColor = defaultTextColor,
  29.     disabledTextColor = defaultTextColor,
  30.     activeBackgroundColor = defaultActiveBackgroundColor,
  31.     inactiveBackgroundColor = defaultInactiveBackgroundColor,
  32.     disabledBackgroundColor = defaultDisabledBackgroundColor,
  33.     blinkColor = defaultBlinkColor,  --background color when button blink
  34.     state = active,                  --possible states: active, inactive, disabled
  35.     isBlink = false                  --if the button blink when clicked
  36. }
  37.  
  38. --- Create a new button with default values and return it.
  39. --- You can use it like this:
  40. --- * buttonName = [button:new()]
  41. ---
  42. --- buttonName will be the name of the new button object, you
  43. --- can call it's functions like this:
  44. --- * buttonName:function()
  45. function button:new(obj)
  46.     obj = obj or {}
  47.     setmetatable(obj, self)
  48.     self.__index = self
  49.     return obj
  50. end
  51.  
  52. --- Deletes the button from the terminal and erases its data.
  53. --- Once this function is called the button no longer exist.
  54. --- If you want to delete the button from the terminal without
  55. --- erasing its data use [button:clear].
  56. function button:delete()
  57.     local background = term.getBackgroundColor()
  58.     self:setBackgroundColor(background)
  59.     self:setTextColor(background)
  60.     self:paint()
  61.     self = {}
  62. end
  63.  
  64. --- Set the text that will be displayed on the button.
  65. ---@param text string The text to be displayed on the button
  66. function button:setLabel(text)
  67.     expect(1, text, "string")
  68.     self.label = tostring(text)
  69. end
  70.  
  71. --- Returns the label of the button.
  72. ---@return string The current label of the button
  73. function button:getLabel()
  74.     return self.label
  75. end
  76.  
  77. --- Set the width of the button. If the width is greater than
  78. --- the number of characters on the label than the label on the
  79. --- button will be centered. Throws if the width is lower than
  80. --- the number of characters on the label.
  81. ---@param int number The width of the button
  82. function button:setWidth(int)
  83.     expect(1, int, "number")
  84.     if int < #self.label then
  85.         error("bad argument to setWidth (can't be lower than label size)", 2)
  86.     end
  87.     self.width = int
  88. end
  89.  
  90. --- Returns the width of the button
  91. ---@return number The width of the button
  92. function button:getWidth()
  93.     return self.width
  94. end
  95.  
  96. --- Set where the button will be displayed on the terminal.
  97. ---@param x number The x coordinate of the button
  98. ---@param y number The y coordinate of the button
  99. function button:setLocation(x, y)
  100.     expect(1, x, "number")
  101.     expect(2, y, "number")
  102.     self.x = x
  103.     self.y = y
  104. end
  105.  
  106. --- Returns the x and y coordinates of the button
  107. ---@return number The x and y coordinates of the button
  108. function button:getLocation()
  109.     return self.x, self.y
  110. end
  111.  
  112. --- Set the current state of the button. Different states dictate
  113. --- the label and background color of the button. If repaint is true
  114. --- the button will be repainted on the terminal.
  115. --- Possible states are: active, inactive and disabled.
  116. --- Throws if the state is invalid.
  117. ---@param state string The state of the button
  118. ---@param repaint boolean If the button should be repainted on the terminal
  119. function button:setState(state, repaint)
  120.     expect(1, state, "string")
  121.     expect(2, repaint, "boolean", "nil")
  122.     if state == active then
  123.         self.state = active
  124.     elseif state == inactive then
  125.         self.state = inactive
  126.     elseif state == disabled then
  127.         self.state = disabled
  128.     else
  129.         error("bad argument to setState (possible states: active, inactive, disabled)", 2)
  130.     end
  131.  
  132.     if repaint then
  133.         self:paint()
  134.     end
  135. end
  136.  
  137. --- Returns the current state of the button.
  138. ---@return string The state of the button
  139. function button:getState()
  140.     return self.state
  141. end
  142.  
  143. --- Set the same text color for all states.
  144. ---@param color number The color to set the text to
  145. function button:setTextColor(color)
  146.     expect(1, color, "number")
  147.     self.activeTextColor = color
  148.     self.inactiveTextColor = color
  149.     self.disabledTextColor = color
  150. end
  151.  
  152. --- Set the text color for the active state.
  153. ---@param color number The color to set the text to
  154. function button:setActiveTextColor(color)
  155.     expect(1, color, "number")
  156.     self.activeTextColor = color
  157. end
  158.  
  159. --- Returns the current text color for the active state of the button.
  160. ---@return number The current text color for the active state
  161. function button:getActiveTextColor()
  162.     return self.activeTextColor
  163. end
  164.  
  165. --- Set the text color for the inactive state.
  166. ---@param color number The color to set the text to
  167. function button:setInactiveTextColor(color)
  168.     expect(1, color, "number")
  169.     self.inactiveTextColor = color
  170. end
  171.  
  172. --- Returns the current text color for the inactive state of the button.
  173. ---@return number The current text color for the inactive state
  174. function button:getInactiveTextColor()
  175.     return self.inactiveTextColor
  176. end
  177.  
  178. --- Set the text color for the disabled state.
  179. ---@param color number The color to set the text to
  180. function button:setDisabledTextColor(color)
  181.     expect(1, color, "number")
  182.     self.disabledTextColor = color
  183. end
  184.  
  185. --- Returns the current text color for the disabled state of the button.
  186. ---@return number The current text color for the disabled state
  187. function button:getDisabledTextColor()
  188.     return self.disabledTextColor
  189. end
  190.  
  191. --- Set the same background color for all states.
  192. ---@param color number The color to set the background to
  193. function button:setBackgroundColor(color)
  194.     expect(1, color, "number")
  195.     self.activeBackgroundColor = color
  196.     self.inactiveBackgroundColor = color
  197.     self.disabledBackgroundColor = color
  198. end
  199.  
  200. --- Set the background color for the active state.
  201. ---@param color number The color to set the background to
  202. function button:setActiveBackgroundColor(color)
  203.     expect(1, color, "number")
  204.     self.activeBackgroundColor = color
  205. end
  206.  
  207. --- Returns the current background color for the active state of the button.
  208. ---@return number The current background color for the active state
  209. function button:getActiveBackgroundColor()
  210.     return self.activeBackgroundColor
  211. end
  212.  
  213. --- Set the background color for the inactive state.
  214. ---@param color number The color to set the background to
  215. function button:setInactiveBackgroundColor(color)
  216.     expect(1, color, "number")
  217.     self.inactiveBackgroundColor = color
  218. end
  219.  
  220. --- Returns the current background color for the inactive state of the button.
  221. ---@return number The current background color for the inactive state
  222. function button:getInactiveBackgroundColor()
  223.     return self.inactiveBackgroundColor
  224. end
  225.  
  226. --- Set the background color for the disabled state.
  227. ---@param color number The color to set the background to
  228. function button:setDisabledBackgroundColor(color)
  229.     expect(1, color, "number")
  230.     self.disabledBackgroundColor = color
  231. end
  232.  
  233. --- Returns the current background color for the disabled state of the button.
  234. ---@return number The current background color for the disabled state
  235. function button:getDisabledBackgroundColor()
  236.     return self.disabledBackgroundColor
  237. end
  238.  
  239. --- Set if the button should blink when clicked.
  240. ---@param boolean boolean If the button should blink when clicked
  241. function button:setBlinking(boolean)
  242.     expect(1, boolean, "boolean")
  243.     self.isBlink = boolean
  244. end
  245.  
  246. --- Returns true if the button is set to blink when clicked.
  247. ---@return boolean If the button is set to blink when clikced
  248. function button:doesBlink()
  249.     return self.isBlink
  250. end
  251.  
  252. --- Set the color the button should turn to when clicked.
  253. ---@param color number The color the button should turn to when blinking
  254. function button:setBlinkColor(color)
  255.     expect(1, color, "number")
  256.     self.blinkColor = color
  257. end
  258.  
  259. --- Returns the color the button is set to turn to when blinking.
  260. ---@return number The color the button turn to when blinking
  261. function button:getBlinkColor()
  262.     return self.blinkColor
  263. end
  264.  
  265. --- Draw the button on the screen using the current button
  266. --- properties. Note that after the function is called the
  267. --- cursor return where it was before drawing the button.
  268. function button:paint()
  269.     --Store current text and background color for later
  270.     local currentTextColor = term.getTextColor()
  271.     local currentBackgroundColor = term.getBackgroundColor()
  272.     --Store current cursor position for later
  273.     local currentX, currentY = term.getCursorPos()
  274.  
  275.     if self.width < #self.label or self.width <= 0 then
  276.         self.width = #self.label
  277.     end
  278.     --set text and background color based on current state
  279.     if self.state == active then
  280.         term.setBackgroundColor(self.activeBackgroundColor)
  281.         term.setTextColor(self.activeTextColor)
  282.     elseif self.state == inactive then
  283.         term.setBackgroundColor(self.inactiveBackgroundColor)
  284.         term.setTextColor(self.inactiveTextColor)
  285.     elseif self.state == disabled then
  286.         term.setBackgroundColor(self.disabledBackgroundColor)
  287.         term.setTextColor(self.disabledTextColor)
  288.     end
  289.     --Draw the button
  290.     term.setCursorPos(self.x, self.y)
  291.     if self.width > #self.label then
  292.         local difference = self.width - #self.label
  293.         for i = 1,math.floor(difference/2) do
  294.             term.write(" ")
  295.         end
  296.         term.write(self.label)
  297.         for k = 1,math.ceil(difference/2) do
  298.             term.write(" ")
  299.         end
  300.     else
  301.         term.write(self.label)
  302.     end
  303.     --Restore the previous terminal status
  304.     term.setTextColor(currentTextColor)
  305.     term.setBackgroundColor(currentBackgroundColor)
  306.     term.setCursorPos(currentX, currentY)
  307. end
  308.  
  309. --- Make the button blink. This function is called by [button:waitForClick]
  310. --- if [button:setBlinking] is set to true.
  311. function button:blink()
  312.     local blinkColor = self.blinkColor
  313.     local currentBackgroundColor = nil
  314.     if self:doesBlink() then
  315.         if self.state == active then
  316.             currentBackgroundColor = self.activeBackgroundColor
  317.         elseif self.state == inactive then
  318.             currentBackgroundColor = self.inactiveBackgroundColor
  319.         elseif self.state == disabled then
  320.             currentBackgroundColor = self.disabledBackgroundColor
  321.         end
  322.  
  323.         if self.state == active then
  324.             self:setActiveBackgroundColor(blinkColor)
  325.         elseif self.state == inactive then
  326.             self:setInactiveBackgroundColor(blinkColor)
  327.         elseif self.state == disabled then
  328.             self:setDisabledBackgroundColor(blinkColor)
  329.         end
  330.  
  331.         self:paint()
  332.         sleep(0.15)
  333.  
  334.         if self.state == active then
  335.             self:setActiveBackgroundColor(currentBackgroundColor)
  336.         elseif self.state == inactive then
  337.             self:setInactiveBackgroundColor(currentBackgroundColor)
  338.         elseif self.state == disabled then
  339.             self:setDisabledBackgroundColor(currentBackgroundColor)
  340.         end
  341.  
  342.         self:paint()
  343.     end
  344. end
  345.  
  346. --- Delete the button from the terminal. It doesn't erase its data.
  347. --- Note that this function use the current background color of the
  348. --- terminal to delete the button.
  349. function button:clear()
  350.     local background = term.getBackgroundColor()
  351.     local currentTextColor = nil
  352.     local currentBackgroundColor = nil
  353.     if self.state == active then
  354.         currentTextColor = self.activeTextColor
  355.         currentBackgroundColor = self.activeBackgroundColor
  356.     elseif self.state == inactive then
  357.         currentTextColor = self.inactiveTextColor
  358.         currentBackgroundColor = self.inactiveBackgroundColor
  359.     elseif self.state == disabled then
  360.         currentTextColor = self.disabledTextColor
  361.         currentBackgroundColor = self.disabledBackgroundColor
  362.     end
  363.  
  364.     if self.state == active then
  365.         self:setActiveTextColor(background)
  366.         self:setActiveBackgroundColor(background)
  367.     elseif self.state == inactive then
  368.         self:setInactiveTextColor(background)
  369.         self:setInactiveBackgroundColor(background)
  370.     elseif self.state == disabled then
  371.         self:setDisabledTextColor(background)
  372.         self:setDisabledBackgroundColor(background)
  373.     end
  374.  
  375.     self:paint()
  376.  
  377.     if self.state == active then
  378.         self:setActiveTextColor(currentTextColor)
  379.         self:setActiveBackgroundColor(currentBackgroundColor)
  380.     elseif self.state == inactive then
  381.         self:setInactiveTextColor(currentTextColor)
  382.         self:setInactiveBackgroundColor(currentBackgroundColor)
  383.     elseif self.state == disabled then
  384.         self:setDisabledTextColor(currentTextColor)
  385.         self:setDisabledBackgroundColor(currentBackgroundColor)
  386.     end
  387. end
  388.  
  389. --- Return true if the button was clicked. This function checks for
  390. --- the [mouse_click] or [monitor_touch] events and returns true if
  391. --- the button was clicked. You can pass an event to this function
  392. --- by using [os.pullEvent] in a table:
  393. --- - local event = {[os.pullEvent()]}
  394. function button:waitForClick(event)
  395.     expect(1, event, "table")
  396.     if event[1] == "mouse_click" and event[2] == 1 then
  397.         px, py = event[3], event[4]
  398.         if px >= self.x and px < self.x+self.width and py == self.y then
  399.             self:blink()
  400.             return true
  401.         else
  402.             return false
  403.         end
  404.     elseif event[1] == "monitor_touch" then
  405.         px, py = event[3], event[4]
  406.         if px >= self.x and px < self.x+self.width and py == self.y then
  407.             self:blink()
  408.             return true
  409.         else
  410.             return false
  411.         end
  412.     else
  413.         return false
  414.     end
  415. end
Advertisement
Add Comment
Please, Sign In to add comment