Created
February 9, 2013 23:39
-
-
Save kane-thornwyrd/4747600 to your computer and use it in GitHub Desktop.
- kGUI : a Graphical User Interface Library for ComputerCraft - oop : an Object Oriented Programming Library for lua with code borrowed from the official Wiki and tuned for my needs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if not oop then os.loadAPI('oop') end | |
if not term then os.loadAPI('term') end | |
if not colors and term.isColour() | |
then os.loadAPI('colors') | |
else colors = { | |
white = 1, | |
orange = 2, | |
magenta = 4, | |
lightBlue = 8, | |
yellow = 16, | |
lime = 32, | |
pink = 64, | |
gray = 128, | |
lightGray = 256, | |
cyan = 512, | |
purple = 1024, | |
blue = 2048, | |
brown = 4096, | |
green = 8192, | |
red = 16384, | |
black = 32768, | |
} | |
end | |
Pixel = oop.inheritsFrom( nil, | |
{ | |
character = ' ', | |
colors = { | |
front = colors.purple, | |
back = colors.black, | |
}, | |
events = {}, | |
out = function(self, screen) | |
screen:setColors( | |
self.colors.front,self.colors.back | |
) | |
screen.outputPeripheral.write(self.character) | |
screen:setColors() | |
end, | |
}) | |
Screen = oop.inheritsFrom( nil, | |
{ | |
outputPeripheral = term, | |
width, height = term.getSize(), | |
backgroundColor = colors.black, | |
foregroundColor = colors.lightGray, | |
childs = {}, | |
pixels = {}, | |
---Function: addChild | |
--[[-- | |
Params: | |
- child: guiBaseElement | |
Return: void | |
--]]-- | |
addChild = function(self, childE) | |
table.insert(self.childs, childE) | |
return self | |
end, | |
---Function: addChilds | |
--[[-- | |
Params: | |
- child: guiBaseElement | |
Return: void | |
--]]-- | |
addChilds = function(self, childs) | |
for k, child in pairs(childs) do | |
self:addChild(child) | |
end | |
return self | |
end, | |
setSizes = function(self, w, h) | |
device_w, device_h | |
= self.outputPeripheral.getSize() | |
w = w or device_w | |
h = h or device_h | |
self.width, self.height = w, h | |
return self | |
end, | |
---Function: cleanReset | |
--[[-- | |
Params: void | |
Return: void | |
--]]-- | |
cleanReset = function(self) | |
self.outputPeripheral.clear() | |
self.outputPeripheral.setCursorPos(1, 1) | |
return self | |
end, | |
---Function: setColors | |
--[[-- | |
Params: | |
- foreground: the fgd color. | |
- background: the bgd color. | |
Return: void | |
--]]-- | |
setColors = function(self, foreground, background) | |
if self.outputPeripheral.isColour() then | |
foreground = foreground | |
or Screen.foregroundColor | |
background = background | |
or Screen.backgroundColor | |
self.outputPeripheral.setTextColor(foreground) | |
self.outputPeripheral.setBackgroundColor(background) | |
end | |
return self | |
end, | |
---Function: render | |
--[[-- | |
Params: void | |
Return: void | |
--]]-- | |
render = function(self) | |
for k, child in ipairs(self.childs) do | |
child:constructRendering(self) | |
end | |
for x, v in pairs(self.pixels) do | |
if type(self.pixels[x]) ~= 'table' | |
then return end | |
for y, d in pairs(self.pixels[x]) do | |
self.outputPeripheral.setCursorPos(x,y) | |
for k, pxl in pairs(d) do | |
pxl:out(self) | |
end | |
end | |
end | |
self.outputPeripheral.setCursorPos(1, self.height) | |
return self | |
end, | |
}) | |
guiBaseElement = oop.inheritsFrom( nil, | |
{ | |
width = 0, | |
height = 0, | |
childs = {}, | |
positionX = 1, | |
positionY = 1, | |
backgroundColor = colors.black, | |
foregroundColor = colors.lightGray, | |
screen = nil, | |
getRenderedPosition = function(self) | |
local screenX, screenY | |
= self.screen.width, self.screen.height | |
local x, y = 0, 0 | |
if type(self.width) == 'boolean' then | |
self.width = self.screen.width | |
end | |
if type(self.height) == 'boolean' then | |
self.height = self.screen.height | |
end | |
if type(self.positionX) == 'boolean' then | |
if self.positionX then | |
self.positionX = self.screen.width | |
else | |
self.positionX = 0 | |
end | |
end | |
if type(self.positionY) == 'boolean' then | |
if self.positionY then | |
self.positionY = self.screen.height | |
else | |
self.positionY = 0 | |
end | |
end | |
if self.width + self.positionX < screenX then | |
x = self.positionX | |
else | |
x = screenX - self.width | |
end | |
if self.height + self.positionY < screenY then | |
y = self.positionY | |
else | |
y = screenY - self.height | |
end | |
if x < 0 then x = 0 end | |
if y < 0 then y = 0 end | |
return x, y | |
end, | |
setRenderOutput = function() | |
end, | |
constructRendering = function(self, screen) | |
self.screen = screen | |
self.positionX, self.positionY | |
= self:getRenderedPosition() | |
--print('x: ',self.positionX,' | y: ',self.positionY) | |
for pw=1,self.width do | |
x = pw + self.positionX | |
for ph=1,self.height do | |
y = ph + self.positionY | |
if type(self.screen.pixels[x]) ~= 'table' | |
then self.screen.pixels[x] = {} end | |
if type(self.screen.pixels[x][y]) ~= 'table' | |
then self.screen.pixels[x][y] = {} end | |
table.insert(self.screen.pixels[x][y], Pixel:create()) | |
end | |
end | |
end, | |
}) | |
textElement = oop.inheritsFrom(guiBaseElement, | |
{ | |
textPosX = false, | |
textPosY = false, | |
text = '', | |
}) | |
button = oop.inheritsFrom(textElement,{ | |
constructRendering = function(self, screen) | |
self.screen = screen | |
self.positionX, self.positionY | |
= self:getRenderedPosition() | |
--print('x: ',self.positionX,' | y: ',self.positionY) | |
for pw=1,self.width do | |
x = pw + self.positionX | |
for ph=1,self.height do | |
y = ph + self.positionY | |
if type(self.screen.pixels[x]) ~= 'table' | |
then self.screen.pixels[x] = {} end | |
if type(self.screen.pixels[x][y]) ~= 'table' | |
then self.screen.pixels[x][y] = {} end | |
pixel = Pixel:create( | |
{ | |
character = '=', | |
colors = { | |
front = self.foregroundColor, | |
back = self.backgroundColor | |
}, | |
}) | |
table.insert(self.screen.pixels[x][y], pixel) | |
end | |
end | |
end, | |
}) | |
Screen | |
:create({outputPeripheral = peripheral.wrap("right")}) | |
:cleanReset() | |
:setSizes() | |
:addChilds({ | |
button_1 = button | |
:create({ | |
text = 'Mon Bouton', | |
width = false, | |
height = 1, | |
positionX = 35, | |
positionY = 0, | |
backgroundColor = colors.blue, | |
foregroundColor = colors.blue, | |
}), | |
button_2 = button | |
:create({ | |
text = 'Mon Bouton 2', | |
width = 5, | |
height = 3, | |
positionX = 20, | |
positionY = 10, | |
backgroundColor = colors.red, | |
foregroundColor = colors.white, | |
}), | |
button_3 = button | |
:create({ | |
text = 'Mon Bouton', | |
width = true, | |
height = 1, | |
positionX = 1, | |
positionY = true, | |
backgroundColor = colors.grey, | |
foregroundColor = colors.lightGray, | |
}), | |
}) | |
:render() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Create a new class that inherits from a base class | |
-- | |
function inheritsFrom( baseClass, baseProperties ) | |
local new_class = {} | |
local class_mt = { __index = new_class } | |
if type(baseProperties) == 'table' then | |
for k, v in pairs(baseProperties) do | |
if k == 1 then return | |
else | |
new_class[k] = v | |
end | |
end | |
end | |
function new_class:create( params ) | |
local newinst = {} | |
if type(params) == 'table' then | |
for k, v in pairs(params) do | |
newinst[k] = v | |
end | |
end | |
setmetatable( newinst, class_mt ) | |
return newinst | |
end | |
if nil ~= baseClass then | |
setmetatable( new_class, { __index = baseClass } ) | |
end | |
-- Implementation of additional OO properties starts here -- | |
-- Return the class object of the instance | |
function new_class:class() | |
return new_class | |
end | |
-- Return the super class object of the instance | |
function new_class:superClass() | |
return baseClass | |
end | |
-- Return true if the caller is an instance of theClass | |
function new_class:isa( theClass ) | |
local b_isa = false | |
local cur_class = new_class | |
while ( nil ~= cur_class ) and ( false == b_isa ) do | |
if cur_class == theClass then | |
b_isa = true | |
else | |
cur_class = cur_class:superClass() | |
end | |
end | |
return b_isa | |
end | |
return new_class | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment