void RefreshView() { // Assume all rule uis will be destroyed List <UIRuleToken> toDestroy = new List <UIRuleToken>(rules); foreach (var rule in editBehavior.rules) { int prevIndex = toDestroy.FindIndex(r => r.editRule == rule); if (prevIndex == -1) { // New rule // Check if we should hide it if (!UIParameterEnum.ShouldSkipValue(rule.condition.type)) { var ruleui = GameObject.Instantiate <UIRuleToken>(ruleTokenPrefab, Vector3.zero, Quaternion.identity, rulesRoot); ruleui.Setup(rule); ruleui.onClick.AddListener(() => NavigationManager.Instance.GoToPage(UIPage.PageId.Rule, rule)); ruleui.onEdit.AddListener(() => NavigationManager.Instance.GoToPage(UIPage.PageId.Rule, rule)); ruleui.onMoveUp.AddListener(() => MoveUp(rule)); ruleui.onMoveDown.AddListener(() => MoveDown(rule)); ruleui.onDuplicate.AddListener(() => DuplicateRule(rule)); ruleui.onRemove.AddListener(() => DeleteRule(rule)); ruleui.onExpand.AddListener(() => ExpandRule(rule)); rules.Add(ruleui); spacer.SetAsLastSibling(); } } else { var ruleui = toDestroy[prevIndex]; // Still there, don't update it toDestroy.RemoveAt(prevIndex); // However, it may need to refresh its display ruleui.Refresh(); // Fix the sibling index int order = editBehavior.rules.IndexOf(rule); ruleui.transform.SetSiblingIndex(order); } } // Remove all remaining rule uis foreach (var ruleui in toDestroy) { rules.Remove(ruleui); GameObject.Destroy(ruleui.gameObject); } }
protected override void SetupControls(string name, System.Func <object> getterFunc, System.Action <object> setterAction, IEnumerable <object> attributes = null) { // Set name nameText.text = name; // List all the enum values and populate the dropdown var initialValue = getterFunc(); int initialValueInt = System.Convert.ToInt32(initialValue); var enumType = initialValue.GetType(); var vals = System.Enum.GetValues(enumType); var validValues = new List <System.Enum>(); foreach (var val in vals) { if (!UIParameterEnum.ShouldSkipValue(val)) { validValues.Add(val as System.Enum); } } for (int i = 0; i < validValues.Count; ++i) { var bitui = GameObject.Instantiate <UIParameterBitfieldBit>(bitPrefab, buttonRoot); int bit = System.Convert.ToInt32(validValues[i]); int backgroundIndex = 1; if (i == 0) { backgroundIndex = 0; } else if (i == validValues.Count - 1) { backgroundIndex = 2; } bitui.Setup(UIParameterEnum.GetNameAttribute(validValues[i], validValues[i].ToString()), (initialValueInt & bit) != 0, backgrounds[backgroundIndex], bitColor, bitColorSelected); bitui.onValueChanged.AddListener((val) => { if (val) { setterAction(System.Enum.Parse(enumType, (System.Convert.ToInt32(getterFunc()) | bit).ToString(), true)); } else { setterAction(System.Enum.Parse(enumType, (System.Convert.ToInt32(getterFunc()) & ~bit).ToString(), true)); } }); bits.Add(bitui); } }