/// <summary> /// Process each individual element we read from the XML> /// </summary> /// <param name="generator">Generator we need to apply it to.</param> /// <param name="element">The element we are currently processing.</param> private void ProccessElement(UIGenerator generator, Element element) { //Check if the required fields are present. if (element.Name == null || element.Position == null || element.Size == null || element.Type == null) { MessageBox.Show( "Cannot add element to layout if the required fields are undefined (required is type, name, position and size)." + $"Element information: Name={element.Name}, Position={element.Position}, Size={element.Size}, Type={element.Type}", "Layout parsing error" ); return; } string elementName = element.Name; Vector2 position = Vector2.FromString(element.Position); Vector2 size = Vector2.FromString(element.Size); Options options = element.Options; UIArgumentList argsList = new UIArgumentList(); //Only popluate the argument list if we have some options available. if (options != null && options.Option != null && options.Option.Count > 0) { argsList.Populate(options.Option); } switch (element.Type.ToLower()) { case "textbox": generator.AddControl <TextBox>(elementName, position.x, position.y, size.x, size.y, argsList); break; case "button": generator.AddControl <Button>(elementName, position.x, position.y, size.x, size.y, argsList); break; case "radio": generator.AddControl <RadioButton>(elementName, position.x, position.y, size.x, size.y, argsList); break; case "checkbox": generator.AddControl <CheckBox>(elementName, position.x, position.y, size.x, size.y, argsList); break; case "list": generator.AddControl <ListBox>(elementName, position.x, position.y, size.x, size.y, argsList); break; case "label": generator.AddControl <Label>(elementName, position.x, position.y, size.x, size.y, argsList); break; } }
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); }