Пример #1
0
        public static Action <object> CreateSetter(MemberExpression expr)
        {
            if (expr == null)
            {
                throw IEDebug.Exception(null, "The expression is not allowed to be null.");
            }
            var             targetGetter = CreateGetter(expr.Expression);
            Action <object> setter;

            if (expr.Member is FieldInfo)
            {
                var asFieldInfo = (FieldInfo)expr.Member;
                setter = value => asFieldInfo.SetValue(targetGetter(), value);
            }
            else if (expr.Member is PropertyInfo)
            {
                var asPropertyInfo = (PropertyInfo)expr.Member;
                setter = value => asPropertyInfo.SetValue(targetGetter(), value, null);
            }
            else
            {
                throw new IEModException(
                          "Expected PropertyInfo or FieldInfo member, but got: "
                          + expr.Member);
            }
            return(setter);
        }
Пример #2
0
        public static Func <object> CreateGetter(Expression expr)
        {
            if (expr == null)
            {
                return(() => null);
            }
            switch (expr.NodeType)
            {
            case ExpressionType.Constant:
                var asConst = (ConstantExpression)expr;
                return(() => asConst.Value);

            case ExpressionType.MemberAccess:
                var asMember     = (MemberExpression)expr;
                var targetGetter = CreateGetter(asMember.Expression);
                if (asMember.Member is FieldInfo)
                {
                    var field = (FieldInfo)asMember.Member;
                    return(() => field.GetValue(targetGetter()));
                }
                if (asMember.Member is PropertyInfo)
                {
                    var prop = (PropertyInfo)asMember.Member;
                    return(() => prop.GetValue(targetGetter(), null));
                }
                throw IEDebug.Exception(null, "Unexpected member type ", asMember.Member.GetType());

            default:
                throw IEDebug.Exception(null, "Unexpected node type {0}", expr.NodeType);
            }
        }
Пример #3
0
        public static void SetObject(string name, Type type, object o)
        {
            var realType = type.IsEnum ? Enum.GetUnderlyingType(type) : type;

            if (realType == typeof(bool))
            {
                PlayerPrefsHelper.SetBool(name, (bool)o);
            }
            else if (realType == typeof(int))
            {
                PlayerPrefs.SetInt(name, (int)o);
            }
            else if (realType == typeof(string))
            {
                PlayerPrefs.SetString(name, (string)o);
            }
            else if (realType == typeof(float))
            {
                PlayerPrefs.SetFloat(name, (float)o);
            }
            else
            {
                IEDebug.Log("Going to try to serialize PlayerPref '{0}' as XML", name);
                SetXmlObject(name, type, o);
            }
        }
Пример #4
0
        public GameObject Button(string caption, string name = null, Vector3?localPos = null)
        {
            /* //+ Reference
             *  DragBtn2 = GameObject.Instantiate(DragBtn1) as GameObject;
             *      DragBtn2.name = "DragActionbarButton";
             *      DragBtn2.transform.parent = DragBtn1.transform.parent;
             *      DragBtn2.transform.localScale = new Vector3(1f, 1f, 1f);
             *      DragBtn2.transform.localPosition = DragBtn1.transform.localPosition;
             *      DragBtn2.transform.localPosition += new Vector3(0f, 50f, 0f);
             *      DragBtn2.ComponentInChildren<UIDragObject>().target = _actionBarTrimB.transform;
             *      DragBtn2.ComponentInChildren<UIMultiSpriteImageButton>().Label.Component<GUIStringLabel>().FormatString = "Drag Hud Bgr";
             */

            if (ExampleButton == null)
            {
                throw IEDebug.Exception(null, "You must initialize ExampleButton to create one.");
            }
            var illegalCharsStr = " :\t\n\r/1#$%^&*().".ToCharArray().Select(char.ToString).ToArray();

            name = name ?? caption.ReplaceAll("_", illegalCharsStr);
            var newButton = (GameObject)Object.Instantiate(ExampleButton);

            newButton.name                    = name;
            newButton.transform.parent        = CurrentParent;
            newButton.transform.localScale    = Vector3.one;
            newButton.transform.localPosition = localPos ?? Vector3.zero;
            newButton.ComponentInDescendants <UIMultiSpriteImageButton>().Label.Component <GUIStringLabel>().FormatString =
                caption;
            return(newButton);
        }
