Posted 28 September 2014 - 03:25 PM
Hey everyone, It's been a long time since I've actually posted any program or anything so I thought I'd share this API. I would like to thank those awesome people in AaP for helping me with the most important function of the API. For those who like OOP will probably like this simple API.
So what can this do? Well it can help you with some simple "multitasking"( I'll show you how in just a minute ) by handling multiple objects with the same event. It has some objects already in it that you can use, but you can ofcourse add your own external objects.
Now lets get onto how todo some "multitasking", first of all you'd want to load the API and create a table where all of your objects are
Here's how you load an object
This is required for objects that are in the API
Here's the functions that this API currently adds
Feel free to post suggestions on what to add and some feedback.
So what can this do? Well it can help you with some simple "multitasking"( I'll show you how in just a minute ) by handling multiple objects with the same event. It has some objects already in it that you can use, but you can ofcourse add your own external objects.
Handling multiple objects
This isn't real multitasking, but it can be very useful if you want todo some simple stuff at the same time, the only thing that can ruin this is the sleep function. So I'd suggest you'd use timers instead of that..Now lets get onto how todo some "multitasking", first of all you'd want to load the API and create a table where all of your objects are
os.loadAPI( "Object" ) -- I've named the file 'Object'
local objects = {}
Now what's the next step? well that would be adding the objects you'd want
--# Don't put '...' as arguments for the objects!
local objects = {
Object.Textbox.new( ... )
Object.Button.new( ... )
}
now this should be the final step, creating the loop that draws and handles the objects
while true do
for i = 1, #objects do
objects[i]:draw()
end
local e = { os.pullEvent() }
for i = 1, #objects do
objects[i]:handle( unpack( e ) )
end
end
Final code
os.loadAPI( "Object" )
local objects = {
Object.Textbox.new({ native_text_color = colors.white; native_background_color = colors.gray; x = 3; y = 4; placeholder = { text = "Input text"; text_color = colors.lightGray;}; width = 17; text_limit = 15; });
Object.Button.new({ y = 6, x = 43, text = "Submit", text_color = colors.white, background_color = colors.red });
}
while true do
for i = 1, #objects do
objects[i]:draw()
end
local e = { os.pullEvent() }
for i = 1, #objects do
objects[i]:handle( unpack( e ) )
end
end
Creating and loading objects
So you want to create your own objects? Here's how a basic object looks like
local object = {}
object.__index = object
object.new = function( ... )
local obj = {}
...
return setmetatable( obj, object )
end
function object:handle( ... )
local e = { ... }
...
end
function object:draw()
...
end
return object
What functions the object contains are enirely up to you, but you need to make sure that you return the object to make sure it's loaded properlyHere's how you load an object
Object.load( Object, "obj" ) -- the first argument is what the Object API has been loaded as, the second is the path of the external object
or you can load it with
Object:load( "obj" )
Download
pastebin get KDjTejqa Object
Pastebin linkThis is required for objects that are in the API
pastebin get xsnDtdK3 draw
Pastebin linkHere's the functions that this API currently adds
load( self, path )
and this is the objects that are in it
Textbox
Dropdown
Button
Text
Feel free to post suggestions on what to add and some feedback.
Edited on 28 September 2014 - 05:33 PM