@@ -23,6 +23,9 @@ namespace our {
2323 // When a state enters, it should call this function and give it the pointer to the application
2424 void enter (Application* app) {
2525 this ->app = app;
26+ app->getMouse ().lockMouse (app->getWindow ());
27+ app->getMouse ().enable (app->getWindow ());
28+ mouse_locked = true ;
2629 }
2730
2831 // This should be called every frame to update all entities containing a FreeCameraControllerComponent
@@ -42,23 +45,12 @@ namespace our {
4245 // Get the entity that we found via getOwner of camera (we could use controller->getOwner())
4346 Entity* entity = camera->getOwner ();
4447
45- // If the left mouse button is pressed, we lock and hide the mouse. This common in First Person Games.
46- if (app->getMouse ().isPressed (GLFW_MOUSE_BUTTON_1 ) && !mouse_locked) {
47- app->getMouse ().lockMouse (app->getWindow ());
48- mouse_locked = true ;
49- // If the left mouse button is released, we unlock and unhide the mouse.
50- } else if (!app->getMouse ().isPressed (GLFW_MOUSE_BUTTON_1 ) && mouse_locked) {
51- app->getMouse ().unlockMouse (app->getWindow ());
52- mouse_locked = false ;
53- }
54-
5548 // We get a reference to the entity's position and rotation
5649 glm::vec3& position = entity->localTransform .position ;
5750 glm::vec3& rotation = entity->localTransform .rotation ;
5851
59- // If the left mouse button is pressed, we get the change in the mouse location
60- // and use it to update the camera rotation
61- if (app->getMouse ().isPressed (GLFW_MOUSE_BUTTON_1 )) {
52+ // While the play state is active, the mouse stays locked
53+ if (mouse_locked) {
6254 glm::vec2 delta = app->getMouse ().getMouseDelta ();
6355 rotation.x -= delta.y * controller->rotationSensitivity ; // The y-axis controls the pitch
6456 rotation.y -= delta.x * controller->rotationSensitivity ; // The x-axis controls the yaw
@@ -105,6 +97,7 @@ namespace our {
10597 if (mouse_locked) {
10698 mouse_locked = false ;
10799 app->getMouse ().unlockMouse (app->getWindow ());
100+ app->getMouse ().enable (app->getWindow ());
108101 }
109102 }
110103 };
0 commit comments