This API allows you to quickly create Buttons on either a monitor or in a computer terminal.
Download:
pastebin get 4j4mJsWw screen
How To Use:
The file has extensive documentation, so make sure to read through it
Here is a simple program I wrote as a demo:
Spoiler
--# Demo for object oriented version of HPWebcamable's Screen API
os.loadAPI("screen") --# Load screen API as 'screen'
local main = screen.new(colors.white) --# Ask the api for a new screen, store it in 'main'
main.addObject({ --# Add an object
name = "obj1",
text = "Safety",
minX=2, minY=2,
maxX=9, maxY=4,
clickable = true,
action = function() --# When clicked, this button will...
main.toggleObjectState("obj1") --# Toggle it's own state
main.getObject("obj2").state = "off" --# Set the other object's state to "off"
main.toggleObjectVisible("obj2") --# Toggle the other object's visibility
main.toggleObjectClickable("obj2") --# Toggle the other object's clickablity
main.draw() --# Draw the screen so that the buttons are updated
end
})
main.addObject({ --# Add another object
name = "obj2",
text = "Fire!",
minX=11, minY=2,
maxX=17, maxY=4,
visible = false,
action = function() --# When clicked...
main.toggleObjectState("obj2") --# Toggles itself
main.draw() --# Updates the screen
end
})
main.draw() --# Draw the screen for the first time
while true do --# Infinte loop
local event = {main.handleEvents(true)} --# Ask the screen to check each event to see if it was a button being clicked
if event[1] == "terminate" then --# The 'true' above is to use os.pullEventRaw, so we'll get terminate events
term.setTextColor(colors.white) term.setBackgroundColor(colors.black) term.clear() term.setCursorPos(1,1) --# Clear the screen if the program is terminated
return --# Stop the program when terminated
end
end
Bugs and Questions:
PROGRAMMING questions should go in the Ask-a-Pro forum
Just comment with any bugs or specific questions
Spoiler
2.0-Each individual screen is an object now
*You still call the functions with a period (.), not a colon ( :)/>
*Note that the default elements are universal, they are the same for every screen you make
-Cleaned up the code
-Slightly changed how text is centered on objects (again)
*Should be centered, or 1 pixel to the left if that isn't possible
-Removed 'state', replaced with 'visble' (objects only) and 'clickable'
*Added toggleVisible function for Objects
*Added toggleClickable function for both element types
*Removed toggleState function
-Added getObject and getClickArea to allow changing of text, position, etc
-Updated documentation (See 'Functions and How to Use Them' below)
1.3
-Changed how object states are stored in preparation for Simple Screen Maker 2!
-Touched up the documentation section
-Fixed a few typos
-toFill function now recognizes false values
1.2.3
-Fixed a *kinda* major issue with drawing objects
*A row of pixels was always missing on the right side
-The defualt for 'hasClickArea' for objects is now 'false'
1.2.2
-Function descriptions are now in 'Functions and How to Use Them' section
-handleEvents can now trigger 'object' events (Same arguments as Click Areas)
-checkPos now returns "click_area" or "object" and its name or the result of its action
-Fixed bug that caused 'off' objects to be triggered when clicked
*Objects no longer make a click area when created, both object boundries and click area boundries are checked by checkPos
1.2.1
-Fixed bug that cause the text of a button to be put in weird places
1.2
-All changes have been added to the 'How to Use' section
-Changed 'checkPos' to 'handleEvents'
*Use instead of os.pullEvent()
*If a Click Area is clicked, it triggers the event 'click_area' (if it doens't have a function)
-Arguments are name,mouse button
-'toggleObject' enables or disables an object
-'toggleObjectState' is what makes an object use on or off
1.1
-Added a default Object and Click Area
*If you leave out something when adding, the values will be filled in
*You can change the defaults with setDefaultObject and setDefaultClickArea
-Added much more descriptive errors
-Added a 'drawObject' function, to draw a single object
-Fixed a few bugs with the states of Click Areas