mirror of
https://github.com/vcmi/vcmi.git
synced 2025-06-02 23:07:36 +02:00
support map scroll
This commit is contained in:
parent
1eea8398b4
commit
836c891c36
@ -28,10 +28,17 @@ void InputSourceGameController::gameControllerDeleter(SDL_GameController * gameC
|
||||
|
||||
InputSourceGameController::InputSourceGameController():
|
||||
lastCheckTime(0),
|
||||
axisValueX(0),
|
||||
axisValueY(0),
|
||||
planDisX(0.0),
|
||||
planDisY(0.0)
|
||||
cursorAxisValueX(0),
|
||||
cursorAxisValueY(0),
|
||||
cursorPlanDisX(0.0),
|
||||
cursorPlanDisY(0.0),
|
||||
scrollAxisMoved(false),
|
||||
scrollStart(Point(0,0)),
|
||||
scrollCurrent(Point(0,0)),
|
||||
scrollAxisValueX(0),
|
||||
scrollAxisValueY(0),
|
||||
scrollPlanDisX(0.0),
|
||||
scrollPlanDisY(0.0)
|
||||
{
|
||||
tryOpenAllGameControllers();
|
||||
}
|
||||
@ -156,25 +163,34 @@ void InputSourceGameController::dispatchTriggerRightClick(int axisValue)
|
||||
|
||||
void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEvent & axis)
|
||||
{
|
||||
tryToConvertCursor();
|
||||
if(axis.axis == SDL_CONTROLLER_AXIS_LEFTX)
|
||||
{
|
||||
if(config.getLeftAxisType() == AxisType::CURSOR_MOTION)
|
||||
axisValueX = getRealAxisValue(axis.value);
|
||||
cursorAxisValueX = getRealAxisValue(axis.value);
|
||||
else if(config.getLeftAxisType() == AxisType::MAP_SCROLL)
|
||||
scrollAxisValueX = getRealAxisValue(axis.value);
|
||||
}
|
||||
else if(axis.axis == SDL_CONTROLLER_AXIS_LEFTY)
|
||||
{
|
||||
if(config.getLeftAxisType() == AxisType::CURSOR_MOTION)
|
||||
axisValueY = getRealAxisValue(axis.value);
|
||||
cursorAxisValueY = getRealAxisValue(axis.value);
|
||||
else if(config.getLeftAxisType() == AxisType::MAP_SCROLL)
|
||||
scrollAxisValueY = getRealAxisValue(axis.value);
|
||||
}
|
||||
if(axis.axis == SDL_CONTROLLER_AXIS_RIGHTX)
|
||||
{
|
||||
if(config.getRightAxisType() == AxisType::CURSOR_MOTION)
|
||||
axisValueX = getRealAxisValue(axis.value);
|
||||
cursorAxisValueX = getRealAxisValue(axis.value);
|
||||
else if(config.getRightAxisType() == AxisType::MAP_SCROLL)
|
||||
scrollAxisValueX = getRealAxisValue(axis.value);
|
||||
}
|
||||
else if(axis.axis == SDL_CONTROLLER_AXIS_RIGHTY)
|
||||
{
|
||||
if(config.getRightAxisType() == AxisType::CURSOR_MOTION)
|
||||
axisValueY = getRealAxisValue(axis.value);
|
||||
cursorAxisValueY = getRealAxisValue(axis.value);
|
||||
else if(config.getRightAxisType() == AxisType::MAP_SCROLL)
|
||||
scrollAxisValueY = getRealAxisValue(axis.value);
|
||||
}
|
||||
else if(config.isLeftClickTrigger(axis.axis))
|
||||
{
|
||||
@ -191,6 +207,17 @@ void InputSourceGameController::handleEventAxisMotion(const SDL_ControllerAxisEv
|
||||
}
|
||||
}
|
||||
|
||||
void InputSourceGameController::tryToConvertCursor()
|
||||
{
|
||||
if(CCS && CCS->curh && CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
|
||||
{
|
||||
const Point & cursorPosition = CCS->curh->getCursorPosition();
|
||||
CCS->curh->ChangeCursor(Cursor::ShowType::SOFTWARE);
|
||||
CCS->curh->cursorMove(cursorPosition.x, cursorPosition.y);
|
||||
GH.input().setCursorPosition(cursorPosition);
|
||||
}
|
||||
}
|
||||
|
||||
void InputSourceGameController::handleEventButtonDown(const SDL_ControllerButtonEvent & button)
|
||||
{
|
||||
const Point & position = GH.input().getCursorPosition();
|
||||
@ -236,19 +263,13 @@ void InputSourceGameController::doCursorMove(int deltaX, int deltaY)
|
||||
if(deltaX == 0 && deltaY == 0)
|
||||
return;
|
||||
const Point & screenSize = GH.screenDimensions();
|
||||
|
||||
// if joystick is connected when intro video plays, using hardware position will move cursor to the right position.
|
||||
bool isHardwareCursor = CCS && CCS->curh && CCS->curh->getShowType() == Cursor::ShowType::HARDWARE;
|
||||
const Point & cursorPosition = isHardwareCursor ? CCS->curh->getCursorPosition() : GH.getCursorPosition();
|
||||
const Point & cursorPosition = GH.getCursorPosition();
|
||||
int newX = std::min(std::max(cursorPosition.x + deltaX, 0), screenSize.x);
|
||||
int newY = std::min(std::max(cursorPosition.y + deltaY, 0), screenSize.y);
|
||||
Point targetPosition{newX, newY};
|
||||
GH.input().setCursorPosition(targetPosition);
|
||||
if(CCS && CCS->curh) {
|
||||
if(CCS->curh->getShowType() == Cursor::ShowType::HARDWARE)
|
||||
CCS->curh->ChangeCursor(Cursor::ShowType::SOFTWARE);
|
||||
if(CCS && CCS->curh)
|
||||
CCS->curh->cursorMove(GH.getCursorPosition().x, GH.getCursorPosition().y);
|
||||
}
|
||||
}
|
||||
|
||||
int InputSourceGameController::getMoveDis(float planDis)
|
||||
@ -263,28 +284,71 @@ void InputSourceGameController::handleUpdate()
|
||||
{
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
|
||||
if(lastCheckTime == 0)
|
||||
{
|
||||
if (lastCheckTime == 0) {
|
||||
lastCheckTime = nowMs;
|
||||
return;
|
||||
}
|
||||
|
||||
long long deltaTime = nowMs - lastCheckTime;
|
||||
|
||||
if(axisValueX == 0)
|
||||
planDisX = 0;
|
||||
else
|
||||
planDisX += ((float)deltaTime / 1000) * ((float)axisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
|
||||
|
||||
if(axisValueY == 0)
|
||||
planDisY = 0;
|
||||
else
|
||||
planDisY += ((float)deltaTime / 1000) * ((float)axisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
|
||||
|
||||
int moveDisX = getMoveDis(planDisX);
|
||||
int moveDisY = getMoveDis(planDisY);
|
||||
planDisX -= moveDisX;
|
||||
planDisY -= moveDisY;
|
||||
doCursorMove(moveDisX, moveDisY);
|
||||
handleCursorUpdate(deltaTime);
|
||||
handleScrollUpdate(deltaTime);
|
||||
lastCheckTime = nowMs;
|
||||
}
|
||||
}
|
||||
|
||||
void InputSourceGameController::handleCursorUpdate(long long deltaTime)
|
||||
{
|
||||
if(cursorAxisValueX == 0)
|
||||
cursorPlanDisX = 0;
|
||||
else
|
||||
cursorPlanDisX += ((float)deltaTime / 1000) * ((float)cursorAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
|
||||
|
||||
if(cursorAxisValueY == 0)
|
||||
cursorPlanDisY = 0;
|
||||
else
|
||||
cursorPlanDisY += ((float)deltaTime / 1000) * ((float)cursorAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
|
||||
|
||||
int moveDisX = getMoveDis(cursorPlanDisX);
|
||||
int moveDisY = getMoveDis(cursorPlanDisY);
|
||||
cursorPlanDisX -= moveDisX;
|
||||
cursorPlanDisY -= moveDisY;
|
||||
doCursorMove(moveDisX, moveDisY);
|
||||
}
|
||||
|
||||
void InputSourceGameController::handleScrollUpdate(long long deltaTime)
|
||||
{
|
||||
if(!scrollAxisMoved && isScrollAxisReleased())
|
||||
{
|
||||
return;
|
||||
}
|
||||
else if(!scrollAxisMoved && !isScrollAxisReleased())
|
||||
{
|
||||
scrollAxisMoved = true;
|
||||
scrollCurrent = scrollStart = GH.input().getCursorPosition();
|
||||
GH.events().dispatchGesturePanningStarted(scrollStart);
|
||||
}
|
||||
else if(scrollAxisMoved && isScrollAxisReleased())
|
||||
{
|
||||
GH.events().dispatchGesturePanningEnded(scrollStart, scrollCurrent);
|
||||
scrollAxisMoved = false;
|
||||
scrollPlanDisX = scrollPlanDisY = 0;
|
||||
return;
|
||||
}
|
||||
scrollPlanDisX += ((float)deltaTime / 1000) * ((float)scrollAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
|
||||
scrollPlanDisY += ((float)deltaTime / 1000) * ((float)scrollAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED;
|
||||
int moveDisX = getMoveDis(scrollPlanDisX);
|
||||
int moveDisY = getMoveDis(scrollPlanDisY);
|
||||
if(moveDisX != 0 || moveDisY != 0)
|
||||
{
|
||||
scrollPlanDisX -= moveDisX;
|
||||
scrollPlanDisY -= moveDisY;
|
||||
scrollCurrent.x += moveDisX;
|
||||
scrollCurrent.y += moveDisY;
|
||||
Point distance(moveDisX, moveDisY);
|
||||
GH.events().dispatchGesturePanning(scrollStart, scrollCurrent, distance);
|
||||
}
|
||||
}
|
||||
|
||||
bool InputSourceGameController::isScrollAxisReleased()
|
||||
{
|
||||
return scrollAxisValueX == 0 && scrollAxisValueY == 0;
|
||||
}
|
||||
|
@ -14,6 +14,7 @@
|
||||
|
||||
#include "GameControllerConfig.h"
|
||||
#include "../gui/Shortcut.h"
|
||||
#include "../../lib/Point.h"
|
||||
|
||||
|
||||
const int AXIS_DEAD_ZOOM = 6000;
|
||||
@ -30,12 +31,20 @@ class InputSourceGameController
|
||||
using GameControllerPtr = std::unique_ptr<SDL_GameController, decltype(&gameControllerDeleter)>;
|
||||
|
||||
std::map<int, GameControllerPtr> gameControllerMap;
|
||||
long long lastCheckTime;
|
||||
int axisValueX;
|
||||
int axisValueY;
|
||||
float planDisX;
|
||||
float planDisY;
|
||||
GameControllerConfig config;
|
||||
long long lastCheckTime;
|
||||
int cursorAxisValueX;
|
||||
int cursorAxisValueY;
|
||||
float cursorPlanDisX;
|
||||
float cursorPlanDisY;
|
||||
|
||||
bool scrollAxisMoved;
|
||||
Point scrollStart;
|
||||
Point scrollCurrent;
|
||||
int scrollAxisValueX;
|
||||
int scrollAxisValueY;
|
||||
float scrollPlanDisX;
|
||||
float scrollPlanDisY;
|
||||
|
||||
void openGameController(int index);
|
||||
int getJoystickIndex(SDL_GameController * controller);
|
||||
@ -43,8 +52,12 @@ class InputSourceGameController
|
||||
void dispatchTriggerShortcuts(const std::vector<EShortcut> & shortcutsVector, int axisValue);
|
||||
void dispatchTriggerLeftClick(int axisValue);
|
||||
void dispatchTriggerRightClick(int axisValue);
|
||||
void tryToConvertCursor();
|
||||
void doCursorMove(int deltaX, int deltaY);
|
||||
int getMoveDis(float planDis);
|
||||
void handleCursorUpdate(long long deltaTime);
|
||||
void handleScrollUpdate(long long deltaTime);
|
||||
bool isScrollAxisReleased();
|
||||
|
||||
public:
|
||||
InputSourceGameController();
|
||||
|
Loading…
x
Reference in New Issue
Block a user