public StateCollection GetState(BasePlayer player, GUIElementBase definition) { PlayerState state; StateCollection collection; if (_states.TryGetValue(player, out state)) { if (state.TryGetValue(GetElementHash(definition), out collection)) { return(collection); } else { if (_defaultStates.TryGetValue(GetElementHash(definition), out collection)) { return(collection); } } } else { if (_defaultStates.TryGetValue(GetElementHash(definition), out collection)) { return(collection); } } return(null); /* * * var ui = new CGUI.PlayerState(); * * var button = ... // definition * button.OnClick += (o, player) => { * ui.SetState(player, button, (state) => { * return new StateCollection() { * ["Text"] = state["IsNew"] ? "Hello new player!" : "Hellow my fellow old friend", * ["IsNew"] = false * }; * }); * } * * * ui.SetState(player, button, () => { * return new StateCollection() { * ["Text"] = "Hello new player!", * ["IsNew"] = true * }; * }); * ui.Show(player, button); * * */ }
public void SetElement(int row, int column, GUIElementBase element) { float width = 1f / _rows; float height = 1f / _columns; element.Parent = _wrap.Name; element.Transform.AnchorMax = new Vector2(1.0f - row * width, 1.0f - column * height); element.Transform.AnchorMin = new Vector2(1.0f - row * width - width, 1.0f - column * height - height); _items[row, column] = element; }
public void SetState(BasePlayer player, GUIElementBase definition) { var state = GetState(player, definition); if (state == null) { return; } foreach (var kvp in state) { definition.GetType().GetProperty(kvp.Key, BindingFlags.Instance | BindingFlags.Public).SetValue(definition, kvp.Value); } }
public GUIWindow(string title = "", GUIElementBase body = null, string elementName = null) { elementName = elementName ?? GUIObject.GenerateId(); _window = new GUIObject(elementName) { new ImageComponent() { Color = new Color(0.1f, 0.8f, 0.5f, 0.7f) }, new CursorComponent(), new RectTransformComponent() }; _titleElement = new GUIObject(elementName + "_title", _window.Name) { new TextComponent() { Text = title }, new RectTransformComponent() { AnchorMin = new Vector2(0f, 0.9f) } }; _closeButton = new GUIButton("X", elementName + "_close", _window.Name); _closeButton.Transform.AnchorMin = new Vector2(0.9f, 0.9f); _closeButton.Click += (o, player) => { _window.HideUI(player); }; _bodyElement = new GUIObject(elementName + "_body", _window.Name) { new EmptyPanelComponent(), new RectTransformComponent() { AnchorMax = new Vector2(1f, 0.9f) } }; _body = body; if (body != null) { _body.Parent = _bodyElement.Name; } }
public void ChangeState(BasePlayer player, GUIElementBase definition, StateSetter setter) { var state = setter.Invoke(GetState(player, definition)); if (state == null) { return; } _states[player][GetElementHash(definition)] = state; foreach (var kvp in state) { definition.GetType().GetProperty(kvp.Key, BindingFlags.Instance | BindingFlags.Public).SetValue(definition, kvp.Value); } }
public GUIList(string elementName = null, GUIElementBase itemTemplate = null) { elementName = elementName ?? GUIObject.GenerateId(); _list = new List <GUIObject>(); _wrap = new GUIObject(elementName + "_wrap") { new EmptyPanelComponent(), new RectTransformComponent() }; _listWrap = new GUIObject(elementName + "_list", _wrap.Name) { new EmptyPanelComponent(), new RectTransformComponent() { OffsetMin = new Vector2(0f, 0.2f) } }; _itemTemplate = itemTemplate ?? new GUIButton("", null, _listWrap.Name); _prevButton = new GUIButton("<", elementName + "_prev", _wrap.Name); var prevTransform = _prevButton.ButtonElement.GetComponent <RectTransformComponent>(); //prevTransform.OffsetMax = new Vector2(0f, 0.5f); prevTransform.AnchorMax = new Vector2(0.5f, 0.2f); prevTransform.AnchorMin = new Vector2(0f, 0f); _nextButton = new GUIButton(">", elementName + "_next", _wrap.Name); var nextTransform = _nextButton.ButtonElement.GetComponent <RectTransformComponent>(); //nextTransform.OffsetMax = new Vector2(0f, 0.5f); nextTransform.AnchorMin = new Vector2(0.5f, 0f); nextTransform.AnchorMax = new Vector2(1f, 0.2f); }
public void RegisterDefaultState(GUIElementBase definition, bool full = false) { var key = GetElementHash(definition); var value = new StateCollection(); var props = definition.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (var prop in props) { value.Add(prop.Name, prop.GetValue(definition)); } if (full) { var objs = definition.Render(); foreach (var obj in objs) { var name = obj.Name; } } _defaultStates.Add(key, value); }
private string GetElementHash(GUIElementBase element) { return(new StringBuilder().Append(element.GetType().FullName).Append(element.Render()?.First().Name).Append(element.GetHashCode()).ToString()); }