Drag window corrected issue

This commit is contained in:
Sergio Martínez
2018-11-14 12:15:45 +01:00
parent 0764d3de04
commit b0c36d29c3

View File

@ -41,12 +41,12 @@ int main()
// General variables // General variables
Vector2 mousePos = { 0 }; Vector2 mousePos = { 0 };
Vector2 position = { 500, 200 }; Vector2 windowPos = { 500, 200 };
Vector2 prevPosition = position;
Vector2 panOffset = mousePos; Vector2 panOffset = mousePos;
bool dragWindow = false; bool dragWindow = false;
SetTargetFPS(120); SetWindowPosition(windowPos.x, windowPos.y);
SetTargetFPS(60);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
@ -56,30 +56,24 @@ int main()
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
mousePos = GetMousePosition(); mousePos = GetMousePosition();
if ((CheckCollisionPointRec(mousePos, (Rectangle){ 0, 0, screenWidth, 20 })) && if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{ {
dragWindow = true; if (CheckCollisionPointRec(mousePos, (Rectangle){ 0, 0, screenWidth, 20 }))
panOffset = mousePos; {
dragWindow = true;
panOffset = mousePos;
}
} }
if (dragWindow) if (dragWindow)
{ {
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) windowPos.x += (mousePos.x - panOffset.x);
{ windowPos.y += (mousePos.y - panOffset.y);
position.x = prevPosition.x + (mousePos.x - panOffset.x),
position.y = prevPosition.y + (mousePos.y - panOffset.y);
}
else if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON))
{
prevPosition = position;
dragWindow = false;
}
}
// NOTE: Input reading and window positioning should be executed in a second thread, if (IsMouseButtonReleased(MOUSE_LEFT_BUTTON)) dragWindow = false;
// actually, full drawing should probably be in the second thread...
SetWindowPosition(position.x, position.y); SetWindowPosition(windowPos.x, windowPos.y);
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Draw // Draw
@ -91,6 +85,7 @@ int main()
// raygui: controls drawing // raygui: controls drawing
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
exitWindow = GuiWindowBox((Rectangle){ 0, 0, screenWidth, screenHeight }, "STANDALONE WINDOW"); exitWindow = GuiWindowBox((Rectangle){ 0, 0, screenWidth, screenHeight }, "STANDALONE WINDOW");
DrawText(FormatText("Mouse Position: [ %.0f, %.0f ]", mousePos.x, mousePos.y), 10, 40, 10, DARKGRAY);
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
EndDrawing(); EndDrawing();