Fixed XML stuff
- Added a XML Guide - Fixed some XML Code
This commit is contained in:
@@ -98,6 +98,29 @@ wrapText = function(str, width)
|
||||
return result
|
||||
end,
|
||||
|
||||
xmlValue = function(name, tab)
|
||||
local var
|
||||
if(type(tab)~="table")then return end
|
||||
if(tab[name]~=nil)then
|
||||
if(type(tab[name])=="table")then
|
||||
if(tab[name].value~=nil)then
|
||||
var = tab[name]:value()
|
||||
end
|
||||
end
|
||||
end
|
||||
if(var==nil)then var = tab["@"..name] end
|
||||
|
||||
if(var=="true")then
|
||||
var = true
|
||||
elseif(var=="false")then
|
||||
var = false
|
||||
elseif(tonumber(var)~=nil)then
|
||||
var = tonumber(var)
|
||||
end
|
||||
return var
|
||||
end,
|
||||
|
||||
|
||||
uuid = function()
|
||||
return string.gsub(string.format('%x-%x-%x-%x-%x', math.random(0, 0xffff), math.random(0, 0xffff), math.random(0, 0xffff), math.random(0, 0x0fff) + 0x4000, math.random(0, 0x3fff) + 0x8000), ' ', '0')
|
||||
end
|
||||
|
||||
@@ -37,4 +37,4 @@ return function(name, basalt)
|
||||
|
||||
object.__index = object
|
||||
return setmetatable(object, base)
|
||||
end
|
||||
end
|
||||
@@ -32,6 +32,15 @@ return function(name, basalt)
|
||||
return self
|
||||
end,
|
||||
|
||||
getAutoSize = function(self)
|
||||
return autoSize
|
||||
end,
|
||||
|
||||
setAutoSize = function(self, bool)
|
||||
autoSize = bool
|
||||
return self
|
||||
end,
|
||||
|
||||
getText = function(self)
|
||||
return text
|
||||
end,
|
||||
|
||||
@@ -53,4 +53,4 @@ return {
|
||||
end
|
||||
return objects
|
||||
end
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,6 @@
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.xmlValue
|
||||
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local bgSymbol = false
|
||||
@@ -26,8 +29,8 @@ return {
|
||||
return bgSymbolColor
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
setValuesByXMLData = function(self, data, scripts)
|
||||
base.setValuesByXMLData(self, data, scripts)
|
||||
if(xmlValue("background-symbol", data)~=nil)then self:setBackgroundSymbol(xmlValue("background-symbol", data), xmlValue("background-symbol-color", data)) end
|
||||
return self
|
||||
end,
|
||||
|
||||
@@ -215,6 +215,9 @@ local lerp = {
|
||||
easeInOutBounce=easeInOutBounce,
|
||||
}
|
||||
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.xmlValue
|
||||
|
||||
return {
|
||||
VisualObject = function(base, basalt)
|
||||
local activeAnimations = {}
|
||||
|
||||
@@ -140,11 +140,12 @@ local function makeText(nSize, sString, nFC, nBC, bBlit)
|
||||
end
|
||||
|
||||
-- The following code is related to basalt and has nothing to do with bigfonts, it creates a plugin which will be added to labels:
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.xmlValue
|
||||
return {
|
||||
Label = function(base)
|
||||
local fontsize = 1
|
||||
local bigfont
|
||||
local autoSize
|
||||
|
||||
local object = {
|
||||
setFontSize = function(self, newFont)
|
||||
@@ -153,7 +154,7 @@ return {
|
||||
if(fontsize>1)then
|
||||
self:setDrawState("label", false)
|
||||
bigfont = makeText(fontsize-1, self:getText(), self:getForeground(), self:getBackground() or colors.lightGray)
|
||||
if(autoSize)then
|
||||
if(self:getAutoSize())then
|
||||
self:getBase():setSize(#bigfont[1][1], #bigfont[1]-1)
|
||||
end
|
||||
else
|
||||
@@ -168,12 +169,6 @@ return {
|
||||
return fontsize
|
||||
end,
|
||||
|
||||
setSize = function(self, w, h, r)
|
||||
base.setSize(self, w, h, r)
|
||||
autoSize = false
|
||||
return self
|
||||
end,
|
||||
|
||||
getSize = function(self)
|
||||
local w, h = base.getSize(self)
|
||||
if(fontsize>1)then
|
||||
@@ -203,7 +198,6 @@ return {
|
||||
|
||||
setValuesByXMLData = function(self, data, scripts)
|
||||
base.setValuesByXMLData(self, data, scripts)
|
||||
if(xmlValue("text", data)~=nil)then self:setText(xmlValue("text", data)) end
|
||||
if(xmlValue("fontSize", data)~=nil)then self:setFontSize(xmlValue("fontSize", data)) end
|
||||
return self
|
||||
end,
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.xmlValue
|
||||
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local inline = true
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
local count = require("utils").tableCount
|
||||
local utils = require("utils")
|
||||
local count = utils.tableCount
|
||||
local xmlValue = utils.xmlValue
|
||||
|
||||
return {
|
||||
VisualObject = function(base, basalt)
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.xmlValue
|
||||
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local shadow = false
|
||||
@@ -28,8 +31,8 @@ return {
|
||||
end)
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
setValuesByXMLData = function(self, data, scripts)
|
||||
base.setValuesByXMLData(self, data, scripts)
|
||||
if(xmlValue("shadow", data)~=nil)then self:setShadow(xmlValue("shadow", data)) end
|
||||
return self
|
||||
end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local images = require("images")
|
||||
|
||||
local utils = require("utils")
|
||||
local xmlValue = utils.xmlValue
|
||||
return {
|
||||
VisualObject = function(base)
|
||||
local textureId, infinitePlay = 1, true
|
||||
@@ -108,8 +109,8 @@ return {
|
||||
self:setDrawState("texture-base", false)
|
||||
end,
|
||||
|
||||
setValuesByXMLData = function(self, data)
|
||||
base.setValuesByXMLData(self, data)
|
||||
setValuesByXMLData = function(self, data, scripts)
|
||||
base.setValuesByXMLData(self, data, scripts)
|
||||
if(xmlValue("texture", data)~=nil)then self:addTexture(xmlValue("texture", data), xmlValue("animate", data)) end
|
||||
if(xmlValue("textureMode", data)~=nil)then self:setTextureMode(xmlValue("textureMode", data)) end
|
||||
if(xmlValue("infinitePlay", data)~=nil)then self:setInfinitePlay(xmlValue("infinitePlay", data)) end
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
local utils = require("utils")
|
||||
local uuid = utils.uuid
|
||||
local xmlValue = utils.xmlValue
|
||||
|
||||
local function newNode(name)
|
||||
local node = {}
|
||||
@@ -147,28 +148,6 @@ function XmlParser:loadFile(xmlFilename, base)
|
||||
end
|
||||
end
|
||||
|
||||
local function xmlValue(name, tab)
|
||||
local var
|
||||
if(type(tab)~="table")then return end
|
||||
if(tab[name]~=nil)then
|
||||
if(type(tab[name])=="table")then
|
||||
if(tab[name].value~=nil)then
|
||||
var = tab[name]:value()
|
||||
end
|
||||
end
|
||||
end
|
||||
if(var==nil)then var = tab["@"..name] end
|
||||
|
||||
if(var=="true")then
|
||||
var = true
|
||||
elseif(var=="false")then
|
||||
var = false
|
||||
elseif(tonumber(var)~=nil)then
|
||||
var = tonumber(var)
|
||||
end
|
||||
return var
|
||||
end
|
||||
|
||||
local function executeScript(scripts)
|
||||
for k,v in pairs(scripts)do
|
||||
if(k~="env")then
|
||||
@@ -193,14 +172,16 @@ return {
|
||||
|
||||
local object = {
|
||||
setValuesByXMLData = function(self, data, scripts)
|
||||
scripts.env[self:getName()] = self
|
||||
local x, y = self:getPosition()
|
||||
local w, h = self:getSize()
|
||||
local w, h = nil, nil
|
||||
if(xmlValue("x", data)~=nil)then x = xmlValue("x", data) end
|
||||
if(xmlValue("y", data)~=nil)then y = xmlValue("y", data) end
|
||||
if(xmlValue("width", data)~=nil)then w = xmlValue("width", data) end
|
||||
if(xmlValue("height", data)~=nil)then h = xmlValue("height", data) end
|
||||
if(xmlValue("background", data)~=nil)then self:setBackground(colors[xmlValue("background", data)]) end
|
||||
|
||||
|
||||
if(xmlValue("script", data)~=nil)then
|
||||
if(scripts[1]==nil)then
|
||||
scripts[1] = {}
|
||||
@@ -215,7 +196,12 @@ return {
|
||||
end
|
||||
end
|
||||
self:setPosition(x, y)
|
||||
self:setSize(w, h)
|
||||
if(w~=nil or h~=nil)then
|
||||
local w1, h1 = self:getSize()
|
||||
if w==nil then w = w1 end
|
||||
if h==nil then h = h1 end
|
||||
self:setSize(w, h)
|
||||
end
|
||||
|
||||
return self
|
||||
end,
|
||||
@@ -259,6 +245,7 @@ return {
|
||||
|
||||
local object = {
|
||||
setValuesByXMLData = function(self, data, scripts)
|
||||
lastXMLReferences = {}
|
||||
base.setValuesByXMLData(self, data, scripts)
|
||||
local xOffset, yOffset = self:getOffset()
|
||||
if(xmlValue("layout", data)~=nil)then self:addLayout(xmlValue("layout", data)) end
|
||||
@@ -281,12 +268,15 @@ return {
|
||||
return self
|
||||
end,
|
||||
|
||||
getXMLElements = function(self)
|
||||
return lastXMLReferences
|
||||
end,
|
||||
|
||||
loadLayout = function(self, path)
|
||||
if(fs.exists(path))then
|
||||
local scripts = {}
|
||||
scripts.env = _ENV
|
||||
scripts.env.basalt = basalt
|
||||
scripts.env.main = self
|
||||
scripts.env.shared = {}
|
||||
local f = fs.open(path, "r")
|
||||
local data = XmlParser:ParseXmlText(f.readAll())
|
||||
|
||||
@@ -37,3 +37,4 @@
|
||||
- Guides
|
||||
- [Introduction to Basalt](guides/introduction.md)
|
||||
- [Dynamic Values](guides/dynamicvalues.md)
|
||||
- [XML](guides/xml.md)
|
||||
|
||||
@@ -51,9 +51,9 @@ Dynamic Values can be used to access various attributes of a UI element, such as
|
||||
|
||||
You can use different accessors to refer to objects when using Dynamic Values:
|
||||
|
||||
1. `parent` The x-coordinate of the element
|
||||
2. `self` The y-coordinate of the element
|
||||
3. `ObjectID` The width of the element
|
||||
1. `parent` Access the parent object
|
||||
2. `self` Access the current object
|
||||
3. `ObjectID` Access a specific object using its ObjectID
|
||||
|
||||
This allows for a more dynamic and responsive layout, as you can adjust an element's attributes based on the properties of other elements. Here's an example of using Dynamic Values to set the position and size of a UI element:
|
||||
|
||||
|
||||
119
docs/guides/xml.md
Normal file
119
docs/guides/xml.md
Normal file
@@ -0,0 +1,119 @@
|
||||
XML (eXtensible Markup Language) is a popular and widely-used markup language for defining structured data. In this guide, we will explain how to use XML with Basalt to create and manage UI elements and layouts more efficiently.
|
||||
|
||||
## Loading an XML File in Basalt
|
||||
|
||||
To load an XML file in Basalt, you'll need to use the `frame:loadLayout` function. This function reads an XML file and returns a table containing the parsed XML data.
|
||||
|
||||
Here's an example of how to load an XML file:
|
||||
|
||||
```lua
|
||||
local basalt = require("basalt")
|
||||
|
||||
local main = basalt.createFrame():loadLayout("path/to/your/layout.xml")
|
||||
```
|
||||
|
||||
Make sure that the specified XML file is accessible and located within your project's file system.
|
||||
|
||||
## Using XML for Basalt
|
||||
|
||||
Basalt uses XML to define UI elements and their properties. By using XML, you can create more organized and easily maintainable UI layouts. Basalt can read and interpret the XML data to create the corresponding UI elements with the specified properties and structure.
|
||||
|
||||
Here's an example of an XML file that defines a simple UI layout for Basalt:
|
||||
|
||||
```xml
|
||||
<frame id="mainFrame">
|
||||
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />
|
||||
<button id="okButton" text="OK" x="10" y="5" />
|
||||
</frame>
|
||||
```
|
||||
|
||||
In this example, we define a `Frame` containing a `Label` and a `Button` with their respective properties, such as `id`, `text`, `x`, and `y`.
|
||||
|
||||
To use the loaded XML data to create the UI elements in Basalt, you can use `main:getXMLElements()`:
|
||||
|
||||
```lua
|
||||
local basalt = require("basalt")
|
||||
|
||||
local main = basalt.createFrame():loadLayout("path/to/your/layout.xml")
|
||||
|
||||
local uiElements = main:getXMLElements()
|
||||
|
||||
local titleLabel = uiElements["titleLabel"]
|
||||
|
||||
titleLabel:setText("New Title")
|
||||
```
|
||||
|
||||
## Using Lua Code in XML
|
||||
|
||||
In addition to defining UI elements, you can also include Lua code within your XML files for Basalt. This allows you to perform more complex operations or customize your UI elements based on conditions or data.
|
||||
|
||||
To include Lua code in your XML file, you can use the `<script>` tag. Any Lua code enclosed within the `<script>` tag will be executed by Basalt when parsing the XML data.
|
||||
|
||||
Here's an example of how to include Lua code in your XML file:
|
||||
|
||||
```xml
|
||||
<frame id="mainFrame">
|
||||
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />
|
||||
<button id="okButton" text="OK" x="10" y="5" />
|
||||
</frame>
|
||||
<script>
|
||||
-- Lua code to change the text of the titleLabel based on a condition
|
||||
if someCondition then
|
||||
titleLabel:setText("Condition Met!")
|
||||
end
|
||||
</script>
|
||||
```
|
||||
|
||||
To share variables or data between multiple `<script>` tags in your XML file, you can use the global shared table provided by Basalt. This table allows you to store values that can be accessed across different `<script>` tags in your XML file.
|
||||
|
||||
Here's an example of using the shared table to share data between two `<script>` tags:
|
||||
|
||||
```xml
|
||||
<script>
|
||||
-- Store a value in the shared table
|
||||
shared.myValue = 42
|
||||
</script>
|
||||
<frame id="mainFrame">
|
||||
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />
|
||||
<button id="okButton" text="OK" x="10" y="5" />
|
||||
</frame>
|
||||
<script>
|
||||
-- Access the stored value from the shared table
|
||||
local myValue = shared.myValue
|
||||
|
||||
-- Perform an operation using the shared value, e.g., update titleLabel's text
|
||||
titleLabel:setText("Shared Value: " .. myValue)
|
||||
</script>
|
||||
```
|
||||
|
||||
In this example, we first store a value in the shared table in one `<script>` tag, and then access that value in another `<script>` tag to update the titleLabel's text.
|
||||
|
||||
You can also include Lua code directly within event attributes of UI elements. This allows you to execute specific actions or manipulate UI elements when certain events occur.
|
||||
|
||||
To include Lua code in an event attribute, simply add the Lua code within the event attribute's value in the XML tag. The Lua code will be executed when the specified event is triggered.
|
||||
|
||||
```xml
|
||||
<frame id="mainFrame">
|
||||
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />
|
||||
<button id="okButton" text="OK" x="10" y="5" onClick="titleLabel:setText('Button clicked!')" />
|
||||
</frame>
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```xml
|
||||
<frame id="mainFrame">
|
||||
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />
|
||||
<button id="okButton" text="OK" x="10" y="5">
|
||||
<onClick>
|
||||
titleLabel:setText('Button clicked!')
|
||||
</onClick>
|
||||
</button>
|
||||
</frame>
|
||||
```
|
||||
|
||||
In both examples, you can see that XML provides a straightforward way to build a Basalt user interface. The XML format allows you to define the UI structure and easily set properties for each UI element, such as position, size, and text.
|
||||
|
||||
Notably, you can access UI elements by their assigned ID directly in the event code. In the examples above, the titleLabel and okButton elements are accessed simply by referencing their IDs. This convenient feature eliminates the need to search for or store references to the elements in your code.
|
||||
|
||||
Remember: IDs have to be unique!
|
||||
Reference in New Issue
Block a user