/// <summary> /// Begins a scaled local area. /// Returns vector to offset GUI controls with to account for zooming to the pivot. /// Using adjustGUILayout does that automatically for GUILayout rects. Theoretically can be nested! /// </summary> public static Vector2 BeginScale(ref Rect rect, Vector2 zoomPivot, float zoom, bool IsEditorWindow, bool adjustGUILayout) { isEditorWindow = IsEditorWindow; Rect screenRect; if (compabilityMode) { // In compability mode, we will assume only one top group and do everything manually, not using reflected calls (-> practically blind) GUI.EndGroup(); screenRect = rect; #if UNITY_EDITOR if (isEditorWindow) { screenRect.y += HEADER_SIZE; } #endif } else { // If it's supported, we take the completely generic way using reflected calls GUIScaleUtils.BeginNoClip(); screenRect = GUIScaleUtils.GUIToScaledSpace(rect); } rect = Scale(screenRect, screenRect.position + zoomPivot, new Vector2(zoom, zoom)); // Now continue drawing using the new clipping group GUI.BeginGroup(rect); rect.position = Vector2.zero; // Adjust because we entered the new group // Because I currently found no way to actually scale to a custom pivot rather than (0, 0), // we'll make use of a cheat and just offset it accordingly to let it appear as if it would scroll to the center // Note, due to that, controls not adjusted are still scaled to (0, 0) Vector2 zoomPosAdjust = rect.center - screenRect.size / 2 + zoomPivot; // For GUILayout, we can make this adjustment here if desired adjustedGUILayout.Add(adjustGUILayout); if (adjustGUILayout) { GUILayout.BeginHorizontal(); GUILayout.Space(rect.center.x - screenRect.size.x + zoomPivot.x); GUILayout.BeginVertical(); GUILayout.Space(rect.center.y - screenRect.size.y + zoomPivot.y); } // Take a matrix backup to restore back later on GUIMatrices.Add(GUI.matrix); // Scale GUI.matrix. After that we have the correct clipping group again. GUIUtility.ScaleAroundPivot(new Vector2(1 / zoom, 1 / zoom), zoomPosAdjust); return(zoomPosAdjust); }
public override void DrawGUI(Event p_event, Rect p_rect) { if (!_initialized) { backgroundTexture = Resources.Load <Texture>("Textures/graph_background"); whiteRectTexture = Resources.Load <Texture>("Textures/white_rect_64"); _graphMenuView = new GraphMenuView(); GUIScaleUtils.CheckInit(); _initialized = true; } zoomedRect = new Rect(0, 0, p_rect.width, p_rect.height); GUI.color = DashEditorCore.Previewer.IsPreviewing ? new Color(0f, 1f, .2f, 1) : new Color(0f, .1f, .2f, 1); GUI.Box(p_rect, ""); if (Graph != null) { // Draw background texture GUI.color = new Color(0, 0, 0, .4f); GUI.DrawTextureWithTexCoords(zoomedRect, backgroundTexture, new Rect(-Graph.viewOffset.x / backgroundTexture.width, Graph.viewOffset.y / backgroundTexture.height, Zoom * p_rect.width / backgroundTexture.width, Zoom * p_rect.height / backgroundTexture.height), true); GUI.color = Color.white; // Draw graph GUIScaleUtils.BeginScale(ref zoomedRect, new Vector2(p_rect.width / 2, p_rect.height / 2), Zoom, false, false); Graph.DrawGUI(zoomedRect); //Graph.DrawComments(zoomedRect); GUIScaleUtils.EndScale(); Graph.DrawComments(p_rect, false); DrawHelp(p_rect); DrawControllerInfo(p_rect); DrawPreviewInfo(p_rect); DrawSelectingRegion(p_rect); } DrawTitle(p_rect); }
/// <summary> /// Ends a scale region previously opened with BeginScale /// </summary> public static void EndScale() { // Set last matrix and clipping group if (GUIMatrices.Count == 0 || adjustedGUILayout.Count == 0) { throw new UnityException("GUIScaleUtility: You are ending more scale regions than you are beginning!"); } GUI.matrix = GUIMatrices[GUIMatrices.Count - 1]; GUIMatrices.RemoveAt(GUIMatrices.Count - 1); // End GUILayout zoomPosAdjustment if (adjustedGUILayout[adjustedGUILayout.Count - 1]) { GUILayout.EndVertical(); GUILayout.EndHorizontal(); } adjustedGUILayout.RemoveAt(adjustedGUILayout.Count - 1); // End the scaled group GUI.EndGroup(); if (compabilityMode) { // In compability mode, we don't know the previous group rect, but as we cannot use top groups there either way, we restore the screen group if (isEditorWindow) // We're in an editor window { GUI.BeginClip(new Rect(0, HEADER_SIZE, Screen.width, Screen.height - HEADER_SIZE)); } else { GUI.BeginClip(new Rect(0, 0, Screen.width, Screen.height)); } } else { // Else, restore the clips (groups) GUIScaleUtils.RestoreClips(); } }