/// <summary> /// Create a toggle (check-box) with a label. /// </summary> /// <param name="rect">position and size of the control</param> /// <param name="label">label</param> /// <param name="value">Is this toggle checked or unchecked?</param> /// <returns>new value of the toggle</returns> public static bool Toggle(Rect rect, string label, bool value) { GUIContext g = GetCurrentContext(); Window window = GetCurrentWindow(); if (window.SkipItems) { return(false); } int id = window.GetID(label); // rect rect = window.GetRect(rect); // interact bool hovered; value = GUIBehavior.ToggleBehavior(rect, id, value, out hovered); // render var state = GUIState.Normal; if (hovered) { state = GUIState.Hover; } if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down) { state = GUIState.Active; } GUIAppearance.DrawToggle(rect, label, value, state); return(value); }
public static int ListBox <T>(string label, IReadOnlyList <T> items, int selectedIndex) { Window window = GetCurrentWindow(); if (window.SkipItems) { return(selectedIndex); } int id = window.GetID(label); PushID("ListboxField" + id); var style = GUIStyle.Basic; BeginHorizontal("Field"); BeginVertical("Items", GUILayout.ExpandWidth(true)); { style.Save(); style.ApplySkin(GUIControlName.ListBox); style.PushStretchFactor(false, 1); style.PushCellSpacing(true, 0); for (var i = 0; i < items.Count; i++) { var item = items[i]; string itemText = item.ToString(); var itemId = window.GetID(string.Format("Item_{0}_{1}", i, itemText)); var textSize = style.CalcSize(itemText, GUIState.Normal); var itemRect = window.GetRect(itemId, textSize); bool hovered; bool on = GUIBehavior.ToggleBehavior(itemRect, id, selectedIndex == i, out hovered); if (on) { selectedIndex = i; } var d = window.DrawList; var state = on ? GUIState.Active : GUIState.Normal; d.DrawBoxModel(itemRect, itemText, style, state); } style.Restore(); } EndVertical(); GUILayout.Space("FieldSpacing", GUISkin.Current.FieldSpacing); GUILayout.Label(label, GUILayout.Width((int)GUISkin.Current.LabelWidth)); EndHorizontal(); PopID(); return(selectedIndex); }
/// <summary> /// Create an auto-layout toggle (check-box) with an label. /// </summary> /// <param name="label">text to display</param> /// <param name="value">Is this toggle checked or unchecked?</param> /// <returns>new value of the toggle</returns> public static bool Toggle(string label, bool value) { var g = GetCurrentContext(); var window = GetCurrentWindow(); if (window.SkipItems) { return(false); } //get or create the root node var id = window.GetID(label); var container = window.RenderTree.CurrentContainer; var node = container.GetNodeById(id); var text = Utility.FindRenderedText(label); if (node == null) { node = new Node(id, $"Toggle<{label}>"); node.UseBoxModel = true; node.RuleSet.Replace(GUISkin.Current[GUIControlName.Toggle]); var size = node.RuleSet.CalcContentBoxSize(text, GUIState.Normal); size.Width += size.Height + node.RuleSet.PaddingLeft; node.AttachLayoutEntry(size); } container.AppendChild(node); node.ActiveSelf = true; // rect node.Rect = window.GetRect(id); // interact value = GUIBehavior.ToggleBehavior(node.Rect, id, value, out var hovered); // render var state = GUIState.Normal; if (hovered) { state = GUIState.Hover; } if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down) { state = GUIState.Active; } GUIAppearance.DrawToggle(node, text, value, state); return(value); }
/// <summary> /// Create a toggle (check-box) with a label. /// </summary> /// <param name="rect">position and size of the control</param> /// <param name="label">label</param> /// <param name="value">Is this toggle checked or unchecked?</param> /// <returns>new value of the toggle</returns> public static bool Toggle(Rect rect, string label, bool value) { var g = GetCurrentContext(); var window = GetCurrentWindow(); if (window.SkipItems) { return(false); } //get or create the root node var id = window.GetID(label); var container = window.AbsoluteVisualList; var node = (Node)container.Find(visual => visual.Id == id); var text = Utility.FindRenderedText(label); if (node == null) { node = new Node(id, $"Toggle<{label}>"); container.Add(node); node.UseBoxModel = true; node.RuleSet.Replace(GUISkin.Current[GUIControlName.Toggle]); } node.ActiveSelf = true; // rect node.Rect = window.GetRect(rect); // interact value = GUIBehavior.ToggleBehavior(node.Rect, id, value, out var hovered); // render var state = GUIState.Normal; if (hovered) { state = GUIState.Hover; } if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down) { state = GUIState.Active; } GUIAppearance.DrawToggle(node, text, value, state); return(value); }
/// <summary> /// Create an auto-layout toggle (check-box) with an label. /// </summary> /// <param name="label">text to display</param> /// <param name="value">Is this toggle checked or unchecked?</param> /// <returns>new value of the toggle</returns> public static bool Toggle(string label, bool value) { GUIContext g = GetCurrentContext(); Window window = GetCurrentWindow(); if (window.SkipItems) { return(false); } int id = window.GetID(label); // rect var style = GUIStyle.Basic; var textSize = style.CalcSize(label, GUIState.Normal); var size = new Size(16 + textSize.Width, 16 > textSize.Height ? 16 : textSize.Height); var rect = window.GetRect(id); // interact bool hovered; value = GUIBehavior.ToggleBehavior(rect, id, value, out hovered); // render var state = GUIState.Normal; if (hovered) { state = GUIState.Hover; } if (g.ActiveId == id && Mouse.Instance.LeftButtonState == KeyState.Down) { state = GUIState.Active; } GUIAppearance.DrawToggle(rect, label, value, state); return(value); }