Just playing around with yoga and unity to see if we could build a ui with it
This commit is contained in:
1 parent
15d5b17fd2
commit
6ad1cd7aa8
60 files changed
+3930
-3
No files matched your search
@@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using Vocaluxe.UI.AbstractElements;
|
||||
|
||||
namespace Vocaluxe.UI.Parser
|
||||
{
|
||||
public static class CUiScreenParser
|
||||
{
|
||||
public static CUiScreen ParseOld(string path)
|
||||
{
|
||||
if(!File.Exists(path))
|
||||
throw new FileNotFoundException(path);
|
||||
|
||||
using (XmlReader reader = XmlReader.Create(path, new XmlReaderSettings { IgnoreWhitespace = true }))
|
||||
{
|
||||
return _ParseElementOld(reader)[0].Item1 as CUiScreen ?? throw new ArgumentException("First node must be a screen element");
|
||||
}
|
||||
}
|
||||
|
||||
private static List<(CUiElement uiElement, string bindingId)> _ParseElementOld(XmlReader reader)
|
||||
{
|
||||
List<(CUiElement, string)> elementList = new List<(CUiElement uiElement, string bindingId)>();
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.NodeType != XmlNodeType.Element)
|
||||
continue;
|
||||
|
||||
string elementName = reader.Name;
|
||||
|
||||
var subtreeReader = reader.ReadSubtree();
|
||||
subtreeReader.Read();
|
||||
List<(CUiElement uiElement, string bindingId)> childElements = _ParseElementOld(subtreeReader);
|
||||
|
||||
Dictionary<string, string> attributes = new Dictionary<string, string>();
|
||||
for (var attrIndex = 0; attrIndex < reader.AttributeCount; attrIndex++)
|
||||
{
|
||||
reader.MoveToAttribute(attrIndex);
|
||||
attributes.Add(reader.Name, reader.Value);
|
||||
}
|
||||
|
||||
attributes.TryGetValue("bindingId", out string bindingId);
|
||||
elementList.Add((CUiElementFactory.CreateElement(elementName, attributes, childElements), bindingId));
|
||||
}
|
||||
|
||||
reader.Close();
|
||||
return elementList;
|
||||
}
|
||||
|
||||
public static CUiScreen Parse(string path)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
throw new FileNotFoundException(path);
|
||||
|
||||
XmlDocument xmlDoc = new XmlDocument();
|
||||
xmlDoc.Load(path);
|
||||
|
||||
return _ParseElement(xmlDoc.DocumentElement).uiElement as CUiScreen ?? throw new ArgumentException("First node must be a screen element");
|
||||
}
|
||||
|
||||
private static (CUiElement uiElement, string bindingId) _ParseElement(XmlElement node)
|
||||
{
|
||||
(CUiElement, string) resultElement = (null,null);
|
||||
|
||||
if (node.NodeType != XmlNodeType.Element)
|
||||
return resultElement;
|
||||
|
||||
List<(CUiElement uiElement, string bindingId)> childElements = new List<(CUiElement uiElement, string bindingId)>();
|
||||
foreach (XmlElement childNode in node.ChildNodes)
|
||||
{
|
||||
childElements.Add(_ParseElement(childNode));
|
||||
}
|
||||
|
||||
Dictionary<string, string> attributes = new Dictionary<string, string>();
|
||||
foreach (XmlAttribute attribute in node.Attributes)
|
||||
{
|
||||
attributes.Add(attribute.Name, attribute.Value);
|
||||
}
|
||||
|
||||
attributes.TryGetValue("bindingId", out string bindingId);
|
||||
resultElement = (CUiElementFactory.CreateElement(node.Name, attributes, childElements), bindingId);
|
||||
|
||||
return resultElement;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using Vocaluxe.UI.AbstractElements;
|
||||
using Vocaluxe.UI.Screens;
|
||||
|
||||
namespace Vocaluxe.UI.Parser
|
||||
{
|
||||
public class CUiScreenStore
|
||||
{
|
||||
private readonly Dictionary<EUiScreenType, CUiScreen> _LoadedScreens = new Dictionary<EUiScreenType, CUiScreen>();
|
||||
|
||||
public void LoadScreens(string path, (int width, int height) initalialSize)
|
||||
{
|
||||
if(!Directory.Exists(path))
|
||||
throw new DirectoryNotFoundException("Screen file path must exist");
|
||||
|
||||
_LoadedScreens.Clear();
|
||||
|
||||
IEnumerable<string> filePaths = Directory.GetFiles(path, "*.xml");
|
||||
|
||||
foreach (var filePath in filePaths)
|
||||
{
|
||||
CUiScreen screen = CUiScreenParser.Parse(filePath);
|
||||
if(initalialSize.width > 0 && initalialSize.height > 0)
|
||||
screen.Resize(initalialSize.width, initalialSize.height);
|
||||
_LoadedScreens.Add(screen.ScreenType, screen);
|
||||
}
|
||||
}
|
||||
|
||||
public Dictionary<EUiScreenType, CUiScreen> GetAllScreen()
|
||||
{
|
||||
// ReadOnlyDictionary would be better but got problems in unity
|
||||
return _LoadedScreens;
|
||||
}
|
||||
|
||||
public CUiScreen GetScreen(EUiScreenType screenType)
|
||||
{
|
||||
_LoadedScreens.TryGetValue(screenType, out CUiScreen screen);
|
||||
return screen;
|
||||
}
|
||||
|
||||
public CUiScreen this[EUiScreenType screenType] => GetScreen(screenType);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user