Skip to content

Commit

Permalink
fix #5311
Browse files Browse the repository at this point in the history
Spring.GetMouseState now returns a sixth value indicating if the cursor is offscreen
  • Loading branch information
rrti committed Jun 11, 2017
1 parent d3d338b commit 04f4604
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 93 deletions.
107 changes: 60 additions & 47 deletions rts/Game/UI/MouseHandler.cpp
Expand Up @@ -68,7 +68,7 @@ CONFIG(float, MouseDragScrollThreshold).defaultValue(0.3f);
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CMouseHandler* mouse = NULL;
CMouseHandler* mouse = nullptr;

static CInputReceiver*& activeReceiver = CInputReceiver::GetActiveReceiverRef();

Expand All @@ -77,11 +77,12 @@ CMouseHandler::CMouseHandler()
: lastx(-1)
, lasty(-1)
, locked(false)
, wasLocked(false)
, offscreen(false)
, doubleClickTime(0.0f)
, scrollWheelSpeed(0.0f)
, activeButton(-1)
, dir(ZeroVector)
, wasLocked(false)
, crossSize(0.0f)
, crossAlpha(0.0f)
, crossMoveScale(0.0f)
Expand Down Expand Up @@ -232,15 +233,17 @@ void CMouseHandler::ReloadCursors()

void CMouseHandler::MouseMove(int x, int y, int dx, int dy)
{
//FIXME don't update with lock?
lastx = x;
lasty = y;
// FIXME: don't update if locked?
lastx = std::abs(x);
lasty = std::abs(y);

// MouseInput passes negative coordinates when cursor leaves the window
offscreen = (x < 0 && y < 0);

const int screenCenterX = globalRendering->viewSizeX / 2 + globalRendering->viewPosX;
const int screenCenterY = globalRendering->viewSizeY / 2 + globalRendering->viewPosY;
const int2 screenCenter = globalRendering->GetScreenCenter();

scrollx += lastx - screenCenterX;
scrolly += lasty - screenCenterY;
scrollx += (lastx - screenCenter.x);
scrolly += (lasty - screenCenter.y);

dir = hide ? camera->GetDir() : camera->CalcPixelDir(x, y);

Expand All @@ -249,7 +252,7 @@ void CMouseHandler::MouseMove(int x, int y, int dx, int dy)
return;
}

const int movedPixels = (int)math::sqrt(float(dx*dx + dy*dy));
const int movedPixels = (int)fastmath::sqrt_sse(float(dx*dx + dy*dy));
buttons[SDL_BUTTON_LEFT].movement += movedPixels;
buttons[SDL_BUTTON_RIGHT].movement += movedPixels;

Expand Down Expand Up @@ -586,10 +589,8 @@ std::string CMouseHandler::GetCurrentTooltip()
if (selTip != "")
return selTip;

if (dist <= range) {
const float3 pos = camera->GetPos() + (dir * dist);
return CTooltipConsole::MakeGroundString(pos);
}
if (dist <= range)
return CTooltipConsole::MakeGroundString(camera->GetPos() + (dir * dist));

return "";
}
Expand All @@ -602,58 +603,70 @@ void CMouseHandler::Update()
if (!hide)
return;

const int2 screenCenter = globalRendering->GetScreenCenter();

// Update MiddleClickScrolling
scrollx *= 0.5f;
scrolly *= 0.5f;
lastx = globalRendering->viewSizeX / 2 + globalRendering->viewPosX;
lasty = globalRendering->viewSizeY / 2 + globalRendering->viewPosY;
if (globalRendering->active) {
mouseInput->SetPos(int2(lastx, lasty));
}
lastx = screenCenter.x;
lasty = screenCenter.y;

if (!globalRendering->active)
return;

mouseInput->SetPos(screenCenter);
}


void CMouseHandler::WarpMouse(int x, int y)
{
if (!locked) {
lastx = x + globalRendering->viewPosX;
lasty = y + globalRendering->viewPosY;
mouseInput->SetPos(int2(lastx, lasty));
}
if (locked)
return;

lastx = x + globalRendering->viewPosX;
lasty = y + globalRendering->viewPosY;

mouseInput->SetPos(int2(lastx, lasty));
}


