From a769783d5d28ebf3fa3ac372fa11f11bb06cd2b0 Mon Sep 17 00:00:00 2001 From: prefect Date: Wed, 28 Sep 2005 23:57:55 +0000 Subject: [PATCH] Fix scroll wheel zooming on Linux. Refactored HandleEvent into CGameView. This was SVN commit r2800. --- source/graphics/GameView.cpp | 51 ++++++++++++++++++++++-------------- source/graphics/GameView.h | 5 ++++ source/main.cpp | 2 +- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/source/graphics/GameView.cpp b/source/graphics/GameView.cpp index 51df2e677c..91048c09b5 100755 --- a/source/graphics/GameView.cpp +++ b/source/graphics/GameView.cpp @@ -57,7 +57,8 @@ CGameView::CGameView(CGame *pGame): m_ViewZoomSmoothness(0.02f), m_ViewSnapSmoothness(0.02f), m_CameraPivot(), - m_CameraDelta() + m_CameraDelta(), + m_ZoomDelta(0) { SViewPort vp; vp.m_X=0; @@ -527,24 +528,18 @@ void CGameView::Update(float DeltaTime) } // Smoothed zooming (move a certain percentage towards the desired zoom distance every frame) - - static float zoom_delta = 0.0f; - - if( hotkeys[HOTKEY_CAMERA_ZOOM_WHEEL_IN] ) - zoom_delta += m_ViewZoomSensitivityWheel; - else if( hotkeys[HOTKEY_CAMERA_ZOOM_WHEEL_OUT] ) - zoom_delta -= m_ViewZoomSensitivityWheel; + // Note that scroll wheel zooming is event-based and handled in game_view_handler if( hotkeys[HOTKEY_CAMERA_ZOOM_IN] ) - zoom_delta += m_ViewZoomSensitivity*DeltaTime; + m_ZoomDelta += m_ViewZoomSensitivity*DeltaTime; else if( hotkeys[HOTKEY_CAMERA_ZOOM_OUT] ) - zoom_delta -= m_ViewZoomSensitivity*DeltaTime; + m_ZoomDelta -= m_ViewZoomSensitivity*DeltaTime; - if (fabsf(zoom_delta) > 0.1f) // use a fairly high limit to avoid nasty flickering when zooming + if (fabsf(m_ZoomDelta) > 0.1f) // use a fairly high limit to avoid nasty flickering when zooming { float zoom_proportion = powf(m_ViewZoomSmoothness, DeltaTime); - CameraLock(forwards * (zoom_delta * (1.0f-zoom_proportion)), false); - zoom_delta *= zoom_proportion; + CameraLock(forwards * (m_ZoomDelta * (1.0f-zoom_proportion)), false); + m_ZoomDelta *= zoom_proportion; } @@ -700,6 +695,11 @@ int game_view_handler(const SDL_Event* ev) CGameView *pView=g_Game->GetView(); + return pView->HandleEvent(ev); +} + +int CGameView::HandleEvent(const SDL_Event* ev) +{ switch(ev->type) { @@ -715,15 +715,26 @@ int game_view_handler(const SDL_Event* ev) return( EV_HANDLED ); case HOTKEY_CAMERA_RESET_ORIGIN: - pView->ResetCamera(); + ResetCamera(); return( EV_HANDLED ); case HOTKEY_CAMERA_RESET: - pView->ResetCameraOrientation(); + ResetCameraOrientation(); return( EV_HANDLED ); case HOTKEY_CAMERA_ROTATE_ABOUT_TARGET: - pView->RotateAboutTarget(); + RotateAboutTarget(); + return( EV_HANDLED ); + + // Mouse wheel must be treated using events instead of polling, + // because SDL auto-generates a sequence of mousedown/mouseup events automatically + // on Linux, and we never get to see the "down" state inside Update(). + case HOTKEY_CAMERA_ZOOM_WHEEL_IN: + m_ZoomDelta += m_ViewZoomSensitivityWheel; + return( EV_HANDLED ); + + case HOTKEY_CAMERA_ZOOM_WHEEL_OUT: + m_ZoomDelta -= m_ViewZoomSensitivityWheel; return( EV_HANDLED ); default: @@ -736,21 +747,21 @@ int game_view_handler(const SDL_Event* ev) if( hotkeys[HOTKEY_CAMERA_BOOKMARK_SAVE] ) { // Attempt to track the ground we're looking at - cameraBookmarks[id] = pView->GetCamera()->GetFocus(); + cameraBookmarks[id] = GetCamera()->GetFocus(); bookmarkInUse[id] = true; } else if( hotkeys[HOTKEY_CAMERA_BOOKMARK_SNAP] ) { if( bookmarkInUse[id] && ( currentBookmark == -1 ) ) { - pView->PushCameraTarget( cameraBookmarks[id] ); + PushCameraTarget( cameraBookmarks[id] ); currentBookmark = id; } } else { if( bookmarkInUse[id] ) - pView->SetCameraTarget( cameraBookmarks[id] ); + SetCameraTarget( cameraBookmarks[id] ); } return( EV_HANDLED ); } @@ -760,7 +771,7 @@ int game_view_handler(const SDL_Event* ev) { case HOTKEY_CAMERA_BOOKMARK_SNAP: if( currentBookmark != -1 ) - pView->PopCameraTarget(); + PopCameraTarget(); currentBookmark = -1; break; default: diff --git a/source/graphics/GameView.h b/source/graphics/GameView.h index d6c69e1505..66ae815854 100755 --- a/source/graphics/GameView.h +++ b/source/graphics/GameView.h @@ -48,6 +48,9 @@ class CGameView: public CJSObject //float m_CameraZoom; std::vector m_CameraTargets; + // Accumulate zooming changes across frames for smoothness + float m_ZoomDelta; + // RenderTerrain: iterate through all terrain patches and submit all patches // in viewing frustum to the renderer, for terrain painting void RenderTerrain(CTerrain *pTerrain); @@ -92,6 +95,8 @@ public: // Render: Render the World void Render(); + int HandleEvent(const SDL_Event* ev); + //Keep the camera in between boundaries/smooth camera scrolling/translating //Should call this whenever moving (translating) the camera void CameraLock(CVector3D Trans, bool smooth=true); diff --git a/source/main.cpp b/source/main.cpp index 6df587cd4b..24bb7dfdc6 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -53,7 +53,7 @@ void kill_mainloop(); // bool keys[SDLK_LAST]; -bool mouse_buttons[5]; // CAN REMOVE AFTER MOVING RESET TO WSDL +bool mouse_buttons[6]; // CAN REMOVE AFTER MOVING RESET TO WSDL bool g_active = true; int g_mouse_x = 50, g_mouse_y = 50;