* Handle mouse inputs. */
| 306 | * Handle mouse inputs. |
| 307 | */ |
| 308 | bool MouseHandler(InputInfo &input, Camera &camera, InputOptions &inputOptions) |
| 309 | { |
| 310 | Mouse::State mouse = input.mouse.GetState(); |
| 311 | if (mouse.leftButton) |
| 312 | { |
| 313 | // Just pressed the left mouse button |
| 314 | if (input.lastMouseXY.x == INT_MAX && input.lastMouseXY.y == INT_MAX) |
| 315 | { |
| 316 | input.lastMouseXY = { mouse.x, mouse.y }; |
| 317 | return false; |
| 318 | } |
| 319 | |
| 320 | // Compute relative change in mouse position, and multiply by the degrees of change per pixel |
| 321 | float degreesPerPixelX = (camera.fov / (float)input.width) * camera.aspect; |
| 322 | float degreesPerPixelY = (camera.fov / (float)input.height); |
| 323 | |
| 324 | input.yaw += (float)(mouse.x - input.lastMouseXY.x) * degreesPerPixelX * inputOptions.rotationSpeed; |
| 325 | input.pitch += (float)(mouse.y - input.lastMouseXY.y) * degreesPerPixelY * inputOptions.rotationSpeed; |
| 326 | |
| 327 | if (abs(input.yaw) >= 360) input.yaw = 0.f; |
| 328 | if (abs(input.pitch) >= 360) input.pitch = 0.f; |
| 329 | |
| 330 | /*char buffer[100]; |
| 331 | sprintf_s(buffer, "mouse delta: (%i, %i)\n", (mouse.x - input.lastMouseXY.x), (mouse.y - input.lastMouseXY.y)); |
| 332 | OutputDebugStringA(buffer);*/ |
| 333 | |
| 334 | // Store current mouse position |
| 335 | input.lastMouseXY = { mouse.x, mouse.y }; |
| 336 | |
| 337 | // Compute and apply the rotation |
| 338 | Rotate(input, camera); |
| 339 | |
| 340 | return true; |
| 341 | } |
| 342 | |
| 343 | if (mouse.rightButton) |
| 344 | { |
| 345 | // Just pressed the right mouse button |
| 346 | if (input.lastMouseXY.x == INT_MAX && input.lastMouseXY.y == INT_MAX) |
| 347 | { |
| 348 | input.lastMouseXY = { mouse.x, mouse.y }; |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | float speed = inputOptions.movementSpeed / 100.f; |
| 353 | if (inputOptions.invertPan) speed *= -1.f; |
| 354 | |
| 355 | float speedX = (float)(mouse.x - input.lastMouseXY.x) * speed; |
| 356 | float speedY = (float)(mouse.y - input.lastMouseXY.y) * -speed; |
| 357 | |
| 358 | // Store current mouse position |
| 359 | input.lastMouseXY = { mouse.x, mouse.y }; |
| 360 | |
| 361 | // Compute new camera position |
| 362 | camera.position = { camera.position.x - (camera.right.x * speedX), camera.position.y - (camera.right.y * speedX), camera.position.z - (camera.right.z * speedX) }; |
| 363 | camera.position = { camera.position.x - (camera.up.x * speedY), camera.position.y - (camera.up.y * speedY), camera.position.z - (camera.up.z * speedY) }; |
| 364 | |
| 365 | return true; |