Advertisement
jesusthekiller

Button API

May 31st, 2013
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.28 KB | None | 0 0
  1. --[[
  2.     Project info:
  3.    
  4.     Name: Button API
  5.     Creator: Jesusthekiller
  6.     Language: Lua (CC)
  7.     Website: None
  8.     License: GNU GPL
  9.         License file can be fount at www.jesusthekiller.com/license-gpl.html
  10.  
  11.     Version: 1.1
  12. ]]--
  13.  
  14. --[[
  15.     Changelog:
  16.  
  17.     1.0:
  18.       Initial release
  19.      
  20.     1.1:
  21.       Added Single-Use buttons
  22. ]]--
  23.  
  24. --[[
  25.     Usage:
  26.       makeButton(localtionArray, buttonbTypeArray, callback, [callback_pressed])
  27.        
  28.         locationArray:
  29.           Array, has to contain those keys:
  30.             * xstart
  31.             * ystart
  32.             * xstop
  33.             * ystop
  34.        
  35.         buttonbTypeArray:
  36.           Array, has to contain those keys:
  37.             * color_bkg
  38.             * click_color_bkg
  39.             * color_text
  40.             * click_color_text
  41.             * text
  42.             * textStartPosX
  43.             * textStartPosY
  44.             * event_value
  45.            
  46.         callback:
  47.           Function with code to be run in parallel with makeButton
  48.           Button will terminate if this code ends.
  49.            
  50.         callback_pressed:
  51.           Optional, function to be run when button is pressed.
  52.           Don't make it infinite (it will freeze button pressed and unclickable until finished)
  53.            
  54.         makeButton will create "button" event with event_value as 1st value when button will be pressed.
  55.      
  56.       makeSingleUseButton(localtionArray, buttonbTypeArray, animationTime, callback, [callback_pressed])
  57.         localtionArray, buttonbTypeArray, callback, callback_pressed same as in makeButton
  58.        
  59.         animatonTime:
  60.           Controls for how long button has to be pressed after clicked. If set to >= 0 then animation will be disabled.
  61.      
  62.         callback_pressed is executed after animation, not in parallel with it.
  63.      
  64.       removeButton(event_value)
  65.         Removes button
  66.       !!By default this API will print to Terminal, if you want monitor output, do term.redirect()!!
  67. ]]--
  68.  
  69. -- Local API:
  70. local function printButton(loc, btype, clicked)
  71.     local bkg, text
  72.    
  73.     local x, y = term.getCursorPos()
  74.    
  75.     if clicked then
  76.         bkg = btype.click_color_bkg
  77.         text = btype.click_color_text
  78.     else
  79.         bkg = btype.color_bkg
  80.         text = btype.color_text
  81.     end
  82.    
  83.     term.setBackgroundColor(bkg)
  84.     term.setTextColor(text)
  85.    
  86.     term.setCursorPos(loc.startx, loc.starty)  -- Write bkg
  87.    
  88.     for i = loc.starty, loc.stopy do
  89.         term.setCursorPos(loc.startx, i)
  90.         for j = loc.startx, loc.stopx do
  91.             write(" ")
  92.         end
  93.     end
  94.    
  95.     term.setCursorPos(btype.textStartPosX, btype.textStartPosY) -- Write text
  96.     write(btype.text)
  97.    
  98.     term.setCursorPos(x, y) -- Restore cursor xy
  99. end
  100.  
  101. -- Global API:
  102.  
  103. function makeButton(loc, btype, callback, callback_pressed)
  104.     printButton(loc, btype, false)
  105.        
  106.     local function wait_click()
  107.         while true do
  108.             local e, b, x, y = os.pullEvent("mouse_click")
  109.            
  110.             if x >= loc.startx and x <= loc.stopx and y >= loc.starty and y <= loc.stopy then
  111.                 os.queueEvent("button", btype.event_value)
  112.                
  113.                 printButton(loc, btype, true)
  114.                
  115.                 local function f1()
  116.                     if callback_pressed then
  117.                         callback_pressed()
  118.                     end
  119.                 end
  120.                
  121.                 local function f2()
  122.                     sleep(0.2)
  123.                 end
  124.                
  125.                 parallel.waitForAll(f1, f2)
  126.                
  127.                 printButton(loc, btype, false)
  128.             end
  129.         end
  130.     end
  131.    
  132.     local function wait_terminate()
  133.         while true do
  134.             local e, v = os.pullEvent("remove_button")
  135.             if v == btype.event_value then
  136.                 return
  137.             end
  138.         end
  139.     end
  140.    
  141.     parallel.waitForAny(wait_click, wait_terminate, callback)
  142. end
  143.  
  144. function makeSingleUseButton(loc, btype, animation, callback, callback_pressed)
  145.     printButton(loc, btype, false)
  146.    
  147.     local function wait_click()
  148.         while true do
  149.             local e, b, x, y = os.pullEvent("mouse_click")
  150.            
  151.             if x >= loc.startx and x <= loc.stopx and y >= loc.starty and y <= loc.stopy then
  152.                 os.queueEvent("button", btype.event_value)
  153.                
  154.                 if animation > 0 then
  155.                     printButton(loc, btype, true)
  156.                     sleep(animation)
  157.                     printButton(loc, btype, false)
  158.                     sleep(0.1)
  159.                 end
  160.                
  161.                 if callback_pressed then
  162.                     callback_pressed()
  163.                 end
  164.                
  165.                 return
  166.             end
  167.         end
  168.     end
  169.    
  170.     local function wait_terminate()
  171.         while true do
  172.             local e, v = os.pullEvent("remove_button")
  173.             if v == btype.event_value then
  174.                 return
  175.             end
  176.         end
  177.     end
  178.    
  179.     parallel.waitForAny(wait_click, wait_terminate, callback)
  180. end
  181.  
  182. function removeButton(event_value)
  183.     os.queueEvent("remove_button", event_value)
  184. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement