Reputation: 145
I am currently trying to code a gui library in love2dcs, however, I was wondering how I would go about structuring classes in a way so that I can declare gui elements like this..
public static class Editor
{
Window MainWindow
public static void Init()
{
MainWindow = new Window("Window0", "", 0, 0, 256, 240) //List of Widgets
{
new Window("Window1", "", 0, 0, 256, 240), //List of Widgets
new Window("Window2", "", 0, 0, 256, 16) //List of Widgets
{
//String
new TextEdit("TextEdit1", "Hello", 0, 0,256, 16) = "HelloWorld0"
}
};
//1st way to check for window2 press
GetWidget("Window2").Pressed += OnWindow2Pressed()
}
public static void Update()
{
MainWindow.Update();
//2nd way to check for window2 press
if (MainWindow.IsPressed("Window2"))
{
GetWidget("TextEdit1").Value = "HelloWorld1!";
}
}
public static void Render()
{
MainWindow.Render();
}
private static void OnWindow2Pressed()
{
Window window2 = GetWidget("Window2");
GetWidget("Window2").Add
(
new TextEdit("TextEdit1", "Hello", 0, 0+(window2.Value.Count*16), 256, 16) = "HelloWorld3"
);
}
}
if anyone knows how to do this and can post some code on how to setup the widget class to accept type parameters so this works I would be very thankful
EDIT: this is more of a class structure question then a how do I do it question
the issue here is I cannot directly inherit from List because I would need to specify both widget type and value type
EDIT2: so there is a attempt at doing it however it doesn't like that I am trying to implicitly cast to a Window from a WidgetList.
public class WidgetList : List<Widget>
{
public Widget Widget { get; set; }
public WidgetList(Widget widget)
{
widget = Widget;
}
public static implicit operator Widget(WidgetList widgetList)
{
return widgetList.Widget;
}
}
public class Widget
{
//Child Widgets
public Widget Parent { get; private set; } = null;
public WidgetList Children { get; private set; } = null;
//Callers
private Widget(string name, string text, int x, int y, int w, int h)
{
Children = new WidgetList(this);
Name = name;
Text = text;
X = x;
Y = y;
W = w;
H = h;
OnCreate();
}
public static WidgetList New(string name, string text, int x, int y, int w, int h)
{
return new WidgetList(new Widget(name, text, x, y, w, h));
}
}
public class Test
{
public static Window Window;
public static void Do()
{
Window = Window.New("Window0", "", 0, 0, 256, 240) //List of Widgets
{
Window.New("Window1", "", 0, 0, 256, 240), //List of Widgets
Window.New("Window1", "", 0, 0, 256, 240) //List of Widgets
};
}
}
Upvotes: 0
Views: 88
Reputation: 886
I made an attempt to improve what you're trying to do.
I could help more if I knew what the library looked like, I've just made a Mock library to make the rest of the design.
Maybe someone can take over from here and give a better suggestion.
DON'T DO THIS: -- public class WidgetList : List<Widget>
unless you really want to improve something that exists in the c# List
class
If you make the Widgets take a type parameter you'll run into co-variance and contravariance issues later, as you won't be able to add child widgets to a list of parent widgets.
public interface ILibraryWidget
{
void OnInit();
void Update();
void Render();
}
public abstract class BaseWidget : ILibraryWidget
{
public void OnInit()
{
throw new NotImplementedException();
}
public void Render()
{
throw new NotImplementedException();
}
public void Update()
{
throw new NotImplementedException();
}
}
public class Window : ILibraryWidget
{
public int X { get; set; }
public int Y { get; set; }
public int W { get; set; }
public int H { get; set; }
public string Name { get; set; }
public string Text { get; set; }
public Window()
{
}
public void OnInit()
{
Console.WriteLine("Im init");
}
public void Update()
{
Console.WriteLine("Im update");
}
public void Render()
{
Console.WriteLine("Im render");
}
}
public class TextEdit : Window
{
public TextEdit()
{
}
public new void OnInit()
{
Console.WriteLine("Im init text");
}
public new void Update()
{
Console.WriteLine("Im update text");
}
public new void Render()
{
Console.WriteLine("Im render text");
}
}
public class Widget
{
public int X { get; set; }
public int Y { get; set; }
public int W { get; set; }
public int H { get; set; }
public string Name { get; set; }
public string Text { get; set; }
private Window Package { get; set; }
public delegate void Pressed();
//Child Widgets
public Widget Parent { get; private set; }
public List<Widget> Children { get; private set; }
//Callers
public Widget()
{
Children = new List<Widget>();
}
public void OnInit()
{
(Package as Window).OnInit();
}
public void Update()
{
(Package as Window).Update();
}
public void Render()
{
(Package as Window).Render();
}
public static List<Widget> New(string name, string text, int x, int y, int w, int h)
{
return new List<Widget> {
new Widget { Name = name, Text = text, X = x, Y = y, W = w, H = h }
};
}
public static void Main(string[] args)
{
Widget testWidget = new Widget
{
Name = "Window0",
Text = "",
X = 0,
Y = 0,
W = 256,
H = 240,
//List of Widgets
Children = new List<Widget>
{
new Widget{ Name = "Window0_0", Text = "", X = 0, Y = 0, W = 256, H = 240, },
new Widget{ Name = "Window0_1", Text = "", X = 0, Y = 0, W = 256, H = 240, }
}
};
}
}
Upvotes: 1