Пример #5
0
 public static void AssertAlive(this GameObject o)
 {
     if (o == null)
     {
         throw IEDebug.Exception(null, "An attempt was made to access a GameObject, but it has been destroyed.");
     }
 }
Пример #6
0
        public static object GetObject(string name, Type type)
        {
            object value;
            var    realType = type.IsEnum ? Enum.GetUnderlyingType(type) : type;

            if (realType == typeof(bool))
            {
                value = PlayerPrefsHelper.GetBool(name, false);
            }
            else if (realType == typeof(int))
            {
                value = PlayerPrefs.GetInt(name, 0);
            }
            else if (realType == typeof(string))
            {
                value = PlayerPrefs.GetString(name, "");
            }
            else if (realType == typeof(float))
            {
                value = PlayerPrefs.GetFloat(name, 0.0f);
            }
            else
            {
                IEDebug.Log("Going to try to deserialize PlayerPref {0} as XML", name);
                return(GetXmlObject(name, type));
            }
            var typedValue = type.IsEnum ? Enum.ToObject(type, value) : value;

            return(typedValue);
        }
Пример #7
0
 public static GameObject Descendant(this Component o, string name)
 {
     if (o == null)
     {
         throw IEDebug.Exception(null, "GameObject cannot be null.");
     }
     return(o.gameObject.Descendant(name));
 }
Пример #8
0
 /// <summary>
 /// Takes an expression which is expected to be a member access expression (e.g. x.Prop1.Field2.Prop3), and returns a setter for setting that member on that target.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="memberAccess"></param>
 /// <returns></returns>
 public static Action <T> CreateSetter <T>(Expression <Func <T> > memberAccess)
 {
     if (!(memberAccess.Body is MemberExpression))
     {
         throw IEDebug.Exception(null, "The topmost expression must be a simple member access expression.");
     }
     return(v => CreateSetter((MemberExpression)memberAccess.Body)(v));
 }
Пример #9
0
 public static T[] ComponentsInDescendants <T>(this GameObject o, bool inactive = true) where T : Component
 {
     if (o == null)
     {
         throw IEDebug.Exception(null, "GameObject cannot be null.");
     }
     return(o.GetComponentsInChildren <T>(inactive));
 }
Пример #10
0
 public static IEnumerable <GameObject> Descendants(this Component o, string name = null)
 {
     if (o == null)
     {
         throw IEDebug.Exception(null, "Component cannot be null.");
     }
     return(o.gameObject.Descendants(name));
 }
Пример #11
0
 public static T Component <T>(this Component o) where T : Component
 {
     if (o == null)
     {
         throw IEDebug.Exception(null, "Component cannot be null.");
     }
     return(o.gameObject.Component <T>());
 }
Пример #12
0
 public static T ComponentInDescendants <T>(this Component c, bool inactive = true) where T : Component
 {
     if (c == null)
     {
         throw IEDebug.Exception(null, "Component cannot be null.");
     }
     return(c.gameObject.ComponentInDescendants <T>(inactive));
 }
Пример #13
0
 public static Component[] Components(this GameObject o, Type t = null)
 {
     if (o == null)
     {
         throw IEDebug.Exception(null, "GameObject cannot be null.");
     }
     return(o.GetComponents(t ?? typeof(Component)));
 }
Пример #14
0
 /// <summary>
 /// Creates a new dropdown/combo box bound to an enum-valued field or property. The enum part is important, as this is how the dropdown's options are generated. Labels are taken from attributes.
 /// </summary>
 /// <typeparam name="T">The type of the field/property. MUST be an enum type.</typeparam>
 /// <param name="enumMemberAccessExpr">A simple member access expression for accessing the property/field you want to bind this control to. For example, <c>() => IEModOptions.YourProperty</c></param>
 /// <param name="width">The width of the dropdown. You have to specify it now because setting it is complicated.</param>
 /// <param name="labelWidth">The width of the GUI label attached to the dropdown. If you set it to 0, there will be no label.</param>
 /// <returns></returns>
 public GameObject EnumBoundDropdown <T>(Expression <Func <T> > enumMemberAccessExpr, int width, int labelWidth)
     where T : struct, IConvertible, IFormattable, IComparable
 {
     if (!typeof(T).IsEnum)
     {
         throw IEDebug.Exception(null, "Expected an enum type, but got {0}", typeof(T));
     }
     return(Dropdown <T>(enumMemberAccessExpr, EnumToChoices(typeof(T)), width, labelWidth));
 }
