Skip to content

Commit

Permalink
Popup now works properly in "Screen Space - Camera" and "World Space"…
Browse files Browse the repository at this point in the history
… canvas modes
  • Loading branch information
yasirkula committed Apr 12, 2021
1 parent e7bbc89 commit 615d906
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 43 deletions.
7 changes: 4 additions & 3 deletions Plugins/IngameDebugConsole/IngameDebugConsole.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,7 @@ MonoBehaviour:
singleton: 1
minimumHeight: 200
enableHorizontalResizing: 0
resizeFromRight: 1
minimumWidth: 240
enablePopup: 1
startInPopupMode: 1
Expand Down Expand Up @@ -1987,9 +1988,9 @@ RectTransform:
m_Father: {fileID: 22457152}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0.5}
m_AnchorMax: {x: 1, y: 0.5}
m_AnchoredPosition: {x: -36, y: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 1000, y: 0}
m_SizeDelta: {x: 72, y: 72}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!224 &22420992
Expand Down
4 changes: 2 additions & 2 deletions Plugins/IngameDebugConsole/Scripts/DebugLogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public class DebugLogManager : MonoBehaviour
[SerializeField]
private RectTransform logWindowTR;

private RectTransform canvasTR;
internal RectTransform canvasTR;

[SerializeField]
private RectTransform logItemsContainer;
Expand Down Expand Up @@ -638,7 +638,7 @@ private void LateUpdate()
if( isLogWindowVisible )
recycledListView.OnViewportHeightChanged();
else
popupManager.OnViewportDimensionsChanged();
popupManager.UpdatePosition( true );

#if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS
CheckScreenCutout();
Expand Down
90 changes: 53 additions & 37 deletions Plugins/IngameDebugConsole/Scripts/DebugLogPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,31 +44,26 @@ public class DebugLogPopup : MonoBehaviour, IPointerClickHandler, IBeginDragHand
private Color normalColor;

private bool isPopupBeingDragged = false;
private Vector2 normalizedPosition;

// Coroutines for simple code-based animations
private IEnumerator moveToPosCoroutine = null;

void Awake()
private void Awake()
{
popupTransform = (RectTransform) transform;
backgroundImage = GetComponent<Image>();
canvasGroup = GetComponent<CanvasGroup>();

normalColor = backgroundImage.color;
}

void Start()
{
halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
}

public void OnViewportDimensionsChanged()
{
if( !gameObject.activeSelf )
return;
halfSize = popupTransform.sizeDelta * 0.5f;

halfSize = popupTransform.sizeDelta * 0.5f * popupTransform.root.localScale.x;
OnEndDrag( null );
Vector2 pos = popupTransform.anchoredPosition;
if( pos.x != 0f || pos.y != 0f )
normalizedPosition = pos.normalized; // Respect the initial popup position set in the prefab
else
normalizedPosition = new Vector2( 0.5f, 0f ); // Right edge by default
}

public void NewLogsArrived( int newInfo, int newWarning, int newError )
Expand Down Expand Up @@ -113,15 +108,15 @@ private void Reset()
}

// A simple smooth movement animation
private IEnumerator MoveToPosAnimation( Vector3 targetPos )
private IEnumerator MoveToPosAnimation( Vector2 targetPos )
{
float modifier = 0f;
Vector3 initialPos = popupTransform.position;
Vector2 initialPos = popupTransform.anchoredPosition;

while( modifier < 1f )
{
modifier += 4f * Time.unscaledDeltaTime;
popupTransform.position = Vector3.Lerp( initialPos, targetPos, modifier );
popupTransform.anchoredPosition = Vector2.Lerp( initialPos, targetPos, modifier );

yield return null;
}
Expand All @@ -145,8 +140,8 @@ public void Show()
// Reset the counters
Reset();

// Update position in case resolution changed while hidden
OnViewportDimensionsChanged();
// Update position in case resolution was changed while the popup was hidden
UpdatePosition( true );
}

// Hide the popup
Expand Down Expand Up @@ -174,23 +169,36 @@ public void OnBeginDrag( PointerEventData data )
// Reposition the popup
public void OnDrag( PointerEventData data )
{
popupTransform.position = data.position;
Vector2 localPoint;
if( RectTransformUtility.ScreenPointToLocalPointInRectangle( debugManager.canvasTR, data.position, data.pressEventCamera, out localPoint ) )
popupTransform.anchoredPosition = localPoint;
}

