1616 * You should have received a copy of the GNU General Public License
1717 * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
1818 */
19+ #include < cmath>
20+ #include " ../fmath.h"
1921#include " MiniMapView.h"
2022#include " ../Savegame/Tile.h"
2123#include " Map.h"
2830#include " ../Resource/ResourcePack.h"
2931#include " ../Ruleset/Armor.h"
3032#include " ../Engine/Options.h"
33+ #include " ../Engine/Screen.h"
3134#include < sstream>
3235
3336namespace OpenXcom
@@ -188,6 +191,13 @@ void MiniMapView::mousePress(Action *action, State *state)
188191 _isMouseScrolled = false ;
189192 SDL_GetMouseState (&_xBeforeMouseScrolling, &_yBeforeMouseScrolling);
190193 _posBeforeMouseScrolling = _camera->getCenterPosition ();
194+ if (!Options::battleDragScrollInvert && _cursorPosition.z == 0 );
195+ {
196+ _cursorPosition.x = action->getDetails ()->motion .x ;
197+ _cursorPosition.y = action->getDetails ()->motion .y ;
198+ // the Z is irrelevant to our mouse position, but we can use it as a boolean to check if the position is set or not
199+ _cursorPosition.z = 1 ;
200+ }
191201 _mouseScrollX = 0 ; _mouseScrollY = 0 ;
192202 _totalMouseMoveX = 0 ; _totalMouseMoveY = 0 ;
193203 _mouseMovedOverThreshold = false ;
@@ -215,18 +225,28 @@ void MiniMapView::mouseClick (Action *action, State *state)
215225 if ((!_mouseMovedOverThreshold) && (SDL_GetTicks () - _mouseScrollingStartTime <= (Options::dragScrollTimeTolerance)))
216226 { _camera->centerOnPosition (_posBeforeMouseScrolling); _redraw = true ; }
217227 _isMouseScrolled = _isMouseScrolling = false ;
228+ stopScrolling (action);
218229 }
219230 }
220231
221232 // Drag-Scroll release: release mouse-scroll-mode
222233 if (_isMouseScrolling)
223234 {
224235 // While scrolling, other buttons are ineffective
225- if (action->getDetails ()->button .button == Options::battleDragScrollButton) _isMouseScrolling = false ; else return ;
236+ if (action->getDetails ()->button .button == Options::battleDragScrollButton)
237+ {
238+ _isMouseScrolling = false ;
239+ stopScrolling (action);
240+ }
241+ else
242+ {
243+ return ;
244+ }
226245 // Check if we have to revoke the scrolling, because it was too short in time, so it was a click
227246 if ((!_mouseMovedOverThreshold) && (SDL_GetTicks () - _mouseScrollingStartTime <= (Options::dragScrollTimeTolerance)))
228247 {
229248 _isMouseScrolled = false ;
249+ stopScrolling (action);
230250 _camera->centerOnPosition (_posBeforeMouseScrolling);
231251 _redraw = true ;
232252 }
@@ -267,8 +287,12 @@ void MiniMapView::mouseOver(Action *action, State *state)
267287 if (0 ==(SDL_GetMouseState (0 ,0 )&SDL_BUTTON (Options::battleDragScrollButton))) { // so we missed again the mouse-release :(
268288 // Check if we have to revoke the scrolling, because it was too short in time, so it was a click
269289 if ((!_mouseMovedOverThreshold) && (SDL_GetTicks () - _mouseScrollingStartTime <= (Options::dragScrollTimeTolerance)))
270- { _camera->centerOnPosition (_posBeforeMouseScrolling); _redraw = true ; }
290+ {
291+ _camera->centerOnPosition (_posBeforeMouseScrolling);
292+ _redraw = true ;
293+ }
271294 _isMouseScrolled = _isMouseScrolling = false ;
295+ stopScrolling (action);
272296 return ;
273297 }
274298
@@ -291,13 +315,13 @@ void MiniMapView::mouseOver(Action *action, State *state)
291315
292316 if (Options::battleDragScrollInvert)
293317 {
294- scrollX = action->getDetails ()->motion .xrel ;
295- scrollY = action->getDetails ()->motion .yrel ;
318+ scrollX = action->getDetails ()->motion .xrel / action-> getXScale () ;
319+ scrollY = action->getDetails ()->motion .yrel / action-> getYScale () ;
296320 }
297321 else
298322 {
299- scrollX = -action->getDetails ()->motion .xrel ;
300- scrollY = -action->getDetails ()->motion .yrel ;
323+ scrollX = -action->getDetails ()->motion .xrel / action-> getXScale () ;
324+ scrollY = -action->getDetails ()->motion .yrel / action-> getYScale () ;
301325 }
302326 _mouseScrollX += scrollX;
303327 _mouseScrollY += scrollY;
@@ -321,7 +345,21 @@ void MiniMapView::mouseOver(Action *action, State *state)
321345 _redraw = true ;
322346
323347 // We don't want to look the mouse-cursor jumping :)
324- action->getDetails ()->motion .x =_xBeforeMouseScrolling; action->getDetails ()->motion .y =_yBeforeMouseScrolling;
348+ if (Options::battleDragScrollInvert)
349+ {
350+ action->getDetails ()->motion .x = _xBeforeMouseScrolling;
351+ action->getDetails ()->motion .y = _yBeforeMouseScrolling;
352+ }
353+ else
354+ {
355+ Position delta (-scrollX, -scrollY, 0 );
356+ _cursorPosition.x = Round (std::min (getX () + getWidth (), std::max (getX (), (int )(Round (_cursorPosition.x / action->getXScale ()) + delta.x ))) * action->getXScale ());
357+ // std::min(Round(getWidth() * action->getXScale() + getX() * action->getXScale()), std::max(Round(getX() * action->getXScale()), _cursorPosition.x + Round(delta.x * action->getXScale())));
358+ _cursorPosition.y = Round (std::min (getY () + getHeight (), std::max (getY (), (int )(Round (_cursorPosition.y / action->getYScale ()) + delta.y ))) * action->getYScale ());
359+ // std::min(Round(getHeight() * action->getYScale() + getY() * action->getYScale()), std::max(Round(getY() * action->getYScale()), _cursorPosition.y + Round(delta.y * action->getYScale())));
360+ action->getDetails ()->motion .x = _cursorPosition.x ;
361+ action->getDetails ()->motion .y = _cursorPosition.y ;
362+ }
325363 _game->getCursor ()->handle (action);
326364 }
327365}
@@ -353,4 +391,14 @@ void MiniMapView::animate()
353391 _redraw = true ;
354392}
355393
394+ void MiniMapView::stopScrolling (Action *action)
395+ {
396+ if (!Options::battleDragScrollInvert)
397+ {
398+ SDL_WarpMouse (_cursorPosition.x , _cursorPosition.y );
399+ action->setMouseAction (_cursorPosition.x /action->getXScale (), _cursorPosition.y /action->getYScale (), _game->getScreen ()->getSurface ()->getX (), _game->getScreen ()->getSurface ()->getY ());
400+ }
401+ // reset our "mouse position stored" flag
402+ _cursorPosition.z = 0 ;
403+ }
356404}
0 commit comments