GUI.Window Resize Window

Hello all!

I am trying to make a GUI.Window that can be resized by placing the mouse in the bottom, bottom/right or right side. I am trying to mimic basic Window usage much like you see on a Mac or Windows OS.

This is my theory and I needed some smarter people to bounce this off of: 1) Create a GUI.Window 2) Check to see if the mouse is over the GUI.Window 3) Check to see if the mouse is over a part of the window (bottom, bottom,right, right) and show the icon to resize the window 4) Change the size of the GUI.Window based on the mouse movement.

Is this even possible?

Thanks!

This is something like what I've used (though I ended up wrapping it in a class), which goes in your window draw function, near the very end so it can be in the lower right corner:

public static Rect ResizeWindow( Rect windowRect, ref bool isResizing, ref Rect resizeStart, Vector2 minWindowSize )
{
    if( styleWindowResize == null )
    {
        // this is a custom style that looks like a // in the lower corner
        styleWindowResize = GUI.skin.GetStyle( "WindowResizer" );
    }

    Vector2 mouse = GUIUtility.ScreenToGUIPoint(new Vector2( Input.mousePosition.x, Screen.height - Input.mousePosition.y ));
    Rect r = GUILayoutUtility.GetRect( gcDrag, styleWindowResize );

    if( Event.current.type == EventType.mouseDown && r.Contains( mouse ) )
    {
        isResizing = true;
        resizeStart = new Rect( mouse.x, mouse.y, windowRect.width, windowRect.height );
        //Event.current.Use();  // the GUI.Button below will eat the event, and this way it will show its active state
    }
    else if( Event.current.type == EventType.mouseUp && isResizing )
    {
        isResizing = false;
    }
    else if( !Input.GetMouseButton( 0 ) )
    {
        // if the mouse is over some other window we won't get an event, this just kind of circumvents that by checking the button state directly
        isResizing = false;
    }
    else if( isResizing )
    {
        windowRect.width = Mathf.Max( minWindowSize.x, resizeStart.width + ( mouse.x - resizeStart.x ) );
        windowRect.height = Mathf.Max( minWindowSize.y, resizeStart.height + ( mouse.y - resizeStart.y ) );
        windowRect.xMax = Mathf.Min( Screen.width, windowRect.xMax );  // modifying xMax affects width, not x
        windowRect.yMax = Mathf.Min( Screen.height, windowRect.yMax );  // modifying yMax affects height, not y
    }

    GUI.Button( r, gcDrag, styleWindowResize );

    return windowRect;
}

I didn't have much luck with using Event.mouseDelta (it got out of sync), which is why this simply stores the starting position and does the difference.

Usage example:

Rect windowRect = new Rect( 50, 50, 100, 100 );
bool isResizing = false;
Rect windowResizeStart = new Rect();
Vector2 minWindowSize = new Vector2( 75, 50 );
static GUIStyle styleWindowResize = null;
static GUIContent gcDrag = new GUIContent( "", "drag to resize" );

void OnGUI()
{
   windowRect = GUILayout.Window( ... YourWindowFunction );
}
void YourWindowFunction( int id )
{
  GUILayout.Label( "...." );
  // other stuff
  windowRect = ResizeWindow( windowRect, ref isResizing, ref windowResizeStart, minWindowsize );
}

well I found this article usefull for my doing and I did my own Structure for window resizing did it all

player cannot turn around the window, except maybe with both X&Y together on witch I haven’t done much thought

all you do is:

ResizeWindow Resize = new ResizeWindow(true);

Resize.ResizeF(ref EquipRec);

you just have to refer to window/button you want to make it resizable

everything works Up/Down/Left/Right

AND IF you want only X axis to be dragable you simply only call:

Resize.ResizeXF(ref EquipRec);

AND if you want only Down to be resizable you simply call:

Resize.ResizeDownF(ref EquipRec);

using UnityEngine;
using System.Collections;

public struct ResizeWindow {
	public int Grab;
	public int MinWinSize;
	
	private bool ResizeUp;
	private bool ResizeDown;
	private bool ResizeLeft;
	private bool ResizeRight;
	
	private float rememberX;
	private float rememberResetX;
	private float rememberY;
	private float rememberResetY;
	// you actually don't name anything true or false with new one just less code to type
	public ResizeWindow (int GrabLength){
		ResizeUp = false;
		ResizeDown = false;
		ResizeLeft = false;
		ResizeRight = false;
		rememberX = 0;
		rememberResetX = 0;
		rememberY = 0;
		rememberResetY = 0;
		Grab = GrabLength;
		MinWinSize = 50;
	}
	
	public ResizeWindow (int GrabLength, int MinimumWindowSize){
		ResizeUp = false;
		ResizeDown = false;
		ResizeLeft = false;
		ResizeRight = false;
		rememberX = 0;
		rememberResetX = 0;
		rememberY = 0;
		rememberResetY = 0;
		Grab = GrabLength;
		MinWinSize = MinimumWindowSize;
	}
	
	
	public void ResizeF (ref Rect ResizingWindow){
		ResizeXF(ref ResizingWindow);
		ResizeYF(ref ResizingWindow);
	}
	
	
	
	
	