Пример #15
0
        public static Func <object> CreateBaseGetter <T>(Expression <Func <T> > memberAccess)
        {
            if (!(memberAccess.Body is MemberExpression))
            {
                throw IEDebug.Exception(null, "The topmost expression must be a simple member access expression.");
            }
            var asMemberExpr = (MemberExpression)memberAccess.Body;

            return(CreateGetter(asMemberExpr.Expression));
        }
Пример #16
0
        /// <summary>
        /// Destroys components of the specified type, with the specified name.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="self"></param>
        /// <param name="newState"></param>
        public static void SetBehaviors <T>(this GameObject self, bool newState) where T : Behaviour
        {
            var components = self.Components <T>().ToList();

            if (components.Count == 0)
            {
                IEDebug.Log(null, "WARNING: In GameObject {0}, told to set behaviors of type {1} to {2}, but no behaviors of this type were found.", self.name, typeof(T), newState);
                return;
            }
            components.ForEach(x => x.enabled = newState);
        }
Пример #17
0
        /// <summary>
        /// Returns the ith child of this game object, or throws an exception.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static GameObject Child(this GameObject o, int i)
        {
            if (o == null)
            {
                throw IEDebug.Exception(null, "The GameObject cannot be null.");
            }
            var child = o.transform.GetChild(i);

            if (child == null)
            {
                throw IEDebug.Exception(null, "The GameObject '{0}' has no child at index {1}", o.name, i);
            }
            return(child.gameObject);
        }
Пример #18
0
        /// <summary>
        /// Creates a new Page object, for the properties screen. ExamplePage must be set before calling this.
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public GameObject Page(string name)
        {
            if (ExamplePage == null)
            {
                throw IEDebug.Exception(null, "You must initialize the ExamplePage to create a Page", null);
            }
            var newPage = new GameObject();

            newPage.transform.parent        = ExamplePage.transform.parent;
            newPage.transform.localScale    = ExamplePage.transform.localScale;
            newPage.transform.localPosition = ExamplePage.transform.localPosition;
            newPage.name = name;

            return(newPage);
        }
Пример #19
0
        public static T Component <T>(this GameObject o) where T : Component
        {
            if (o == null)
            {
                throw IEDebug.Exception(null, $"When trying to get component of type {typeof(T)}: GameObject cannot be null.");
            }
            var components = o.Components <T>();

            if (components.Length > 1 || components.Length == 0)
            {
                UnityPrinter.ShallowPrinter.Print(o);
                throw IEDebug.Exception(null, "GameObject '{0}' has {1} components of type {2}, but told to pick exactly one.",
                                        o.name, components.Length, typeof(T));
            }
            return(components[0]);
        }
Пример #20
0
        /// <summary>
        /// Gets the child with this name, or throws an exception.
        /// </summary>
        /// <param name="o"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static GameObject Child(this GameObject o, string name)
        {
            if (o == null)
            {
                throw IEDebug.Exception(null, "When trying to get Child with name '{0}': gameObject cannot be null.", name);
            }
            var seq = o.Children().Where(x => x.gameObject.name == name).ToList();

            if (seq.Count == 0 || seq.Count > 1)
            {
                UnityPrinter.ShallowPrinter.Print(o);
                throw IEDebug.Exception(null,
                                        "GameObject '{0}' has {1} children with the name '{2}', but told to pick exactly one.", o.name, seq.Count, name);
            }
            return(seq[0]);
        }
Пример #21
0
        /// <summary>
        /// Also returns components in Self.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="o"></param>
        /// <param name="inactive"></param>
        /// <returns></returns>
        public static T ComponentInDescendants <T>(this GameObject o, bool inactive = true) where T : Component
        {
            if (o == null)
            {
                throw IEDebug.Exception(null, "GameObject cannot be null.");
            }
            var components = o.ComponentsInDescendants <T>(inactive);

            if (components.Length == 0 || components.Length > 1)
            {
                throw IEDebug.Exception(null,
                                        "GameObject '{0}' has {1} components of type {2} in its children, but told to pick exactly one.", o.name,
                                        components.Length, typeof(T));
            }
            return(components[0]);
        }
