コード例 #1
0
ファイル: UIGenerator.cs プロジェクト: Syntasu/hp-calc
        public T AddControl <T>(string name, float x, float y, float width, float height, UIArgumentList args)
            where T : Control
        {
            //Check if we are not adding any duplicates.
            if (Controls.ContainsKey(name))
            {
                MessageBox.Show("Layout error, trying to add elements with same name. Ignoring element....", "Layout parsing warning");
                return(default(T));
            }

            //Translate the position and the size.
            Vector2 position = grid.Translate(x, y);
            Vector2 size     = grid.Translate(width, height);

            //Create and set the properties of a control.
            Control control = (Control)Activator.CreateInstance(typeof(T));

            control.Location = position;
            control.Size     = size;
            control.Text     = args.Get <string>(UIArgumentProperty.Value);

            //Add the specific properties for each type of control.
            if (control is TextBox textbox)
            {
                textbox.Multiline = args.Get <bool>(UIArgumentProperty.Multiline);
                textbox.ReadOnly  = args.Get <bool>(UIArgumentProperty.Readonly);
            }
            else if (control is CheckBox checkbox)
            {
                checkbox.Checked = args.Get <bool>(UIArgumentProperty.Checked);

                checkbox.CheckedChanged += (obj, sender) =>
                                           MessagePump.DispatchMessage(name, "checked", checkbox.Checked);
            }
            else if (control is RadioButton radioButton)
            {
                radioButton.Checked = args.Get <bool>(UIArgumentProperty.Checked);

                radioButton.CheckedChanged += (obj, sender) =>
                                              MessagePump.DispatchMessage(name, "checked", radioButton.Checked);
            }
            else if (control is Button button)
            {
                button.Click += (obj, sender) =>
                                MessagePump.DispatchMessage(name, "click");
            }

            UIDesc description = new UIDesc(
                name, args.Get <bool>(UIArgumentProperty.Visible),
                new Vector2(x, y), new Vector2(width, height), control
                );

            //Add the control to the collection.
            Controls.Add(name, description);
            return(control as T);
        }