I was building a resizeable window with a second control inside it for one of my pet projects, but have hit an interesting snag. When the mouse is held down in the proper area for dragging, the coordinates for the current mouse position change rapidly back and forth, and I’m having a heck of a time figuring out why. Here’s the actual calculation portion of the code:
foreach (KeyValuePair<int, LayoutControl> kvp in mainLayout)
{
// Debug.Log (kvp.Value.placement);
kvp.Value.placement = GUI.Window(9 + kvp.Key, kvp.Value.placement, DrawControl, "");
LayoutControl lc;
mainLayout.TryGetValue(kvp.Key, out lc);
if(dragDirection != Drag.N currentMousePosition != lastMousePosition)
{
Debug.Log (currentMousePosition + " : " + lastMousePosition);
switch(dragDirection)
{
case Drag.L:
float difference = currentMousePosition.x - lastMousePosition.x;
mainLayout[kvp.Key].placement = new Rect(lc.placement.x + difference, lc.placement.y, lc.placement.width - difference, lc.placement.height);
lastMousePosition = currentMousePosition;
break;
}
}
}
And the DrawControl function where currentMousePosition is changed:
void DrawControl(int windowID)
{
LayoutControl lc;
mainLayout.TryGetValue(windowID - 9, out lc);
switch(lc.type)
{
case LayoutControl.controlTypes.N:
break;
case LayoutControl.controlTypes.T1:
lc.data = GUI.TextField(new Rect(25, 25, lc.placement.width - 50, lc.placement.height - 25), lc.data);
break;
}
if(GUI.Button(new Rect(0, 0, 20, 20), "X"))
{
mainLayout.Remove(windowID - 9);
}
if(GUI.Button(new Rect(lc.placement.width - 20, 0, 20, 20), "S"))
{
if(showSettings > 0)
showSettings = 0;
else
showSettings = windowID;
}
Event ce = Event.current;
if(ce.type == EventType.MouseDrag)
{
currentMousePosition = ce.mousePosition;
}
if(ce.type == EventType.MouseDown ce.button == 0)
{
// Check if mouse is down inside drag areas
if(ce.mousePosition.y > 20 ce.mousePosition.y < lc.placement.height)
{
if(ce.mousePosition.x < 25)
{
dragDirection = Drag.L;
lastMousePosition = ce.mousePosition;
}
else if(ce.mousePosition.x > lc.placement.width - 25)
{
dragDirection = Drag.R;
lastMousePosition = ce.mousePosition;
}
}
}
if(ce.type == EventType.MouseUp ce.button == 0)
{
Debug.Log ("Released");
dragDirection = Drag.N;
}
GUI.DragWindow();
}
My big question would be why does it jump like that? The size changes on even a fast mouse click without moving the mouse. Heaven help me if I actually move the mouse as it goes wild, expanding rapidly and even seeming to reverse itself completely.