	public void ResizeXF (ref Rect ResizingWindow){
		ResizeLeftF (ref ResizingWindow);
		ResizeRightF (ref ResizingWindow);
	}
	
	
	public void ResizeYF (ref Rect ResizingWindow){
		ResizeUpF (ref ResizingWindow);
		ResizeDownF (ref ResizingWindow);
	}
	
	
	
	
	// WARNING /*Input.mousePosition.y * -1 + Screen.height*/ is correct positioning pixles
	public void ResizeUpF (ref Rect ResizingWindow){
		if (Event.current.type.ToString() == "mouseDown" && Event.current.mousePosition.y < (Grab)){
			ResizeUp = true;
			rememberY = ResizingWindow.y + ResizingWindow.height;
			rememberResetY = ResizingWindow.y;
		}
		if (ResizeUp){
			ResizingWindow.y = (Screen.height-Input.mousePosition.y) -1;
			ResizingWindow.height = rememberY - (Screen.height-Input.mousePosition.y);
			if (Event.current.type.ToString() == "mouseUp"){
				ResizeUp = false;
			}
			else {
				FalseItY (ref ResizingWindow, 'U');
			}
		}
	}
	
	public void ResizeDownF (ref Rect ResizingWindow){
		if (Event.current.type.ToString() == "mouseDown" && Event.current.mousePosition.y > (ResizingWindow.height - Grab)){
			ResizeDown = true;
			rememberY = ResizingWindow.y + ResizingWindow.height;
			rememberResetY = ResizingWindow.y;
		}
		if (ResizeDown){
			ResizingWindow.height = Event.current.mousePosition.y + 1;
			if (Event.current.type.ToString() == "mouseUp"){
				ResizeDown = false;
			}
			else {
				FalseItY (ref ResizingWindow, 'D');
			}
		}
	}
	
	public void ResizeLeftF (ref Rect ResizingWindow){
		if (Event.current.type.ToString() == "mouseDown" && Event.current.mousePosition.x < (Grab)){
			ResizeLeft = true;
			rememberX = ResizingWindow.x + ResizingWindow.width;
			rememberResetX = ResizingWindow.x;
		}
		if (ResizeLeft){
			ResizingWindow.x = Input.mousePosition.x - 1;
			ResizingWindow.width = rememberX - Input.mousePosition.x;
			if (Event.current.type.ToString() == "mouseUp"){
				ResizeLeft = false;
			}
			else {
				FalseItX (ref ResizingWindow, 'L');
			}
		}
	}
	
	public void ResizeRightF (ref Rect ResizingWindow){
		if (Event.current.type.ToString() == "mouseDown" && Event.current.mousePosition.x > (ResizingWindow.width - Grab)){
			ResizeRight = true;
			rememberX = ResizingWindow.x + ResizingWindow.width;
			rememberResetX = ResizingWindow.x;
		}
		if (ResizeRight){
			ResizingWindow.width = Event.current.mousePosition.x + 1;
			if (Event.current.type.ToString() == "mouseUp"){
				ResizeRight = false;
			}
			else {
				FalseItX (ref ResizingWindow, 'R');
			}
		}
	}
	
	void FalseItY (ref Rect Window, char c){
		if ((Event.current.mousePosition.x < 0 || Event.current.mousePosition.x > Window.width)
			&& (ResizeLeft == false && ResizeRight == false)
			|| (Window.height < MinWinSize)  ){
			
			ResizeUp = false;
			ResizeDown = false;
			
			if (Window.height < MinWinSize+1){
				if (c == 'U'){
					Window.y -= Grab;
					Window.height += Grab;
				}
				else if (c == 'D'){
					Window.height += Grab;
				}
				// just a glich workaround (if PLayer pulles the window too fast so window becomes turned around)
				if (Window.height < Grab){
					if (c == 'U'){
						Window.y = rememberResetY;
						Window.height = rememberY - rememberResetY ;
					}
					else if (c == 'D'){
						Window.height = rememberY - rememberResetY ;
					}
				}
			}
		}
	}
	void FalseItX (ref Rect Window, char c){
		if ( (Event.current.mousePosition.y < 0 || Event.current.mousePosition.y > Window.height)
			&& (ResizeUp == false && ResizeDown == false)
			|| (Window.width < MinWinSize)  ){
			
			ResizeLeft = false;
			ResizeRight = false;
			
			if (Window.width < MinWinSize+1){
				if (c == 'L'){
					Window.x -= Grab;
					Window.width += Grab;
				}
				else if (c == 'R'){
					Window.width += Grab;
				}
				// just a glich workaround (if PLayer pulles the window too fast so window becomes turned around)
				if (Window.width < Grab){
					if (c == 'L'){
						Window.x = rememberResetX;
						Window.width = rememberX - rememberResetX ;
					}
					else if (c == 'R'){
						Window.width = rememberX - rememberResetX ;
					}
				}
			}
		}
	}
}

don’t ask me but I love ref more than return less code to write

and I don’t like classes as you have to instantiate 1 and I have ridden writing more code I hate to write that . just to call something that’s always same

am I haven’t played with static as I think I don’t have enough experience with it even tho it maybe would be faster but for resizing IDK

code is revetivelly simple

only thing is IF you press the upper or left you’ll end up with shifting right side or down side a bit

Yes, that's what I would try. I know you can resize the window just by calling GUI.Window again with different Rect.

I make this. Hope help you.
link text