From fb2b2bc9e5a380067076505607d784fe607e001a Mon Sep 17 00:00:00 2001 From: SilentSpike Date: Sat, 18 Jul 2015 16:59:05 +0100 Subject: [PATCH] Improve freecam handling --- .../spectator/functions/fnc_handleCamera.sqf | 81 +++++++++---------- .../functions/fnc_handleInterface.sqf | 9 +-- .../spectator/functions/fnc_handleMouse.sqf | 12 ++- .../spectator/functions/fnc_updateCamera.sqf | 3 + 4 files changed, 46 insertions(+), 59 deletions(-) diff --git a/addons/spectator/functions/fnc_handleCamera.sqf b/addons/spectator/functions/fnc_handleCamera.sqf index bd80a0853a..1362ad2d80 100644 --- a/addons/spectator/functions/fnc_handleCamera.sqf +++ b/addons/spectator/functions/fnc_handleCamera.sqf @@ -1,6 +1,6 @@ /* * Author: F3 Project, Head, SilentSpike - * Handles camera manipulation according to input + * Handles free camera manipulation according to input * * Arguments: * None @@ -16,48 +16,41 @@ #include "script_component.hpp" -// Kill PFH when display is closed -if (isNull (GETUVAR(GVAR(display),displayNull))) exitWith { [_this select 1] call CBA_fnc_removePerFrameHandler; }; +// Kill PFH when not in free cam (or display is closed) +if ((GVAR(camMode) != 0) || isNull (GETUVAR(GVAR(display),displayNull))) exitWith { [_this select 1] call CBA_fnc_removePerFrameHandler; }; -// Different behaviour based on camera mode -switch (GVAR(camMode)) do { - case 0: { // Free - _oldPos = getPosASL GVAR(camera); - _mX = 0; - _mY = 0; - _mZ = 0; - if (GVAR(camDolly) select 0) then { // Forward - _mY = _mY + GVAR(camSpeed); - }; - if (GVAR(camDolly) select 1) then { // Backward - _mY = _mY - GVAR(camSpeed); - }; - if (GVAR(camDolly) select 2) then { // Left - _mX = _mX - GVAR(camSpeed); - }; - if (GVAR(camDolly) select 3) then { // Right - _mX = _mX + GVAR(camSpeed); - }; - if (GVAR(camBoom) select 0) then { // Up - _mZ = _mZ + 0.5; - }; - if (GVAR(camBoom) select 1) then { // Down - _mZ = _mZ - 0.5; - }; - - _pan = (GVAR(camPan) + 360) % 360; - _x = (_oldPos select 0) + (_mX * cos(_pan)) + (_mY * sin(_pan)); - _y = (_oldPos select 1) - (_mX * sin(_pan)) + (_mY * cos(_pan)); - _z = (_oldPos select 2) + _mZ; - - // Prevent camera going under terrain - GVAR(camPos) = [_x,_y,_z max (getTerrainHeightASL [_x,_y])]; - - // Update camera position and rotation - GVAR(camera) setPosASL GVAR(camPos); - GVAR(camera) setDir GVAR(camPan); - [GVAR(camera), GVAR(camTilt), GVAR(camBank)] call BIS_fnc_setPitchBank; - }; - case 1: { // Internal - }; +_oldPos = getPosASL GVAR(camera); +_mX = 0; +_mY = 0; +_mZ = 0; +if (GVAR(camDolly) select 0) then { // Forward + _mY = _mY + GVAR(camSpeed); }; +if (GVAR(camDolly) select 1) then { // Backward + _mY = _mY - GVAR(camSpeed); +}; +if (GVAR(camDolly) select 2) then { // Left + _mX = _mX - GVAR(camSpeed); +}; +if (GVAR(camDolly) select 3) then { // Right + _mX = _mX + GVAR(camSpeed); +}; +if (GVAR(camBoom) select 0) then { // Up + _mZ = _mZ + 0.5; +}; +if (GVAR(camBoom) select 1) then { // Down + _mZ = _mZ - 0.5; +}; + +_pan = (GVAR(camPan) + 360) % 360; +_x = (_oldPos select 0) + (_mX * cos(_pan)) + (_mY * sin(_pan)); +_y = (_oldPos select 1) - (_mX * sin(_pan)) + (_mY * cos(_pan)); +_z = (_oldPos select 2) + _mZ; + +// Prevent camera going under terrain +GVAR(camPos) = [_x,_y,_z max (getTerrainHeightASL [_x,_y])]; + +// Update camera position and rotation +GVAR(camera) setPosASL GVAR(camPos); +GVAR(camera) setDir GVAR(camPan); +[GVAR(camera), GVAR(camTilt), GVAR(camBank)] call BIS_fnc_setPitchBank; diff --git a/addons/spectator/functions/fnc_handleInterface.sqf b/addons/spectator/functions/fnc_handleInterface.sqf index 58f10cde11..088972a066 100644 --- a/addons/spectator/functions/fnc_handleInterface.sqf +++ b/addons/spectator/functions/fnc_handleInterface.sqf @@ -43,9 +43,7 @@ switch (toLower _mode) do { // Initalize display variables GVAR(ctrlKey) = false; GVAR(mouse) = [false,false]; - GVAR(mouseDelta) = [0.5,0.5]; GVAR(mousePos) = [0.5,0.5]; - GVAR(mousePosOld) = [0.5,0.5]; // Initalize the camera view GVAR(camera) = "Camera" camCreate GVAR(camPos); @@ -54,9 +52,6 @@ switch (toLower _mode) do { GVAR(camera) camSetFOV GVAR(camFOV); - // Handle camera movement - [FUNC(handleCamera), 0] call CBA_fnc_addPerFrameHandler; - // Populate the unit list [] call FUNC(updateUnits); @@ -169,15 +164,13 @@ switch (toLower _mode) do { } else { GVAR(camZoom) = (GVAR(camZoom) - (_zChange * GVAR(camZoom) * 0.2)) max 0.1; }; - [] call FUNC(handleCamera); }; case "onmousemoving": { private ["_x","_y"]; _x = _args select 1; _y = _args select 2; - GVAR(mousePos) = [_x,_y]; - [] call FUNC(handleMouse); + [_x,_y] call FUNC(handleMouse); }; // Keyboard events case "onkeydown": { diff --git a/addons/spectator/functions/fnc_handleMouse.sqf b/addons/spectator/functions/fnc_handleMouse.sqf index e3921e77cb..4c3ef6067b 100644 --- a/addons/spectator/functions/fnc_handleMouse.sqf +++ b/addons/spectator/functions/fnc_handleMouse.sqf @@ -16,24 +16,22 @@ #include "script_component.hpp" -private ["_x","_y","_leftButton","_rightButton","_oldX","_oldY","_deltaX","_deltaY"]; +params ["_x","_y"]; +private ["_leftButton","_rightButton","_oldX","_oldY","_deltaX","_deltaY"]; -_x = GVAR(mousePos) select 0; -_y = GVAR(mousePos) select 1; _leftButton = GVAR(mouse) select 0; _rightButton = GVAR(mouse) select 1; -_oldX = GVAR(mousePosOld) select 0; -_oldY = GVAR(mousePosOld) select 1; +_oldX = GVAR(mousePos) select 0; +_oldY = GVAR(mousePos) select 1; // Get change in pos _deltaX = _oldX - _x; _deltaY = _oldY - _y; -GVAR(mouseDelta) = [_deltaX, _deltaY]; if (_rightButton && !_leftButton) then { GVAR(camPan) = GVAR(camPan) - (_deltaX * 360); GVAR(camTilt) = ((GVAR(camTilt) + (_deltaY * 180)) min 89) max -89; }; -GVAR(mousePosOld) = [_x,_y]; +GVAR(mousePos) = [_x,_y]; diff --git a/addons/spectator/functions/fnc_updateCamera.sqf b/addons/spectator/functions/fnc_updateCamera.sqf index 679fae56b7..440b7103ab 100644 --- a/addons/spectator/functions/fnc_updateCamera.sqf +++ b/addons/spectator/functions/fnc_updateCamera.sqf @@ -21,6 +21,9 @@ if (GVAR(camMode) == 0) then { // Free // HUD stuff showCinemaBorder false; cameraEffectEnableHUD false; + + // Handle camera movement + [FUNC(handleCamera), 0] call CBA_fnc_addPerFrameHandler; } else { // When null unit is given choose random if (isNull _newUnit) then {