如何构造类以适合此类gui系统?

时间:2019-05-14 03:01:56

标签: c# user-interface

我目前正在尝试在love2dcs中编写GUI库, 但是,我想知道如何以某种方式构造类,以便可以这样声明gui元素。

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"
    );
  }
}

如果有人知道如何执行此操作,并且可以发布一些代码来设置小部件类以接受类型参数,那么这将奏效,

编辑:这更多的是一个类结构问题,然后是一个我该怎么做的问题

这里的问题是我不能直接从List继承,因为我需要同时指定窗口小部件类型和值类型

EDIT2 :因此尝试进行此操作,但是不喜欢我试图从WidgetList隐式转换为Window。

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

1 个答案:

答案 0 :(得分:1)

我试图改善您的工作。
如果我知道库的外观,可以提供更多帮助,我刚刚制作了一个Mock库来完成其余的设计。
也许有人可以从这里接管并提出更好的建议。

请勿这样做:-public class WidgetList : List<Widget> 除非您真的想改善c#List类中存在的内容

如果您将小部件设为类型参数,稍后将遇到协方差和自变量问题,因为您无法将子小部件添加到父小部件列表中。

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

            };

        }
    }