//Creates ellement public UIEllementMeta Make(UI ui, XmlNode node, Transform parent, object context) { //Filter Attributes string tags = this._name; Dictionary<string, string> attrs = new Dictionary<string, string> (); foreach (XmlAttribute field in node.Attributes) { if(field.Name.ToLower() == "id") { tags += " "+field.Value; }else if (this._fields.Contains (field.Name)) { if (!attrs.ContainsKey (field.Name)) { attrs.Add (field.Name, field.Value); } } } //Creating object. GameObject g = new GameObject (this._name); g.transform.parent = parent; UIEllementMeta meta = new UIEllementMeta (g,node, tags); if (this.Builder != null) { this.Builder (ui, g, meta, attrs, context); } return meta; }
protected void bindText(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("bind")) { Text txt = this.getText (self, meta.Node); string bind = attributes ["bind"]; //If the bind is set to value, we presume the super structure contianing the ellement is handling the setting of the bindpath. Otherwise we pull the bind path. if (bind != "value") { meta.BindPath = bind; } UIEllementBinding binding = () => { if(meta.BindPath != null) { string propName = null; int propIndex = -1; //Array indexed values are refed using #name:#index int pos = meta.BindPath.IndexOf (":"); if (pos != -1) { propName = meta.BindPath.Substring (0, pos); propIndex = Convert.ToInt32 (meta.BindPath.Substring (pos+1, meta.BindPath.Length - (pos + 1))); } else { propName = meta.BindPath; } Type type = context.GetType (); PropertyInfo info = type.GetProperty (propName); if (info != null) { if (propIndex == -1) { txt.text = info.GetValue (context, null).ToString (); } else if (info.PropertyType.GetInterfaces ().Contains (typeof(IList))) { // Because I'm lazey, currently only implementing for lists IList value = (IList)info.GetValue (context, null); txt.text = value [propIndex].ToString (); } else { txt.text = meta.Node.Value; } } else { txt.text = meta.Node.Value; } } }; binding.Invoke (); meta.UpdateBinding += binding; } }
protected void textFontSize(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("size")) { Text txt = this.getText (self, meta.Node); txt.fontSize = Convert.ToInt32 (attributes ["size"]); } }
/*End Binding*/ /*Start Text*/ protected void textMaterial(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("material") && UIAssets.Self != null) { Text txt = this.getText (self, meta.Node); txt.material = UIAssets.Self.Materials.Find (x => x.name == attributes ["material"]); } }
protected void textColor(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("color")) { Text txt = this.getText (self, meta.Node); txt.color = readColor (attributes ["color"]); } }
protected void textFont(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("font") && UIAssets.Self != null) { Text txt = this.getText (self, meta.Node); Font font = UIAssets.Self.Fonts.Find (x => x.name == attributes ["font"]); if (font != null) { txt.font = font; } } }
protected void imgMaterial(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("material") && UIAssets.Self != null) { Image img = this.getImage (self, meta.Node); Material m = UIAssets.Self.Materials.Find (x => x.name == attributes ["material"]); if (m != null) { img.material = m; } } }
//Calculate the positioning of an ellement protected void position(UI renderer, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { Vector2 min = Vector2.zero; Vector2 max = Vector2.one; //If the min and max fields have been set use them, otherwise default to fullscreen if (attributes.ContainsKey ("min")) { min = readCord (attributes ["min"]); } if (attributes.ContainsKey ("max")) { max = readCord (attributes ["max"]); } meta.Rect.anchorMin = min; meta.Rect.anchorMax = max; meta.Rect.localScale = Vector3.one; meta.Rect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, 1); meta.Rect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, 1); meta.Rect.offsetMin = Vector2.zero; meta.Rect.offsetMax = Vector2.zero; }
protected void imgBackground(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("background") && UIAssets.Self != null) { Image img = this.getImage (self, meta.Node); if (attributes ["background"].IndexOf ("$") == 0) { } else { Sprite s = UIAssets.Self.Images.Find (x => x.name == attributes ["background"]); if (s != null) { img.sprite = s; } } } }
protected void imageRenderMode(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("renderMode")) { Image img = this.getImage (self, meta.Node); img.type = (Image.Type)Enum.Parse (typeof(Image.Type), attributes ["renderMode"]); } }
/*End Text*/ /*Start Image*/ protected void imageColor(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("color")) { Image img = this.getImage (self, meta.Node); img.color = this.readColor (attributes ["color"]); } }
/*End Image*/ protected void foreachChild(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (attributes.ContainsKey ("foreach")) { UIEllementBinding binding = () => { meta.ContinueRenderChildren = false; string propName = attributes ["foreach"]; Type t = context.GetType (); PropertyInfo prop = t.GetProperty (propName); if (prop != null) { if (prop.PropertyType.GetInterfaces ().Contains (typeof(IList))) { IList data = (IList)prop.GetValue (context, null); /* Create needed ellements */ if (meta.Object.transform.childCount < data.Count) { foreach (object entry in data) { foreach (XmlNode child in meta.Node.ChildNodes) { meta.AddChild (render.Render (child, self.transform, context)); } } } //Toggle on ellements that we'll be using, disable the ones we won't be using int ellementsNeeded = 0; List<UIEllementMeta> active = new List<UIEllementMeta> (); foreach (UIEllementMeta child in meta) { //Enable ellements if (ellementsNeeded < data.Count) { child.Object.SetActive (true); child.BindPath = propName; active.Add (child); } else { //Disable elements child.Object.SetActive (false); } ellementsNeeded++; } //Layout enabled ellements this.groupLayout (active); } } }; binding.Invoke (); meta.UpdateBinding += binding; } }
/* Start Binding */ protected void bindView(UI render, GameObject self, UIEllementMeta meta, Dictionary<string, string> attributes, object context) { if (context.GetType ().GetInterfaces ().Contains (typeof(IUIBindable)) && meta.Node.ParentNode == null) { IUIBindable bindable = (IUIBindable)context; bindable.View = meta; } }