void CMouseHandler::ShowMouse()
{
if (hide) {
hide = false;
cursorText = "none"; // force hardware cursor rebinding (else we have standard b&w cursor)

// I don't use SDL_ShowCursor here 'cos it would cause a flicker with hwCursor
// (flicker caused by switching between default cursor and later the really one e.g. `attack`)
// instead update state and cursor at the same time
if (hardwareCursor) {
hwHide = true;
} else {
SDL_ShowCursor(SDL_DISABLE);
}
if (!hide)
return;

hide = false;
cursorText = "none"; // force hardware cursor rebinding (else we have standard b&w cursor)

// don't use SDL_ShowCursor here, it would cause a flicker with hwCursor
// (by switching between default cursor and later the real one, e.g. `attack`)
// instead update state and cursor at the same time
if (hardwareCursor) {
hwHide = true;
} else {
SDL_ShowCursor(SDL_DISABLE);
}
}


void CMouseHandler::HideMouse()
{
if (!hide) {
hwHide = true;
SDL_ShowCursor(SDL_DISABLE);
mouseInput->SetWMMouseCursor(NULL);
scrollx = 0.f;
scrolly = 0.f;
lastx = globalRendering->viewSizeX / 2 + globalRendering->viewPosX;
lasty = globalRendering->viewSizeY / 2 + globalRendering->viewPosY;
mouseInput->SetPos(int2(lastx, lasty));
hide = true;
}
if (hide)
return;

hwHide = true;
SDL_ShowCursor(SDL_DISABLE);
mouseInput->SetWMMouseCursor(nullptr);

const int2 screenCenter = globalRendering->GetScreenCenter();

scrollx = 0.0f;
scrolly = 0.0f;
lastx = screenCenter.x;
lasty = screenCenter.y;

mouseInput->SetPos(screenCenter);
hide = true;
}


Expand Down
8 changes: 4 additions & 4 deletions rts/Game/UI/MouseHandler.h
Expand Up @@ -75,6 +75,10 @@ class CMouseHandler
int lasty;

bool locked;
/// Stores if the mouse was locked or not before going into direct control,
/// so we can restore it when we return to normal.
bool wasLocked;
bool offscreen;

float doubleClickTime;
float scrollWheelSpeed;
Expand All @@ -95,10 +99,6 @@ class CMouseHandler
int activeButton;
float3 dir;

/// Stores if the mouse was locked or not before going into direct control,
/// so we can restore it when we return to normal.
bool wasLocked;

/// locked mouse indicator size
float crossSize;
float crossAlpha;
Expand Down
32 changes: 17 additions & 15 deletions rts/Lua/LuaUnsyncedRead.cpp
Expand Up @@ -1845,29 +1845,29 @@ int LuaUnsyncedRead::GetCmdDescIndex(lua_State* L)

int LuaUnsyncedRead::GetBuildFacing(lua_State* L)
{
if (guihandler == NULL) {
if (guihandler == nullptr)
return 0;
}

lua_pushnumber(L, guihandler->buildFacing);
return 1;
}


int LuaUnsyncedRead::GetBuildSpacing(lua_State* L)
{
if (guihandler == NULL) {
if (guihandler == nullptr)
return 0;
}

lua_pushnumber(L, guihandler->buildSpacing);
return 1;
}


int LuaUnsyncedRead::GetGatherMode(lua_State* L)
{
if (guihandler == NULL) {
if (guihandler == nullptr)
return 0;
}

lua_pushnumber(L, guihandler->GetGatherMode());
return 1;
}
Expand All @@ -1877,9 +1877,9 @@ int LuaUnsyncedRead::GetGatherMode(lua_State* L)

int LuaUnsyncedRead::GetActivePage(lua_State* L)
{
if (guihandler == NULL) {
if (guihandler == nullptr)
return 0;
}

lua_pushnumber(L, guihandler->GetActivePage());
lua_pushnumber(L, guihandler->GetMaxPage());
return 2;
Expand All @@ -1892,10 +1892,12 @@ int LuaUnsyncedRead::GetMouseState(lua_State* L)
{
lua_pushnumber(L, mouse->lastx - globalRendering->viewPosX);
lua_pushnumber(L, globalRendering->viewSizeY - mouse->lasty - 1);

lua_pushboolean(L, mouse->buttons[SDL_BUTTON_LEFT].pressed);
lua_pushboolean(L, mouse->buttons[SDL_BUTTON_MIDDLE].pressed);
lua_pushboolean(L, mouse->buttons[SDL_BUTTON_RIGHT].pressed);
return 5;
lua_pushboolean(L, mouse->offscreen);
return 6;
}


Expand All @@ -1909,13 +1911,14 @@ int LuaUnsyncedRead::GetMouseCursor(lua_State* L)

int LuaUnsyncedRead::GetMouseStartPosition(lua_State* L)
{
if (mouse == NULL) {
if (mouse == nullptr)
return 0;
}

const int button = luaL_checkint(L, 1);
if ((button <= 0) || (button > NUM_BUTTONS)) {

if ((button <= 0) || (button > NUM_BUTTONS))
return 0;
}

const CMouseHandler::ButtonPressEvt& bp = mouse->buttons[button];
lua_pushnumber(L, bp.x);
lua_pushnumber(L, bp.y);
Expand All @@ -1933,9 +1936,8 @@ int LuaUnsyncedRead::GetMouseStartPosition(lua_State* L)
int LuaUnsyncedRead::GetClipboard(lua_State* L)
{
char* text = SDL_GetClipboardText();
if (text == NULL) {
if (text == nullptr)
return 0;
}
lua_pushstring(L, text);
SDL_free(text);
return 1;
Expand Down
1 change: 1 addition & 0 deletions rts/Rendering/GlobalRendering.h
Expand Up @@ -53,6 +53,7 @@ class CGlobalRendering {
void SaveWindowPosAndSize();
void UpdateGLConfigs();

int2 GetScreenCenter() const { return {viewPosX + (viewSizeX >> 1), viewPosY + (viewSizeY >> 1)}; }
int2 GetWantedViewSize(const bool fullscreen);

bool CheckGLMultiSampling() const;
Expand Down

0 comments on commit 04f4604

Please sign in to comment.