/// <summary> /// It is currently active only when Notch Simulator tab is present. /// </summary> void OnGUI() { win = this; //Sometimes even with flag I can see it in hierarchy until I move a mouse over it?? EditorApplication.RepaintHierarchyWindow(); bool enableSimulation = NotchSimulatorUtility.enableSimulation; EditorGUI.BeginChangeCheck(); string shortcut = ShortcutManager.instance.GetShortcutBinding(NotchSolutionShortcuts.toggleSimulationShortcut).ToString(); if (string.IsNullOrEmpty(shortcut)) { shortcut = "None"; } NotchSimulatorUtility.enableSimulation = EditorGUILayout.BeginToggleGroup($"Simulate ({shortcut})", NotchSimulatorUtility.enableSimulation); EditorGUI.indentLevel++; NotchSimulatorUtility.selectedDevice = (SimulationDevice)EditorGUILayout.EnumPopup(NotchSimulatorUtility.selectedDevice); NotchSimulatorUtility.flipOrientation = EditorGUILayout.Toggle("Flip Orientation", NotchSimulatorUtility.flipOrientation); var simulationDevice = SimulationDatabase.db[NotchSimulatorUtility.selectedDevice]; //Draw warning about wrong aspect ratio if (enableSimulation) { ScreenOrientation gameViewOrientation = NotchSimulatorUtility.GetGameViewOrientation(); Vector2 simSize = gameViewOrientation == ScreenOrientation.Portrait ? simulationDevice.screenSize : new Vector2(simulationDevice.screenSize.y, simulationDevice.screenSize.x); Vector2 gameViewSize = NotchSimulatorUtility.GetMainGameViewSize(); if (gameViewOrientation == ScreenOrientation.Landscape) { var flip = gameViewSize.x; gameViewSize.x = gameViewSize.y; gameViewSize.y = flip; } var simAspect = NotchSolutionUtility.ScreenRatio(simulationDevice.screenSize); var gameViewAspect = NotchSolutionUtility.ScreenRatio(gameViewSize); var aspectDiff = Math.Abs((simAspect.x / simAspect.y) - (gameViewAspect.x / gameViewAspect.y)); if (aspectDiff > 0.01f) { EditorGUILayout.HelpBox($"The selected simulation device has an aspect ratio of {simAspect.y}:{simAspect.x} ({simulationDevice.screenSize.y}x{simulationDevice.screenSize.x}) but your game view is currently in aspect {gameViewAspect.y}:{gameViewAspect.x} ({gameViewSize.y}x{gameViewSize.x}). The overlay mockup will be stretched from its intended ratio.", MessageType.Warning); } } EditorGUI.indentLevel--; EditorGUILayout.EndToggleGroup(); bool changed = EditorGUI.EndChangeCheck(); if (changed) { UpdateAllMockups(); } UpdateSimulatorTargets(); }
public override void OnInspectorGUI() { var orientationType = serializedObject.FindProperty("orientationType"); var portrait = serializedObject.FindProperty("portraitOrDefaultPaddings"); var landscape = serializedObject.FindProperty("landscapePaddings"); var influence = serializedObject.FindProperty("influence"); var flipPadding = serializedObject.FindProperty("flipPadding"); (bool landscapeCompatible, bool portraitCompatible) = NotchSolutionUtility.GetOrientationCompatibility(); if (portraitCompatible && landscapeCompatible) { EditorGUILayout.PropertyField(orientationType); EditorGUILayout.Separator(); } bool dual = orientationType.enumValueIndex == (int)SupportedOrientations.Dual; if (dual) { EditorGUILayout.LabelField("Portrait Orientation", EditorStyles.boldLabel); } if (portraitCompatible && landscapeCompatible) { EditorGUI.indentLevel++; } for (int i = 0; i < 4; i++) { portrait.Next(enterChildren: true); EditorGUILayout.PropertyField(portrait); } if (portraitCompatible && landscapeCompatible) { EditorGUI.indentLevel--; } if (dual) { EditorGUILayout.Separator(); EditorGUILayout.LabelField("Landscape Orientation", EditorStyles.boldLabel); EditorGUI.indentLevel++; for (int i = 0; i < 4; i++) { landscape.Next(enterChildren: true); EditorGUILayout.PropertyField(landscape); } EditorGUI.indentLevel--; } EditorGUILayout.Separator(); EditorGUILayout.PropertyField(influence); EditorGUILayout.PropertyField(flipPadding); serializedObject.ApplyModifiedProperties(); }
/// <summary> /// Draw the part of fields in <see cref="AdaptationBase">. /// </summary> //public static void Draw(SerializedObject serializedObject) public override void OnInspectorGUI() { AdaptationBase adaptation = (AdaptationBase)target; var supportedProp = serializedObject.FindProperty("supportedOrientations"); var portraitProp = serializedObject.FindProperty("portraitOrDefaultAdaptation"); var landscapeProp = serializedObject.FindProperty("landscapeAdaptation"); (bool landscapeCompatible, bool portraitCompatible) = NotchSolutionUtility.GetOrientationCompatibility(); if (portraitCompatible && landscapeCompatible) { EditorGUILayout.PropertyField(supportedProp); EditorGUILayout.Separator(); } bool dual = supportedProp.enumValueIndex == (int)SupportedOrientations.Dual; if (dual) { EditorGUILayout.LabelField("Portrait Orientation", EditorStyles.boldLabel); } portraitProp.Next(enterChildren: true); if (portraitCompatible && landscapeCompatible) { EditorGUI.indentLevel++; } DrawGenButton(adaptation, forPortrait: true); for (int i = 0; i < 3; i++) { EditorGUILayout.PropertyField(portraitProp); portraitProp.Next(enterChildren: false); } if (portraitCompatible && landscapeCompatible) { EditorGUI.indentLevel--; } if (dual) { EditorGUILayout.Separator(); EditorGUILayout.LabelField("Landscape Orientation", EditorStyles.boldLabel); landscapeProp.Next(enterChildren: true); EditorGUI.indentLevel++; DrawGenButton(adaptation, forPortrait: false); for (int i = 0; i < 3; i++) { EditorGUILayout.PropertyField(landscapeProp); landscapeProp.Next(enterChildren: false); } EditorGUI.indentLevel--; } serializedObject.ApplyModifiedProperties(); }
private void UpdateRect() { if (!IsActive()) { return; } SafeAreaPaddingSides selectedOrientation = orientationType == SafeAreaPaddingOrientationType.DualOrientation ? NotchSolutionUtility.GetCurrentOrientation() == ScreenOrientation.Landscape ? landscapePaddings : portraitOrDefaultPaddings : portraitOrDefaultPaddings; m_Tracker.Clear(); m_Tracker.Add(this, rectTransform, (LockSide(selectedOrientation.left) ? DrivenTransformProperties.AnchorMinX : 0) | (LockSide(selectedOrientation.right) ? DrivenTransformProperties.AnchorMaxX : 0) | (LockSide(selectedOrientation.bottom) ? DrivenTransformProperties.AnchorMinY : 0) | (LockSide(selectedOrientation.top) ? DrivenTransformProperties.AnchorMaxY : 0) | (LockSide(selectedOrientation.left) && LockSide(selectedOrientation.right) ? (DrivenTransformProperties.SizeDeltaX | DrivenTransformProperties.AnchoredPositionX) : 0) | (LockSide(selectedOrientation.top) && LockSide(selectedOrientation.bottom) ? (DrivenTransformProperties.SizeDeltaY | DrivenTransformProperties.AnchoredPositionY) : 0) ); bool LockSide(SafeAreaPaddingMode sapm) { switch (sapm) { case SafeAreaPaddingMode.Safe: case SafeAreaPaddingMode.SafeBalanced: case SafeAreaPaddingMode.Zero: return(true); //When "Unlocked" is supported, it will be false. default: return(false); } } //Lock the anchor mode to full stretch first. rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.one; var topRect = GetTopLevelRect(); var safeAreaRelative = GetScreenSafeAreaRelative(); #if DEBUG_NOTCH_SOLUTION Debug.Log($"Top {topRect} safe {safeAreaRelative} min {safeAreaRelative.xMin} {safeAreaRelative.yMin}"); #endif var safeAreaPaddingsRelativeLDUR = new float[4] { safeAreaRelative.xMin, safeAreaRelative.yMin, 1 - (safeAreaRelative.yMin + safeAreaRelative.height), 1 - (safeAreaRelative.xMin + safeAreaRelative.width), }; #if DEBUG_NOTCH_SOLUTION Debug.Log($"SafeLDUR {string.Join(" ", safeAreaPaddingsRelativeLDUR.Select(x => x.ToString()))}"); #endif var currentRect = rectTransform.rect; //TODO : Calculate the current padding relative, to enable "Unlocked" mode. (Not forcing zero padding) var finalPaddingsLDUR = new float[4] { 0, 0, 0, 0 }; switch (selectedOrientation.left) { case SafeAreaPaddingMode.Safe: finalPaddingsLDUR[0] = topRect.width * safeAreaPaddingsRelativeLDUR[0]; break; case SafeAreaPaddingMode.SafeBalanced: finalPaddingsLDUR[0] = safeAreaPaddingsRelativeLDUR[3] > safeAreaPaddingsRelativeLDUR[0] ? topRect.width * safeAreaPaddingsRelativeLDUR[3] : topRect.width * safeAreaPaddingsRelativeLDUR[0]; break; } switch (selectedOrientation.right) { case SafeAreaPaddingMode.Safe: finalPaddingsLDUR[3] = topRect.width * safeAreaPaddingsRelativeLDUR[3]; break; case SafeAreaPaddingMode.SafeBalanced: finalPaddingsLDUR[3] = safeAreaPaddingsRelativeLDUR[0] > safeAreaPaddingsRelativeLDUR[3] ? topRect.width * safeAreaPaddingsRelativeLDUR[0] : topRect.width * safeAreaPaddingsRelativeLDUR[3]; break; } switch (selectedOrientation.bottom) { case SafeAreaPaddingMode.Safe: finalPaddingsLDUR[1] = topRect.height * safeAreaPaddingsRelativeLDUR[1]; break; case SafeAreaPaddingMode.SafeBalanced: finalPaddingsLDUR[1] = safeAreaPaddingsRelativeLDUR[2] > safeAreaPaddingsRelativeLDUR[1] ? topRect.height * safeAreaPaddingsRelativeLDUR[2] : topRect.height * safeAreaPaddingsRelativeLDUR[1]; break; } switch (selectedOrientation.top) { case SafeAreaPaddingMode.Safe: finalPaddingsLDUR[2] = topRect.height * safeAreaPaddingsRelativeLDUR[2]; break; case SafeAreaPaddingMode.SafeBalanced: finalPaddingsLDUR[2] = safeAreaPaddingsRelativeLDUR[1] > safeAreaPaddingsRelativeLDUR[2] ? topRect.height * safeAreaPaddingsRelativeLDUR[1] : topRect.height * safeAreaPaddingsRelativeLDUR[2]; break; } #if DEBUG_NOTCH_SOLUTION Debug.Log($"FinalLDUR {string.Join(" ", finalPaddingsLDUR.Select(x => x.ToString()))}"); #endif //Combined padding becomes size delta. var sizeDelta = rectTransform.sizeDelta; sizeDelta.x = -(finalPaddingsLDUR[0] + finalPaddingsLDUR[3]); sizeDelta.y = -(finalPaddingsLDUR[1] + finalPaddingsLDUR[2]); rectTransform.sizeDelta = sizeDelta; //The rect remaining after subtracted the size delta. Vector2 rectWidthHeight = new Vector2(topRect.width + sizeDelta.x, topRect.height + sizeDelta.y); #if DEBUG_NOTCH_SOLUTION Debug.Log($"RectWidthHeight {rectWidthHeight}"); #endif //Anchor position's answer is depending on pivot too. Where the pivot point is defines where 0 anchor point is. Vector2 zeroPosition = new Vector2(rectTransform.pivot.x * topRect.width, rectTransform.pivot.y * topRect.height); Vector2 pivotInRect = new Vector2(rectTransform.pivot.x * rectWidthHeight.x, rectTransform.pivot.y * rectWidthHeight.y); #if DEBUG_NOTCH_SOLUTION Debug.Log($"zeroPosition {zeroPosition}"); #endif //Calculate like zero position is at bottom left first, then diff with the real zero position. rectTransform.anchoredPosition3D = new Vector3( finalPaddingsLDUR[0] + pivotInRect.x - zeroPosition.x, finalPaddingsLDUR[1] + pivotInRect.y - zeroPosition.y, rectTransform.anchoredPosition3D.z); }
#pragma warning restore 0649 protected override void UpdateRect() { PerEdgeEvaluationModes selectedOrientation = orientationType == SupportedOrientations.Dual ? NotchSolutionUtility.GetCurrentOrientation() == ScreenOrientation.Landscape ? landscapePaddings : portraitOrDefaultPaddings : portraitOrDefaultPaddings; m_Tracker.Clear(); m_Tracker.Add(this, rectTransform, (LockSide(selectedOrientation.left) ? DrivenTransformProperties.AnchorMinX : 0) | (LockSide(selectedOrientation.right) ? DrivenTransformProperties.AnchorMaxX : 0) | (LockSide(selectedOrientation.bottom) ? DrivenTransformProperties.AnchorMinY : 0) | (LockSide(selectedOrientation.top) ? DrivenTransformProperties.AnchorMaxY : 0) | (LockSide(selectedOrientation.left) && LockSide(selectedOrientation.right) ? (DrivenTransformProperties.SizeDeltaX | DrivenTransformProperties.AnchoredPositionX) : 0) | (LockSide(selectedOrientation.top) && LockSide(selectedOrientation.bottom) ? (DrivenTransformProperties.SizeDeltaY | DrivenTransformProperties.AnchoredPositionY) : 0) ); bool LockSide(EdgeEvaluationMode saem) { switch (saem) { case EdgeEvaluationMode.On: case EdgeEvaluationMode.Balanced: case EdgeEvaluationMode.Off: return(true); //When "Unlocked" is supported, it will be false. default: return(false); } } //Lock the anchor mode to full stretch first. rectTransform.anchorMin = Vector2.zero; rectTransform.anchorMax = Vector2.one; var topRect = GetCanvasRect(); var safeAreaRelative = SafeAreaRelative; #if DEBUG_NOTCH_SOLUTION Debug.Log($"Top {topRect} safe {safeAreaRelative} min {safeAreaRelative.xMin} {safeAreaRelative.yMin}"); #endif var relativeLDUR = new float[4] { safeAreaRelative.xMin, safeAreaRelative.yMin, 1 - (safeAreaRelative.yMin + safeAreaRelative.height), 1 - (safeAreaRelative.xMin + safeAreaRelative.width), }; #if DEBUG_NOTCH_SOLUTION Debug.Log($"SafeLDUR {string.Join(" ", relativeLDUR.Select(x => x.ToString()))}"); #endif // fixed: sometimes relativeLDUR will be NAN when start at some android devices. // if relativeLDUR is NAN then sizeDelta will be NAN, the safe area will be wrong. if (float.IsNaN(relativeLDUR[0])) { relativeLDUR[0] = 0; } if (float.IsNaN(relativeLDUR[1])) { relativeLDUR[1] = 0; } if (float.IsNaN(relativeLDUR[2])) { relativeLDUR[2] = 0; } if (float.IsNaN(relativeLDUR[3])) { relativeLDUR[3] = 0; } var currentRect = rectTransform.rect; //TODO : Calculate the current padding relative, to enable "Unlocked" mode. (Not forcing zero padding) var finalPaddingsLDUR = new float[4] { 0, 0, 0, 0 }; switch (selectedOrientation.left) { case EdgeEvaluationMode.On: finalPaddingsLDUR[0] = topRect.width * relativeLDUR[0]; break; case EdgeEvaluationMode.Balanced: finalPaddingsLDUR[0] = relativeLDUR[3] > relativeLDUR[0] ? topRect.width * relativeLDUR[3] : topRect.width * relativeLDUR[0]; break; } switch (selectedOrientation.right) { case EdgeEvaluationMode.On: finalPaddingsLDUR[3] = topRect.width * relativeLDUR[3]; break; case EdgeEvaluationMode.Balanced: finalPaddingsLDUR[3] = relativeLDUR[0] > relativeLDUR[3] ? topRect.width * relativeLDUR[0] : topRect.width * relativeLDUR[3]; break; } switch (selectedOrientation.bottom) { case EdgeEvaluationMode.On: finalPaddingsLDUR[1] = topRect.height * relativeLDUR[1]; break; case EdgeEvaluationMode.Balanced: finalPaddingsLDUR[1] = relativeLDUR[2] > relativeLDUR[1] ? topRect.height * relativeLDUR[2] : topRect.height * relativeLDUR[1]; break; } switch (selectedOrientation.top) { case EdgeEvaluationMode.On: finalPaddingsLDUR[2] = topRect.height * relativeLDUR[2]; break; case EdgeEvaluationMode.Balanced: finalPaddingsLDUR[2] = relativeLDUR[1] > relativeLDUR[2] ? topRect.height * relativeLDUR[1] : topRect.height * relativeLDUR[2]; break; } //Apply influence to the calculated padding finalPaddingsLDUR[0] *= influence; finalPaddingsLDUR[1] *= influence; finalPaddingsLDUR[2] *= influence; finalPaddingsLDUR[3] *= influence; if (flipPadding) { float remember = 0; finalPaddingsLDUR[0] = remember; finalPaddingsLDUR[0] = finalPaddingsLDUR[3]; finalPaddingsLDUR[3] = remember; finalPaddingsLDUR[1] = remember; finalPaddingsLDUR[1] = finalPaddingsLDUR[2]; finalPaddingsLDUR[2] = remember; } #if DEBUG_NOTCH_SOLUTION Debug.Log($"FinalLDUR {string.Join(" ", finalPaddingsLDUR.Select(x => x.ToString()))}"); #endif //Combined padding becomes size delta. var sizeDelta = rectTransform.sizeDelta; sizeDelta.x = -(finalPaddingsLDUR[0] + finalPaddingsLDUR[3]); sizeDelta.y = -(finalPaddingsLDUR[1] + finalPaddingsLDUR[2]); rectTransform.sizeDelta = sizeDelta; //The rect remaining after subtracted the size delta. Vector2 rectWidthHeight = new Vector2(topRect.width + sizeDelta.x, topRect.height + sizeDelta.y); #if DEBUG_NOTCH_SOLUTION Debug.Log($"RectWidthHeight {rectWidthHeight}"); #endif //Anchor position's answer is depending on pivot too. Where the pivot point is defines where 0 anchor point is. Vector2 zeroPosition = new Vector2(rectTransform.pivot.x * topRect.width, rectTransform.pivot.y * topRect.height); Vector2 pivotInRect = new Vector2(rectTransform.pivot.x * rectWidthHeight.x, rectTransform.pivot.y * rectWidthHeight.y); #if DEBUG_NOTCH_SOLUTION Debug.Log($"zeroPosition {zeroPosition}"); #endif //Calculate like zero position is at bottom left first, then diff with the real zero position. rectTransform.anchoredPosition3D = new Vector3( finalPaddingsLDUR[0] + pivotInRect.x - zeroPosition.x, finalPaddingsLDUR[1] + pivotInRect.y - zeroPosition.y, rectTransform.anchoredPosition3D.z); }