Пример #22
0
        /// <summary>
        /// Creates a new checkbox bound to a boolean-valued property or field. Labels are taken from attributes.
        /// </summary>
        /// <param name="memberAccessExpr">A simple member access expression for accessing the property/field you want to bind this control to. For example, <c>() => IEModOptions.YourProperty</c></param>
        /// <returns></returns>
        public GameObject Checkbox(Expression <Func <bool> > memberAccessExpr)
        {
            /*
             * //+ Reference
             *  FixBackerNamesChbox.transform.parent = ModPage;
             *      FixBackerNamesChbox.name = "FixBackerNames";
             *      FixBackerNamesChbox.transform.localScale = new Vector3 (1, 1, 1);
             *      FixBackerNamesChbox.transform.localPosition = new Vector3 (-210, 150, 0);
             *      StringId++;
             *      FixBackerNamesChbox.GetComponent<UIOptionsTag> ().CheckboxLabel = new GUIDatabaseString (StringId);
             *      StringId++;
             *      FixBackerNamesChbox.GetComponent<UIOptionsTag> ().TooltipString = new GUIDatabaseString (StringId);
             *      FixBackerNamesChbox.GetComponent<UIOptionsTag> ().Checkbox.startsChecked = PlayerPrefs.GetInt ("FixBackerNames", 0) > 0 ? true : false;
             *      FixBackerNamesChbox.GetComponent<UIOptionsTag> ().Checkbox.onStateChange = (UICheckbox.OnStateChange) new UICheckbox.OnStateChange((test, state) => {
             */
            if (ExampleCheckbox == null)
            {
                IEDebug.Exception(null, "You must initialize the ExampleCheckbox to create a check box.", null);
            }
            var asMemberExpr = (MemberExpression)memberAccessExpr.Body;
            var member       = asMemberExpr.Member;

            IEDebug.Log("Creating Checkbox : {0}", member.Name);
            var setter = ReflectHelper.CreateSetter(memberAccessExpr);
            var chBox  = (GameObject)GameObject.Instantiate(ExampleCheckbox);

            chBox.transform.parent = CurrentParent;
            var getter = ReflectHelper.CreateGetter(memberAccessExpr);

            chBox.name = asMemberExpr.Member.Name;

            var uiTag = chBox.GetComponent <UIOptionsTag>();

            chBox.transform.localScale    = new Vector3(1, 1, 1);
            chBox.transform.localPosition = new Vector3(0, 0, 0);
            var label = GetLabel(member);
            var desc  = GetDesc(member);

            uiTag.CheckboxLabel = IEModString.Register(label);
            uiTag.TooltipString = IEModString.Register(desc);

            uiTag.Checkbox.startsChecked  = getter();
            uiTag.Checkbox.onStateChange += (sender, state) => setter(state);

            IEDebug.Log("IEMod created: " + chBox.name);
            return(chBox);
        }
Пример #23
0
        public static GameObject Descendant(this GameObject o, string name)
        {
            if (o == null)
            {
                throw IEDebug.Exception(null, "GameObject cannot be null.");
            }
            var descendants = o.Descendants(name);
            var gameObjects = descendants.ToArray();

            if (gameObjects.Length == 0 || gameObjects.Length > 1)
            {
                throw IEDebug.Exception(null,
                                        "Game object '{0}' has {1} descendants with the name '{2}', but told to pick exactly one.", o.name,
                                        gameObjects.Count(), name);
            }
            return(gameObjects[0]);
        }
Пример #24
0
        private static object GetXmlObject(string name, Type type)
        {
            if (!PlayerPrefs.HasKey(name))
            {
                return(null);
            }
            var xmlSerializer = new XmlSerializer(type);
            var content       = PlayerPrefs.GetString(name);

            if (String.IsNullOrEmpty(content))
            {
                return(Activator.CreateInstance(type));
            }
            object obj = null;

            try {
                obj = xmlSerializer.Deserialize(new StringReader(content));
            }
            catch (Exception) {
                IEDebug.Log($"Error when deserializing: {name}");
            }
            return(obj);
        }
