/// <summary> /// Draws the icon in a square rect, with a custom shader that makes the icon look better when down-scaled. /// This also handles mouseover effects, and linier color spacing. /// </summary> //[Obsolete("Draw the texture like you normally would instead.")] public void Draw(Rect rect, float drawSize) { if (Event.current.type != EventType.Repaint) { return; } Texture iconTex; if (!GUI.enabled) { iconTex = this.Inactive; } else if (rect.Contains(Event.current.mousePosition)) { GUIHelper.RequestRepaint(); iconTex = this.Highlighted; } else { iconTex = this.Active; } rect = rect.AlignCenter(drawSize, drawSize); this.Draw(rect, iconTex); }
/// <summary> /// Ends the group. /// </summary> public void EndGroup() { EditorGUILayout.EndHorizontal(); GUIHelper.PushGUIEnabled(false); GUILayout.EndScrollView(); GUIHelper.PopGUIEnabled(); EditorGUILayout.EndVertical(); if (this.targetPage != this.currentPage) { GUIHelper.RequestRepaint(); } if (this.currentPage != null && Event.current.type == EventType.Repaint) { if (this.isAnimating && this.targetPage != null && this.targetPage != this.currentPage) { this.t = this.t + this.time.DeltaTime * this.AnimationSpeed; this.scrollPosition.x = Mathf.Lerp(this.currentPage.Rect.x, this.targetPage.Rect.x, Mathf.Min(1f, MathUtilities.Hermite01(t))); this.currentHeight = Mathf.Lerp(this.currentPage.Rect.height, this.targetPage.Rect.height, Mathf.Min(1f, MathUtilities.Hermite01(t))); if (this.t >= 1f) { this.currentPage.IsVisible = false; this.currentPage = this.targetPage; this.targetPage = null; this.scrollPosition.x = 0f; this.currentHeight = this.currentPage.Rect.height; this.t = 1f; } } else { this.t = 0f; this.isAnimating = false; this.scrollPosition.x = this.currentPage.Rect.x; this.currentHeight = this.currentPage.Rect.height; if (this.targetPage != null && this.targetPage != this.currentPage && this.targetPage.IsVisible) { this.isAnimating = true; this.scrollPosition.x = this.targetPage.Order > this.currentPage.Order ? 0 : this.scrollPosition.x = this.OuterRect.width; this.t = 0; } } } foreach (var page in this.pages.GFValueIterator()) { page.OnEndGroup(); } this.time.Update(); if (this.isAnimating == false && this.nextPage != null) { this.targetPage = this.nextPage; this.nextPage = null; } }
private static void Update() { if (guiState.Update().IsNewFrame&& IsDragInProgress) { AllowDrop = true; if (IsDragInProgress) { GUIHelper.RequestRepaint(); } } if (IsDragInProgress) { // Ensure drop event! if (WasDragPerformed == false) { if (Event.current.type == EventType.DragPerform || Event.current.type == EventType.MouseMove || Event.current.type == EventType.MouseUp) { //Debug.Log(Event.current.type + " - " + Event.current.rawType); WasDragPerformed = true; if (Event.current.type == EventType.DragPerform && IsHoveringDropZone) { Event.current.Use(); } GUIHelper.RequestRepaint(); } } if (IsHoveringDropZone && GUIHelper.CurrentWindowHasFocus) { if (CurrentHoveringDropZone.IsAccepted == false) { DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } else if (CurrentDraggingHandle.CurrentMethod == DragAndDropMethods.Move) { DragAndDrop.visualMode = DragAndDropVisualMode.Move; } else if (CurrentDraggingHandle.CurrentMethod == DragAndDropMethods.Reference) { DragAndDrop.visualMode = DragAndDropVisualMode.Link; } else if (CurrentDraggingHandle.CurrentMethod == DragAndDropMethods.Copy) { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } } } else { if (Event.current.type == EventType.DragUpdated) { // TODO Start virtual drag. } } }
private void FinalizeDropObject() { if (this.WillDrop) { this.WillDrop = false; this.IsBeingClaimed = false; this.IsReadyToBeClaimed = false; this.isDragging = false; this.isMouseDown = false; this.IsHovering = false; currentHoveringDragHandle = null; DragAndDropManager.WasDragPerformed = false; DragAndDropManager.CurrentDraggingHandle = null; DragAndDropManager.CurrentHoveringDropZone = null; GUIHelper.RequestRepaint(); if (this.OnDragFinnished != null) { this.OnDragFinnished(this.dropEvent); } } }
/// <summary> /// Updates the delta time. /// </summary> public void Update() { // We need some data to start with, otherwise things won't animate smoothly in the beginning. if (this.deltaTimeIndex != this.averageDeltaTimes.Length) { GUIHelper.RequestRepaint(); } if (this.guiState.Update().IsNewFrame) { this.newDeltaTime = (this.stopwatch.ElapsedMilliseconds - this.prevMillisecondsElapsed) / 1000f; this.prevMillisecondsElapsed = this.stopwatch.ElapsedMilliseconds; if (this.newDeltaTime <= DELTA_TIME_THRESHOLD) { unchecked { this.averageDeltaTimes[this.averageDeltaTimeIndex++ % this.averageDeltaTimes.Length] = this.newDeltaTime; if (this.deltaTimeIndex != this.averageDeltaTimes.Length) { this.deltaTimeIndex++; } } } this.averageDeltaTime = 0f; for (int i = 0; i < this.averageDeltaTimes.Length; i++) { this.averageDeltaTime += this.averageDeltaTimes[i]; } this.averageDeltaTime /= this.averageDeltaTimes.Length; } if (Event.current.type == EventType.Layout) { this.deltaTime = Mathf.Min(this.newDeltaTime, this.averageDeltaTime); } }
/// <summary> /// Begins the table. /// </summary> public void BeginTable(int rowCount) { marginFix = marginFix ?? new GUIStyle(); this.currColIndex = 0; this.rows = this.rows ?? new TableRow[0]; if (this.rowCount != rowCount) { this.isDirty = 3; this.rowCount = rowCount; } this.RowIndexFrom = Math.Min(this.rowCount, this.RowIndexFrom); this.RowIndexTo = Math.Min(this.rowCount, this.RowIndexTo); if (this.rows.Length != rowCount) { Array.Resize(ref this.rows, rowCount); } if (Event.current.type == EventType.Layout) { for (int i = this.RowIndexFrom; i < this.RowIndexTo; i++) { var row = rows[i]; if (row.tmpRowHeight > 0) { if (row.rowHeight != row.tmpRowHeight) { this.isDirty = 3; row.rowHeight = row.tmpRowHeight; } row.tmpRowHeight = 0; this.rows[i] = row; } } if (this.isDirty > 0) { this.UpdateSpaceAllocation(); GUIHelper.RequestRepaint(); } } if (Event.current.type == EventType.Repaint) { var p = GUIUtility.GUIToScreenPoint(this.OuterRect.position); if (this.outerVisibleRect != p) { this.outerVisibleRect = p; this.isDirty = 3; } } var outerRect = EditorGUILayout.BeginVertical(this.GetOuterRectLayoutOptions()); if (Event.current.type != EventType.Layout && outerRect.height > 1 || Event.current.type == EventType.Repaint) { if (this.OuterRect != outerRect) { this.isDirty = 3; this.OuterRect = outerRect; } } Vector2 newScrollPos; if (this.DrawScrollBars()) { newScrollPos = GUILayout.BeginScrollView(this.ScrollPos, false, false, this.GetScrollViewOptions(true)); } else { this.ignoreScrollView = Event.current.rawType == EventType.ScrollWheel; if (this.ignoreScrollView) { GUIHelper.PushEventType(EventType.Ignore); } newScrollPos = GUILayout.BeginScrollView(this.ScrollPos, false, false, GUIStyle.none, GUIStyle.none, this.GetScrollViewOptions(false)); if (this.ignoreScrollView) { GUIHelper.PopEventType(); this.isDirty = 3; } } if (newScrollPos != this.ScrollPos) { this.nextScrollPos = newScrollPos; this.isDirty = 3; } var rect = GUILayoutUtility.GetRect(0, this.totalHeight); if (Event.current.type != EventType.Layout && rect.width > 1) { this.innerVisibleRect = GUIClipInfo.VisibleRect; if (this.DrawScrollView) { var scrollDelta = this.nextScrollPos - this.ScrollPos; this.innerVisibleRect.y += scrollDelta.y; if (scrollDelta != Vector2.zero) { this.isDirty = 3; } } if (rect != this.contentRect) { this.isDirty = 3; } if (this.contentRect != rect) { this.isDirty = 3; } this.contentRect = rect; } if (this.isDirty > 0) { GUIHelper.RequestRepaint(); if (Event.current.type == EventType.Repaint) { this.isDirty--; } } }
/// <summary> /// Recaluclates cell and column sizes in the next frame. /// </summary> public void MarkDirty() { this.isDirty = true; GUIHelper.RequestRepaint(); }
internal void Update() { this.lastSeenEvent = Event.current.type; this.SetCurrentDragAndDropMethod(); if (this.lastSeenEvent == EventType.Repaint) { this.FinalizeDropObject(); } if (Event.current.isMouse || Event.current.type == EventType.DragUpdated) { Rect screenSpaceRect; if (this.DragHandleRect.HasValue) { screenSpaceRect = this.DragHandleRect.Value; } else { screenSpaceRect = this.Rect; } Vector2 screenPos = GUIUtility.GUIToScreenPoint(new Vector2(screenSpaceRect.x, screenSpaceRect.y)); screenSpaceRect.x = screenPos.x; screenSpaceRect.y = screenPos.y; this.IsHovering = screenSpaceRect.Contains(GUIUtility.GUIToScreenPoint(Event.current.mousePosition)); } this.OnDragStarted = false; if (DragAndDropManager.IsDragInProgress == false) { if (Event.current.isMouse) { if (this.IsHovering) { if (this.Enabled && Event.current.type == EventType.MouseDown == true && Event.current.button == 0) { this.isMouseDown = true; this.mouseDownPostionOffset = Event.current.mousePosition - new Vector2(this.Rect.x, this.Rect.y); GUIHelper.RemoveFocusControl(); Event.current.Use(); DragAndDrop.PrepareStartDrag(); } if (this.isMouseDown && Event.current.type == EventType.MouseDrag) { this.isDragging = true; this.OnDragStarted = true; if (this.Object != null) { DragAndDrop.objectReferences = new UnityEngine.Object[0]; DragAndDrop.paths = null; DragAndDrop.SetGenericData(this.Object.GetType().Name, this.Object); DragAndDrop.StartDrag("Odin Drag Operation"); } } } } } else { GUIHelper.RequestRepaint(); } if (this.isDragging) { GUIHelper.RequestRepaint(); DragAndDropManager.CurrentDraggingHandle = this; if (EditorWindow.mouseOverWindow != null) { EditorWindow.mouseOverWindow.Focus(); } if (DragAndDropManager.WasDragPerformed) { this.IsReadyToBeClaimed = true; if (DragAndDropManager.IsHoveringDropZone == false) { this.DropObject(DropEvents.Canceled); } } } else { if (this.IsHovering) { if (currentHoveringDragHandle == null || this.LayoutDepth >= currentHoveringDragHandle.LayoutDepth) { currentHoveringDragHandle = this; } } else if (currentHoveringDragHandle == this) { currentHoveringDragHandle = null; } this.IsHovering = currentHoveringDragHandle == this; } }
/// <summary> /// Draws a objectpicker butter, in the given rect. This one is designed to look good on top of DrawDropZone(). /// </summary> public static object ObjectPickerZone(Rect rect, object value, Type type, bool allowSceneObjects, int id) { var btnId = GUIUtility.GetControlID(FocusType.Passive); var objectPicker = ObjectPicker.GetObjectPicker(type.FullName + "+" + btnId, type); var selectRect = rect.AlignBottom(15).AlignCenter(45); var uObj = value as UnityEngine.Object; selectRect.xMin = Mathf.Max(selectRect.xMin, rect.xMin); var hide = IsDragging || Event.current.type == EventType.Repaint && !rect.Contains(Event.current.mousePosition); if (hide) { GUIHelper.PushColor(new Color(0, 0, 0, 0)); GUIHelper.PushGUIEnabled(false); } bool hideInspectorBtn = !hide && !(uObj); if (hideInspectorBtn) { GUIHelper.PushGUIEnabled(false); GUIHelper.PushColor(new Color(0, 0, 0, 0)); } var inspectBtn = rect.AlignRight(14); inspectBtn.height = 14; SirenixEditorGUI.BeginDrawOpenInspector(inspectBtn, uObj, rect); SirenixEditorGUI.EndDrawOpenInspector(inspectBtn, uObj); if (hideInspectorBtn) { GUIHelper.PopColor(); GUIHelper.PopGUIEnabled(); } if (GUI.Button(selectRect, "select", SirenixGUIStyles.TagButton)) { GUIHelper.RemoveFocusControl(); objectPicker.ShowObjectPicker(allowSceneObjects, rect, false); Event.current.Use(); } if (Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.KeyDown && EditorGUIUtility.keyboardControl == id) { objectPicker.ShowObjectPicker(allowSceneObjects, rect, false); Event.current.Use(); } if (hide) { GUIHelper.PopColor(); GUIHelper.PopGUIEnabled(); } if (objectPicker.IsReadyToClaim) { GUIHelper.RequestRepaint(); GUI.changed = true; var newValue = objectPicker.ClaimObject(); Event.current.Use(); return(newValue); } if (Event.current.keyCode == KeyCode.Delete && Event.current.type == EventType.KeyDown && EditorGUIUtility.keyboardControl == id) { Event.current.Use(); GUI.changed = true; return(null); } if (uObj && Event.current.type == EventType.MouseUp && rect.Contains(Event.current.mousePosition) && EditorGUIUtility.hotControl == id && Event.current.button == 0) { EditorGUIUtility.PingObject(uObj); } return(value); }
/// <summary> /// A draggable zone for both Unity and non-unity objects. /// </summary> public static object DragZone(Rect rect, object value, Type type, bool allowMove, bool allowSwap, int id) { if (value == null) { return(null); } // Unity null if (typeof(UnityEngine.Object).IsAssignableFrom(value.GetType()) && !(value as UnityEngine.Object)) { return(value); } var t = Event.current.type; var isMouseOver = rect.Contains(Event.current.mousePosition); var unityObject = value as UnityEngine.Object; if (isMouseOver && t == EventType.MouseDown) { GUIHelper.RemoveFocusControl(); GUIUtility.hotControl = id; GUIUtility.keyboardControl = id; dragginObjects = new object[] { }; DragAndDrop.PrepareStartDrag(); GUIHelper.RequestRepaint(); isAccepted = false; dropZoneObject = null; draggingId = 0; currentDragIsMove = false; } if (isAccepted && draggingId == id) { GUIHelper.RequestRepaint(); GUI.changed = true; draggingId = 0; // TODO: Validate drop zone object, and only return that if it's assignable from type. return(allowMove ? (allowSwap ? dropZoneObject : null) : value); } if (GUIUtility.hotControl != id) { return(value); } else if (t == EventType.MouseMove) { GUIHelper.RequestRepaint(); draggingId = 0; DragAndDrop.PrepareStartDrag(); DragAndDrop.objectReferences = new UnityEngine.Object[] { }; //GUIHelper.RemoveFocusControl(); dragginObjects = new object[] { }; currentDragIsMove = false; } if (Event.current.type == EventType.MouseDrag && isMouseOver && (DragAndDrop.objectReferences == null || DragAndDrop.objectReferences.Length == 0)) { isAccepted = false; dropZoneObject = null; draggingId = id; DragAndDrop.StartDrag("Movable drag"); if (unityObject) { DragAndDrop.objectReferences = new UnityEngine.Object[] { unityObject }; dragginObjects = new object[] { }; } else { DragAndDrop.objectReferences = new UnityEngine.Object[] { }; dragginObjects = new object[] { value }; } DragAndDrop.activeControlID = 0; currentDragIsMove = allowMove; Event.current.Use(); GUIHelper.RequestRepaint(); } return(value); }
/// <summary> /// A drop zone area for bot Unity and non-unity objects. /// </summary> public static object DropZone(Rect rect, object value, Type type, bool allowSceneObjects, int id) { if (rect.Contains(Event.current.mousePosition)) { var t = Event.current.type; if (t == EventType.DragUpdated || t == EventType.DragPerform) { object obj = null; if (obj == null) { obj = dragginObjects.Where(x => x != null && x.GetType().InheritsFrom(type)).FirstOrDefault(); } if (obj == null) { obj = DragAndDrop.objectReferences.Where(x => x != null && x.GetType().InheritsFrom(type)).FirstOrDefault(); } if (type.InheritsFrom <Component>() || type.IsInterface) { if (obj == null) { obj = dragginObjects.OfType <GameObject>().Where(x => x != null).Select(x => x.GetComponent(type)).Where(x => x != null).FirstOrDefault(); } if (obj == null) { obj = DragAndDrop.objectReferences.OfType <GameObject>().Where(x => x != null).Select(x => x.GetComponent(type)).Where(x => x != null).FirstOrDefault(); } } bool acceptsDrag = obj != null; if (acceptsDrag && allowSceneObjects == false) { var uObj = (UnityEngine.Object)obj; if (uObj != null) { if (typeof(Component).IsAssignableFrom(uObj.GetType())) { uObj = ((Component)uObj).gameObject; } acceptsDrag = EditorUtility.IsPersistent(uObj); } } if (acceptsDrag) { isHoveringAcceptedDropZone = true; bool move = Event.current.modifiers != EventModifiers.Control && draggingId != 0 && currentDragIsMove; if (move) { DragAndDrop.visualMode = DragAndDropVisualMode.Move; } else { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } Event.current.Use(); if (t == EventType.DragPerform) { if (!move) { draggingId = 0; } DragAndDrop.objectReferences = new UnityEngine.Object[] { }; DragAndDrop.AcceptDrag(); GUI.changed = true; GUIHelper.RemoveFocusControl(); dragginObjects = new object[] { }; currentDragIsMove = false; isAccepted = true; dropZoneObject = value; DragAndDrop.activeControlID = 0; GUIHelper.RequestRepaint(); return(obj); } else { DragAndDrop.activeControlID = id; } } else { isHoveringAcceptedDropZone = false; DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } } return(value); }
/// <summary> /// A drop zone area for bot Unity and non-unity objects. /// </summary> public static object DropZone(Rect rect, object value, Type type, bool allowSceneObjects, int id) { if (rect.Contains(Event.current.mousePosition)) { var t = Event.current.type; if (t == EventType.DragUpdated || t == EventType.DragPerform) { // This bit disables all dropzones inside the provided preventDropAreaRect. // // RootNode1 // ChileNode1 // ChileNode2 // ChileNode2.1 // ChileNode2.2 // ChileNode3 // RootNode2 // // If the RootNode has provided a preventDropAreaRect, then that means that the RootNode won't be able to be dragged into any of its child nodes. if (preventDropAreaRect.Contains(new Vector2(rect.x, rect.y)) && preventDropAreaRect.Contains(new Vector2(rect.xMax, rect.yMax))) { return(value); } object obj = null; if (obj == null) { obj = dragginObjects.Where(x => x != null && x.GetType().InheritsFrom(type)).FirstOrDefault(); } if (obj == null) { obj = DragAndDrop.objectReferences.Where(x => x != null && x.GetType().InheritsFrom(type)).FirstOrDefault(); } if (type.InheritsFrom <Component>() || type.IsInterface) { if (obj == null) { obj = dragginObjects.OfType <GameObject>().Where(x => x != null).Select(x => x.GetComponent(type)).Where(x => x != null).FirstOrDefault(); } if (obj == null) { obj = DragAndDrop.objectReferences.OfType <GameObject>().Where(x => x != null).Select(x => x.GetComponent(type)).Where(x => x != null).FirstOrDefault(); } } bool acceptsDrag = obj != null; if (acceptsDrag && allowSceneObjects == false) { var uObj = obj as UnityEngine.Object; if (uObj != null) { if (typeof(Component).IsAssignableFrom(uObj.GetType())) { uObj = ((Component)uObj).gameObject; } acceptsDrag = EditorUtility.IsPersistent(uObj); } } if (acceptsDrag) { hoveringAcceptedDropZone = id; bool move = Event.current.modifiers != EventModifiers.Control && draggingId != 0 && currentDragIsMove; if (move) { DragAndDrop.visualMode = DragAndDropVisualMode.Move; } else { DragAndDrop.visualMode = DragAndDropVisualMode.Copy; } Event.current.Use(); if (t == EventType.DragPerform) { if (!move) { draggingId = 0; //preventDropAreaRect = new Rect(); } // Calling this here makes Unity crash on MacOS // DragAndDrop.objectReferences = new UnityEngine.Object[] { }; DragAndDrop.AcceptDrag(); GUI.changed = true; GUIHelper.RemoveFocusControl(); dragginObjects = new object[] { }; currentDragIsMove = false; isAccepted = true; dropZoneObject = value; preventDropAreaRect = new Rect(); DragAndDrop.activeControlID = 0; GUIHelper.RequestRepaint(); return(obj); } else { DragAndDrop.activeControlID = id; } } else { hoveringAcceptedDropZone = 0; DragAndDrop.visualMode = DragAndDropVisualMode.Rejected; } } } else { if (hoveringAcceptedDropZone == id) { hoveringAcceptedDropZone = 0; } } return(value); }
/// <summary> /// Draws a objectpicker button in the given rect. This one is designed to look good on top of DrawDropZone(). /// </summary> public static object ObjectPickerZone(Rect rect, object value, Type type, bool allowSceneObjects, int id) { // TODO: btnId wasn't used, but the GetControlID call is probably still important. //var btnId = GUIUtility.GetControlID(FocusType.Passive); GUIUtility.GetControlID(FocusType.Passive); var objectPicker = ObjectPicker.GetObjectPicker(type.FullName + "+" + GUIHelper.CurrentWindowInstanceID.ToString() + "+" + id, type); var selectRect = rect.AlignBottom(15).AlignCenter(45); var uObj = value as UnityEngine.Object; selectRect.xMin = Mathf.Max(selectRect.xMin, rect.xMin); var hide = IsDragging || Event.current.type == EventType.Repaint && !rect.Contains(Event.current.mousePosition); if (hide) { GUIHelper.PushColor(new Color(0, 0, 0, 0)); GUIHelper.PushGUIEnabled(false); } bool hideInspectorBtn = !hide && !(uObj); if (hideInspectorBtn) { GUIHelper.PushGUIEnabled(false); GUIHelper.PushColor(new Color(0, 0, 0, 0)); } var inspectBtn = rect.AlignRight(14); inspectBtn.height = 14; SirenixEditorGUI.BeginDrawOpenInspector(inspectBtn, uObj, rect); SirenixEditorGUI.EndDrawOpenInspector(inspectBtn, uObj); if (hideInspectorBtn) { GUIHelper.PopColor(); GUIHelper.PopGUIEnabled(); } if (GUI.Button(selectRect, "select", SirenixGUIStyles.TagButton)) { GUIHelper.RemoveFocusControl(); objectPicker.ShowObjectPicker(value, allowSceneObjects, rect, false); Event.current.Use(); } if (Event.current.keyCode == KeyCode.Return && Event.current.type == EventType.KeyDown && EditorGUIUtility.keyboardControl == id) { objectPicker.ShowObjectPicker(value, allowSceneObjects, rect, false); Event.current.Use(); } if (hide) { GUIHelper.PopColor(); GUIHelper.PopGUIEnabled(); } if (objectPicker.IsReadyToClaim) { GUIHelper.RequestRepaint(); GUI.changed = true; var newValue = objectPicker.ClaimObject(); Event.current.Use(); return(newValue); } if (objectPicker.IsPickerOpen && typeof(UnityEngine.Object).IsAssignableFrom(type)) { return(objectPicker.CurrentSelectedObject); } if (Event.current.keyCode == KeyCode.Delete && Event.current.type == EventType.KeyDown && EditorGUIUtility.keyboardControl == id) { Event.current.Use(); GUI.changed = true; return(null); } if (uObj && Event.current.rawType == EventType.MouseUp && rect.Contains(Event.current.mousePosition) && Event.current.button == 0) { // For components ping the attached game object instead, because then Unity can figure out to ping prefabs in the project window too. UnityEngine.Object pingObj = uObj; if (pingObj is Component) { pingObj = (pingObj as Component).gameObject; } EditorGUIUtility.PingObject(pingObj); } return(value); }