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

Window API [Simple]

Started by Sp1rit, 10 January 2013 - 11:00 AM
Sp1rit #1
Posted 10 January 2013 - 12:00 PM
Hey guys,

here is a small window api I wrote for myself.

It is definitely missing some functionality which will follow.
I will update it as im working on it for myself. :rolleyes:/>/>

Code: http://pastebin.com/mLqWVEUf

Screenshot:
Spoiler

Functions:
Spoiler
window.new(x, y, width, length)
windowA:getTitle()
windowA:setTitle(title)
windowA:setLine(line, text, color) (line is relative to the window. 1 always first line, 2 second line...) (color is optional)
windowA:getLine(line)
windowA:clearLine(line)
windowA:setBackgroundColor(color)
windowA:setBorderColor(color)
windowA:setTitleColor(color)
windowA:draw()

Example (From Screenshot):
Spoiler
os.loadAPI("apis/window")
term.clear()

reactor = window.new(1,1,22,9)
reactor:setTitle("Nuklear Reaktor")
reactor:setBorderColor(colors.red)
reactor:setTitleColor(colors.green)
reactor:setLine(1, "Status: Online")
reactor:setLine(2, "Hitze:  100")
reactor:setLine(3, "Max.:   10000")
reactor:setLine(4, "Zeit:   2h 59m 59s")
reactor:setLine(5, "Output: 280 EU/t")
reactor:draw()

mfsu1 = window.new(24,1,21,6)
mfsu1:setTitle("MFSU Base")
mfsu1:setBorderColor(colors.green)
mfsu1:setTitleColor(colors.red)
mfsu1:setLine(1, "Energie: 10000000")
mfsu1:setLine(2, "Max.:	10000000")
mfsu1:draw()

mfsu2 = window.new(24,8,21,4)
mfsu2:setLine(1, "Energie: 10000000")
mfsu2:setLine(2, "Max.:	10000000")
mfsu2:draw()

os.pullEvent("key")
term.clear()
term.setCursorPos(1,1)
Changelog:
Spoiler10.01.2013
Windows now draw backgrounds (Color changeable) - allows overlaying windows.
Line colors! - windowA:setLine(line, text, color)
fuze #2
Posted 11 January 2013 - 09:18 AM
Very nice! I'll be using this in some projects for sure!
zekesonxx #3
Posted 11 January 2013 - 10:42 AM
Looks great. One note, NuClear
NeverCast #4
Posted 11 January 2013 - 11:35 AM
This is nice, I might use this code as a base for my Windows API if you don't mind, if I ever publish it I'll be sure to link back to here for credit.
Saves me writing the skeleton code myself.
Sp1rit #5
Posted 11 January 2013 - 11:41 AM
Im glad you like it.
I just updated the code.

Changes:
Windows now draw backgrounds (Color changeable) - allows overlaying windows.
Line colors! - windowA:setLine(line, text, color)



Next up i will implement custom content functions (setCursorPos, write, etc. relative to window).

Looks great. One note, NuClear
The text is german so it´s "Nuklear" for me :P/>

This is nice, I might use this code as a base for my Windows API if you don't mind, if I ever publish it I'll be sure to link back to here for credit.
Saves me writing the skeleton code myself.
No problem you are free to use it as you like. Just give some credit and we are good. ;)/>
NeverCast #6
Posted 11 January 2013 - 11:58 AM
Im glad you like it.
I just updated the code.

Changes:
Windows now draw backgrounds (Color changeable) - allows overlaying windows.
Line colors! - windowA:setLine(line, text, color)

[redacted image]

That was fast, I'll have to update again!
Can I make a request to be able to change the borders for both the sides, bottom and top? Just the character used, I would like to have "=" on the top, and also the ability to change the layout of the title?

-- Sets the title offset from the top left
w:setTitleOffset(x,y)
-- Auto centers the title, disregarding the x offset
w:setTitleLayout("center")
-- Normal layout
w:setTitleLayout("normal")
-- Changes the top border layout
-- Valid positions are top, bottom, left, right, middle ( middle is the line under the title )
w:setBorderCharacter("top", "=") 
-- Removes the line under the title
-- Alternatively setting the border character to "" would remove it entirely so even the "+" characters on the sides wouldn't show up.
w:disableTitleUnderline(true)

These are all changes I was going to add to the API anyway, so if you wouldn't mind having them in there, that'd be great.

This is nice, I might use this code as a base for my Windows API if you don't mind, if I ever publish it I'll be sure to link back to here for credit.
Saves me writing the skeleton code myself.
No problem you are free to use it as you like. Just give some credit and we are good. ;)/>

Will do, Thanks :)/>