diff --git a/AVVR/Assets/_Scripts/SpectatorWindow.cs b/AVVR/Assets/_Scripts/SpectatorWindow.cs index eb0fbbd8ce94cf65937da6e4dfc715c74e6be08f..81460cef9ccdc8170e9987628158a41410a5a9ed 100644 --- a/AVVR/Assets/_Scripts/SpectatorWindow.cs +++ b/AVVR/Assets/_Scripts/SpectatorWindow.cs @@ -7,9 +7,18 @@ public class SpectatorWindow : MonoBehaviour private RenderTexture renderTexture; private bool showWindow = true; private Rect windowRect = new Rect(100, 100, 800, 450); - + + [Header("Window Settings")] [SerializeField] private bool enableWindowDrag = true; [SerializeField] private Vector2Int renderResolution = new Vector2Int(1920, 1080); + [SerializeField] private Vector2 minWindowSize = new Vector2(400, 225); + [SerializeField] private Vector2 maxWindowSize = new Vector2(1920, 1080); + + private bool isResizing = false; + private Rect resizeHandleRect; + private Vector2 resizeStartMouse; + private Vector2 resizeStartSize; + private readonly float resizeHandleSize = 20f; private void Awake() { @@ -55,6 +64,9 @@ public class SpectatorWindow : MonoBehaviour // Draw the window with solid background GUI.backgroundColor = Color.black; windowRect = GUI.Window(0, windowRect, DrawWindowContents, "Spectator View", backgroundStyle); + + // Handle resizing outside of the window function + HandleResizing(); } private void DrawWindowContents(int windowID) @@ -71,10 +83,68 @@ public class SpectatorWindow : MonoBehaviour showWindow = false; } - // Window dragging - if (enableWindowDrag) + // Draw resize handle (visual indicator) + resizeHandleRect = new Rect(windowRect.width - resizeHandleSize, + windowRect.height - resizeHandleSize, + resizeHandleSize, resizeHandleSize); + + GUI.Box(resizeHandleRect, "↘"); + + // Window dragging (only if not resizing) + if (enableWindowDrag && !isResizing) + { + GUI.DragWindow(new Rect(0, 0, windowRect.width - resizeHandleSize, 20)); + } + } + + private void HandleResizing() + { + // Convert resize handle to screen coordinates + Rect screenResizeRect = new Rect( + windowRect.x + windowRect.width - resizeHandleSize, + windowRect.y + windowRect.height - resizeHandleSize, + resizeHandleSize, + resizeHandleSize + ); + + Event e = Event.current; + if (e.type == EventType.MouseDown && e.button == 0 && screenResizeRect.Contains(e.mousePosition)) + { + isResizing = true; + resizeStartMouse = e.mousePosition; + resizeStartSize = new Vector2(windowRect.width, windowRect.height); + e.Use(); + } + else if (e.type == EventType.MouseUp && e.button == 0) { - GUI.DragWindow(); + isResizing = false; + } + else if (isResizing && e.type == EventType.MouseDrag) + { + // Calculate new size + float newWidth = Mathf.Clamp(resizeStartSize.x + (e.mousePosition.x - resizeStartMouse.x), + minWindowSize.x, maxWindowSize.x); + float newHeight = Mathf.Clamp(resizeStartSize.y + (e.mousePosition.y - resizeStartMouse.y), + minWindowSize.y, maxWindowSize.y); + + // Maintain aspect ratio (16:9) + float targetAspect = 16f / 9f; + float currentAspect = newWidth / newHeight; + + if (currentAspect > targetAspect) + { + newWidth = newHeight * targetAspect; + } + else + { + newHeight = newWidth / targetAspect; + } + + // Apply new size + windowRect.width = newWidth; + windowRect.height = newHeight; + + e.Use(); } } @@ -87,14 +157,14 @@ public class SpectatorWindow : MonoBehaviour } } - // Public methods to control the window + // Public methods public void ShowWindow() => showWindow = true; public void HideWindow() => showWindow = false; public void ToggleWindow() => showWindow = !showWindow; public void SetWindowSize(float width, float height) { - windowRect.width = width; - windowRect.height = height; + windowRect.width = Mathf.Clamp(width, minWindowSize.x, maxWindowSize.x); + windowRect.height = Mathf.Clamp(height, minWindowSize.y, maxWindowSize.y); } } \ No newline at end of file