Пример #25
0
        /// <summary>
        /// Creates a dropdown bound to a field or property. You need to supply a collection of dropdown choices.
        /// </summary>
        public GameObject Dropdown <T>(Expression <Func <T> > memberAccessExpr,
                                       IEnumerable <IEDropdownChoice> choices,
                                       int width, int labelWidth,
                                       string label   = null,
                                       string tooltip = null
                                       )
        {
            #region Old code

            /*
             * //+ FOR REFERENCE:
             * const int nerfedXPTableWidth = 515;
             * UIDropdownMenu drop;
             * nerfedXPTableDropdown.transform.GetChild(1).GetChild(0).localScale = new Vector3(nerfedXPTableWidth, 32, 1); // width of combobox
             * nerfedXPTableDropdown.transform.GetChild(1).GetChild(3).localPosition = new Vector3(nerfedXPTableWidth - 27, 10, 0); // position of down arrow
             * nerfedXPTableDropdown.transform.GetChild(1).GetChild(2).GetChild(0).localScale = new Vector3(nerfedXPTableWidth, 37, 0); // width of dropdown
             * nerfedXPTableDropdown.transform.GetChild(1).Component<UIDropdownMenu>().OptionRootText.lineWidth = nerfedXPTableWidth;
             * nerfedXPTableDropdown.transform.GetChild(1).Component<UIDropdownMenu>().SelectedText.lineWidth = nerfedXPTableWidth;
             * nerfedXPTableDropdown.transform.GetChild(1).Component<UIDropdownMenu>().OptionGrid.cellWidth = nerfedXPTableWidth;
             * nerfedXPTableDropdown.transform.GetChild(1).Component<UIDropdownMenu>().Options = nerfedXPTableChoices;
             * //nerfedXPTableDropdown.transform.GetChild(1).Component<UIDropdownMenu>().SelectedItem = nerfedXPTableChoices[PlayerPrefs.GetInt("NerfedXPTableSetting")];
             * //nerfedXPTableDropdown.transform.GetChild(1).Component<UIDropdownMenu>().OnDropdownOptionChangedEvent += new UIDropdownMenu.DropdownOptionChanged(OnNerfedXPTableSettingChanged);
             * nerfedXPTableDropdown.transform.GetChild(0).Component<UILabel>().lineWidth = 300;
             * nerfedXPTableDropdown.transform.GetChild(0).Component<UILabel>().shrinkToFit = false;
             * nerfedXPTableDropdown.transform.GetChild(0).Component<GUIStringLabel>().DatabaseString = new GUIDatabaseString(++StringId);
             * nerfedXPTableDropdown.Component<UIOptionsTag>().TooltipString = new GUIDatabaseString(++StringId);
             * // end of adding dropdown
             */

            #endregion

            if (ExampleDropdown == null)
            {
                throw IEDebug.Exception(null, "You must initialize the ExampleDropdown to create a dropdown.", null);
            }
            var pos          = new Vector3(0, 0, 0);
            var getter       = ReflectHelper.CreateGetter(memberAccessExpr);
            var setter       = ReflectHelper.CreateSetter(memberAccessExpr);
            var asMemberExpr = (MemberExpression)memberAccessExpr.Body;
            var comboBox     = (UIOptionsTag)Object.Instantiate(ExampleDropdown);
            comboBox.transform.parent = CurrentParent;
            //+ Basic setup

            comboBox.name = asMemberExpr.Member.Name;
            //! You have to explicitly set localPosition and localScale to something after Instantiate!!!
            //! Otherwise, the UI will broken, but no exception will be reported.
            comboBox.transform.localPosition = pos;
            comboBox.transform.localScale    = new Vector3(1, 1, 1);

            var dropdown = comboBox.transform.ComponentsInDescendants <UIDropdownMenu>(true).Single();
            label   = label ?? ReflectHelper.GetLabelInfo(asMemberExpr.Member);
            tooltip = tooltip ?? ReflectHelper.GetDescriptionInfo(asMemberExpr.Member);

            //+ Set the labels
            //There are multiple things called Label in this visual tree, but only one with a GUIStringLabel component.
            var guiLabel = comboBox.transform.ComponentsInDescendants <GUIStringLabel>(true).Single();
            if (labelWidth > 0)
            {
                guiLabel.DatabaseString = IEModString.Register(label);
                var uiLabel = guiLabel.gameObject.Component <UILabel>();
                uiLabel.lineWidth = labelWidth;
            }
            else
            {
                guiLabel.DatabaseString = IEModString.Register("");
                var uiLabel = guiLabel.gameObject.Component <UILabel>();
                uiLabel.lineWidth = labelWidth;
            }

            var uiTag = comboBox.Component <UIOptionsTag>();
            uiTag.TooltipString = IEModString.Register(tooltip);

            //+ set the choices and SelectedItem
            var choicesArr = choices.ToArray();
            dropdown.Options      = choicesArr.ToArray();
            dropdown.SelectedItem = choicesArr.SingleOrDefault(x => object.Equals(x.Value, getter())) ?? dropdown.Options[0];

            //+ Set position and scale
            //To get the names, I used the GetChild calls as reference, and dumped the comboBox to the log using UnityObjectDumper.
            var comboBoxBackground = comboBox.Descendant("Background");
            comboBoxBackground.transform.localScale = new Vector3(width, 32, 1);             //this is the width of the combobox

            var arrowThing = comboBox.Descendant("ArrowPivot");
            arrowThing.transform.localPosition = new Vector3(width - 27, 10, 0);
            dropdown.OptionRootText.lineWidth  = width;
            dropdown.SelectedText.lineWidth    = width;
            dropdown.OptionGrid.cellWidth      = width;
            var optGrid = dropdown.OptionGrid;
            //+ Set the default handler that binds the control to the member
            dropdown.OnDropdownOptionChangedEvent += option => {
                var asChoice = (IEDropdownChoice)option;
                setter((T)asChoice.Value);
            };

            return(comboBox.gameObject);
        }
