public void DoGUI() { if (isEnable) { bool changed; using (new GUILayout.HorizontalScope()) { changed = isOpen != GUILayout.Toggle(isOpen, "❏ " + name, Style.toggle); titleAction?.Invoke(); } if (changed) { isOpen = !isOpen; if (isOpen) { isMoved = false; rect.position = RGUIUtility.GetMouseScreenPos() + Vector2.right * 50f; onOpen?.Invoke(this); } else { CloseWindow(); } } if (isOpen) { WindowInvoker.Add(this); } } }
public static int Popup(Rect launchRect, int mouseButton, int selectionIndex, string[] displayOptions, string label = "") { var ret = selectionIndex; var controlId = GUIUtility.GetControlID(FocusType.Passive); // not Popup Owner if (popupControlId != controlId) { var ev = Event.current; var pos = ev.mousePosition; if ((ev.type == EventType.MouseUp) && ((mouseButton < 0) || (ev.button == mouseButton)) && launchRect.Contains(pos) && displayOptions != null && displayOptions.Any() ) { popupWindow.pos = RGUIUtility.GetMouseScreenPos(Vector2.one * 150f); popupControlId = controlId; ev.Use(); } } // Active else { var type = Event.current.type; var result = popupWindow.result; if (result.HasValue && type == EventType.Layout) { if (result.Value >= 0) // -1 when the popup is closed by clicking outside the window { ret = result.Value; } popupWindow.result = null; popupControlId = 0; } else { if ((type == EventType.Layout) || (type == EventType.Repaint)) { var buttonStyle = RGUIStyle.popupFlatButton; var contentSize = Vector2.zero; for (var i = 0; i < displayOptions.Length; ++i) { var textSize = buttonStyle.CalcSize(RGUIUtility.TempContent(displayOptions[i])); contentSize.x = Mathf.Max(contentSize.x, textSize.x); contentSize.y += textSize.y; } var margin = buttonStyle.margin; contentSize.y += Mathf.Max(0, displayOptions.Length - 1) * Mathf.Max(margin.top, margin.bottom); // is this right? var vbarSkin = GUI.skin.verticalScrollbar; var vbarSize = vbarSkin.CalcScreenSize(Vector2.zero); var vbarMargin = vbarSkin.margin; var hbarSkin = GUI.skin.horizontalScrollbar; var hbarSize = hbarSkin.CalcScreenSize(Vector2.zero); var hbarMargin = hbarSkin.margin; const float offset = 5f; contentSize += new Vector2(vbarSize.x + vbarMargin.horizontal, hbarSize.y + hbarMargin.vertical) + Vector2.one * offset; var size = RGUIStyle.popup.CalcScreenSize(contentSize); var maxSize = new Vector2(Screen.width, Screen.height) - popupWindow.pos; popupWindow.size = Vector2.Min(size, maxSize); } popupWindow.label = label; popupWindow.displayOptions = displayOptions; WindowInvoker.Add(popupWindow); } } return(ret); }
public static int SearchablePopup(Rect launchRect, int mouseButton, int selectionIndex, string[] displayOptions, ref string searchTerms, string label = "") { var ret = selectionIndex; var controlId = GUIUtility.GetControlID(FocusType.Passive); // not Popup Owner if (s_popupControlId != controlId) { var ev = Event.current; var pos = ev.mousePosition; if ((ev.type == EventType.MouseUp) && ((mouseButton < 0) || (ev.button == mouseButton)) && launchRect.Contains(pos) && displayOptions != null && displayOptions.Length > 0 ) { s_searchablePopupWindow.pos = RGUIUtility.GetMouseScreenPos(Vector2.one * 150f); s_popupControlId = controlId; ev.Use(); } } // Active else { var type = Event.current.type; var result = s_searchablePopupWindow.result; if (s_searchablePopupWindow._searchTerms != null) { if (!searchTerms.Equals(s_searchablePopupWindow._searchTerms)) { ret = -1; } searchTerms = s_searchablePopupWindow._searchTerms; } if (result.HasValue && type == EventType.Layout) { if (result.Value >= 0) // -1 when the popup is closed by clicking outside the window { ret = result.Value; } s_searchablePopupWindow.result = null; s_popupControlId = 0; } else { if ((type == EventType.Layout) || (type == EventType.Repaint)) { var buttonStyle = new GUIStyle(InterfaceMaker.CustomSkin.button) { padding = new RectOffset(24, 48, 2, 2) }; var textfieldStyle = InterfaceMaker.CustomSkin.textField; var contentSize = Vector2.zero; for (var i = 0; i < displayOptions.Length; ++i) { var textSize = buttonStyle.CalcSize(RGUIUtility.TempContent(displayOptions[i])); contentSize.x = Mathf.Max(contentSize.x, textSize.x); contentSize.y += textSize.y; } if (displayOptions.Length == 0) { contentSize.x += 150; } var margin = buttonStyle.margin; contentSize.y += Mathf.Max(0, displayOptions.Length - 1) * Mathf.Max(margin.top, margin.bottom); // is this right? var vbarSkin = InterfaceMaker.CustomSkin.verticalScrollbar; var vbarSize = vbarSkin.CalcScreenSize(Vector2.zero); var vbarMargin = vbarSkin.margin; var hbarSkin = InterfaceMaker.CustomSkin.horizontalScrollbar; var hbarSize = hbarSkin.CalcScreenSize(Vector2.zero); var hbarMargin = hbarSkin.margin; const float offset = 5f; contentSize += new Vector2(vbarSize.x + vbarMargin.horizontal, hbarSize.y + hbarMargin.vertical) + Vector2.one * offset; contentSize.y += textfieldStyle.CalcHeight(RGUIUtility.TempContent("t"), contentSize.x); var size = InterfaceMaker.CustomSkin.GetStyle("popup").CalcScreenSize(contentSize); var maxSize = new Vector2(Screen.width, Screen.height) - s_searchablePopupWindow.pos; s_searchablePopupWindow.size = Vector2.Min(size, maxSize); } s_searchablePopupWindow.SetSearchTerms(searchTerms); s_searchablePopupWindow.label = label; s_searchablePopupWindow.displayOptions = displayOptions; WindowInvoker.Add(s_searchablePopupWindow); } } return(ret); }
static object ColorField(object obj) { void ColorBox(Rect r, Color col) { using (new BackgroundColorScope(col)) { GUI.Box(r, "", Style.whiteRect); } } var color = (Color)obj; #if UNITY_EDITOR if (RGUILayoutUtility.IsInEditorWindow()) { var ret = EditorGUILayout.ColorField(color); return(ret); } #endif var id = GUIUtility.GetControlID(FocusType.Passive); var rect = GUILayoutUtility.GetRect(new GUIContent(), GUI.skin.textField); // color bar ColorBox(rect, new Color(color.r, color.g, color.b, 1f)); var alphaRect = rect; // alpha bar background(black) alphaRect.y = alphaRect.yMax - ColorFieldSetting.alphaBarHeight; alphaRect.height = ColorFieldSetting.alphaBarHeight; ColorBox(alphaRect, Color.black); // alpha bar alphaRect.width *= color.a; ColorBox(alphaRect, Color.white); // label Color.RGBToHSV(color, out var h, out var s, out var v); var yuvY = 0.299f * color.r + 0.587f * color.g + 0.114f * color.b; var fontColor = yuvY >= 0.4f ? ColorFieldSetting.labelColorDark : ColorFieldSetting.labelColorLight; using (new ColorScope(fontColor)) { GUI.Label(rect, $" HSV {h:0.00} {s:0.00} {v:0.00}"); } // button using (new ColorScope(Color.clear)) { if (GUI.Button(rect, "", Style.whiteRect)) { colorPicker = new IMColorPicker(color); colorPicker.SetWindowPosition(colorPickerLastPos ?? RGUIUtility.GetMouseScreenPos()); colorPickerControlId = id; } } if ((colorPicker != null) && (colorPickerControlId == id)) { WindowInvoker.Add(colorPicker); if (colorPicker.destroy) { colorPicker = null; colorPickerControlId = 0; } else { color = colorPicker.color; colorPickerLastPos = colorPicker.windowRect.position; } } return(color); }