public void Focus(SElement elem) { string id = GetFirstComponentID(elem).ToString(); // if window, use GUI.FocusWindow GUI.FocusControl(id); }
public virtual void HandleChange(object sender, ListChangedEventArgs e) { Parent?.HandleChange(null, null); UpdateStyle(); if (sender == null || e == null) { return; } // TODO Also send event to backend. if (Root != null) { if (e.ListChangedType == ListChangedType.ItemAdded) { SElement child = Children[e.NewIndex]; int disposeIndex = Root.DisposingChildren.IndexOf(child); if (0 <= disposeIndex) { Root.DisposingChildren.RemoveAt(disposeIndex); } child.UpdateStyle(); } else if (e.ListChangedType == ListChangedType.ItemDeleted) { // TODO Dispose. // Root.DisposingChildren.Add(null); } } }
public string NextComponentName(SElement elem) { ++_GlobalComponentSemaphore; ++_ComponentSemaphore; _ComponentElements.Add(elem); return((_ComponentElements.Count - 1).ToString()); }
public override void UpdateStyle() { // This will get called again once this element gets added to the root. if (Root == null) { return; } Size = new Vector2( Parent.Size.x + ((SGroup)Parent).Border * 2f, Backend.LineHeight ); Position = Vector2.zero; base.UpdateStyle(); float x = Size.x; for (int i = 0; i < Children.Count; i++) { SElement child = Children[i]; x -= child.Size.x; child.Position.x = x; } }
public void HandleChange(object sender, ListChangedEventArgs e) { // TODO Also send event to backend. if (e.ListChangedType == ListChangedType.ItemAdded) { SElement child = Children[e.NewIndex]; child.Root = this; child.Parent = null; if (Backend.UpdateStyleOnGUI) { // TODO individual scheduled updates _ScheduledUpdateStyle = true; } else { if (child.Enabled) { child.UpdateStyle(); } } int disposeIndex = DisposingChildren.IndexOf(child); if (0 <= disposeIndex) { DisposingChildren.RemoveAt(disposeIndex); } } else if (e.ListChangedType == ListChangedType.ItemDeleted) { // TODO Dispose. // DisposingChildren.Add(null); } }
public void MoveTextFieldCursor(SElement elem, ref int?cursor, ref int?selection) { if (!IsFocused(GetFirstComponentID(elem))) { return; } TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); if (elem is STextField) { editor.text = ((STextField)elem).Text; } if (cursor != null) { editor.cursorIndex = cursor.Value; } if (selection != null) { editor.selectIndex = selection.Value; } else { editor.selectIndex = cursor.Value; } cursor = null; selection = null; }
public void Button(SElement elem, Vector2 position, Vector2 size, string text, TextAnchor alignment = TextAnchor.MiddleCenter, Texture icon = null) { if (elem != null && IsOnGUIRepainting) { GUI.backgroundColor = elem.Background; } Vector2 border_ = ((elem as SButton) == null ? new Vector2?() : (elem as SButton).Border) ?? new Vector2(4f, 4f); if (!(elem is SButton)) { size += border_; } TextAnchor prevAlignment = _Skin.label.alignment; _Skin.button.alignment = alignment; PreparePosition(elem, ref position); Rect bounds = new Rect(position, size); RegisterNextComponentIn(elem); RegisterOperation(EGUIOperation.Draw, EGUIComponent.Button, bounds, text); GUI.Button(bounds, new GUIContent(text, icon)); _Skin.label.alignment = alignment; }
public bool IsRelativeContext(SElement elem) { if (!IsOnGUIRepainting) { // Everything's absolute in event-handling mode. return(false); } if (elem == null) { // Root is absolute. return(true); } if (elem is SGroup) { return(true); } // SWindowTitleBar gets rendered with the SGroup if (elem is SWindowTitleBar) { return(true); } return(false); }
public int HandleMouseEvent(Event e) { // Console.WriteLine(); // Console.WriteLine("SGUI-IM: Handling mouse event " + e); SGroup lastWindowDragging = _WindowDragging; EventType type = e.type; EMouseStatus status = EMouseStatus.Outside; switch (type) { case EventType.MouseUp: status = EMouseStatus.Up; break; case EventType.MouseDown: status = EMouseStatus.Down; break; case EventType.MouseDrag: status = EMouseStatus.Drag; break; } Vector2 pos = e.mousePosition; int handled = HandleMouseEventIn(e, null); if (handled != -1) { if (type != EventType.MouseMove) { _RegisteredNextElement = false; for (int i = 0; i < _OPs.Count; i++) { _RecreateOperation(i, handled); } _ComponentElements[handled]?.SetMouseStatus(_Secret, status, pos); } else { for (int i = 0; i < _Elements.Count; i++) { SElement elem = _Elements[i]; if (elem == null) { continue; } // TODO replace GetFirstComponentID with possible future HasComponentID elem.SetMouseStatus(_Secret, GetFirstComponentID(elem) == handled ? EMouseStatus.Inside : EMouseStatus.Outside, pos); } } } if (lastWindowDragging == null && _WindowDragging != null) { IList <SElement> children = _WindowDragging.Parent?.Children ?? _WindowDragging.Root.Children; children.Remove(_WindowDragging); children.Insert(children.Count, _WindowDragging); } // Console.WriteLine("SGUI-IM: Mouse event handled: " + handled); // Console.WriteLine(); return(handled); }
public void StartClip(SElement elem) { Vector2 position = elem.Position; PreparePosition(elem, ref position); RegisterNextComponentIn(elem); StartClip(new Rect(position, elem.Size)); }
public void StartClip(SElement elem, Rect bounds) { Vector2 position = Vector2.zero; PreparePosition(elem, ref position); bounds.position += position; RegisterNextComponentIn(elem); StartClip(bounds); }
public void UpdateChildren() { for (int i = 0; i < Children.Count; i++) { SElement child = Children[i]; child.Root = Root; child.Parent = this; child.Update(); } }
public void TextField(SElement elem, Vector2 position, Vector2 size, ref string text) { PreparePosition(elem, ref position); if (elem != null && IsOnGUIRepainting) { GUI.backgroundColor = elem.Background; } Event e = Event.current; bool keyEvent = e.isKey; bool keyDown = e.type == EventType.KeyDown; KeyCode keyCode = e.keyCode; bool submit = keyDown && keyCode == KeyCode.Return; STextField field = elem as STextField; if (field != null && field.OverrideTab && keyEvent && (e.keyCode == KeyCode.Tab || e.character == '\t')) { /*e.Use();*//*NO*/ } RegisterNextComponentIn(elem); string prevText = text; TextField(new Rect(position, elem.Size), ref text); if (IsOnGUIRepainting) { elem.SetFocused(_Secret, IsFocused(CurrentComponentID)); } else if (field != null && elem.IsFocused) { TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl); field.SetCursorIndex(_Secret, editor.cursorIndex, editor.selectIndex); if (submit) { text = field.TextOnSubmit ?? prevText; } if (prevText != text && field.OnTextUpdate != null) { field.OnTextUpdate(field, prevText); } if (keyEvent && field.OnKey != null) { field.OnKey(field, keyDown, keyCode); } if (submit && field.OnSubmit != null) { field.OnSubmit(field, prevText); } } }
public int RegisterNextComponentIn(SElement elem) { _RegisteredNextElement = true; GUI.SetNextControlName(NextComponentName(elem)); if (!_Elements.Contains(elem)) { _Elements.Add(elem); } // Console.WriteLine("SGUI-IM: Registered E" + CurrentElementID + "C" + CurrentComponentID + " (" + elem + ")"); return(CurrentComponentID); }
private void Text_(SElement elem, Vector2 position, Vector2 size, string text, TextAnchor alignment = TextAnchor.MiddleCenter, Texture icon = null, bool registerProperly = true) { float y = 0f; Vector2 iconScale = ((elem as SLabel) == null ? new Vector2?() : (elem as SLabel).IconScale) ?? ((elem as SButton) == null ? new Vector2?() : (elem as SButton).IconScale) ?? Vector2.one; float iconWidth = icon != null ? icon.width * iconScale.x : 0f; float iconHeight = icon != null ? icon.height * iconScale.y : 0f; if (icon != null) { iconWidth += 1f; } PreparePosition(elem, ref position); if (icon != null) { y = size.y * 0.5f - LineHeight * 0.5f; } Rect bounds = new Rect(position + new Vector2(iconWidth, y), size - new Vector2(iconWidth, y * 2f)); Color prevColor = _Skin.label.normal.textColor; _Skin.label.normal.textColor = (elem == null ? new Color?() : elem.Foreground) ?? _Skin.label.normal.textColor; TextAnchor prevAlignment = _Skin.label.alignment; _Skin.label.alignment = alignment; RegisterNextComponentIn(elem); RegisterOperation(EGUIOperation.Draw, EGUIComponent.Label, registerProperly ? bounds : NULLRECT, text); if (icon != null) { text = " " + text; } GUI.Label(bounds, text); _Skin.label.normal.textColor = prevColor; _Skin.label.alignment = prevAlignment; if (icon != null) { Texture( null, position + new Vector2(0f, size.y * 0.5f - iconHeight * 0.5f), new Vector2(iconWidth, iconHeight), icon, ((elem == null || elem.Colors == null) ? new Color?() : elem.Colors.AtOr(1, Color.white)) ?? Color.white ); } }
public void Update() { if (!Visible) { return; } Main = this; if (!Backend.Initialized) { return; } for (int i = 0; i < Children.Count; i++) { SElement child = Children[i]; child.Root = this; child.Parent = null; if (child.Enabled) { child.Update(); } } if (AdoptedChildren.Count != 0) { for (int i = 0; i < AdoptedChildren.Count; i++) { SElement child = AdoptedChildren[i]; if (child.Parent == null && !Children.Contains(child)) { // Child had no memory who its parents were, so let's just adopt it. Children.Add(child); } else if (child.Parent != null && !child.Parent.Children.Contains(child)) { // Child remembers about its parents, but parents not about child. Let's force parents to care. child.Parent.Children.Add(child); } } AdoptedChildren.Clear(); } if (DisposingChildren.Count != 0 && !Backend.RenderOnGUI) { for (int i = 0; i < DisposingChildren.Count; i++) { DisposingChildren[i].Dispose(); } DisposingChildren.Clear(); } }
public virtual void UpdateChildrenStyles() { for (int i = 0; i < Children.Count; i++) { SElement child = Children[i]; child.Root = Root; child.Parent = this; if (child.Enabled) { child.UpdateStyle(); } } }
public void RenderChildren() { for (int i = 0; i < Children.Count; i++) { SElement child = Children[i]; child.Root = Root; child.Parent = this; if (!child.Visible) { continue; } child.Render(); } }
public override void HandleChange(object sender, ListChangedEventArgs e) { if (e != null && e.ListChangedType == ListChangedType.ItemAdded) { SElement elem = Children[e.NewIndex]; if (elem is SWindowTitleBar) { Children.RemoveAt(e.NewIndex); return; } } base.HandleChange(sender, e); }
public void UpdateStyle() { if (Backend.UpdateStyleOnGUI && Backend.CurrentRoot == null) { _ScheduledUpdateStyle = true; return; } for (int i = 0; i < Children.Count; i++) { SElement child = Children[i]; child.Root = this; child.Parent = null; child.UpdateStyle(); } }
public void GrowToFit(SElement elem) { Vector2 max = elem.Position + elem.Size; ContentSize.x = Mathf.Max(ContentSize.x, max.x + GrowExtra.x); ContentSize.y = Mathf.Max(ContentSize.y, max.y + GrowExtra.y); if ((AutoGrowDirection & EDirection.Horizontal) == EDirection.Horizontal) { Size.x = ContentSize.x; } if ((AutoGrowDirection & EDirection.Vertical) == EDirection.Vertical) { Size.y = ContentSize.y; } }
public void OnGUI() { SGUIRoot root = CurrentRoot; if (_Reason.isMouse || _Reason.type == EventType.ScrollWheel) { LastMouseEventConsumed = HandleMouseEvent(Event.current) != -1; return; } // Your normal rendering run. for (int i = 0; i < root.Children.Count; i++) { SElement child = root.Children[i]; child.Root = root; child.Parent = null; if (!child.Visible) { continue; } child.Render(); } // After the normal (possibly) repaint run, check for any mouse movement. if (IsOnGUIRepainting) { Vector2 mousePosition = Input.mousePosition; mousePosition = new Vector2( mousePosition.x, root.Size.y - mousePosition.y - 1f ); if (_LastMousePosition.x <= -1f && _LastMousePosition.y <= -1f) { _LastMousePosition = mousePosition; } else { HandleMouseEvent(new Event(Event.current.displayIndex) { type = EventType.MouseMove, mousePosition = mousePosition, delta = mousePosition - _LastMousePosition }); _LastMousePosition = mousePosition; } } }
public int HandleMouseEventIn(Event e, SElement elem) { int handled; if (elem != null) { if (!elem.Visible) { return(-1); } if (elem is SGroup) { return(HandleMouseEventInGroup(e, (SGroup)elem)); } if (!new Rect(elem.AbsoluteOffset + elem.Position, elem.Size).Contains(e.mousePosition)) { return(-1); } if (!elem.Enabled) { return(GetFirstComponentID(elem)); } } IList <SElement> children = (elem == null ? null : elem.Children) ?? CurrentRoot.Children; for (int i = children.Count - 1; 0 <= i; i--) { if ((handled = HandleMouseEventIn(e, children[i])) != -1) { return(handled); } } if (elem == null) { return(-1); } if (e.type == EventType.ScrollWheel) { return(-1); } return(GetFirstComponentID(elem)); }
public override void OnInit() { OrigColors = new Color[Elem.Colors.Length]; Elem.Colors.CopyTo(OrigColors, 0); for (int i = 0; i < Elem.Children.Count; i++) { SElement child = Elem.Children[i]; if (child.Modifiers.Where(m => m is SFadeAnimation) != null) { continue; } SFadeAnimation clone = (SFadeAnimation)MemberwiseClone(); clone.Elem = child; child.Modifiers.Add(clone); } }
public void PreparePosition(SElement elem, ref Vector2 position) { if (elem == null) { return; } // IMGUI draws relative to the current window. // If this is a window or similar, don't add the absolute offset. // If not, add the element position to draw relative to *that*. if (IsRelativeContext(elem)) { return; } position += elem.InnerOrigin; PreparePosition(elem.Parent, ref position); }
public void AutoLayoutHorizontal(int index, SElement elem) { if (elem == null || !elem.Visible) { return; } if (index == 0) { _CurrentAutoLayoutHorizontalX = 0f; } if (AutoLayoutVerticalStretch) { elem.Size.y = InnerSize.y; } elem.Position = new Vector2(_CurrentAutoLayoutHorizontalX, 0f); _CurrentAutoLayoutHorizontalX += elem.InnerSize.x + AutoLayoutPadding; GrowToFit(elem); }
public void AutoLayoutVertical(int index, SElement elem) { if (elem == null || !elem.Visible) { return; } if (index == 0) { _CurrentAutoLayoutVerticalY = 0f; } if (AutoLayoutVerticalStretch) { elem.Size.x = InnerSize.x; } elem.Position = new Vector2(0f, _CurrentAutoLayoutVerticalY); _CurrentAutoLayoutVerticalY += elem.InnerSize.y + AutoLayoutPadding; GrowToFit(elem); }
public void AutoLayoutLabeledInput(int index, SElement elem) { if (elem == null || index >= 2) { return; } if (index == 0) { Background = Background * 0f; if ((AutoGrowDirection & EDirection.Horizontal) == EDirection.Horizontal) { Size.x = Parent.InnerSize.x; } elem.Position = Vector2.zero; if (AutoLayoutLabelWidth <= 0f) { elem.Size = Backend.MeasureText(((SLabel)elem).Text, font: elem.Font); } else { elem.Size = new Vector2(AutoLayoutLabelWidth, Backend.LineHeight); } if ((AutoGrowDirection & EDirection.Vertical) == EDirection.Vertical) { Size.y = elem.Size.y; } } else if (index == 1) { elem.Position = new Vector2(Children[0].Size.x + AutoLayoutPadding, 0f); elem.Size = new Vector2(Size.x - Children[0].Size.x - AutoLayoutPadding, Size.y); } }
public void Button(SElement elem, Vector2 position, Vector2 size, string text, TextAnchor alignment = TextAnchor.MiddleCenter, Texture icon = null) { GUI.skin.font = (Font)elem?.Font ?? _Font; if (elem != null && IsOnGUIRepainting) { GUI.backgroundColor = elem.Background; } Vector2 border_ = (elem as SButton)?.Border ?? new Vector2(4f, 4f); if (!(elem is SButton)) { size += border_; } Rect(elem, position, size, GUI.backgroundColor); Text_( elem, position + border_, size - border_ * 2f, text, alignment, icon, false); }
public void Text(SElement elem, Vector2 position, Vector2 size, string text, TextAnchor alignment = TextAnchor.MiddleCenter, Texture icon = null) { Text_(elem, position, size, text, alignment, icon); }