Skip to content

Commit 9c8692c

Browse files
committed
drag scroll changes
make non-inverted drag scrolling grab the map make inverted drag scroll grab the camera and hide the selection cursor make it all scale according to display settings apply the same behaviour to the minimap
1 parent 467fd76 commit 9c8692c

4 files changed

Lines changed: 96 additions & 18 deletions

File tree

‎src/Battlescape/BattlescapeState.cpp‎

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
#include "../Engine/CrossPlatform.h"
8484
#include "../Menu/LoadGameState.h"
8585
#include "../Menu/SaveGameState.h"
86+
#include "../fmath.h"
8687

8788
namespace OpenXcom
8889
{
@@ -619,19 +620,26 @@ void BattlescapeState::mapOver(Action *action)
619620
if (Options::battleDragScrollInvert)
620621
{
621622
_map->getCamera()->scrollXY(
622-
-action->getDetails()->motion.xrel,
623-
-action->getDetails()->motion.yrel, false);
623+
-action->getDetails()->motion.xrel / action->getXScale(),
624+
-action->getDetails()->motion.yrel / action->getYScale(), false);
625+
action->getDetails()->motion.x = _xBeforeMouseScrolling;
626+
action->getDetails()->motion.y = _yBeforeMouseScrolling;
627+
_map->setCursorType(CT_NONE);
624628
}
625629
else
626630
{
631+
Position delta = _map->getCamera()->getMapOffset();
627632
_map->getCamera()->scrollXY(
628-
action->getDetails()->motion.xrel,
629-
action->getDetails()->motion.yrel, false);
633+
action->getDetails()->motion.xrel / action->getXScale(),
634+
action->getDetails()->motion.yrel / action->getYScale(), false);
635+
delta = _map->getCamera()->getMapOffset() - delta;
636+
_cursorPosition.x = std::min(_game->getScreen()->getWidth() - (int)(Round(action->getXScale())), std::max(0, (int)(_cursorPosition.x + Round(delta.x * action->getXScale()))));
637+
_cursorPosition.y = std::min(_game->getScreen()->getHeight() - (int)(Round(action->getYScale())), std::max(0, (int)(_cursorPosition.y + Round(delta.y * action->getYScale()))));
638+
action->getDetails()->motion.x = _cursorPosition.x;
639+
action->getDetails()->motion.y = _cursorPosition.y;
630640
}
631641

632642
// We don't want to look the mouse-cursor jumping :)
633-
action->getDetails()->motion.x = _xBeforeMouseScrolling;
634-
action->getDetails()->motion.y = _yBeforeMouseScrolling;
635643
_game->getCursor()->handle(action);
636644
}
637645
}
@@ -651,6 +659,13 @@ void BattlescapeState::mapPress(Action *action)
651659
_isMouseScrolled = false;
652660
SDL_GetMouseState(&_xBeforeMouseScrolling, &_yBeforeMouseScrolling);
653661
_mapOffsetBeforeMouseScrolling = _map->getCamera()->getMapOffset();
662+
if (!Options::battleDragScrollInvert && _cursorPosition.z == 0);
663+
{
664+
_cursorPosition.x = action->getDetails()->motion.x;
665+
_cursorPosition.y = action->getDetails()->motion.y;
666+
// 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
667+
_cursorPosition.z = 1;
668+
}
654669
_totalMouseMoveX = 0; _totalMouseMoveY = 0;
655670
_mouseMovedOverThreshold = false;
656671
_mouseScrollingStartTime = SDL_GetTicks();
@@ -1430,9 +1445,10 @@ inline void BattlescapeState::handle(Action *action)
14301445
if (_game->getCursor()->getVisible() || action->getDetails()->button.button == SDL_BUTTON_RIGHT)
14311446
{
14321447
State::handle(action);
1433-
if (_isMouseScrolling)
1448+
1449+
if (_isMouseScrolling && !Options::battleDragScrollInvert)
14341450
{
1435-
_map->setSelectorPosition(_xBeforeMouseScrolling / action->getXScale(), _yBeforeMouseScrolling / action->getYScale());
1451+
_map->setSelectorPosition(_cursorPosition.x / action->getXScale(), _cursorPosition.y / action->getYScale());
14361452
}
14371453

14381454
if (action->getDetails()->type == SDL_MOUSEBUTTONDOWN)
@@ -2121,8 +2137,20 @@ void BattlescapeState::resize(int &dX, int &dY)
21212137
*/
21222138
void BattlescapeState::stopScrolling(Action *action)
21232139
{
2124-
SDL_WarpMouse(_xBeforeMouseScrolling, _yBeforeMouseScrolling);
2125-
action->setMouseAction(_xBeforeMouseScrolling, _yBeforeMouseScrolling, _map->getX(), _map->getY());
2140+
if (Options::battleDragScrollInvert)
2141+
{
2142+
SDL_WarpMouse(_xBeforeMouseScrolling, _yBeforeMouseScrolling);
2143+
action->setMouseAction(_xBeforeMouseScrolling, _yBeforeMouseScrolling, _map->getX(), _map->getY());
2144+
_battleGame->setupCursor();
2145+
}
2146+
else
2147+
{
2148+
SDL_WarpMouse(_cursorPosition.x, _cursorPosition.y);
2149+
action->setMouseAction(_cursorPosition.x/action->getXScale(), _cursorPosition.y/action->getYScale(), _game->getScreen()->getSurface()->getX(), _game->getScreen()->getSurface()->getY());
2150+
_map->setSelectorPosition(_cursorPosition.x / action->getXScale(), _cursorPosition.y / action->getYScale());
2151+
}
2152+
// reset our "mouse position stored" flag
2153+
_cursorPosition.z = 0;
21262154
}
21272155

21282156
}

‎src/Battlescape/BattlescapeState.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class BattlescapeState : public State
7979
bool _mouseMovedOverThreshold;
8080
bool _mouseOverIcons;
8181
std::string _currentTooltip;
82+
Position _cursorPosition;
8283
/// Popups a context sensitive list of actions the user can choose from.
8384
void handleItemClick(BattleItem *item);
8485
/// Shifts the red colors of the visible unit buttons backgrounds.

‎src/Battlescape/MiniMapView.cpp‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
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"
@@ -28,6 +30,7 @@
2830
#include "../Resource/ResourcePack.h"
2931
#include "../Ruleset/Armor.h"
3032
#include "../Engine/Options.h"
33+
#include "../Engine/Screen.h"
3134
#include <sstream>
3235

3336
namespace 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
}

‎src/Battlescape/MiniMapView.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class MiniMapView : public InteractiveSurface
4545
bool _isMouseScrolled;
4646
int _xBeforeMouseScrolling, _yBeforeMouseScrolling;
4747
int _mouseScrollX, _mouseScrollY;
48-
Position _posBeforeMouseScrolling;
48+
Position _posBeforeMouseScrolling, _cursorPosition;
4949
Uint32 _mouseScrollingStartTime;
5050
int _totalMouseMoveX, _totalMouseMoveY;
5151
bool _mouseMovedOverThreshold;
@@ -68,6 +68,7 @@ class MiniMapView : public InteractiveSurface
6868
int down ();
6969
/// Updates the minimap animation.
7070
void animate();
71+
void stopScrolling(Action *action);
7172
};
7273

7374
}

0 commit comments

Comments
 (0)