// Smoothly translate the popup to the nearest edge
public void OnEndDrag( PointerEventData data )
{
int screenWidth = Screen.width;
int screenHeight = Screen.height;
isPopupBeingDragged = false;
UpdatePosition( false );
}

public void UpdatePosition( bool immediately )
{
Vector2 canvasSize = debugManager.canvasTR.rect.size;

Vector3 pos = popupTransform.position;
float canvasWidth = canvasSize.x;
float canvasHeight = canvasSize.y;

// normalizedPosition allows us to glue the popup to a specific edge of the screen. It becomes useful when
// the popup is at the right edge and we switch from portrait screen orientation to landscape screen orientation.
// Without normalizedPosition, popup could jump to bottom or top edges instead of staying at the right edge
Vector2 pos = immediately ? new Vector2( normalizedPosition.x * canvasWidth, normalizedPosition.y * canvasHeight ) : popupTransform.anchoredPosition;

// Find distances to all four edges
float distToLeft = pos.x;
float distToRight = Mathf.Abs( pos.x - screenWidth );
float distToLeft = canvasWidth * 0.5f + pos.x;
float distToRight = canvasWidth - distToLeft;

float distToBottom = Mathf.Abs( pos.y );
float distToTop = Mathf.Abs( pos.y - screenHeight );
float distToBottom = canvasHeight * 0.5f + pos.y;
float distToTop = canvasHeight - distToBottom;

float horDistance = Mathf.Min( distToLeft, distToRight );
float vertDistance = Mathf.Min( distToBottom, distToTop );
Expand All @@ -199,31 +207,39 @@ public void OnEndDrag( PointerEventData data )
if( horDistance < vertDistance )
{
if( distToLeft < distToRight )
pos = new Vector3( halfSize.x, pos.y, 0f );
pos = new Vector2( canvasWidth * -0.5f + halfSize.x, pos.y );
else
pos = new Vector3( screenWidth - halfSize.x, pos.y, 0f );
pos = new Vector2( canvasWidth * 0.5f - halfSize.x, pos.y );

pos.y = Mathf.Clamp( pos.y, halfSize.y, screenHeight - halfSize.y );
pos.y = Mathf.Clamp( pos.y, canvasHeight * -0.5f + halfSize.y, canvasHeight * 0.5f - halfSize.y );
}
else
{
if( distToBottom < distToTop )
pos = new Vector3( pos.x, halfSize.y, 0f );
pos = new Vector2( pos.x, canvasHeight * -0.5f + halfSize.y );
else
pos = new Vector3( pos.x, screenHeight - halfSize.y, 0f );
pos = new Vector2( pos.x, canvasHeight * 0.5f - halfSize.y );

pos.x = Mathf.Clamp( pos.x, halfSize.x, screenWidth - halfSize.x );
pos.x = Mathf.Clamp( pos.x, canvasWidth * -0.5f + halfSize.x, canvasWidth * 0.5f - halfSize.x );
}

normalizedPosition.Set( pos.x / canvasWidth, pos.y / canvasHeight );

// If another smooth movement animation is in progress, cancel it
if( moveToPosCoroutine != null )
{
StopCoroutine( moveToPosCoroutine );
moveToPosCoroutine = null;
}

// Smoothly translate the popup to the specified position
moveToPosCoroutine = MoveToPosAnimation( pos );
StartCoroutine( moveToPosCoroutine );

isPopupBeingDragged = false;
if( immediately )
popupTransform.anchoredPosition = pos;
else
{
// Smoothly translate the popup to the specified position
moveToPosCoroutine = MoveToPosAnimation( pos );
StartCoroutine( moveToPosCoroutine );
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.yasirkula.ingamedebugconsole",
"displayName": "In-game Debug Console",
"version": "1.4.6",
"version": "1.4.7",
"documentationUrl": "https://github.com/yasirkula/UnityIngameDebugConsole",
"changelogUrl": "https://github.com/yasirkula/UnityIngameDebugConsole/releases",
"licensesUrl": "https://github.com/yasirkula/UnityIngameDebugConsole/blob/master/LICENSE.txt",
Expand Down

0 comments on commit 615d906

Please sign in to comment.