using System.Collections.Generic;
public static void Main()
var ui = new DynamicUI(V2.Zero);
ui.BeginGroup("Test Group");
ui.BeginHorizontalLayout();
ui.AddElement<TextElement>(element =>
element.Value = string.Format("This is a text element, part of a {0} layout in group ID: {1}", ui.Layout.Direction.ToString(), ui.Group.ID);
ui.BeginGroup("Test Group/2");
ui.BeginVerticalLayout();
ui.AddElement<TextElement>(element =>
element.Value = string.Format("This is a text element, part of a {0} layout in group ID: {1}", ui.Layout.Direction.ToString(), ui.Group.ID);
private HashList<Group> groups = new HashList<Group>();
private DynamicUIObject context;
return groups[groups.Count - 1];
public DynamicUI(V2 position, string name = "Default")
context = new DynamicUIObject(position, name);
public Group BeginGroup(string id = "", Action<Group> modifier = null)
if (string.IsNullOrEmpty(id))
id = Guid.NewGuid().ToString();
if (groups.Any(grp => grp.ID == id))
Console.WriteLine("Group " + id + " already exists! Cannot begin a duplicate. Returning existing copy...");
return groups.Where(grp => grp.ID == id).Single();
var newGroup = new Group(id);
Console.WriteLine("No groups left to end! Returning...");
public Layout BeginVerticalLayout()
return Group.BeginVerticalLayout();
public Layout BeginHorizontalLayout()
return Group.BeginHorizontalLayout();
public T AddElement<T>(Action<T> modifier = null)
where T : UIElementBase, new()
return Layout.AddElement<T>(modifier);
public class DynamicUIObject
public DynamicUIObject(V2 position, string name)
public V2(float x, float y)
private List<UIElementBase> elements = new List<UIElementBase>();
private DynamicUIObject context;
private Directions layoutDirection;
public Directions Direction
public Layout(Directions direction)
context = new DynamicUIObject(V2.Zero, direction.ToString() + " Layout");
layoutDirection = direction;
public T AddElement<T>(Action<T> modifier = null)
where T : UIElementBase, new()
Console.WriteLine("Layout " + layoutDirection.ToString());
private HashList<Layout> stack = new HashList<Layout>();
return stack[stack.Count - 1];
public Layout BeginVerticalLayout()
stack.Push(new Layout(Layout.Directions.Vertical));
public Layout BeginHorizontalLayout()
stack.Push(new Layout(Layout.Directions.Horizontal));
Console.WriteLine("No layouts left to end! Returning...");
Console.WriteLine("Group " + ID);
public class HashList<T> : List<T>
var item = this[Count - 1];
var item = this[Count - 1];
public class UIElementBase : UIBase
public class TextElement : UIElement<TextElement>
public string Value = "Foo";
public override TextElement GetType()
public override string ToString()
public class NumberElement : UIElement<NumberElement>
public override NumberElement GetType()
public override string ToString()
public abstract class UIElement<T> : UIElementBase where T : UIElementBase
public new abstract T GetType();
public new abstract string ToString();
Console.WriteLine(ToString());