Update nGUI.lua
This commit is contained in:
143
nGUI.lua
143
nGUI.lua
@@ -23,7 +23,7 @@ local activeFrame
|
||||
local frames = {}
|
||||
|
||||
frame.new = function(name, screen)
|
||||
local newElement = {name=name, fWindow = window.create((screen~=nil and screen or term.native()),1,1,w,h), objects={},bgcolor = colors.black, fgcolor=colors.white}
|
||||
local newElement = {name=name, fWindow = window.create((screen~=nil and screen or term.native()),1,1,w,h), objects={},bgcolor = colors.black, fgcolor=colors.white, title="", titlebgcolor = colors.lightBlue, titlefgcolor = colors.black, align="left"}
|
||||
if(frames[name] == nil)then
|
||||
frames[name] = newElement
|
||||
newElement.fWindow.setVisible(false)
|
||||
@@ -40,6 +40,32 @@ function frame:getName()
|
||||
return self.name
|
||||
end
|
||||
|
||||
function frame:setBackground(color)
|
||||
self.bgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:setForeground(color)
|
||||
self.fgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:setTitle(title,fgcolor,bgcolor)
|
||||
self.title=title
|
||||
if(fgcolor~=nil)then self.titlefgcolor = fgcolor end
|
||||
if(bgcolor~=nil)then self.titlebgcolor = bgcolor end
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:setTextAlign(align)
|
||||
self.textAlign = align
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:show()
|
||||
self.draw = true
|
||||
self.changed = true
|
||||
@@ -50,22 +76,25 @@ function frame:show()
|
||||
activeFrame:hide()
|
||||
end
|
||||
activeFrame = self
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:hide()
|
||||
self.fWindow.setVisible(false)
|
||||
self.changed = true
|
||||
self.draw = false
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:addTimer(name)
|
||||
local newElement = {frame = self, timer=5, repeats=0}
|
||||
local newElement = {name=name,frame = self, timer=5, repeats=0}
|
||||
if(self.objects[name] == nil)then
|
||||
self.objects[name] = newElement
|
||||
return setmetatable(newElement, timer);
|
||||
else
|
||||
return nil, "id "..name.." already exists";
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function timer:setTime(time, repeats)
|
||||
@@ -75,6 +104,7 @@ function timer:setTime(time, repeats)
|
||||
else
|
||||
self.repeats = -1
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function timer:start(time, repeats)
|
||||
@@ -82,25 +112,29 @@ function timer:start(time, repeats)
|
||||
if(time~=nil)then self.timer = time end
|
||||
if(repeats~=nil)then self.repeats = repeats end
|
||||
self.timeObj = os.startTimer(self.timer)
|
||||
return self
|
||||
end
|
||||
|
||||
function timer:cancel()
|
||||
self.active = false
|
||||
os.cancelTimer(self.timeObj)
|
||||
return self
|
||||
end
|
||||
|
||||
function timer:onCall(func)
|
||||
self.call = func
|
||||
return self
|
||||
end
|
||||
|
||||
function frame:addCheckbox(name)
|
||||
local newElement = {frame = self, x=1, y=1, bgcolor=colors.lightBlue, fgcolor=colors.black,symbol="X",changed=true,checked=false,drawCalls=0}
|
||||
local newElement = {name=name,frame = self, x=1, y=1, w=1, h=1, bgcolor=colors.lightBlue, fgcolor=colors.black,symbol="X",changed=true,checked=false,drawCalls=0}
|
||||
if(self.objects[name] == nil)then
|
||||
self.objects[name] = newElement
|
||||
return setmetatable(newElement, checkbox);
|
||||
else
|
||||
return nil, "id "..name.." already exists";
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:show()
|
||||
@@ -108,6 +142,7 @@ function checkbox:show()
|
||||
self.draw = true
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:hide()
|
||||
@@ -115,27 +150,37 @@ function checkbox:hide()
|
||||
self.draw = false
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:setPosition(x,y)
|
||||
self.x = tonumber(x)
|
||||
self.y = tonumber(y)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:setBackground(color)
|
||||
self.bgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:setForeground(color)
|
||||
self.fgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:setSymbol(symbol)
|
||||
self.symbol = string.sub(symbol,1,1)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:onClick(func)
|
||||
self.clickFunc = func
|
||||
return self
|
||||
end
|
||||
|
||||
function checkbox:drawObject()
|
||||
@@ -154,13 +199,14 @@ function checkbox:drawObject()
|
||||
end
|
||||
|
||||
function frame:addLabel(name)
|
||||
local newElement = {frame = self, x=1, y=1, bgcolor=self.bgcolor, fgcolor=self.fgcolor,text="Label",changed=true,drawCalls=0}
|
||||
local newElement = {name=name,frame = self, x=1, y=1,w=1,h=1, bgcolor=self.bgcolor, fgcolor=self.fgcolor,text="Label",changed=true,drawCalls=0}
|
||||
if(self.objects[name] == nil)then
|
||||
self.objects[name] = newElement
|
||||
return setmetatable(newElement, label);
|
||||
else
|
||||
return nil, "id "..name.." already exists";
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function label:show()
|
||||
@@ -168,6 +214,7 @@ function label:show()
|
||||
self.draw = true
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function label:hide()
|
||||
@@ -175,27 +222,38 @@ function label:hide()
|
||||
self.draw = false
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function label:setPosition(x,y)
|
||||
self.x = tonumber(x)
|
||||
self.y = tonumber(y)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function label:setBackground(color)
|
||||
self.bgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function label:setForeground(color)
|
||||
self.fgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function label:setText(text)
|
||||
self.text = text
|
||||
self.w = string.len(text)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function label:onClick(func)
|
||||
self.clickFunc = func
|
||||
return self
|
||||
end
|
||||
|
||||
function label:drawObject()
|
||||
@@ -210,13 +268,14 @@ function label:drawObject()
|
||||
end
|
||||
|
||||
function frame:addTextbox(name)
|
||||
local newElement = {frame = self, x=1, y=1, bgcolor=colors.lightBlue, fgcolor=colors.black,w=3,h=1,text="",changed=true,drawCalls=0}
|
||||
local newElement = {name=name,frame = self, x=1, y=1, bgcolor=colors.lightBlue, fgcolor=colors.black,w=3,h=1,text="",changed=true,drawCalls=0}
|
||||
if(self.objects[name] == nil)then
|
||||
self.objects[name] = newElement
|
||||
return setmetatable(newElement, textbox);
|
||||
else
|
||||
return nil, "id "..name.." already exists";
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:show()
|
||||
@@ -224,6 +283,7 @@ function textbox:show()
|
||||
self.draw = true
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:hide()
|
||||
@@ -231,41 +291,49 @@ function textbox:hide()
|
||||
self.draw = false
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:setPosition(x,y)
|
||||
self.x = tonumber(x)
|
||||
self.y = tonumber(y)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:setSize(w,l)
|
||||
self.w = tonumber(w)
|
||||
self.l = tonumber(l)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:setBackground(color)
|
||||
self.bgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:setForeground(color)
|
||||
self.fgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:setText(text)
|
||||
self.text = tostring(text)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:onChange(func)
|
||||
self.changeFunc = func
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:onClick(func)
|
||||
self.clickFunc = func
|
||||
return self
|
||||
end
|
||||
|
||||
function textbox:drawObject()
|
||||
@@ -288,13 +356,14 @@ function textbox:drawObject()
|
||||
end
|
||||
|
||||
function frame:addButton(name)
|
||||
local newElement = {frame = self, x=1, y=1, bgcolor=colors.lightBlue, fgcolor=colors.black,w=3,h=1,text="Click",align="center",changed=true,drawCalls=0}
|
||||
local newElement = {name=name,frame = self, x=1, y=1, bgcolor=colors.lightBlue, fgcolor=colors.black,w=3,h=1,text="Click",align="center",changed=true,drawCalls=0}
|
||||
if(self.objects[name] == nil)then
|
||||
self.objects[name] = newElement
|
||||
return setmetatable(newElement, button);
|
||||
else
|
||||
return nil, "id "..name.." already exists";
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function button:show()
|
||||
@@ -302,6 +371,7 @@ function button:show()
|
||||
self.draw = true
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function button:hide()
|
||||
@@ -309,55 +379,63 @@ function button:hide()
|
||||
self.draw = false
|
||||
self.changed = true
|
||||
end
|
||||
return self
|
||||
end
|
||||
|
||||
function button:setPosition(x,y)
|
||||
self.x = tonumber(x)
|
||||
self.y = tonumber(y)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function button:setSize(w,l)
|
||||
self.w = tonumber(w)
|
||||
self.l = tonumber(l)
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function button:setAlign(align)
|
||||
self.align = align
|
||||
function button:setTextAlign(align)
|
||||
self.textAlign = align
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function button:setBackground(color)
|
||||
self.bgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function button:setForeground(color)
|
||||
self.fgcolor = color
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function button:setText(text)
|
||||
self.text = text
|
||||
self.changed = true
|
||||
return self
|
||||
end
|
||||
|
||||
function button:onClick(func)
|
||||
self.clickFunc = func
|
||||
return self
|
||||
end
|
||||
|
||||
function button:drawObject()
|
||||
if(self.draw)then
|
||||
local text = string.sub(self.text, 1, self.w)
|
||||
local n = self.w-string.len(text)
|
||||
if(self.align=="left")then
|
||||
if(self.textAlign=="left")then
|
||||
text = text..string.rep(" ", n)
|
||||
end
|
||||
if(self.align=="right")then
|
||||
if(self.textAlign=="right")then
|
||||
text = string.rep(" ", n)..text
|
||||
end
|
||||
if(self.align=="center")then
|
||||
if(self.textAlign=="center")then
|
||||
text = string.rep(" ", math.floor(n/2))..text..string.rep(" ", math.floor(n/2))
|
||||
text = text..(string.len(text) < self.w and " " or "")
|
||||
end
|
||||
@@ -374,7 +452,7 @@ end
|
||||
local function checkMouseClick(clicktype,x,y)
|
||||
activeFrame.textboxActive = false
|
||||
for k,v in pairs(activeFrame.objects)do
|
||||
if(v.__type=="Button")then
|
||||
if not(v.__type=="Timer")then
|
||||
if(v.x<=x)and(v.x+v.w>x)and(v.y<=y)and(v.y+v.h>y)then
|
||||
if(v.clickFunc~=nil)then
|
||||
v.clickFunc(v,clicktype)
|
||||
@@ -389,18 +467,12 @@ local function checkMouseClick(clicktype,x,y)
|
||||
v.frame.cursorX = v.x+(string.len(v.text) < v.w and string.len(v.text) or v.w-1)
|
||||
v.frame.cursorY = v.y
|
||||
v.frame.fWindow.setCursorBlink(true)
|
||||
if(v.clickFunc~=nil)then
|
||||
v.clickFunc(v,clicktype)
|
||||
end
|
||||
end
|
||||
end
|
||||
if(v.__type=="Checkbox")then
|
||||
if(v.x==x)and(v.y==y)then
|
||||
v.checked = not v.checked
|
||||
v.changed = true
|
||||
if(v.clickFunc~=nil)then
|
||||
v.clickFunc(v,clicktype)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -416,7 +488,7 @@ local function checkTimer(timeObject)
|
||||
v.call(v)
|
||||
if(v.repeats~=0)then
|
||||
v.timeObj = os.startTimer(v.timer)
|
||||
v.repeats = v.repeats > 0 and v.repeats-1
|
||||
v.repeats = (v.repeats > 0 and v.repeats-1 or v.repeats)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -426,6 +498,25 @@ end
|
||||
local function drawObjects()
|
||||
if(activeFrame.draw)then
|
||||
activeFrame.fWindow.clear()
|
||||
|
||||
if(activeFrame.title~="")then
|
||||
local text = string.sub(activeFrame.title, 1, w)
|
||||
local n = w-string.len(text)
|
||||
if(activeFrame.textAlign=="left")then
|
||||
text = text..string.rep(" ", n)
|
||||
end
|
||||
if(activeFrame.textAlign=="right")then
|
||||
text = string.rep(" ", n)..text
|
||||
end
|
||||
if(activeFrame.textAlign=="center")then
|
||||
text = string.rep(" ", math.floor(n/2))..text..string.rep(" ", math.floor(n/2))
|
||||
text = text..(string.len(text) < w and " " or "")
|
||||
end
|
||||
activeFrame.fWindow.setBackgroundColor(activeFrame.titlebgcolor)
|
||||
activeFrame.fWindow.setTextColor(activeFrame.titlefgcolor)
|
||||
activeFrame.fWindow.setCursorPos(1,1)
|
||||
activeFrame.fWindow.write(text)
|
||||
end
|
||||
for k,v in pairs(activeFrame.objects)do
|
||||
if(v.draw~=nil)then
|
||||
v:drawObject()
|
||||
@@ -481,7 +572,7 @@ function frame.startUpdate()
|
||||
checkForChangedObjects()
|
||||
while true do
|
||||
local event, p1,p2,p3 = os.pullEvent()
|
||||
debug(event,p1,p2,p3)
|
||||
--debug(event,p1,p2,p3)
|
||||
if(event=="mouse_click")then
|
||||
checkMouseClick(p1,p2,p3)
|
||||
end
|
||||
@@ -498,17 +589,23 @@ end
|
||||
|
||||
function debug(...)
|
||||
local args = {...}
|
||||
activeFrame.debugLabel:setPosition(2,h)
|
||||
activeFrame.debugLabel:setPosition(1,h)
|
||||
local str = "[Debug] "
|
||||
for k,v in pairs(args)do
|
||||
str = str..v..(#args~=k and ", " or "")
|
||||
str = str..tostring(v)..(#args~=k and ", " or "")
|
||||
end
|
||||
activeFrame.debugLabel:setText(str)
|
||||
activeFrame.debugLabel:show()
|
||||
end
|
||||
|
||||
function frame.get(name)
|
||||
frame.debug = debug
|
||||
|
||||
function frame.get(name)
|
||||
return frames[name];
|
||||
end
|
||||
|
||||
function frame.getActiveFrame()
|
||||
return activeFrame
|
||||
end
|
||||
|
||||
return frame;
|
||||
Reference in New Issue
Block a user