Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Project info:
- Name: Button API
- Creator: Jesusthekiller
- Language: Lua (CC)
- Website: None
- License: GNU GPL
- License file can be fount at www.jesusthekiller.com/license-gpl.html
- Version: 1.1
- ]]--
- --[[
- Changelog:
- 1.0:
- Initial release
- 1.1:
- Added Single-Use buttons
- ]]--
- --[[
- Usage:
- makeButton(localtionArray, buttonbTypeArray, callback, [callback_pressed])
- locationArray:
- Array, has to contain those keys:
- * xstart
- * ystart
- * xstop
- * ystop
- buttonbTypeArray:
- Array, has to contain those keys:
- * color_bkg
- * click_color_bkg
- * color_text
- * click_color_text
- * text
- * textStartPosX
- * textStartPosY
- * event_value
- callback:
- Function with code to be run in parallel with makeButton
- Button will terminate if this code ends.
- callback_pressed:
- Optional, function to be run when button is pressed.
- Don't make it infinite (it will freeze button pressed and unclickable until finished)
- makeButton will create "button" event with event_value as 1st value when button will be pressed.
- makeSingleUseButton(localtionArray, buttonbTypeArray, animationTime, callback, [callback_pressed])
- localtionArray, buttonbTypeArray, callback, callback_pressed same as in makeButton
- animatonTime:
- Controls for how long button has to be pressed after clicked. If set to >= 0 then animation will be disabled.
- callback_pressed is executed after animation, not in parallel with it.
- removeButton(event_value)
- Removes button
- !!By default this API will print to Terminal, if you want monitor output, do term.redirect()!!
- ]]--
- -- Local API:
- local function printButton(loc, btype, clicked)
- local bkg, text
- local x, y = term.getCursorPos()
- if clicked then
- bkg = btype.click_color_bkg
- text = btype.click_color_text
- else
- bkg = btype.color_bkg
- text = btype.color_text
- end
- term.setBackgroundColor(bkg)
- term.setTextColor(text)
- term.setCursorPos(loc.startx, loc.starty) -- Write bkg
- for i = loc.starty, loc.stopy do
- term.setCursorPos(loc.startx, i)
- for j = loc.startx, loc.stopx do
- write(" ")
- end
- end
- term.setCursorPos(btype.textStartPosX, btype.textStartPosY) -- Write text
- write(btype.text)
- term.setCursorPos(x, y) -- Restore cursor xy
- end
- -- Global API:
- function makeButton(loc, btype, callback, callback_pressed)
- printButton(loc, btype, false)
- local function wait_click()
- while true do
- local e, b, x, y = os.pullEvent("mouse_click")
- if x >= loc.startx and x <= loc.stopx and y >= loc.starty and y <= loc.stopy then
- os.queueEvent("button", btype.event_value)
- printButton(loc, btype, true)
- local function f1()
- if callback_pressed then
- callback_pressed()
- end
- end
- local function f2()
- sleep(0.2)
- end
- parallel.waitForAll(f1, f2)
- printButton(loc, btype, false)
- end
- end
- end
- local function wait_terminate()
- while true do
- local e, v = os.pullEvent("remove_button")
- if v == btype.event_value then
- return
- end
- end
- end
- parallel.waitForAny(wait_click, wait_terminate, callback)
- end
- function makeSingleUseButton(loc, btype, animation, callback, callback_pressed)
- printButton(loc, btype, false)
- local function wait_click()
- while true do
- local e, b, x, y = os.pullEvent("mouse_click")
- if x >= loc.startx and x <= loc.stopx and y >= loc.starty and y <= loc.stopy then
- os.queueEvent("button", btype.event_value)
- if animation > 0 then
- printButton(loc, btype, true)
- sleep(animation)
- printButton(loc, btype, false)
- sleep(0.1)
- end
- if callback_pressed then
- callback_pressed()
- end
- return
- end
- end
- end
- local function wait_terminate()
- while true do
- local e, v = os.pullEvent("remove_button")
- if v == btype.event_value then
- return
- end
- end
- end
- parallel.waitForAny(wait_click, wait_terminate, callback)
- end
- function removeButton(event_value)
- os.queueEvent("remove_button", event_value)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement