Just playing around with yoga and unity to see if we could build a ui with it

This commit is contained in:
lukeIam committed 2018-08-27 20:04:20 +02:00
1 parent 15d5b17fd2
commit 6ad1cd7aa8
60 files changed
+3930 -3

No files matched your search

@@ -0,0 +1,7 @@
namespace Vocaluxe.UI.AbstractElements
{
public abstract class CController
{
public abstract void RegisterChild(CController child, string id);
}
}
+156
View File
@@ -0,0 +1,156 @@
using System;
using System.Collections.Generic;
using Facebook.Yoga;
namespace Vocaluxe.UI.AbstractElements
{
public abstract class CUiElement : YogaNode
{
private static YogaConfig _Config = new YogaConfig();
#region Add Z component
private int _Z = -1;
public int LayoutZ
{
get => _Z;
set => _Z = value;
}
#endregion
public abstract CController BasicController { get; }
public static T CreateInstance<T> (Dictionary<string, string> properties, IEnumerable<(CUiElement uiElement, string bindingId)> children) where T : CUiElement, new()
{
var newInstance = new T();
_SetYogaProperties(newInstance, properties);
foreach ((CUiElement uiElement, string bindingId) in children)
{
newInstance.AddChild(uiElement);
newInstance.BasicController.RegisterChild(uiElement.BasicController, bindingId);
}
return newInstance;
}
private static void _SetYogaProperties(CUiElement targetElement, Dictionary<string, string> properties)
{
foreach (KeyValuePair<string, string> keyValue in properties)
{
switch (keyValue.Key.ToLower())
{
case "width":
targetElement.Width = Int32.Parse(keyValue.Value);
break;
case "height":
targetElement.Height = Int32.Parse(keyValue.Value);
break;
case "flexdirection":
Enum.TryParse(keyValue.Value, true, out YogaFlexDirection flexDirection);
targetElement.FlexDirection = flexDirection;
break;
case "wrap":
Enum.TryParse(keyValue.Value, true, out YogaWrap wrap);
targetElement.Wrap = wrap;
break;
case "aligncontent":
Enum.TryParse(keyValue.Value, true, out YogaAlign alignContent);
targetElement.AlignContent = alignContent;
break;
case "alignitems":
Enum.TryParse(keyValue.Value, true, out YogaAlign alignItems);
targetElement.AlignItems = alignItems;
break;
case "alignself":
Enum.TryParse(keyValue.Value, true, out YogaAlign alignSelf);
targetElement.AlignSelf = alignSelf;
break;
case "positiontype":
Enum.TryParse(keyValue.Value, true, out YogaPositionType positionType);
targetElement.PositionType = positionType;
break;
case "aspectratio":
targetElement.AspectRatio = float.Parse(keyValue.Value);
break;
case "flex":
targetElement.Flex = float.Parse(keyValue.Value);
break;
case "flexbasis":
targetElement.FlexBasis = float.Parse(keyValue.Value);
break;
case "flexgrow":
targetElement.FlexGrow = float.Parse(keyValue.Value);
break;
case "flexshrink":
targetElement.FlexShrink = float.Parse(keyValue.Value);
break;
case "justifycontent":
Enum.TryParse(keyValue.Value, true, out YogaJustify justifyContent);
targetElement.JustifyContent = justifyContent;
break;
case "margin":
targetElement.Margin = float.Parse(keyValue.Value);
break;
case "marginbottom":
targetElement.MarginBottom = float.Parse(keyValue.Value);
break;
case "margintop":
targetElement.MarginTop = float.Parse(keyValue.Value);
break;
case "marginleft":
targetElement.MarginLeft = float.Parse(keyValue.Value);
break;
case "marginright":
targetElement.MarginRight = float.Parse(keyValue.Value);
break;
case "padding":
targetElement.Padding = float.Parse(keyValue.Value);
break;
case "paddingbottom":
targetElement.PaddingBottom = float.Parse(keyValue.Value);
break;
case "paddingtop":
targetElement.PaddingTop = float.Parse(keyValue.Value);
break;
case "paddingleft":
targetElement.PaddingLeft = float.Parse(keyValue.Value);
break;
case "paddingright":
targetElement.PaddingRight = float.Parse(keyValue.Value);
break;
case "borderwidth":
targetElement.BorderWidth = float.Parse(keyValue.Value);
break;
case "borderbottomwidth":
targetElement.BorderBottomWidth = float.Parse(keyValue.Value);
break;
case "bordertopwidth":
targetElement.BorderTopWidth = float.Parse(keyValue.Value);
break;
case "borderleftwidth":
targetElement.BorderLeftWidth = float.Parse(keyValue.Value);
break;
case "borderrightwidth":
targetElement.BorderRightWidth = float.Parse(keyValue.Value);
break;
case "minwidth":
targetElement.MinWidth = float.Parse(keyValue.Value);
break;
case "maxwidth":
targetElement.MaxWidth = float.Parse(keyValue.Value);
break;
case "minheight":
targetElement.MinHeight = float.Parse(keyValue.Value);
break;
case "maxheight":
targetElement.MaxHeight = float.Parse(keyValue.Value);
break;
}
}
}
}
}
+18
View File
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Vocaluxe.UI.Screens;
namespace Vocaluxe.UI.AbstractElements
{
public abstract class CUiScreen:CUiElement
{
public void Resize(int width, int height)
{
Width = width;
Height = height;
CalculateLayout();
}
public abstract EUiScreenType ScreenType { get; }
}
}
@@ -0,0 +1,40 @@
using System;
using Vocaluxe.UI.AbstractElements;
using Vocaluxe.UI.BasicElements.Text;
namespace Vocaluxe.UI.BasicElements.Button
{
public class CControllerElementButton: CController
{
private CControllerElementText _DescriptionController;
public override void RegisterChild(CController child, string id)
{
Enum.TryParse(id, out EElementButtonComponents parsedId);
switch (parsedId)
{
case EElementButtonComponents.TxtDescription:
_DescriptionController = child as CControllerElementText ?? throw new ArgumentException("TxtDescription must be a text element");
break;
case EElementButtonComponents.Unknown:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public event EventHandler Clicked;
public void RaiseClicked()
{
Clicked?.Invoke(this, null);
}
public string Description
{
get => _DescriptionController.Text;
set => _DescriptionController.Text = value;
}
}
}
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using Vocaluxe.UI.AbstractElements;
namespace Vocaluxe.UI.BasicElements.Button
{
public class CUiElementButton:CUiElement
{
public CControllerElementButton Controller => new CControllerElementButton();
public override CController BasicController => Controller;
public static CUiElementButton CreateInstance(Dictionary<string, string> properties, IEnumerable<(CUiElement, string)> children)
{
return CUiElement.CreateInstance<CUiElementButton>(properties, children);
}
}
}
@@ -0,0 +1,9 @@
namespace Vocaluxe.UI.BasicElements.Button
{
public enum EElementButtonComponents
{
Unknown,
TxtDescription
}
}
@@ -0,0 +1,15 @@
using Vocaluxe.UI.AbstractElements;
namespace Vocaluxe.UI.BasicElements.Text
{
public class CControllerElementText: CController
{
public override void RegisterChild(CController child, string id)
{
}
public string Text { get; set; }
}
}
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using Vocaluxe.UI.AbstractElements;
namespace Vocaluxe.UI.BasicElements.Text
{
public class CUiElementText:CUiElement
{
public CControllerElementText Controller => new CControllerElementText();
public override CController BasicController => Controller;
public static CUiElementText CreateInstance(Dictionary<string, string> properties, IEnumerable<(CUiElement uiElement, string bindingId)> children)
{
return CUiElement.CreateInstance<CUiElementText>(properties, children);
}
}
}
+30
View File
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using Vocaluxe.UI.AbstractElements;
namespace Vocaluxe.UI
{
public static class CUiElementFactory
{
private delegate CUiElement CreateElementDelgate(Dictionary<string, string> properties, IEnumerable<(CUiElement uiElement, string bindingId)> children);
private static readonly Dictionary<string, CreateElementDelgate> _Mapping = new Dictionary<string, CreateElementDelgate>()
{
{"screenmain", Screens.ScreenMain.CUiScreenMain.CreateInstance},
{"button", BasicElements.Button.CUiElementButton.CreateInstance},
{"text", BasicElements.Text.CUiElementText.CreateInstance}
};
public static CUiElement CreateElement(string type, Dictionary<string, string> properties, IEnumerable<(CUiElement uiElement, string bindingId)> children)
{
_Mapping.TryGetValue(type.ToLower(), out var factoryFunction);
if (factoryFunction == null)
{
throw new ArgumentException("The given id does not match any known element type");
}
return factoryFunction(properties, children);
}
}
}
+10
View File
@@ -0,0 +1,10 @@
using Vocaluxe.UI.Screens;
namespace Vocaluxe.UI
{
public static class DummyClass
{
// Should be somewhere in Vocaluxe
public static EUiScreenType CurrentScreen => EUiScreenType.ScreenMain;
}
}
+88
View File
@@ -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;
}
}
}
+45
View File
@@ -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);
}
}
+36
View File
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Vocaluxe.UI")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Vocaluxe.UI")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("db5e8ec8-5d98-4d0a-8bc6-83b81544c71a")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
+10
View File
@@ -0,0 +1,10 @@
namespace Vocaluxe.UI.Screens
{
public enum EUiScreenType
{
None,
ScreenMain
}
}
@@ -0,0 +1,55 @@
using System;
using Vocaluxe.UI.AbstractElements;
using Vocaluxe.UI.BasicElements.Button;
namespace Vocaluxe.UI.Screens.ScreenMain
{
public class CControllerScreenMain: CController
{
private CControllerElementButton _BtnSingController;
private CControllerElementButton _BtnPartyController;
private CControllerElementButton _BtnOptionsController;
private CControllerElementButton _BtnProfilesController;
private CControllerElementButton _BtnExitController;
public override void RegisterChild(CController child, string id)
{
Enum.TryParse(id, true, out EScreenMainComponents parsedId);
switch (parsedId)
{
case EScreenMainComponents.BtnSing:
_BtnSingController = child as CControllerElementButton ?? throw new ArgumentException("BtnSing must be a button element");
_BtnSingController.Clicked += BtnSingClicked;
break;
case EScreenMainComponents.BtnParty:
_BtnPartyController = child as CControllerElementButton ?? throw new ArgumentException("BtnParty must be a button element");
_BtnPartyController.Clicked += BtnPartyClicked;
break;
case EScreenMainComponents.BtnOptions:
_BtnOptionsController = child as CControllerElementButton ?? throw new ArgumentException("BtnOptions must be a button element");
_BtnOptionsController.Clicked += BtnOptionsClicked;
break;
case EScreenMainComponents.BtnProfiles:
_BtnProfilesController = child as CControllerElementButton ?? throw new ArgumentException("BtnProfiles must be a button element");
_BtnProfilesController.Clicked += BtnProfilesClicked;
break;
case EScreenMainComponents.BtnExit:
_BtnExitController = child as CControllerElementButton ?? throw new ArgumentException("BtnExit must be a button element");
_BtnExitController.Clicked += BtnExitClicked;
break;
case EScreenMainComponents.Unknown:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public event EventHandler BtnSingClicked;
public event EventHandler BtnPartyClicked;
public event EventHandler BtnOptionsClicked;
public event EventHandler BtnProfilesClicked;
public event EventHandler BtnExitClicked;
}
}
@@ -0,0 +1,18 @@
using System.Collections.Generic;
using Vocaluxe.UI.AbstractElements;
namespace Vocaluxe.UI.Screens.ScreenMain
{
public class CUiScreenMain:CUiScreen
{
public CControllerScreenMain Controller => new CControllerScreenMain();
public override CController BasicController => Controller;
public static CUiScreen CreateInstance(Dictionary<string, string> properties, IEnumerable<(CUiElement uiElement, string bindingId)> children)
{
return CUiElement.CreateInstance<CUiScreenMain>(properties, children);
}
public override EUiScreenType ScreenType => EUiScreenType.ScreenMain;
}
}
@@ -0,0 +1,13 @@
namespace Vocaluxe.UI.Screens.ScreenMain
{
public enum EScreenMainComponents
{
Unknown,
BtnSing,
BtnParty,
BtnOptions,
BtnProfiles,
BtnExit
}
}
+54
View File
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Vocaluxe.UI.AbstractElements;
using Vocaluxe.UI.Parser;
using Vocaluxe.UI.Screens;
namespace Vocaluxe.UI
{
// Fasade
public class CUiConnector
{
private readonly CUiScreenStore _Store = new CUiScreenStore();
public CUiConnector(int width, int height)
{
_Store.LoadScreens(@"D:\ProfileFolders\Desktop\Screens", (width,height));
}
public Dictionary<EUiScreenType, CUiScreen> GetAllScreens()
{
return _Store.GetAllScreen();
}
public EUiScreenType GetCurrentScreen()
{
return DummyClass.CurrentScreen;
}
// Dummy to fake a call
public void ChangeScreen(EUiScreenType newScreen)
{
OnScreenChange?.Invoke(this, DummyClass.CurrentScreen);
}
public event EventHandler<EUiScreenType> OnScreenChange;
#region oldStuff
public CUiScreen GetCurrentScreenOld()
{
return _Store[DummyClass.CurrentScreen];
}
// Dummy
public void ChangeScreenOld(EUiScreenType newScreen)
{
OnScreenChangeOld?.Invoke(this, _Store[DummyClass.CurrentScreen]);
}
public event EventHandler<CUiScreen> OnScreenChangeOld;
#endregion
}
}
+4
View File
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Facebook.Yoga" version="1.5.0-pre1" targetFramework="net471" />
</packages>