public static GuiItemEvent CreateScrollView(Rect scrollRect, ref Vector2 scrollPos, ref List <GuiItem> scrollItems, string label, string listName, int maxShowItems = 0) { Vector2 labelSize = SNStyles.GetGuiItemStyle(GuiItemType.LABEL).CalcSize(new GUIContent(label)); if (maxShowItems > 0) { scrollRect.height = maxShowItems * (scrollItems[0].Rect.height + 2); } else { scrollRect.height = scrollRect.height - labelSize.y + 10; } GUI.Label(new Rect(scrollRect.x, scrollRect.y + 5, labelSize.x, labelSize.y), label, SNStyles.GetGuiItemStyle(GuiItemType.LABEL, textAnchor: TextAnchor.MiddleLeft)); GUI.Label(new Rect(scrollRect.x + labelSize.x + 5, scrollRect.y + 5, scrollRect.width - labelSize.x, labelSize.y), listName, SNStyles.GetGuiItemStyle(GuiItemType.LABEL, GuiColor.Green, textAnchor: TextAnchor.MiddleLeft)); scrollPos = GUI.BeginScrollView(new Rect(scrollRect.x, scrollRect.y + labelSize.y + 10, scrollRect.width, scrollRect.height), scrollPos, new Rect(scrollItems[0].Rect.x, scrollItems[0].Rect.y, scrollItems[0].Rect.width, scrollItems.Count * (scrollItems[0].Rect.height + 2))); GuiItemEvent result = scrollItems.DrawGuiItemsGroup(); GUI.EndScrollView(); return(result); }
//to be called from OnGui public static GuiItemEvent DrawGuiItemsGroup(this List <GuiItem> guiItems) { var e = Event.current; for (int i = 0; i < guiItems.Count; ++i) { if (!guiItems[i].Enabled) { continue; } switch (guiItems[i].Type) { case GuiItemType.NORMALBUTTON: if (GUI.Button(guiItems[i].Rect, guiItems[i].Content, SNStyles.GetGuiItemStyle(guiItems[i]))) { return(new GuiItemEvent(i, e.button, false)); } break; case GuiItemType.TOGGLEBUTTON: if (GUI.Button(guiItems[i].Rect, guiItems[i].Content, SNStyles.GetGuiItemStyle(guiItems[i]))) { //guiItems[i].State = SetStateInverse(guiItems[i].State); return(new GuiItemEvent(i, e.button, false)); } break; case GuiItemType.TAB: if (GUI.Button(guiItems[i].Rect, guiItems[i].Content, SNStyles.GetGuiItemStyle(guiItems[i]))) { if (e.button == 0) { SetStateInverseTAB(guiItems, i); } return(new GuiItemEvent(i, e.button, false)); } break; case GuiItemType.LABEL: GUI.Label(guiItems[i].Rect, guiItems[i].Name, SNStyles.GetGuiItemStyle(guiItems[i])); break; case GuiItemType.TEXTFIELD: GUI.TextArea(guiItems[i].Rect, guiItems[i].Name, SNStyles.GetGuiItemStyle(guiItems[i])); break; } } return(new GuiItemEvent(-1, -1, false)); }
public static void CreateHorizontalSlider(Rect rect, ref float sliderValue, float leftValue, float rightValue, string label, Event <object> onSliderValueChangedEvent) { Vector2 labelSize = SNStyles.GetGuiItemStyle(GuiItemType.LABEL).CalcSize(new GUIContent(label)); GUI.Label(new Rect(rect.x, rect.y + 5, labelSize.x, labelSize.y), label, SNStyles.GetGuiItemStyle(GuiItemType.LABEL, textAnchor: TextAnchor.MiddleLeft)); GUI.Label(new Rect(rect.x + labelSize.x + 5, rect.y + 5, rect.width - labelSize.x, labelSize.y), string.Format("{0:#.##}", sliderValue), SNStyles.GetGuiItemStyle(GuiItemType.LABEL, GuiColor.Green, textAnchor: TextAnchor.MiddleLeft)); object value = GUI.HorizontalSlider(new Rect(rect.x, rect.y + labelSize.y + 5, rect.width, 10), sliderValue, leftValue, rightValue); if ((float)value != sliderValue) { lock (value) { onSliderValueChangedEvent.Trigger(value); } } }
public static void CreateDropdown(Rect rect, ref bool showList, ref int listEntry, GUIContent[] listContent) { int dropDownListHash = "DropDownList".GetHashCode(); int controlID = GUIUtility.GetControlID(dropDownListHash, FocusType.Passive); bool done = false; Rect listRect = new Rect(rect.x, rect.y + rect.height, rect.width, /*Styles.GetGUIStyle(null, Button.BUTTONTYPE.NORMAL_CENTER).CalcHeight(listContent[0], 1.0f)*/ rect.height * listContent.Length); if (Event.current.GetTypeForControl(controlID) == EventType.MouseDown) { if (rect.Contains(Event.current.mousePosition)) { GUIUtility.hotControl = controlID; showList = !showList; } } if (Event.current.GetTypeForControl(controlID) == EventType.MouseUp && showList) { if (listRect.Contains(Event.current.mousePosition)) { done = true; } } GUI.Button(rect, listContent[listEntry]); if (showList) { GUI.Box(listRect, ""); listEntry = GUI.SelectionGrid(listRect, listEntry, listContent, 1, SNStyles.GetGuiItemStyle(GuiItemType.NORMALBUTTON)); } if (done) { showList = false; } }