Advertisement
Guest User

clickapi

a guest
Jan 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.91 KB | None | 0 0
  1. -- This is the click API:
  2. -- This allows you to easily make clickable areas
  3. -- and buttons with or without borders.
  4. -- Chock500 - 22|12|12,016HE
  5.  
  6. os.loadAPI("chockos/config")
  7. -- Simplifies the button function
  8. function fnButton()
  9.   local event, button, X, Y = os.pullEvent( "mouse_click" )
  10.   return button, X, Y
  11. end
  12.  
  13. -- Checks if an area is clicked, rather than a point
  14. function ifButton(X,Y,minX,minY,maxX,maxY)
  15.   -- X&Y is input from fnButton()
  16.   -- The other variables are to define the square
  17.   -- in which it checks
  18.   if X >= minX and X <= maxX and Y >= minY and Y <= maxY then
  19.     return true
  20.   else
  21.     return false
  22.   end
  23. end
  24.  
  25. -- Draws a button with or without a border
  26. function obButton(X,Y,label,oX,oY,length,border)
  27.   -- X&Y (int) are inputs from fnButton()'s X&Y
  28.   -- Label (str) is what the button displays
  29.   -- oX&oY (int<16) are the position of the
  30.   -- top-left corner of the button
  31.   -- border (true/false) enables or disables the
  32.   -- border
  33.   width = 0
  34.   if border == true then
  35.     -- with border
  36.     -- this is so bodged, I can't explain it, even
  37.     -- just after finishing it. Also, the Y value
  38.     -- cannot be greater than 15 for some reason.
  39.     -- (It breaks if you do that).
  40.     -- All you need to know is that it draws a box
  41.     -- around the label text and includes it in its
  42.     -- hitbox.
  43.     --          x---------x
  44.     --          |Like this|
  45.     --          x---------x
  46.     --
  47.     -- Essentially, this is the ifButton() function,
  48.     -- but it draws a label. Also, have the
  49.     -- fnButton() function after all of the
  50.     -- 'if obButton() then ... end's, otherwise they
  51.     -- will not draw before you click once
  52.     oX = oX + 1
  53.     oY = oY + 2
  54.     term.setCursorPos(oX-1,oY-1)
  55.     print("|"..label.."|")
  56.     width = 2
  57.     term.setCursorPos(oX-1,oY-2)
  58.     j = 0
  59.     yOff = 0
  60.     while j < 2 do
  61.       i = 0
  62.       term.setCursorPos(oX-1,(oY-2)+yOff)
  63.       print("x")
  64.       while i < length do
  65.         term.setCursorPos(oX+i,(oY-2)+yOff)
  66.         print("-")
  67.         i = i + 1
  68.       end
  69.       term.setCursorPos(oX+length,(oY-2)+yOff)
  70.       print("x")
  71.       yOff = 2
  72.       j = j + 1
  73.     end
  74.     return ifButton(X+1,Y+2,oX,oY,oX+(length)+1,oY+width)
  75.   else
  76.     -- Without border
  77.     term.setCursorPos(oX,oY)
  78.     print(label)
  79.     return ifButton(X,Y,oX,oY,oX+(length-1),oY)
  80.   end
  81. end
  82. -- Returns true or false
  83.  
  84. -- Exactly the same as obButton(), except has
  85. -- colours
  86. function colObButton(X,Y,label,oX,oY,length,border,textcol,buttoncol)
  87.   -- X&Y (int) are inputs from fnButton()'s X&Y
  88.   -- Label (str) is what the button displays
  89.   -- oX&oY (int<16) are the position of the
  90.   -- top-left corner of the button
  91.   -- border (true/false) enables or disables the
  92.   -- border
  93.   term.setBackgroundColour(buttoncol)
  94.   term.setTextColour(textcol)
  95.   width = 0
  96.   if border == true then
  97.     -- with border
  98.     -- this is so bodged, I can't explain it, even
  99.     -- just after finishing it. Also, the Y value
  100.     -- cannot be greater than 15 for some reason.
  101.     -- (It breaks if you do that).
  102.     -- All you need to know is that it draws a box
  103.     -- around the label text and includes it in its
  104.     -- hitbox.
  105.     --          x---------x
  106.     --          |Like this|
  107.     --          x---------x
  108.     --
  109.     -- Essentially, this is the ifButton() function,
  110.     -- but it draws a label. Also, have the
  111.     -- fnButton() function after all of the
  112.     -- 'if obButton() then ... end's, otherwise they
  113.     -- will not draw before you click once
  114.     oX = oX + 1
  115.     oY = oY + 2
  116.     term.setCursorPos(oX-1,oY-1)
  117.     print("|"..label.."|")
  118.     width = 2
  119.     term.setCursorPos(oX-1,oY-2)
  120.     j = 0
  121.     yOff = 0
  122.     while j < 2 do
  123.       i = 0
  124.       term.setCursorPos(oX-1,(oY-2)+yOff)
  125.       print("x")
  126.       while i < length do
  127.         term.setCursorPos(oX+i,(oY-2)+yOff)
  128.         print("-")
  129.         i = i + 1
  130.       end
  131.       term.setCursorPos(oX+length,(oY-2)+yOff)
  132.       print("x")
  133.       yOff = 2
  134.       j = j + 1
  135.     end
  136.     defaultColours()
  137.     return ifButton(X+1,Y+2,oX,oY,oX+(length)+1,oY+width)
  138.   else
  139.     -- Without border
  140.     term.setCursorPos(oX,oY)
  141.     print(label)
  142.     defaultColours()
  143.     return ifButton(X,Y,oX,oY,oX+(length-1),oY)
  144.   end
  145. end
  146.  
  147. function defaultColours()
  148.   term.setBackgroundColour(config.defaultBackgroundColour())
  149.   term.setTextColour(config.defaultTextColour())
  150. end
  151. -- Returns true or false
  152.  
  153.  
  154. function buttonArray(x,y,buttons)
  155.   -- This function allows you to simplify menus
  156.   -- to only allow buttons to be pushed.
  157.   -- x&y are inputs from fnButton().
  158.   -- buttons is a bit more complicated:
  159.   -- First, you need to used obButton() to create
  160.   -- a button inside a function. The function has
  161.   -- to return an array:
  162.   --
  163.   -- function exampleButton(x,y)
  164.   --   return {obButton(x,y,"eg",1,1,2,true),"exampledestination"}
  165.   -- end
  166.   --
  167.   -- The destination will determine what
  168.   -- page/function the os will go to next after
  169.   -- pushing the button
  170.   loops = 0
  171.   pushed = false
  172.   buttons[table.getn(buttons)+1] =  {false,false}
  173.   while loops < table.getn(buttons) and pushed == false do
  174.     pushed = buttons[loops+1][1]
  175.     loops = loops+1
  176.   end
  177.   return (buttons[loops][2])
  178.   -- returns next destination or false, if no
  179.   -- button is clicked
  180. end
  181.  
  182. function listButtons(dx,dy,buttons,buttonData,buttonReturn)
  183.   length = table.getn(buttons)
  184.   buttonLoop = 1
  185.   maxLen = 0
  186.   while buttonLoop <= length do
  187.     term.setCursorPos(dx,dy+buttonLoop-1)
  188.     if buttonData ~= false then term.setTextColour(buttonData[buttonLoop]) end
  189.     print(buttons[buttonLoop])
  190.     if string.len(buttons[buttonLoop]) > maxLen then
  191.       maxLen = string.len(buttons[buttonLoop])
  192.     end
  193.     buttonLoop = buttonLoop + 1
  194.   end
  195.   term.setTextColour(config.defaultTextColour())
  196.   button, x, y = fnButton()
  197.   while not ifButton(x,y,dx,dy,dx+maxLen-1,dy+length) do
  198.     button,x,y=fnButton()
  199.   end
  200.  
  201.   return buttonReturn[y-dy+1]
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement