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); }
public void SetControlVisibility(string name, bool state) { foreach (var control in Controls) { if (control.Key.ToLower() == name.ToLower()) { UIDesc description = control.Value; description.Visible = state; description.DetermineVisibillity(); } } }
/// <summary> /// Refresh the positions and the size of the user interface. /// We adjust the grid and translate all the positions and sizes again. /// </summary> /// <param name="width"> The current width. </param> /// <param name="height"> The current height. </param> public void Refresh(int width, int height) { grid.Refresh(width, height); //Read in the descriptions, use that to determine location and size. foreach (var control in Controls) { UIDesc desc = control.Value; desc.ControlRef.Location = grid.Translate(desc.Position.x, desc.Position.y); desc.ControlRef.Size = grid.Translate(desc.Size.x, desc.Size.y); desc.DetermineVisibillity(); } }