How do I structure classes to fit this type of gui system?

Shadowblitz16

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
            };
        }
    }
heyNow

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, }
                }

            };

        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I correctly add type-hints to Mixin classes?

How should I structure a Java application, where do I put my classes?

How do I make this GUI?

how do i let text fit to UIButton?

How do I create an Arbitrary for a System.Type?

How do I get ConfigurationSection property as a System.Type

How do I declare a System data type in UWP/RT XAML?

How do i fit a square around a point?

How do I structure JSON?

How do I 'fit a line' to a cluster of pixels?

How do I subset a list with mixed data type and data structure?

How do I fix this type of error System.Diagnostics.DebuggerStepThrough()

How do generic types fit into the type hierarchy?

How do I fit tensorflow ImageDataGenerator

How do I know if this Motherboard will fit in the case?

How do I use an IF type structure in an UPDATE statement?

How do I make an annotation, that adds all classes of that type to a list

How do I include a list type feature in sklearn.svm.libsvm.fit() classifier?

How do I fit a line to this data?

How do I deserialize this type of data structure

How do I make sure all GUI input variables can be accessed by certain classes and function calls?

How do I address static members of classes added via Add-Type, from other classes?

How can I optimize the structure of my classes

How do I fit my image into the container?

How do I unitize a variable in structure that is of type structure

How do I fit the borders in the input text type?

agda: How do I tell the type system that two types are equal?

How do I display my array that is of a structure type

How do I fit this curve?