This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
HPWebcamAble's profile picture

Screen API

Started by HPWebcamAble, 19 February 2015 - 10:17 PM
HPWebcamAble #1
Posted 19 February 2015 - 11:17 PM
Description:

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


Spoiler2.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
Edited on 31 January 2016 - 05:14 PM
InDieTasten #2
Posted 20 February 2015 - 11:11 AM
Yeah, nice post. As a suggestion I would add a default template within your api, so that one can just tell you the color of the button and everything else is just default. This way the user code won't have this massive initialization section. It's much easier for beginners to start of with only knowing some parameters of the button, like color and position, and if needed can just add and learn those things they need rather than everything. Code is then cleaner at the beginning and easier to understand. ;)/>
Also, your explanation would be shorter, because any further parameters and options a button can have just a short documentation entry. As an example I often refer to bootstrap. You need to know just the most basic stuff. If you want to be really specific at some points, you just look it up on the documentation.

Also, why not some more interact-able things? Like scroll-able lists, color pickers, all these things. Or labels, borders, check-boxes, just to mention some.
HPWebcamAble #3
Posted 20 February 2015 - 04:33 PM
SpoilerYeah, nice post. As a suggestion I would add a default template within your api, so that one can just tell you the color of the button and everything else is just default. This way the user code won't have this massive initialization section. It's much easier for beginners to start of with only knowing some parameters of the button, like color and position, and if needed can just add and learn those things they need rather than everything. Code is then cleaner at the beginning and easier to understand. ;)/>
Also, your explanation would be shorter, because any further parameters and options a button can have just a short documentation entry. As an example I often refer to bootstrap. You need to know just the most basic stuff. If you want to be really specific at some points, you just look it up on the documentation.

I thought about it, but I wanted to get it to a usable state first. I'll add this next.

Also, why not some more interact-able things? Like scroll-able lists, color pickers, all these things. Or labels, borders, check-boxes, just to mention some.

When I get a chance I'll definitely add some more things :)/>
HPWebcamAble #4
Posted 15 March 2015 - 05:07 PM
Update! 1.2.1 1.2.2!

Check out the UPDATES spoiler in the original post for changes
Edited on 18 January 2016 - 06:45 PM
HPWebcamAble #5
Posted 29 December 2015 - 10:00 PM
Update! Version 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
-Updated demo program
Edited on 18 January 2016 - 06:46 PM
HPWebcamAble #6
Posted 18 January 2016 - 07:47 PM
Update! Version 2.0

Spoiler-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)
Edited on 18 January 2016 - 06:48 PM
X3ME #7
Posted 28 February 2016 - 10:11 AM
Where is the documentation ( Yes i decided to use your api mate! :)/> )
HPWebcamAble #8
Posted 28 February 2016 - 04:15 PM
Where is the documentation ( Yes i decided to use your api mate! :)/> )

In the file on pastebin, below the version history ;)/>
(Starts at line 97)