private static void AddChildren(ref XmlNode parent, ref Element rootElement, ref BitmapHelper bitHelper) { for (int i = 0; i < parent.ChildNodes.Count; i++) { XmlNode child = parent.ChildNodes[i]; List<Element.ElementProperties> Properties = new List<Element.ElementProperties>(); Element.ElementProperties property = new Element.ElementProperties(); int width = 0; int height = 0; string ElementText = ""; if (child.Attributes != null) { for (int pi = 0; pi < child.Attributes.Count; pi++) { property = new Element.ElementProperties(); property.Value = child.Attributes[pi].Value; property.type = Element.GetPropertyType(child.Attributes[pi].Name); if (property.type == Element.ElementProperties.PropertyType.Content) { ElementText = (string)property.Value; } Properties.Add(property); } Properties.Add(property); } Element.ElementProperties Width = GetProperty(Properties.ToArray(), Element.ElementProperties.PropertyType.Width); Element.ElementProperties Height = GetProperty(Properties.ToArray(), Element.ElementProperties.PropertyType.Height); Element.ElementType elementType = new Element.ElementType(); elementType = Element.GetElementType(child.Name.ToLower()); int[] size = bitHelper.GetTextWidth(ElementText); if (Width != null) { int iWidth = int.Parse((string)Width.Value); if (iWidth > size[0]) size[0] = iWidth; } if (Height != null) { int iHeight = int.Parse((string)Height.Value); size[1] = iHeight; } Bitmap AfterNine = (System.Drawing.Bitmap)bitHelper.GetImage(size, bitHelper.GetElementImage(elementType)); System.Drawing.Bitmap finalImage = ResizeImage(AfterNine, new Size(AfterNine.Width, size[1])); if (ElementText.Length > 0) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(finalImage)) { g.DrawString(ElementText, new Font("Segoe UI", 11, FontStyle.Regular, GraphicsUnit.Pixel), Brushes.Black, new RectangleF(0, 0, finalImage.Width, finalImage.Height)); // requires font, brush etc } } Element.ElementData elementData = new Element.ElementData(finalImage, new TextureID(finalImage), Properties.ToArray()); Element element = new Element(elementData, elementType); AddChildren(ref child, ref element, ref bitHelper); rootElement.Children.Add(element); } }
public static Element GetUIElements(XmlReader LayoutReader, ref BitmapHelper bitHelper) { List<XMLItem> Items = new List<XMLItem>(); Element parentElem = new Element(null, Element.ElementType.Window); Array props = Enum.GetValues(typeof(Element.ElementProperties.PropertyType)); XmlDocument document = new XmlDocument(LayoutReader.NameTable); document.Load(LayoutReader); XmlNode RootWindow = document.SelectSingleNode("Window"); AddChildren(ref RootWindow, ref parentElem, ref bitHelper); return parentElem; }
//MenuBar = 0, //ContentBackground = 1, //Button = 2, //ButtonPressed = 3, //SideButton = 4, //SideButtonPressed = 5, //TopButton = 6, //TopButtonPressed = 7, //Transparent = 8 public Image GetElementImage(Element.ElementType type, bool Pressed = false) { switch (type) { case Element.ElementType.Button: if (Pressed) return images[3]; else return images[2]; case Element.ElementType.Content: return images[1]; case Element.ElementType.TopBar: return images[0]; case Element.ElementType.Textbox: return images[8]; case Element.ElementType.Window: return images[1]; default: return images[8]; } }
public static void OnClickEvent(Element.ElementProperties prop) { switch ((string)prop.Value) { case "StartGame()": StartGame(); break; } }
private static Element.ElementProperties GetProperty(Element.ElementProperties[] props, Element.ElementProperties.PropertyType type) { Element.ElementProperties property = null; for (int i = 0; i < props.Length; i++) { if (props[i].type == type) { property = props[i]; } } return property; }
private Element IsMouseOverChildren(int x, int y, Element child) { Element mO = null; Element MouseOver = null; if (new System.Drawing.Rectangle(x, y, 1, 1).IntersectsWith(new System.Drawing.Rectangle(child.X, child.Y, child.Width, child.Height))) { if(child.HasProperty(Element.ElementProperties.PropertyType.OnClick)) MouseOver = child; } for (int i = 0; i < child.Children.Count; i++) if((mO = IsMouseOverChildren(x, y, child.Children[i])) != null) MouseOver = mO; return MouseOver; }
private void DrawItem(Element item, int width, int y) { if (item.eData != null) { int ItemWidth = item.Width; switch (item.eType) { case Element.ElementType.Window: break; case Element.ElementType.Button: break; case Element.ElementType.Content: break; case Element.ElementType.Label: break; case Element.ElementType.Listbox: break; case Element.ElementType.Text: break; case Element.ElementType.Textarea: break; case Element.ElementType.Textbox: break; case Element.ElementType.TopBar: break; } int center = (width / 2) - (ItemWidth / 2); for (int i = 0; i < item.eData.Properties.Length; i++) { if (item.eData.Properties[i].type == Element.ElementProperties.PropertyType.PositionY) y += int.Parse((string)item.eData.Properties[i].Value); } item.X = center; item.Y = y; item.DrawTexture(); } for (int i = 0; i < item.Children.Count; i++) { DrawItem(item.Children[i], width, y); } }
void Element_OnEvent(ElementEventType type, Element Value) { switch (type) { case ElementEventType.Normal: break; case ElementEventType.OnClick: GameCore.OnClickEvent(GetProperty(this.eData.Properties, ElementProperties.PropertyType.OnClick)); break; case ElementEventType.MouseOver: break; } }
public bool HasProperty(Element.ElementProperties.PropertyType type) { return GetProperty(this.eData.Properties, type) == null ? false : true; }