diff --git a/Basalt/Frame.lua b/Basalt/Frame.lua index e8ef02f..4f40369 100644 --- a/Basalt/Frame.lua +++ b/Basalt/Frame.lua @@ -19,6 +19,8 @@ return function(name, parent, pTerm, basalt) local object = {} local variables = {} local theme = {} + local dynamicValues = {} + local dynValueId = 0 local termObject = pTerm or term.current() local monSide = "" @@ -65,37 +67,11 @@ return function(name, parent, pTerm, basalt) end end - local function duplicateTerm(term1, term2) - local both = {} - setmetatable(both, { - __index = function(_, k) - if (type(term1[k]) == "function") then - return function(...) - pcall(term1[k], ...) - return term2[k](...) - end - else - return term1[k] - end - end, - __call = function(_, f, ...) - pcall(term2[f], ...) - return term1[f](...) - end, - __newindex = function(_, k, v) - term1[k] = v - term2[k] = v - end - }) - return both - end - if (parent ~= nil) then base.parent = parent base.width, base.height = parent:getSize() base.bgColor = parent:getTheme("FrameBG") base.fgColor = parent:getTheme("FrameText") - print(parent:getTheme("FrameBG")) else base.width, base.height = termObject.getSize() base.bgColor = basalt.getTheme("BasaltBG") @@ -168,6 +144,89 @@ return function(name, parent, pTerm, basalt) return false end + local function stringToNumber(str) + local ok, err = pcall(load("return " .. str)) + if not(ok)then error(str.." is not a valid dynamic code") end + return load("return " .. str)() + end + + local function newDynamicValue(_, obj, str) + dynValueId = dynValueId + 1 + dynamicValues[dynValueId] = {0, str, {}, obj, dynValueId} + return dynamicValues[dynValueId] + end + + local function dynValueGetObjects(obj, str) + local names = {} + local t = {} + for v in str:gmatch("%a+%.x") do + local name = v:gsub("%.x", "") + if(name~="self")and(name~="parent")then + table.insert(names, name) end + end + for v in str:gmatch("%w+%.y") do + local name = v:gsub("%.y", "") + if(name~="self")and(name~="parent")then table.insert(names, name) end + end + for v in str:gmatch("%a+%.w") do + local name = v:gsub("%.w", "") + if(name~="self")and(name~="parent")then + table.insert(names, name) + + end + end + for v in str:gmatch("%a+%.h") do + local name = v:gsub("%.h", "") + if(name~="self")and(name~="parent")then + table.insert(names, name) end + end + for k,v in pairs(names)do + t[v] = getObject(v) + if(t[v]==nil)then + error("Dynamic Values - unable to find object "..v) + end + end + t["self"] = obj + t["parent"] = obj:getParent() + return t + end + + local function dynValueObjectToNumber(str, objList) + local newStr = str + for v in str:gmatch("%w+%.x") do + newStr = newStr:gsub(v, objList[v:gsub("%.x", "")]:getX()) + end + for v in str:gmatch("%w+%.y") do + newStr = newStr:gsub(v, objList[v:gsub("%.y", "")]:getY()) + end + for v in str:gmatch("%w+%.w") do + newStr = newStr:gsub(v, objList[v:gsub("%.w", "")]:getWidth()) + end + for v in str:gmatch("%w+%.h") do + newStr = newStr:gsub(v, objList[v:gsub("%.h", "")]:getHeight()) + end + return newStr + end + + + local function recalculateDynamicValues() + if(#dynamicValues>0)then + for n=1,dynValueId do + if(dynamicValues[n]~=nil)then + local numberStr + if(#dynamicValues[n][3]<=0)then dynamicValues[n][3] = dynValueGetObjects(dynamicValues[n][4], dynamicValues[n][2]) end + numberStr = dynValueObjectToNumber(dynamicValues[n][2], dynamicValues[n][3]) + dynamicValues[n][1] = stringToNumber(numberStr) + end + end + end + end + + local function getDynamicValue(id) + return dynamicValues[id][1] + end + + object = { barActive = false, barBackground = colors.gray, @@ -176,6 +235,10 @@ return function(name, parent, pTerm, basalt) barTextAlign = "left", isMoveable = false, + newDynamicValue = newDynamicValue, + recalculateDynamicValues = recalculateDynamicValues, + getDynamicValue = getDynamicValue, + getType = function(self) return objectType end; @@ -243,10 +306,14 @@ return function(name, parent, pTerm, basalt) return self end; - getOffset = function(self) -- internal + getOffset = function(self) return xOffset, yOffset end; + getOffsetInternal = function(self) -- internal + return xOffset < 0 and math.abs(xOffset) or -xOffset, yOffset < 0 and math.abs(yOffset) or -yOffset + end; + removeFocusedObject = function(self) if (basalt.getFocusedObject() ~= nil) then basalt.getFocusedObject():loseFocusHandler() @@ -264,7 +331,7 @@ return function(name, parent, pTerm, basalt) local obx, oby = self:getAnchorPosition() self.parent:setCursor(_blink or false, (_xCursor or 0)+obx-1, (_yCursor or 0)+oby-1, color or cursorColor) else - local obx, oby = self:getAbsolutePosition(self:getAnchorPosition()) + local obx, oby = self:getAbsolutePosition(self:getAnchorPosition(self:getX(), self:getY(), true)) cursorBlink = _blink or false if (_xCursor ~= nil) then xCursor = obx + _xCursor - 1 @@ -361,7 +428,6 @@ return function(name, parent, pTerm, basalt) setValuesByXMLData = function(self, data) base.setValuesByXMLData(self, data) - if(xmlValue("moveable", data)~=nil)then if(xmlValue("moveable", data))then self:setMoveable(true) end end if(xmlValue("scrollable", data)~=nil)then if(xmlValue("scrollable", data))then self:setScrollable(true) end end if(xmlValue("monitor", data)~=nil)then self:setMonitor(xmlValue("monitor", data)):show() end @@ -378,14 +444,19 @@ return function(name, parent, pTerm, basalt) if(xmlValue("minScroll", data)~=nil)then self:setMaxScroll(xmlValue("minScroll", data)) end if(xmlValue("importantScroll", data)~=nil)then self:setImportantScroll(xmlValue("importantScroll", data)) end - - for k,v in pairs(_OBJECTS)do - if(k~="Animation")then - addXMLObjectType(data[string.lower(k)], self["add"..k], self) + local objectList = data:children() + + for k,v in pairs(objectList)do + if(v.___name~="animation")then + local name = v.___name:gsub("^%l", string.upper) + if(_OBJECTS[name]~=nil)then + addXMLObjectType(v, self["add"..name], self) + end end end - addXMLObjectType(data["animation"], self.addAnimation, self) + addXMLObjectType(data["frame"], self.addFrame, self) + addXMLObjectType(data["animation"], self.addAnimation, self) return self end, @@ -545,12 +616,12 @@ return function(name, parent, pTerm, basalt) end; mouseHandler = function(self, event, button, x, y) - local xO, yO = self:getOffset() - xO = xO < 0 and math.abs(xO) or -xO - yO = yO < 0 and math.abs(yO) or -yO if (self.drag) then + local xO, yO = self.parent:getOffset() + xO = xO < 0 and math.abs(xO) or -xO + yO = yO < 0 and math.abs(yO) or -yO if (event == "mouse_drag") then - local parentX = 1; + local parentX = 1 local parentY = 1 if (self.parent ~= nil) then parentX, parentY = self.parent:getAbsolutePosition(self.parent:getAnchorPosition()) diff --git a/Basalt/Object.lua b/Basalt/Object.lua index 4e6d326..3e6725d 100644 --- a/Basalt/Object.lua +++ b/Basalt/Object.lua @@ -1,5 +1,4 @@ local basaltEvent = require("basaltEvent") -local dynValue = require("dynamicValues") local utils = require("utils") local split = utils.splitString local numberFromString = utils.numberFromString @@ -23,6 +22,9 @@ return function(name) local shadowColor = colors.black local borderColor = colors.black + local isEnabled = true + local isDragging = false + local dragStartX, dragStartY, dragXOffset, dragYOffset = 0, 0, 0, 0 local visualsChanged = true @@ -50,16 +52,48 @@ return function(name) return self end; + enable = function(self) + isEnabled = true + return self + end; + + disable = function(self) + isEnabled = false + return self + end; + + generateXMLEventFunction = function(self, func, val) + local createF = function(str) + if(str:sub(1,1)=="#")then + local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len())) + if(o~=nil)and(o.internalObjetCall~=nil)then + func(self,function()o:internalObjetCall()end) + end + else + func(self,self:getBaseFrame():getVariable(str)) + end + end + if(type(val)=="string")then + createF(val) + elseif(type(val)=="table")then + for k,v in pairs(val)do + createF(v) + end + end + return self + end, + setValuesByXMLData = function(self, data) local baseFrame = self:getBaseFrame() - if(xmlValue("x", data)~=nil)then self:setPosition(xmlValue("x", data), self:getY()) end - if(xmlValue("y", data)~=nil)then self:setPosition(self:getX(), xmlValue("y", data)) end - if(xmlValue("width", data)~=nil)then self:setSize(xmlValue("width", data), self:getHeight()) end - if(xmlValue("height", data)~=nil)then self:setSize(self:getWidth(), xmlValue("height", data)) end + if(xmlValue("x", data)~=nil)then self:setPosition(xmlValue("x", data), self.y) end + if(xmlValue("y", data)~=nil)then self:setPosition(self.x, xmlValue("y", data)) end + if(xmlValue("width", data)~=nil)then self:setSize(xmlValue("width", data), self.height) end + if(xmlValue("height", data)~=nil)then self:setSize(self.width, xmlValue("height", data)) end if(xmlValue("bg", data)~=nil)then self:setBackground(colors[xmlValue("bg", data)]) end if(xmlValue("fg", data)~=nil)then self:setForeground(colors[xmlValue("fg", data)]) end if(xmlValue("value", data)~=nil)then self:setValue(colors[xmlValue("value", data)]) end if(xmlValue("visible", data)~=nil)then if(xmlValue("visible", data))then self:show() else self:hide() end end + if(xmlValue("enabled", data)~=nil)then if(xmlValue("enabled", data))then self:enable() else self:disable() end end if(xmlValue("zIndex", data)~=nil)then self:setZIndex(xmlValue("zIndex", data)) end if(xmlValue("anchor", data)~=nil)then self:setAnchor(xmlValue("anchor", data)) end if(xmlValue("shadow", data)~=nil)then if(xmlValue("shadow", data))then self:showShadow(true) end end @@ -71,20 +105,20 @@ return function(name) if(xmlValue("borderBottom", data)~=nil)then if(xmlValue("borderBottom", data))then borderBottom = true else borderBottom = false end end if(xmlValue("borderColor", data)~=nil)then self:setBorder(colors[xmlValue("borderColor", data)]) end if(xmlValue("ignoreOffset", data)~=nil)then if(xmlValue("ignoreOffset", data))then self:ignoreOffset(true) end end - if(xmlValue("onClick", data)~=nil)then self:onClick(baseFrame:getVariable(xmlValue("onClick", data))) end - if(xmlValue("onClickUp", data)~=nil)then self:onClickUp(baseFrame:getVariable(xmlValue("onClickUp", data))) end - if(xmlValue("onScroll", data)~=nil)then self:onScroll(baseFrame:getVariable(xmlValue("onScroll", data))) end - if(xmlValue("onDrag", data)~=nil)then self:onDrag(baseFrame:getVariable(xmlValue("onDrag", data))) end - if(xmlValue("onKey", data)~=nil)then self:onKey(baseFrame:getVariable(xmlValue("onKey", data))) end - if(xmlValue("onKeyUp", data)~=nil)then self:onKeyUp(baseFrame:getVariable(xmlValue("onKeyUp", data))) end - if(xmlValue("onChange", data)~=nil)then self:onChange(baseFrame:getVariable(xmlValue("onChange", data))) end - if(xmlValue("onResize", data)~=nil)then self:onResize(baseFrame:getVariable(xmlValue("onResize", data))) end - if(xmlValue("onReposition", data)~=nil)then self:onReposition(baseFrame:getVariable(xmlValue("onReposition", data))) end - if(xmlValue("onEvent", data)~=nil)then self:onEvent(baseFrame:getVariable(xmlValue("onEvent", data))) end - if(xmlValue("onGetFocus", data)~=nil)then self:onGetFocus(baseFrame:getVariable(xmlValue("onGetFocus", data))) end - if(xmlValue("onLoseFocus", data)~=nil)then self:onLoseFocus(baseFrame:getVariable(xmlValue("onLoseFocus", data))) end - if(xmlValue("onBackgroundKey", data)~=nil)then self:onBackgroundKey(baseFrame:getVariable(xmlValue("onBackgroundKey", data))) end - if(xmlValue("onBackgroundKeyUp", data)~=nil)then self:onBackgroundKeyUp(baseFrame:getVariable(xmlValue("onBackgroundKeyUp", data))) end + if(xmlValue("onClick", data)~=nil)then self:generateXMLEventFunction(self.onClick, xmlValue("onClick", data)) end + if(xmlValue("onClickUp", data)~=nil)then self:generateXMLEventFunction(self.onClickUp, xmlValue("onClickUp", data)) end + if(xmlValue("onScroll", data)~=nil)then self:generateXMLEventFunction(self.onScroll, xmlValue("onScroll", data)) end + if(xmlValue("onDrag", data)~=nil)then self:generateXMLEventFunction(self.onDrag, xmlValue("onDrag", data)) end + if(xmlValue("onKey", data)~=nil)then self:generateXMLEventFunction(self.onKey, xmlValue("onKey", data)) end + if(xmlValue("onKeyUp", data)~=nil)then self:generateXMLEventFunction(self.onKeyUp, xmlValue("onKeyUp", data)) end + if(xmlValue("onChange", data)~=nil)then self:generateXMLEventFunction(self.onChange, xmlValue("onChange", data)) end + if(xmlValue("onResize", data)~=nil)then self:generateXMLEventFunction(self.onResize, xmlValue("onResize", data)) end + if(xmlValue("onReposition", data)~=nil)then self:generateXMLEventFunction(self.onReposition, xmlValue("onReposition", data)) end + if(xmlValue("onEvent", data)~=nil)then self:generateXMLEventFunction(self.onEvent, xmlValue("onEvent", data)) end + if(xmlValue("onGetFocus", data)~=nil)then self:generateXMLEventFunction(self.onGetFocus, xmlValue("onGetFocus", data)) end + if(xmlValue("onLoseFocus", data)~=nil)then self:generateXMLEventFunction(self.onLoseFocus, xmlValue("onLoseFocus", data)) end + if(xmlValue("onBackgroundKey", data)~=nil)then self:generateXMLEventFunction(self.onBackgroundKey, xmlValue("onBackgroundKey", data)) end + if(xmlValue("onBackgroundKeyUp", data)~=nil)then self:generateXMLEventFunction(self.onBackgroundKeyUp, xmlValue("onBackgroundKeyUp", data)) end return self end, @@ -172,6 +206,10 @@ return function(name) return self.parent end; + getObjectReferencesForDynVal = function(self, str) + + end, + setPosition = function(self, xPos, yPos, rel) if(type(xPos)=="number")then self.x = rel and self:getX()+xPos or xPos @@ -179,36 +217,26 @@ return function(name) if(type(yPos)=="number")then self.y = rel and self:getY()+yPos or yPos end - if(type(xPos)=="string")then - self.x = dynValue(xPos, function() return self:getParent():getWidth() end) + if(self.parent~=nil)then + if(type(xPos)=="string")then + self.x = self.parent:newDynamicValue(self, xPos) + end + if(type(yPos)=="string")then + self.y = self.parent:newDynamicValue(self, yPos) + end + self.parent:recalculateDynamicValues() end - if(type(xPos)=="table")then - local str = xPos[1] - table.remove(xPos, 1) - local fList = xPos - self.x = dynValue(str, function() return self:getParent():getWidth() end, fList) - end - if(type(yPos)=="string")then - self.y = dynValue(yPos, function() return self:getParent():getHeight() end) - end - if(type(yPos)=="table")then - local str = yPos[1] - table.remove(yPos, 1) - local fList = yPos - self.y = dynValue(str, function() return self:getParent():getHeight() end, fList) - end - self:calculateDynamicValues() eventSystem:sendEvent("basalt_reposition", self) visualsChanged = true return self end; getX = function(self) - return type(self.x)=="number" and self.x or self.x:get() + return type(self.x) == "number" and self.x or self.x[1] end; getY = function(self) - return type(self.y)=="number" and self.y or self.y:get() + return type(self.y) == "number" and self.y or self.y[1] end; getPosition = function(self) @@ -232,36 +260,26 @@ return function(name) if(type(height)=="number")then self.height = rel and self.height+height or height end - if(type(width)=="string")then - self.width = dynValue(width, function() return self:getParent():getWidth() end) + if(self.parent~=nil)then + if(type(width)=="string")then + self.width = self.parent:newDynamicValue(self, width) + end + if(type(height)=="string")then + self.height = self.parent:newDynamicValue(self, height) + end + self.parent:recalculateDynamicValues() end - if(type(width)=="table")then - local str = width[1] - table.remove(width, 1) - local fList = width - self.width = dynValue(str, function() return self:getParent():getWidth() end, fList) - end - if(type(height)=="string")then - self.height = dynValue(height, function() return self:getParent():getHeight() end) - end - if(type(height)=="table")then - local str = height[1] - table.remove(height, 1) - local fList = height - self.height = dynValue(str, function() return self:getParent():getHeight() end, fList) - end - self:calculateDynamicValues() eventSystem:sendEvent("basalt_resize", self) visualsChanged = true return self end; getHeight = function(self) - return type(self.height)=="number" and self.height or self.height:get() + return type(self.height) == "number" and self.height or self.height[1] end; getWidth = function(self) - return type(self.width)=="number" and self.width or self.width:get() + return type(self.width) == "number" and self.width or self.width[1] end; getSize = function(self) @@ -277,7 +295,7 @@ return function(name) end, setBackground = function(self, color) - self.bgColor = color + self.bgColor = color or false visualsChanged = true return self end; @@ -287,7 +305,7 @@ return function(name) end; setForeground = function(self, color) - self.fgColor = color + self.fgColor = color or false visualsChanged = true return self end; @@ -516,9 +534,11 @@ return function(name) end; onDrag = function(self, ...) - for _,v in pairs(table.pack(...))do - if(type(v)=="function")then - self:registerEvent("mouse_drag", v) + if(isEnabled)then + for _,v in pairs(table.pack(...))do + if(type(v)=="function")then + self:registerEvent("mouse_drag", v) + end end end return self @@ -627,38 +647,67 @@ return function(name) end; mouseHandler = function(self, event, button, x, y) - local objX, objY = self:getAbsolutePosition(self:getAnchorPosition()) - local w, h = self:getSize() - local yOff = false - - if(objY-1 == y)and(self:getBorder("top"))then - y = y+1 - yOff = true - end - - if (objX <= x) and (objX + w > x) and (objY <= y) and (objY + h > y) and (isVisible) then - if (self.parent ~= nil) then - self.parent:setFocusedObject(self) + if(isEnabled)and(isVisible)then + local objX, objY = self:getAbsolutePosition(self:getAnchorPosition()) + local w, h = self:getSize() + local yOff = false + + if(objY-1 == y)and(self:getBorder("top"))then + y = y+1 + yOff = true + end + if(event=="mouse_up")then + isDragging = false + end + + if(isDragging)and(event=="mouse_drag")then + local xO, yO, parentX, parentY = 0, 0, 1, 1 + if (self.parent ~= nil) then + xO, yO = self.parent:getOffset() + xO = xO < 0 and math.abs(xO) or -xO + yO = yO < 0 and math.abs(yO) or -yO + parentX, parentY = self.parent:getAbsolutePosition(self.parent:getAnchorPosition()) + end + local dX, dY = x + dragXOffset - (parentX - 1) + xO, y + dragYOffset - (parentY - 1) + yO + local val = eventSystem:sendEvent(event, self, event, button, dX, dY, dragStartX, dragStartY, x, y) + end + + + if (objX <= x) and (objX + w > x) and (objY <= y) and (objY + h > y) then + if(event=="mouse_click")then + isDragging = true + dragStartX, dragStartY = x, y + dragXOffset, dragYOffset = objX - x, objY - y + end + if(event~="mouse_drag")then + if (self.parent ~= nil) then + self.parent:setFocusedObject(self) + end + local val = eventSystem:sendEvent(event, self, event, button, x, y) + if(val~=nil)then return val end + end + return true end - local val = eventSystem:sendEvent(event, self, event, button, x, y) - if(val~=nil)then return val end - return true end return false end; keyHandler = function(self, event, key) - if (self:isFocused()) then - local val = eventSystem:sendEvent(event, self, event, key) - if(val~=nil)then return val end - return true + if(isEnabled)then + if (self:isFocused()) then + local val = eventSystem:sendEvent(event, self, event, key) + if(val~=nil)then return val end + return true + end end return false end; backgroundKeyHandler = function(self, event, key) - local val = eventSystem:sendEvent("background_"..event, self, event, key) - if(val~=nil)then return val end + if(isEnabled)then + local val = eventSystem:sendEvent("background_"..event, self, event, key) + if(val~=nil)then return val end + end return true end; diff --git a/Basalt/libraries/dynamicValues.lua b/Basalt/libraries/dynamicValues.lua deleted file mode 100644 index 9604706..0000000 --- a/Basalt/libraries/dynamicValues.lua +++ /dev/null @@ -1,67 +0,0 @@ -return function(value, parentF, ...) - local cache - local fList = ... - if(...~=nil)then - if(type(...)~="table")then - fList = table.pack(...) - end - end - - local function numberFromString(str) - return load("return " .. str)() - end - - local function replacePercentage(str, parentValue) - local _fullStr = str - for v in _fullStr:gmatch("%d+%%") do - local pValue = v:gsub("%%", "") - str = str:gsub(v.."%", parentValue / 100 * math.max(math.min(tonumber(pValue),100),0)) - end - return str - end - - local function fToNumber(str) - for k,v in pairs(fList)do - if(type(v)=="function")then - for _ in str:gmatch("f"..k)do - str = string.gsub(str, "f"..k, v()) - end - end - end - return str - end - - local function calculateValue() - if(value~=nil)then - if(type(value)=="string")then - if(fList~=nil and #fList>0)then - cache = math.floor(numberFromString(replacePercentage(fToNumber(value), parentF() or 1))+0.5) - else - cache = math.floor(numberFromString(replacePercentage(value, parentF() or 1))) - end - end - end - return cache - end - - local public = { - getType = function(self) - return "DynamicValue" - end, - - get = function(self) - return cache or calculateValue() - end, - - calculate = function(self) - calculateValue() - return self - end, - - setParent = function(self, p) - parentF = p - return self - end - } - return public -end \ No newline at end of file diff --git a/Basalt/libraries/imageConverter.lua b/Basalt/libraries/imageConverter.lua deleted file mode 100644 index cc548a5..0000000 --- a/Basalt/libraries/imageConverter.lua +++ /dev/null @@ -1,16 +0,0 @@ -local function PPMToBasalt() - -end - - - -local function loadPPM(path) -local image = {} - - - return image -end - -return { - loadPPM = loadPPM -} \ No newline at end of file diff --git a/Basalt/libraries/logSystem.lua b/Basalt/libraries/logSystem.lua deleted file mode 100644 index b954707..0000000 --- a/Basalt/libraries/logSystem.lua +++ /dev/null @@ -1,16 +0,0 @@ -local logDir = "" -local logFileName = "log.txt" - -return { - setLogDir = function(dir) - logDir = dir - end, - - setLogFileName = function(name) - logFileName = name - end, - - __call = function() - --somelogs - end -} \ No newline at end of file diff --git a/Basalt/main.lua b/Basalt/main.lua index cf1cefe..e431308 100644 --- a/Basalt/main.lua +++ b/Basalt/main.lua @@ -164,6 +164,41 @@ local function basaltUpdateEvent(event, p1, p2, p3, p4) drawFrames() end +local basaltError = function(errMsg) + baseTerm.clear() + baseTerm.setBackgroundColor(colors.black) + baseTerm.setTextColor(colors.red) + local w,h = baseTerm.getSize() + + local splitString = function(str, sep) + if sep == nil then + sep = "%s" + end + local t={} + for v in string.gmatch(str, "([^"..sep.."]+)") do + table.insert(t, v) + end + return t + end + local words = splitString(errMsg, " ") + local line = "Basalt error: " + local yPos = 1 + for n=1,#words do + baseTerm.setCursorPos(1,yPos) + if(#line+#words[n] basalt.debugList:getHeight())then - basalt.debugList:setIndexOffset(basalt.debugList:getItemCount() - basalt.debugList:getHeight()) + basalt.debugList:setOffset(basalt.debugList:getItemCount() - basalt.debugList:getHeight()) end basalt.debugLabel:show() end, diff --git a/Basalt/objects/Animation.lua b/Basalt/objects/Animation.lua index 1b7bdeb..e9ff569 100644 --- a/Basalt/objects/Animation.lua +++ b/Basalt/objects/Animation.lua @@ -8,6 +8,10 @@ local lerp = function(s, e, pct) return s + (e - s) * pct end +local linear = function (t) + return t +end + local flip = function (x) return 1 - x end @@ -25,6 +29,7 @@ local easeInOut = function(t) end local lerp = { + linear = linear, lerp = lerp, flip=flip, easeIn=easeIn, @@ -48,6 +53,8 @@ return function(name) local nextWaitTimer = 0 local lastFunc local loop=false + local autoDestroy = false + local mode = "easeOut" local _OBJ @@ -58,6 +65,7 @@ return function(name) end local function onPlay(self) + if(index==1)then self:animationStartHandler() end if (animations[index] ~= nil) then call(animations[index].f) animationTime = animations[index].t @@ -106,9 +114,9 @@ return function(name) table.insert(animations, {t=time, f={f}}) end end + - local function predefinedLerp(v1,v2,d,t,get,set,mode) - mode = mode or "easeOut" + local function predefinedLerp(v1,v2,d,t,get,set) local x,y addAnimationPart(t+0.05, function() x,y = get(_OBJ) @@ -135,6 +143,32 @@ return function(name) return self end; + setMode = function(self, newMode) + mode = newMode + return self + end, + + generateXMLEventFunction = function(self, func, val) + local createF = function(str) + if(str:sub(1,1)=="#")then + local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len())) + if(o~=nil)and(o.internalObjetCall~=nil)then + func(self,function()o:internalObjetCall()end) + end + else + func(self,self:getBaseFrame():getVariable(str)) + end + end + if(type(val)=="string")then + createF(val) + elseif(type(val)=="table")then + for k,v in pairs(val)do + createF(v) + end + end + return self + end, + setValuesByXMLData = function(self, data) loop = xmlValue("loop", data)==true and true or false if(xmlValue("object", data)~=nil)then @@ -151,24 +185,21 @@ return function(name) local y = xmlValue("y", data["move"]) local duration = xmlValue("duration", data["move"]) local time = xmlValue("time", data["move"]) - local mode = xmlValue("mode", data["move"]) - self:move(x, y, duration, time, mode) + self:move(x, y, duration, time) end if(data["size"]~=nil)then local w = xmlValue("width", data["size"]) local h = xmlValue("height", data["size"]) local duration = xmlValue("duration", data["size"]) local time = xmlValue("time", data["size"]) - local mode = xmlValue("mode", data["size"]) - self:size(w, h, duration, time, mode) + self:size(w, h, duration, time) end if(data["offset"]~=nil)then local x = xmlValue("x", data["offset"]) local y = xmlValue("y", data["offset"]) local duration = xmlValue("duration", data["offset"]) local time = xmlValue("time", data["offset"]) - local mode = xmlValue("mode", data["offset"]) - self:offset(x, y, duration, time, mode) + self:offset(x, y, duration, time) end if(data["textColor"]~=nil)then local duration = xmlValue("duration", data["textColor"]) @@ -215,29 +246,14 @@ return function(name) self:changeText(duration, timer or 0, table.unpack(t)) end end - if(xmlValue("onDone", data)~=nil)then - local value = xmlValue("onDone", data) - if(value:sub(1,1)=="#")then - value = xmlValue("onDone", data):sub(2,value:len()) - local o = self:getBaseFrame():getDeepObject(value) - if(o~=nil)then - self:onAnimationDone(function()o:internalObjetCall()end) - end - else - local f = self:getBaseFrame():getVariable(value) - if(f~=nil)then - self:onAnimationDone(f) - end + if(xmlValue("onDone", data)~=nil)then self:generateXMLEventFunction(self.onDone, xmlValue("onDone", data)) end + if(xmlValue("onStart", data)~=nil)then self:generateXMLEventFunction(self.onDone, xmlValue("onStart", data)) end + if(xmlValue("autoDestroy", data)~=nil)then + if(xmlValue("autoDestroy", data))then + autoDestroy = true end end - if(xmlValue("autoRemove", data)~=nil)then - if(xmlValue("autoRemove", data)~=false)then - self:onAnimationDone(function() self.parent:removeObject(self) end) - end - else - self:onAnimationDone(function() self.parent:removeObject(self) end) - end - + mode = xmlValue("mode", data) or mode if(xmlValue("play", data)~=nil)then if(xmlValue("play", data))then self:play(loop) end end return self end, @@ -263,7 +279,7 @@ return function(name) offset = function(self, x, y, duration, timer, obj) _OBJ = obj or _OBJ - predefinedLerp(x,y,duration,timer or 0,_OBJ.getOffset,_OBJ.setOffset) + predefinedLerp(x,y,duration,timer or 0,_OBJ.getOffsetInternal,_OBJ.setOffset) return self end, @@ -329,13 +345,31 @@ return function(name) return self end; - onAnimationDone = function(self, f) + onDone = function(self, f) eventSystem:registerEvent("animation_done", f) return self end, + onStart = function(self, f) + eventSystem:registerEvent("animation_start", f) + return self + end, + + setAutoDestroy = function(self, destroy) + autoDestroy = destroy~=nil and destroy or true + return self + end, + animationDoneHandler = function(self) eventSystem:sendEvent("animation_done", self) + if(autoDestroy)then + self.parent:removeObject(self) + self = nil + end + end; + + animationStartHandler = function(self) + eventSystem:sendEvent("animation_start", self) end; clear = function(self) diff --git a/Basalt/objects/Program.lua b/Basalt/objects/Program.lua index d7569cd..92cc44c 100644 --- a/Basalt/objects/Program.lua +++ b/Basalt/objects/Program.lua @@ -559,7 +559,7 @@ return function(name, parent) if not (curProcess:isDead()) then if not (paused) then local absX, absY = self:getAbsolutePosition(self:getAnchorPosition(nil, nil, true)) - curProcess:resume(event, button, x - (absX - 1), y - (absY - 1)) + curProcess:resume(event, button, x-absX+1, y-absY+1) end end return true diff --git a/Basalt/objects/Progressbar.lua b/Basalt/objects/Progressbar.lua index 13b4177..390ca7b 100644 --- a/Basalt/objects/Progressbar.lua +++ b/Basalt/objects/Progressbar.lua @@ -36,7 +36,7 @@ return function(name) if(xmlValue("progressSymbol", data)~=nil)then activeBarSymbol = xmlValue("progressSymbol", data) end if(xmlValue("backgroundSymbol", data)~=nil)then bgBarSymbol = xmlValue("backgroundSymbol", data) end if(xmlValue("progressSymbolColor", data)~=nil)then activeBarSymbolCol = colors[xmlValue("progressSymbolColor", data)] end - if(xmlValue("onDone", data)~=nil)then self:onProgressDone(baseFrame:getVariable(xmlValue("onDone", data))) end + if(xmlValue("onDone", data)~=nil)then self:generateXMLEventFunction(self.onProgressDone, xmlValue("onDone", data)) end return self end, diff --git a/Basalt/objects/Radio.lua b/Basalt/objects/Radio.lua index d73c9e9..d09412c 100644 --- a/Basalt/objects/Radio.lua +++ b/Basalt/objects/Radio.lua @@ -1,5 +1,6 @@ local Object = require("Object") local utils = require("utils") +local xmlValue = utils.getValueFromXML return function(name) local base = Object(name) @@ -12,6 +13,8 @@ return function(name) local itemSelectedFG local boxSelectedBG local boxSelectedFG + local boxNotSelectedBG + local boxNotSelectedFG local selectionColorActive = true local symbol = "\7" local align = "left" @@ -35,8 +38,10 @@ return function(name) base.setValuesByXMLData(self, data) if(xmlValue("selectionBG", data)~=nil)then itemSelectedBG = colors[xmlValue("selectionBG", data)] end if(xmlValue("selectionFG", data)~=nil)then itemSelectedFG = colors[xmlValue("selectionFG", data)] end - if(xmlValue("boxBG", data)~=nil)then itemSelectedBG = colors[xmlValue("boxBG", data)] end - if(xmlValue("boxFG", data)~=nil)then itemSelectedFG = colors[xmlValue("boxFG", data)] end + if(xmlValue("boxBG", data)~=nil)then boxSelectedBG = colors[xmlValue("boxBG", data)] end + if(xmlValue("inactiveBoxBG", data)~=nil)then boxNotSelectedBG = colors[xmlValue("inactiveBoxBG", data)] end + if(xmlValue("inactiveBoxFG", data)~=nil)then boxNotSelectedFG = colors[xmlValue("inactiveBoxFG", data)] end + if(xmlValue("boxFG", data)~=nil)then boxSelectedFG = colors[xmlValue("boxFG", data)] end if(xmlValue("symbol", data)~=nil)then symbol = xmlValue("symbol", data) end if(data["item"]~=nil)then local tab = data["item"] @@ -139,7 +144,7 @@ return function(name) self.parent:writeText(value.x + 2 + obx - 1, value.y + oby - 1, value.text, itemSelectedBG, itemSelectedFG) end else - self.parent:drawBackgroundBox(value.x + obx - 1, value.y + oby - 1, 1, 1, self.bgColor) + self.parent:drawBackgroundBox(value.x + obx - 1, value.y + oby - 1, 1, 1, boxNotSelectedBG or self.bgColor) self.parent:writeText(value.x + 2 + obx - 1, value.y + oby - 1, value.text, value.bgCol, value.fgCol) end end diff --git a/Basalt/objects/Textfield.lua b/Basalt/objects/Textfield.lua index 09f0715..a65e140 100644 --- a/Basalt/objects/Textfield.lua +++ b/Basalt/objects/Textfield.lua @@ -34,11 +34,12 @@ return function(name) return pos end - local function updateColors(self) - local fgLine = tHex[self.fgColor]:rep(fgLines[textY]:len()) - local bgLine = tHex[self.bgColor]:rep(bgLines[textY]:len()) + local function updateColors(self, l) + l = l or textY + local fgLine = tHex[self.fgColor]:rep(fgLines[l]:len()) + local bgLine = tHex[self.bgColor]:rep(bgLines[l]:len()) for k,v in pairs(rules)do - local pos = stringGetPositions(lines[textY], v[1]) + local pos = stringGetPositions(lines[l], v[1]) if(#pos>0)then for x=1,#pos/2 do local xP = x*2 - 1 @@ -53,7 +54,7 @@ return function(name) end for k,v in pairs(keyWords)do for _,b in pairs(v)do - local pos = stringGetPositions(lines[textY], b) + local pos = stringGetPositions(lines[l], b) if(#pos>0)then for x=1,#pos/2 do local xP = x*2 - 1 @@ -62,8 +63,14 @@ return function(name) end end end - fgLines[textY] = fgLine - bgLines[textY] = bgLine + fgLines[l] = fgLine + bgLines[l] = bgLine + end + + local function updateAllColors(self) + for n=1,#lines do + updateColors(self, n) + end end local object = { @@ -75,10 +82,24 @@ return function(name) return objectType end; + setBackground = function(self, bg) + base.setBackground(self, bg) + updateAllColors(self) + return self + end, + + setForeground = function(self, fg) + base.setForeground(self, fg) + updateAllColors(self) + return self + end, + setValuesByXMLData = function(self, data) base.setValuesByXMLData(self, data) if(data["lines"]~=nil)then - for k,v in pairs(data["lines"]["line"])do + local l = data["lines"]["line"] + if(l.properties~=nil)then l = {l} end + for k,v in pairs(l)do self:addLine(v:value()) end end diff --git a/Basalt/objects/Thread.lua b/Basalt/objects/Thread.lua index fa65e0f..b7e4d54 100644 --- a/Basalt/objects/Thread.lua +++ b/Basalt/objects/Thread.lua @@ -8,6 +8,18 @@ return function(name) local cRoutine local isActive = false + local generateXMLEventFunction = function(self, str) + if(str:sub(1,1)=="#")then + local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len())) + if(o~=nil)and(o.internalObjetCall~=nil)then + return (function()o:internalObjetCall()end) + end + else + return self:getBaseFrame():getVariable(str) + end + return self + end + object = { name = name, getType = function(self) @@ -29,7 +41,7 @@ return function(name) setValuesByXMLData = function(self, data) local f - if(xmlValue("thread", data)~=nil)then f = self:getBaseFrame():getVariable(xmlValue("thread", data)) end + if(xmlValue("thread", data)~=nil)then f = generateXMLEventFunction(self, xmlValue("thread", data)) end if(xmlValue("start", data)~=nil)then if(xmlValue("start", data))and(f~=nil)then self:start(f) end end return self end, diff --git a/Basalt/objects/Timer.lua b/Basalt/objects/Timer.lua index ac7c4b3..62ca3ee 100644 --- a/Basalt/objects/Timer.lua +++ b/Basalt/objects/Timer.lua @@ -11,6 +11,27 @@ return function(name) local eventSystem = basaltEvent() local timerIsActive = false + local generateXMLEventFunction = function(self, func, val) + local createF = function(str) + if(str:sub(1,1)=="#")then + local o = self:getBaseFrame():getDeepObject(str:sub(2,str:len())) + if(o~=nil)and(o.internalObjetCall~=nil)then + func(self,function()o:internalObjetCall()end) + end + else + func(self,self:getBaseFrame():getVariable(str)) + end + end + if(type(val)=="string")then + createF(val) + elseif(type(val)=="table")then + for k,v in pairs(val)do + createF(v) + end + end + return self + end + local object = { name = name, getType = function(self) @@ -21,7 +42,7 @@ return function(name) if(xmlValue("time", data)~=nil)then timer = xmlValue("time", data) end if(xmlValue("repeat", data)~=nil)then savedRepeats = xmlValue("repeat", data) end if(xmlValue("start", data)~=nil)then if(xmlValue("start", data))then self:start() end end - if(xmlValue("onCall", data)~=nil)then self:onCall(getBaseFrame():getVariable(xmlValue("onCall", data))) end + if(xmlValue("onCall", data)~=nil)then generateXMLEventFunction(self, self.onCall, xmlValue("onCall", data)) end return self end, diff --git a/docs/objects/Basalt.md b/docs/objects/Basalt.md index 024725d..e80aeaf 100644 --- a/docs/objects/Basalt.md +++ b/docs/objects/Basalt.md @@ -178,26 +178,24 @@ Shedules a function which gets called in a coroutine. After the coroutine is fin 1. `function` a function which should get executed #### Returns: -1. `coroutine` it returns the coroutine which got created to execute the function +1. `function` it returns the function which you have to execute in order to start the coroutine #### Usage: * Creates a shedule which switches the color between red and gray ```lua local mainFrame = basalt.createFrame() local aButton = mainFrame:addButton():setText("Click me") -aButton:onClick(function() - basalt.shedule(function() - aButton:setBackground(colors.red) - os.sleep(0.1) - aButton:setBackground(colors.gray) - os.sleep(0.1) - aButton:setBackground(colors.red) - os.sleep(0.1) - aButton:setBackground(colors.gray) - os.sleep(0.1) - aButton:setBackground(colors.red) - os.sleep(0.1) - aButton:setBackground(colors.gray) - end) -end) +aButton:onClick(basalt.shedule(function(self) + self:setBackground(colors.red) + os.sleep(0.1) + self:setBackground(colors.gray) + os.sleep(0.1) + self:setBackground(colors.red) + os.sleep(0.1) + self:setBackground(colors.gray) + os.sleep(0.1) + self:setBackground(colors.red) + os.sleep(0.1) + self:setBackground(colors.gray) +end)) ```