Пример #26
0
        /// <summary>
        /// Creates a new dropdown/combo box bound to an enum-valued field or property. The enum part is important, as this is how the dropdown's options are generated. Labels are taken from attributes.
        /// </summary>
        /// <typeparam name="T">The type of the field/property. MUST be an enum type.</typeparam>
        /// <param name="enumMemberAccessExpr">A simple member access expression for accessing the property/field you want to bind this control to. For example, <c>() => IEModOptions.YourProperty</c></param>
        /// <param name="width">The width of the dropdown. You have to specify it now because setting it is complicated.</param>
        /// <param name="labelWidth">The width of the GUI label attached to the dropdown. If you set it to 0, there will be no label.</param>
        /// <param name="localPos">The transform.localPosition of the dropdown. You can set this later.</param>
        /// <returns></returns>
        public GameObject EnumBoundDropdown <T>(Expression <Func <T> > enumMemberAccessExpr, int width, int labelWidth)
            where T : struct, IConvertible, IFormattable, IComparable
        {
            /*
             *
             * //+ FOR REFERENCE:
             * const int nerfedXPTableWidth = 515;
             * UIDropdownMenu drop;
             * nerfedXPTableDropdown.transform.GetChild(1).GetChild(0).localScale = new Vector3(nerfedXPTableWidth, 32, 1); // width of combobox
             * nerfedXPTableDropdown.transform.GetChild(1).GetChild(3).localPosition = new Vector3(nerfedXPTableWidth - 27, 10, 0); // position of down arrow
             * nerfedXPTableDropdown.transform.GetChild(1).GetChild(2).GetChild(0).localScale = new Vector3(nerfedXPTableWidth, 37, 0); // width of dropdown
             * nerfedXPTableDropdown.transform.GetChild(1).GetComponent<UIDropdownMenu>().OptionRootText.lineWidth = nerfedXPTableWidth;
             * nerfedXPTableDropdown.transform.GetChild(1).GetComponent<UIDropdownMenu>().SelectedText.lineWidth = nerfedXPTableWidth;
             * nerfedXPTableDropdown.transform.GetChild(1).GetComponent<UIDropdownMenu>().OptionGrid.cellWidth = nerfedXPTableWidth;
             * nerfedXPTableDropdown.transform.GetChild(1).GetComponent<UIDropdownMenu>().Options = nerfedXPTableChoices;
             * //nerfedXPTableDropdown.transform.GetChild(1).GetComponent<UIDropdownMenu>().SelectedItem = nerfedXPTableChoices[PlayerPrefs.GetInt("NerfedXPTableSetting")];
             * //nerfedXPTableDropdown.transform.GetChild(1).GetComponent<UIDropdownMenu>().OnDropdownOptionChangedEvent += new UIDropdownMenu.DropdownOptionChanged(OnNerfedXPTableSettingChanged);
             * nerfedXPTableDropdown.transform.GetChild(0).GetComponent<UILabel>().lineWidth = 300;
             * nerfedXPTableDropdown.transform.GetChild(0).GetComponent<UILabel>().shrinkToFit = false;
             * nerfedXPTableDropdown.transform.GetChild(0).GetComponent<GUIStringLabel>().DatabaseString = new GUIDatabaseString(++StringId);
             * nerfedXPTableDropdown.GetComponent<UIOptionsTag>().TooltipString = new GUIDatabaseString(++StringId);
             * // end of adding dropdown
             */
            if (!typeof(T).IsEnum)
            {
                IEDebug.Exception(null, "Expected an enum type, but got {0}", typeof(T));
            }
            if (ExampleComboBox == null)
            {
                IEDebug.Exception(null, "You must initialize the ExampleComboBox to create a combo box.", null);
            }
            var pos          = new Vector3(0, 0, 0);
            var getter       = ReflectHelper.CreateGetter(enumMemberAccessExpr);
            var setter       = ReflectHelper.CreateSetter(enumMemberAccessExpr);
            var asMemberExpr = (MemberExpression)enumMemberAccessExpr.Body;
            var comboBox     = (GameObject)GameObject.Instantiate(ExampleComboBox);

            comboBox.transform.parent = CurrentParent;
            //+ Basic setup

            comboBox.name = asMemberExpr.Member.Name;
            //! You have to explicitly set localPosition and localScale to something after Instantiate!!!
            //! Otherwise, the UI will broken, but no exception will be reported.
            comboBox.transform.localPosition = pos;
            comboBox.transform.localScale    = new Vector3(1, 1, 1);

            var dropdown = comboBox.transform.GetComponentsInChildren <UIDropdownMenu>(true).Single();
            var label    = GetLabel(asMemberExpr.Member);
            var desc     = GetDesc(asMemberExpr.Member);

            //+ Set the labels
            //There are multiple things called Label in this visual tree, but only one with a GUIStringLabel component.
            var guiLabel = comboBox.transform.GetComponentsInChildren <GUIStringLabel>(true).Single();

            if (labelWidth > 0)
            {
                guiLabel.DatabaseString = IEModString.Register(label);
                var uiLabel = guiLabel.gameObject.GetComponent <UILabel>();
                uiLabel.lineWidth = labelWidth;
            }
            else
            {
                guiLabel.DatabaseString = IEModString.Register("");
                var uiLabel = guiLabel.gameObject.GetComponent <UILabel>();
                uiLabel.lineWidth = labelWidth;
            }

            var uiTag = comboBox.GetComponent <UIOptionsTag>();

            uiTag.TooltipString = IEModString.Register(desc);

            //+ set the choices and SelectedItem
            var choices = EnumToChoices(typeof(T));

            dropdown.Options      = choices.Cast <object>().ToArray();
            dropdown.SelectedItem = choices.SingleOrDefault(x => x.Value.Equals(getter())) ?? dropdown.Options[0];

            //+ Set position and scale
            //To get the names, I used the GetChild calls as reference, and dumped the comboBox to the log using UnityObjectDumper.
            var comboBoxBackground = comboBox.GetDescendant("Background");
            var oldWidth           = comboBoxBackground.transform.localScale.x;

            comboBoxBackground.transform.localScale = new Vector3(width, 32, 1);             //this is the width of the combobox

            var arrowThing = comboBox.GetDescendant("ArrowPivot");

            arrowThing.transform.localPosition = new Vector3(width - 27, 10, 0);

            dropdown.OptionRootText.lineWidth = width;
            dropdown.SelectedText.lineWidth   = width;
            dropdown.OptionGrid.cellWidth     = width;
            var optGrid = dropdown.OptionGrid;

            //+ Set the default handler that binds the control to the member
            dropdown.OnDropdownOptionChangedEvent += option => {
                var asChoice = (IEComboBoxChoice)option;
                setter((T)asChoice.Value);
            };

            return(comboBox);
        }
Пример #27
0
 public void Print(Object o)
 {
     IEDebug.Log(PrintString(o));
 }