Ticket #56: cameraFollowingMode.diff
| File cameraFollowingMode.diff, 12.3 KB (added by markelov, 20 months ago) |
|---|
-
source/ps/Hotkey.cpp
74 74 { HOTKEY_WIREFRAME, "wireframe", SDLK_w, 0 }, 75 75 { HOTKEY_TOGGLEFULLSCREEN, "togglefullscreen", 0, 0 }, 76 76 { HOTKEY_CAMERA_RESET, "camera.reset", 0, 0 }, 77 { HOTKEY_CAMERA_FOLLOW, "camera.follow", 0, 0 }, 77 78 { HOTKEY_CAMERA_ZOOM_IN, "camera.zoom.in", SDLK_PLUS, SDLK_KP_PLUS }, 78 79 { HOTKEY_CAMERA_ZOOM_OUT, "camera.zoom.out", SDLK_MINUS, SDLK_KP_MINUS }, 79 80 { HOTKEY_CAMERA_ZOOM_WHEEL_IN, "camera.zoom.wheel.in", MOUSE_WHEELUP, 0 }, -
source/ps/Hotkey.h
55 55 HOTKEY_WIREFRAME, 56 56 HOTKEY_TOGGLEFULLSCREEN, 57 57 HOTKEY_CAMERA_RESET, 58 HOTKEY_CAMERA_FOLLOW, 58 59 HOTKEY_CAMERA_ZOOM_IN, 59 60 HOTKEY_CAMERA_ZOOM_OUT, 60 61 HOTKEY_CAMERA_ZOOM_WHEEL_IN, -
source/gui/scripting/ScriptFunctions.cpp
72 72 g_GUI->PopPage(); 73 73 } 74 74 75 /** 76 * Start / stop camera following mode 77 * @param entityid unit id to follow for. If zero - stop following mode 78 */ 79 void CameraFollow(void* UNUSED(cbdata), entity_id_t entityid) 80 { 81 if(g_Game && g_Game->GetView()) 82 g_Game->GetView()->CameraFollow(entityid); 83 } 84 75 85 bool IsNewSimulation(void* UNUSED(cbdata)) 76 86 { 77 87 return true; // XXX: delete this function … … 313 323 scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &PushGuiPage>("PushGuiPage"); 314 324 scriptInterface.RegisterFunction<void, std::wstring, CScriptVal, &SwitchGuiPage>("SwitchGuiPage"); 315 325 scriptInterface.RegisterFunction<void, &PopGuiPage>("PopGuiPage"); 326 scriptInterface.RegisterFunction<void, entity_id_t, &CameraFollow>("CameraFollow"); 316 327 317 328 // Simulation<->GUI interface functions: 318 329 scriptInterface.RegisterFunction<bool, &IsNewSimulation>("IsNewSimulation"); -
source/graphics/GameView.cpp
52 52 #include "scripting/ScriptableObject.h" 53 53 #include "simulation/LOSManager.h" 54 54 #include "simulation2/Simulation2.h" 55 #include "simulation2/components/ICmpPosition.h" 55 56 56 57 extern int g_xres, g_yres; 57 58 … … 273 274 } 274 275 275 276 CGameView::CGameView(CGame *pGame): 276 m(new CGameViewImpl(pGame)) 277 m(new CGameViewImpl(pGame)), 278 m_cameraFollowingEntity(0) 277 279 { 278 280 SViewPort vp; 279 281 vp.m_X=0; … … 529 531 g_Renderer.GetWaterManager()->UnloadWaterTextures(); 530 532 } 531 533 532 533 534 static void ClampDistance(CGameViewImpl* m, bool smooth) 534 535 { 535 536 if (!m->ConstrainCamera) … … 603 604 if (hotkeys[HOTKEY_CAMERA_ROTATE_DOWN]) 604 605 m->RotateX.AddSmoothly(m->ViewRotateXSpeed * DeltaTime); 605 606 606 float moveRightward = 0.f; 607 float moveForward = 0.f; 608 609 if (hotkeys[HOTKEY_CAMERA_PAN]) 607 // Process camera movements only if it is not in following mode 608 if(m_cameraFollowingEntity == 0) 610 609 { 611 moveRightward += m->ViewDragSpeed * mouse_dx; 612 moveForward += m->ViewDragSpeed * -mouse_dy; 613 } 610 float moveRightward = 0.f; 611 float moveForward = 0.f; 614 612 615 if (g_mouse_active) 616 { 617 if (g_mouse_x >= g_xres - 2 && g_mouse_x < g_xres) 618 moveRightward += m->ViewScrollSpeed * DeltaTime; 619 else if (g_mouse_x <= 3 && g_mouse_x >= 0) 620 moveRightward -= m->ViewScrollSpeed * DeltaTime; 613 if (hotkeys[HOTKEY_CAMERA_PAN]) 614 { 615 moveRightward += m->ViewDragSpeed * mouse_dx; 616 moveForward += m->ViewDragSpeed * -mouse_dy; 617 } 621 618 622 if (g_mouse_y >= g_yres - 2 && g_mouse_y < g_yres) 623 moveForward -= m->ViewScrollSpeed * DeltaTime; 624 else if (g_mouse_y <= 3 && g_mouse_y >= 0) 625 moveForward += m->ViewScrollSpeed * DeltaTime; 626 } 619 if (g_mouse_active) 620 { 621 if (g_mouse_x >= g_xres - 2 && g_mouse_x < g_xres) 622 moveRightward += m->ViewScrollSpeed * DeltaTime; 623 else if (g_mouse_x <= 3 && g_mouse_x >= 0) 624 moveRightward -= m->ViewScrollSpeed * DeltaTime; 627 625 628 if (hotkeys[HOTKEY_CAMERA_PAN_KEYBOARD]) 626 if (g_mouse_y >= g_yres - 2 && g_mouse_y < g_yres) 627 moveForward -= m->ViewScrollSpeed * DeltaTime; 628 else if (g_mouse_y <= 3 && g_mouse_y >= 0) 629 moveForward += m->ViewScrollSpeed * DeltaTime; 630 } 631 632 if (hotkeys[HOTKEY_CAMERA_PAN_KEYBOARD]) 633 { 634 if (hotkeys[HOTKEY_CAMERA_RIGHT]) 635 moveRightward += m->ViewScrollSpeed * DeltaTime; 636 if (hotkeys[HOTKEY_CAMERA_LEFT]) 637 moveRightward -= m->ViewScrollSpeed * DeltaTime; 638 if (hotkeys[HOTKEY_CAMERA_UP]) 639 moveForward += m->ViewScrollSpeed * DeltaTime; 640 if (hotkeys[HOTKEY_CAMERA_DOWN]) 641 moveForward -= m->ViewScrollSpeed * DeltaTime; 642 } 643 644 if (moveRightward || moveForward) 645 { 646 float s = sin(m->RotateY.GetSmoothedValue()); 647 float c = cos(m->RotateY.GetSmoothedValue()); 648 m->PosX.AddSmoothly(c * moveRightward); 649 m->PosZ.AddSmoothly(-s * moveRightward); 650 m->PosX.AddSmoothly(s * moveForward); 651 m->PosZ.AddSmoothly(c * moveForward); 652 } 653 } // if(m_cameraFollowingEntity == 0) 654 655 /** 656 * if camera is in following mode (m_cameraFollowingEntity > 0) 657 * follow the unit, do not process camera movement actions 658 * see http://trac.wildfiregames.com/ticket/56 for more description 659 */ 660 else 629 661 { 630 if (hotkeys[HOTKEY_CAMERA_RIGHT]) 631 moveRightward += m->ViewScrollSpeed * DeltaTime; 632 if (hotkeys[HOTKEY_CAMERA_LEFT]) 633 moveRightward -= m->ViewScrollSpeed * DeltaTime; 634 if (hotkeys[HOTKEY_CAMERA_UP]) 635 moveForward += m->ViewScrollSpeed * DeltaTime; 636 if (hotkeys[HOTKEY_CAMERA_DOWN]) 637 moveForward -= m->ViewScrollSpeed * DeltaTime; 638 } 662 debug_assert(m && m->Game && m->Game->GetSimulation2()); 639 663 640 if (moveRightward || moveForward) 641 { 642 float s = sin(m->RotateY.GetSmoothedValue()); 643 float c = cos(m->RotateY.GetSmoothedValue()); 644 m->PosX.AddSmoothly(c * moveRightward); 645 m->PosZ.AddSmoothly(-s * moveRightward); 646 m->PosX.AddSmoothly(s * moveForward); 647 m->PosZ.AddSmoothly(c * moveForward); 664 CmpPtr<ICmpPosition> cmpPosition(*(m->Game->GetSimulation2()), m_cameraFollowingEntity); 665 if (!cmpPosition.null() && cmpPosition->IsInWorld()) 666 { 667 CVector3D pos = cmpPosition->GetInterpolatedTransform(m->Game->GetSimulation2()->GetLastFrameOffset(), false).GetTranslation(); 668 // move the camera after unit 669 // use smoothed values of rotation around X and Y, since we need to hold user's rotation done 670 // in this function above 671 CCamera targetCam = m->ViewCamera; 672 targetCam.m_Orientation.SetIdentity(); 673 targetCam.m_Orientation.RotateX(m->RotateX.GetSmoothedValue()); 674 targetCam.m_Orientation.RotateY(m->RotateY.GetSmoothedValue()); 675 targetCam.m_Orientation.Translate(m->PosX.GetValue(), m->PosY.GetValue(), m->PosZ.GetValue()); 676 677 CVector3D pivot = targetCam.GetFocus(); 678 CVector3D delta = pos - pivot; 679 m->PosX.AddSmoothly(delta.X); 680 m->PosY.AddSmoothly(delta.Y); 681 m->PosZ.AddSmoothly(delta.Z); 682 } 683 else 684 m_cameraFollowingEntity = 0; 648 685 } 649 686 650 687 if (hotkeys[HOTKEY_CAMERA_ZOOM_IN]) … … 758 795 } 759 796 */ 760 797 798 761 799 m->RotateY.Wrap(-(float)M_PI, (float)M_PI); 762 800 763 801 // Update the camera matrix … … 824 862 m->RotateY.SetValueSmoothly(DEGTORAD(m->ViewRotateYDefault)); 825 863 } 826 864 865 827 866 InReaction game_view_handler(const SDL_Event_* ev) 828 867 { 829 868 // put any events that must be processed even if inactive here -
source/graphics/GameView.h
23 23 extern float g_YMinOffset; 24 24 25 25 #include "renderer/Scene.h" 26 #include "simulation2/system/Entity.h" 26 27 27 28 #include "lib/input.h" // InReaction - can't forward-declare enum 28 29 … … 46 47 private: 47 48 static const float cameraPivotMargin; 48 49 CGameViewImpl* m; 50 entity_id_t m_cameraFollowingEntity; 49 51 50 52 // Check whether lighting environment has changed and update vertex data if necessary 51 53 void CheckLightEnv(); … … 85 87 void MoveCameraTarget(const CVector3D& target); 86 88 void ResetCameraTarget(const CVector3D& target); 87 89 void ResetCameraAngleZoom(); 90 void CameraFollow(entity_id_t entityid) { m_cameraFollowingEntity = entityid; } 88 91 89 92 CCamera *GetCamera(); 90 93 CCinemaManager* GetCinema(); -
source/simulation2/Simulation2.cpp
277 277 //////////////////////////////////////////////////////////////// 278 278 279 279 CSimulation2::CSimulation2(CUnitManager* unitManager, CTerrain* terrain) : 280 m(new CSimulation2Impl(unitManager, terrain)) 280 m(new CSimulation2Impl(unitManager, terrain)), 281 m_frameOffset(0.0f) 281 282 { 282 283 } 283 284 … … 367 368 368 369 void CSimulation2::Interpolate(float frameLength, float frameOffset) 369 370 { 371 m_frameOffset = frameOffset; 370 372 m->Interpolate(frameLength, frameOffset); 371 373 } 372 374 -
source/simulation2/Simulation2.h
155 155 std::string GenerateSchema(); 156 156 157 157 private: 158 float m_frameOffset; 158 159 CSimulation2Impl* m; 159 160 160 161 NONCOPYABLE(CSimulation2); 162 163 public: 164 float GetLastFrameOffset() const { return m_frameOffset; } 161 165 }; 162 166 163 167 #endif // INCLUDED_SIMULATION2 -
binaries/data/mods/public/maps/scenarios/Combat_demo.xml
372 372 </Entity> 373 373 </Entities> 374 374 <Paths/> 375 </Scenario> 376 No newline at end of file 375 </Scenario> -
binaries/data/mods/public/gui/session_new/session.xml
62 62 <object hotkey="killUnit"> 63 63 <action on="Press">performCommand(g_Selection.toList()[0], "delete");</action> 64 64 </object> 65 66 <!-- camera.follow mode - follow the first unit in the selection --> 67 <object hotkey="camera.follow"> 68 <action on="Press">performCommand(g_Selection.toList()[0], "camera.follow");</action> 69 </object> 65 70 66 71 <!-- ================================ ================================ --> 67 72 <!-- Developer / Debug items --> -
binaries/data/mods/public/gui/session_new/input.js
24 24 var placementPosition; 25 25 var placementEntity; 26 26 27 // the unit camera is following for 28 var followingEntity = 0; 29 27 30 var mouseX = 0; 28 31 var mouseY = 0; 29 32 var mouseIsOverObject = false; … … 730 733 // Performs the specified command (delete, town bell, repair, etc.) 731 734 function performCommand(entity, commandName) 732 735 { 736 // entity depending commands 733 737 if (entity) 734 738 { 735 739 switch (commandName) … … 741 745 break; 742 746 } 743 747 } 748 749 // common commands 750 if(commandName == "camera.follow") 751 { 752 var l_followingEntity = 0; 753 754 if(entity) 755 { 756 // stop following mode if it is performing for the same unit 757 var entState = GetEntityState(entity); 758 if (entState && isUnit(entState) && (followingEntity != entity)) 759 l_followingEntity = entity; 760 } 761 762 // calling that function with followingEntity = 0 will stop following mode 763 if(l_followingEntity != followingEntity) 764 { 765 followingEntity = l_followingEntity; 766 Engine.CameraFollow(followingEntity); 767 } 768 } 744 769 } -
binaries/data/config/default.cfg
113 113 114 114 ; > CAMERA SETTINGS 115 115 hotkey.camera.reset = "H" ; Reset camera rotation to default. 116 hotkey.camera.follow = "F" ; Follow the first unit in the selection 116 117 hotkey.camera.reset.origin = "Ctrl+H" ; Reset camera to origin. 117 118 hotkey.camera.zoom.in = Plus, Equals, NumPlus ; Zoom camera in. 118 119 hotkey.camera.zoom.out = Minus, NumMinus ; Zoom camera out.
