Posted 21 December 2014 - 03:39 AM
Hey dare, I've been working on this api I call structure and I run out of things I could add to it that would be useful. Anyone got ideas of things I could add to it?
here's what I have so far
I use it to shorten code, like this
to this
it's creates objects too so you can change things like OBJ1.x to something else then redraw and it will change positions
it also has even listeners too, they work like this
in order for the eventListener to work you need to use the loop
heres a sample little snippet
here's what I have so far
function create(this)
--checks
if this.ospull then
os.pullEvent = os.pullEventRaw
end
--modules
this.draw = {index={}};
this.loop = {};
this.event = {listeners={}};
this.timer = {tims={}};
--timer methods
function this.timer.add(sec,func)
TinyTim = os.startTimer(sec)
table.insert(this.timer.tims,TinyTim,func)
end
--draw methods
function this.draw.rect(drawOBJ)
function drawOBJ.draw()
local y = drawOBJ.y
for i = 1,drawOBJ.height do
paintutils.drawLine(drawOBJ.x,y,(drawOBJ.x+drawOBJ.width),y,drawOBJ.backgroundColor)
y = y + 1
end
end
return drawOBJ
end
function this.draw.line(drawOBJ)
function drawOBJ.draw()
paintutils.drawLine(drawOBJ.startX,drawOBJ.startY,drawOBJ.endX,drawOBJ.endY,drawOBJ.backgroundColor)
end
return drawOBJ
end
function this.draw.text(drawOBJ)
function drawOBJ.draw()
term.setCursorPos(drawOBJ.x,drawOBJ.y)
write(drawOBJ.text)
end
return drawOBJ
end
function this.draw.addOBJ(obj,index)
table.insert(this.draw.index,index,obj)
end
function this.draw.removeOBJ(index)
this.draw.index[index] = nil
end
function this.draw.clear()
this.draw.index = {}
end
function this.draw.update(clr)
if clr then
term.setBackgroundColor(this.backgroundColor)
term.clear()
term.setCursorPos(1,1)
end
for i = 1,#this.draw.index do
this.draw.index[i].draw()
end
end
--event methods
function this.event.addListener(event,func)
if this.event.listeners[event] then
table.insert(this.event.listeners[event],func)
else
this.event.listeners[event] = {}
table.insert(this.event.listeners[event],func)
end
end
--loop methods
function this.loop.start()
this.running = true
if this.load then
this.load()
end
os.startTimer(.1)
while this.running do
e,p1,p2,p3,p4,p5 = os.pullEvent()
this.draw.update(true)
if(this.event.listeners[e])then
for i = 1,#this.event.listeners[e] do
this.event.listeners[e][i](e,p1,p2,p3,p4,p5)
end
end
if this.update then
this.update()
end
end
end
function this.loop.stop()
this.running = false
end
this.event.addListener("timer",function() os.startTimer(.1) end)
this.event.addListener("timer",function(e,p1) if(this.timer.tims[p1])then this.timer.tims[p1]();this.timer.tims[p1]=nil; end end)
return this;
end
I use it to shorten code, like this
y = 2
for i = 1, do
paintutils.drawLine(2,y,4,y,colors.red)
y = y + 1
end
y = 6
for i = 1, do
paintutils.drawLine(7,y,9,y,colors.red)
y = y + 1
end
to this
game = create({backgroundColor=colors.black;})
OBJ1 = game.draw.rect({x=2,y=2,width=4,height=4,backgroundColor=colors.red})
OBJ2 = game.draw.rect({x=7,y=7,width=4,height=4,backgroundColor=colors.red})
game.draw.addOBJ(OBJ1,1)
game.draw.addOBJ(OBJ2,2)
game.draw.update(true)
it's creates objects too so you can change things like OBJ1.x to something else then redraw and it will change positions
it also has even listeners too, they work like this
game = create({backgroundColor=colors.black;})
game.event.addListener("mouse_click",function() print("Hello") end)
game.loop.start()
in order for the eventListener to work you need to use the loop
heres a sample little snippet
os.loadAPI("structure")-- load structure
game = structure.create({backgroundColor=colors.black;clr=true}) --create a game object
--created a load method, this will be ran once before the loop starts
function game.load()
OBJ1 = game.draw.rect({x=2,y=2,width=4,height=4,backgroundColor=colors.red}) --create a rectangle object
OBJ2 = game.draw.rect({x=7,y=7,width=4,height=4,backgroundColor=colors.red}) --create second a rectangle object
time = game.draw.text({x=1,y=1,text=textutils.formatTime(os.time(), true) }) --create a text object
--added both objects to the list
game.draw.addOBJ(OBJ1,3)
game.draw.addOBJ(OBJ2,2)
game.draw.addOBJ(time,1)
--added a listener for keybroad press
game.event.addListener("key",function() OBJ2.x = OBJ2.x + 1 end)
end
--this will be ran everytime the loop, well loops
function game.update()
time.text = textutils.formatTime(os.time(), true)
end
game.loop.start()--starts the loop
Edited on 21 December 2014 - 02:40 AM