//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; }
/* Start Binding */ protected void attachView (UI render, GameObject self, UIElementMeta meta, object context) { if (context.GetType ().GetInterfaces ().Contains (typeof(IUIBindable)) && meta.Node.ParentNode == null) { IUIBindable bindable = (IUIBindable)context; bindable.View = meta; } }
protected void bindDiv(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes ["bind"] != null) { UIElementBinding bind = ()=>{ 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 (info.PropertyType.GetInterfaces ().Contains (typeof(IList)) && propIndex != -1) { // Because I'm lazey, currently only implementing for lists IList value = (IList)info.GetValue (context, null); meta.Data = value [propIndex]; return; } } meta.Data = null; } }; meta.UpdateBinding += bind; } }
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; } }
//Creates ellement public UIElementMeta Make (UI ui, XmlNode node, Transform parent, object context) { //Filter Attributes string tags = this._name; //Creating object. GameObject g = new GameObject (this._name); g.transform.parent = parent; UIElementMeta meta = new UIElementMeta (g,node, tags); if (this.Builder != null) { this.Builder (ui, g, meta, context); } return meta; }
protected void imageRenderMode (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["renderMode"] != null) { meta.Image.type = (Image.Type)Enum.Parse (typeof(Image.Type), meta.Node.Attributes["renderMode"].Value); } }
protected void imgMaterial (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["material"] != null && UIAssets.Self != null) { Material m = UIAssets.Self.Materials.Find (x => x.name == meta.Node.Attributes ["material"].Value); if (m != null) { meta.Image.material = m; } } }
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"]); } }
protected void imgBackground (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["image"] != null && UIAssets.Self != null) { if (meta.Node.Attributes ["image"].Value.IndexOf ("$") == 0) { } else { Sprite s = UIAssets.Self.Images.Find (x => x.name == meta.Node.Attributes ["image"].Value); if (s != null) { meta.Image.sprite = s; } } } }
protected void foreachChild (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["foreach"] != null) { UIElementBinding binding = () => { meta.ContinueRenderChildren = false; string propName = meta.Node.Attributes ["foreach"].Value; 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) { int needed = data.Count - meta.Object.transform.childCount; for(int i = 0; i< needed; i++){ 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<UIElementMeta> active = new List<UIElementMeta> (); foreach (UIElementMeta 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; } }
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; } } } }
/*End Binding*/ /*Start Slider*/ protected void sliderInit(UI render, GameObject self, UIElementMeta meta, object context){ meta.Slider.fillRect.anchorMin = Vector2.zero; meta.Slider.fillRect.anchorMax = Vector2.one; meta.Slider.fillRect.localScale = Vector3.one; meta.Slider.fillRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, 1); meta.Slider.fillRect.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, 1); meta.Slider.fillRect.offsetMin = Vector2.zero; meta.Slider.fillRect.offsetMax = Vector2.zero; }
protected void dragRecieveBind(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes ["recieveDragBind"] != null) { string name = meta.Node.Attributes["recieveDragBind"].Value; UIElementBinding bind = () => {; DragRecieve del = (DragRecieve)Delegate.CreateDelegate(typeof(DragRecieve), context, name); if(del != null) { meta.DragableRecieve.Dragable += del; }else{ Debug.LogError("Method "+name+" does not match delegate DragRecieve"); } }; meta.UpdateBinding += bind; } }
protected void bindSlider(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes ["bind"] != null) { meta.Data = context; UIElementBinding binding = ()=>{ Type type = context.GetType(); PropertyInfo info = type.GetProperty(meta.Node.Attributes["bind"].Value); if(info != null) { float value = 0; if(info.PropertyType == typeof(float)) { value = (float)info.GetValue(context, null); } meta.Slider.normalizedValue = value; } }; binding.Invoke(); meta.UpdateBinding += binding; } }
protected void bindClick(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["onClick"] != null) { meta.Data = context; meta.Button.onClick.AddListener (() => { Type type = context.GetType(); MethodInfo info = type.GetMethod(meta.Node.Attributes["onClick"].Value); if(info != null) { info.Invoke(context, null); } }); } }
protected void bindImg (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["bindImage"] != null) { meta.Data = context; UIElementBinding binding = ()=>{ Type type = context.GetType(); PropertyInfo info = type.GetProperty(meta.Node.Attributes["bindImage"].Value); if(info != null) { if(info.PropertyType == typeof(Sprite)) { meta.Image.sprite = (Sprite)info.GetValue(context, null); } } }; binding.Invoke(); meta.UpdateBinding += binding; } }
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"]); } }
/*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"]); } }
/*End Image*/ /*Start Drag*/ protected void dragInit(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes ["dragable"] != null) { Dragable drag = meta.Dragable; drag.Meta = meta; } }
/*End Slider*/ /*Start Text*/ protected void txtDefaultValue(UI render, GameObject self, UIElementMeta meta, object context) { meta.Text.text = meta.Node.InnerText; }
protected void dragRecieveInit(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes ["recieveDrag"] != null) { DragableRecieve recieve = meta.DragableRecieve; } }
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; } } }
/*End Drag*/ /*Verbs*/ protected void show(UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["show"] != null) { meta.UpdateBinding += () => { Type type = context.GetType(); PropertyInfo info = type.GetProperty(meta.Node.Attributes["show"].Value); if(info != null) { if(info.PropertyType == typeof(bool)) { self.SetActive((bool)info.GetValue(context, null)); } } }; } }
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"]); } }
//Calculate the positioning of an ellement protected void position (UI renderer, GameObject self, UIElementMeta meta, 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 (meta.Node.Attributes["min"] != null) { min = readCord (meta.Node.Attributes["min"].Value); } if (meta.Node.Attributes["max"] != null) { max = readCord (meta.Node.Attributes ["max"].Value); } 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; }
/*End Text*/ /*Start Image*/ protected void imageColor (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["color"] != null) { meta.Image.color = this.readColor (meta.Node.Attributes ["color"].Value); } }
protected void textFont (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["font"] != null && UIAssets.Self != null) { Font font = UIAssets.Self.Fonts.Find (x => x.name == meta.Node.Attributes ["font"].Value); if (font != null) { meta.Text.font = font; } } }
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 textFontSize (UI render, GameObject self, UIElementMeta meta, object context) { if (meta.Node.Attributes["size"] != null) { meta.Text.fontSize = Convert.ToInt32 (meta.Node.Attributes ["size"].Value); } }
/*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"]); } }