Skip to content

Commit

Permalink
fix #6036
Browse files Browse the repository at this point in the history
add mmbScroll return-arg to Spring.GetMouseState

Conflicts:
	rts/Game/UI/MouseHandler.h
  • Loading branch information
rt committed Sep 1, 2018
1 parent ee4abbc commit 4d331bf
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 52 deletions.
84 changes: 42 additions & 42 deletions rts/Game/UI/MouseHandler.cpp
Expand Up @@ -102,7 +102,7 @@ CMouseHandler::CMouseHandler()

CMouseHandler::~CMouseHandler()
{
if (hwHide)
if (hwHideCursor)
SDL_ShowCursor(SDL_ENABLE);

configHandler->RemoveObserver(this);
Expand All @@ -120,8 +120,8 @@ void CMouseHandler::InitStatic()

void CMouseHandler::KillStatic()
{
IMouseInput::FreeInstance(mouseInput);
spring::SafeDelete(mouse);
IMouseInput::FreeInstance(mouseInput);
}


Expand Down Expand Up @@ -221,18 +221,18 @@ void CMouseHandler::MouseMove(int x, int y, int dx, int dy)

// calculating deltas (scroll{x,y} = last{x,y} - screenCenter.{x,y})
// is not required when using relative motion mode, can add directly
scrollx += (dx * hide);
scrolly += (dy * hide);
scrollx += (dx * hideCursor);
scrolly += (dy * hideCursor);

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

if (locked) {
camHandler->GetCurrentController().MouseMove(float3(dx, dy, invertMouse ? -1.0f : 1.0f));
return;
}

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

if (game != nullptr && !game->IsGameOver())
Expand All @@ -257,27 +257,23 @@ void CMouseHandler::MousePress(int x, int y, int button)
if (button > NUM_BUTTONS)
return;

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

if (game != nullptr && !game->IsGameOver())
playerHandler.Player(gu->myPlayerNum)->currentStats.mouseClicks++;

ButtonPressEvt& bp = buttons[button];
ButtonPressEvt& bp = buttons[activeButtonIdx = button];
bp.chorded = (buttons[SDL_BUTTON_LEFT].pressed || buttons[SDL_BUTTON_RIGHT].pressed);
bp.pressed = true;
bp.time = gu->gameTime;
bp.x = x;
bp.y = y;
bp.camPos = camera->GetPos();
bp.dir = dir;
bp.dir = (dir = GetCursorCameraDir(x, y));
bp.movement = 0;

activeButtonIdx = button;

if (activeReceiver && activeReceiver->MousePress(x, y, button))
return;

if (inMapDrawer && inMapDrawer->IsDrawMode()) {
if (inMapDrawer != nullptr && inMapDrawer->IsDrawMode()) {
inMapDrawer->MousePress(x, y, button);
return;
}
Expand Down Expand Up @@ -329,8 +325,14 @@ void CMouseHandler::MousePress(int x, int y, int button)
* GetSelectionBoxCoeff
* returns the topright & bottomleft corner positions of the SelectionBox in (cam->right, cam->up)-space
*/
void CMouseHandler::GetSelectionBoxCoeff(const float3& pos1, const float3& dir1, const float3& pos2, const float3& dir2, float2& topright, float2& btmleft)
{
void CMouseHandler::GetSelectionBoxCoeff(
const float3& pos1,
const float3& dir1,
const float3& pos2,
const float3& dir2,
float2& topright,
float2& btmleft
) {
float dist = CGround::LineGroundCol(pos1, pos1 + dir1 * globalRendering->viewRange * 1.4f, false);
if(dist < 0) dist = globalRendering->viewRange * 1.4f;
float3 gpos1 = pos1 + dir1 * dist;
Expand Down Expand Up @@ -368,11 +370,11 @@ void CMouseHandler::MouseRelease(int x, int y, int button)
if (button > NUM_BUTTONS)
return;

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

buttons[button].pressed = false;

if (inMapDrawer && inMapDrawer->IsDrawMode()){
if (inMapDrawer != nullptr && inMapDrawer->IsDrawMode()) {
inMapDrawer->MouseRelease(x, y, button);
return;
}
Expand Down Expand Up @@ -461,7 +463,7 @@ void CMouseHandler::MouseWheel(float delta)

void CMouseHandler::DrawSelectionBox()
{
dir = hide ? camera->GetDir() : camera->CalcPixelDir(lastx, lasty);
dir = GetCursorCameraDir(lastx, lasty);

if (activeReceiver)
return;
Expand Down Expand Up @@ -515,6 +517,7 @@ void CMouseHandler::DrawSelectionBox()
}


float3 CMouseHandler::GetCursorCameraDir(int x, int y) const { (hideCursor? camera->GetDir() : camera->CalcPixelDir(x, y)); }
float3 CMouseHandler::GetWorldMapPos() const
{
const float3 cameraPos = camera->GetPos();
Expand Down Expand Up @@ -594,7 +597,7 @@ void CMouseHandler::Update()
{
SetCursor(newCursor);

if (!hide)
if (!hideCursor)
return;

const int2 screenCenter = globalRendering->GetScreenCenter();
Expand Down Expand Up @@ -629,22 +632,17 @@ void CMouseHandler::WarpMouse(int x, int y)

void CMouseHandler::ShowMouse()
{
if (!hide)
if (!hideCursor)
return;

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

SDL_SetRelativeMouseMode(SDL_FALSE);

// don't use SDL_ShowCursor here since it would cause flickering 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);
}
ToggleHwCursor(hardwareCursor);

// force a warp back to center
mouseInput->SetPos(globalRendering->GetScreenCenter());
Expand All @@ -653,17 +651,17 @@ void CMouseHandler::ShowMouse()

void CMouseHandler::HideMouse()
{
if (hide)
if (hideCursor)
return;

hide = true;
hwHide = true;
hideCursor = true;
hwHideCursor = true;

SDL_ShowCursor(SDL_DISABLE);
// signal that we are only interested in relative motion events when MMB-scrolling
// this way the mouse position will never change so it is also unnecessary to call
// SDL_WarpMouseInWindow with associated warts (i.e. filtering of motion events by
// hand...); technically supercedes SDL_ShowCursor as well
// SDL_WarpMouseInWindow and handle the associated wart of filtering motion events
// technically supercedes SDL_ShowCursor as well
SDL_SetRelativeMouseMode(SDL_TRUE);

const int2 screenCenter = globalRendering->GetScreenCenter();
Expand All @@ -681,24 +679,26 @@ void CMouseHandler::HideMouse()
void CMouseHandler::ToggleMiddleClickScroll()
{
if (locked) {
locked = false;
ShowMouse();
} else {
locked = true;
HideMouse();
}

locked = !locked;
mmbScroll = !mmbScroll;
}


void CMouseHandler::ToggleHwCursor(const bool& enable)
void CMouseHandler::ToggleHwCursor(bool enable)
{
hardwareCursor = enable;
if (hardwareCursor) {
hwHide = true;
if ((hardwareCursor = enable)) {
hwHideCursor = true;
} else {
mouseInput->SetWMMouseCursor(nullptr);
SDL_ShowCursor(SDL_DISABLE);
}

// force hardware cursor rebinding, otherwise we get a standard b&w cursor
cursorText = "none";
}

Expand Down Expand Up @@ -726,14 +726,14 @@ void CMouseHandler::SetCursor(const std::string& cmdName, const bool forceRebind
activeCursorIdx = cursorCommandMap[""];
}

if (!hardwareCursor || hide)
if (!hardwareCursor || hideCursor)
return;

if (loadedCursors[activeCursorIdx].IsHWValid()) {
hwHide = false;
hwHideCursor = false;
loadedCursors[activeCursorIdx].BindHwCursor(); // calls SDL_ShowCursor(SDL_ENABLE);
} else {
hwHide = true;
hwHideCursor = true;
SDL_ShowCursor(SDL_DISABLE);
mouseInput->SetWMMouseCursor(nullptr);
}
Expand Down Expand Up @@ -864,7 +864,7 @@ void CMouseHandler::DrawCursor()
return;
}

if (hide)
if (hideCursor)
return;

if (hardwareCursor && loadedCursors[activeCursorIdx].IsHWValid())
Expand Down
23 changes: 16 additions & 7 deletions rts/Game/UI/MouseHandler.h
Expand Up @@ -62,42 +62,51 @@ class CMouseHandler
return nullptr;
}

float3 GetCursorCameraDir(int x, int y) const;
float3 GetWorldMapPos() const;

std::string GetCurrentTooltip() const;

const std::string& GetCurrentCursor() const { return newCursor; }
float GetCurrentCursorScale() const { return cursorScale; }

void ToggleHwCursor(const bool& enable);
void ToggleHwCursor(bool enable);

/// @see ConfigHandler::ConfigNotifyCallback
void ConfigNotify(const std::string& key, const std::string& value);

private:
void SetCursor(const std::string& cmdName, const bool forceRebind = false);

static void GetSelectionBoxCoeff(const float3& pos1, const float3& dir1, const float3& pos2, const float3& dir2, float2& topright, float2& btmleft);

void DrawScrollCursor();
void DrawFPSCursor();

static void GetSelectionBoxCoeff(
const float3& pos1,
const float3& dir1,
const float3& pos2,
const float3& dir2,
float2& topright,
float2& btmleft
);

public:
int lastx = -1;
int lasty = -1;
int activeButtonIdx = -1;
int activeCursorIdx = -1;

/// true if movement is locked, i.e. during MMB-scroll or in FPS-mode
bool locked = false;
/// Stores if the mouse was locked or not before going into direct control,
/// so we can restore it when we return to normal.
/// stores if mouse was locked before going into FPS-mode,
/// so we can restore it when we return to normal control
bool wasLocked = false;
bool offscreen = false;
bool mmbScroll = false;

private:
bool hide = true;
bool hwHide = true;
bool hideCursor = true;
bool hwHideCursor = true;
bool hardwareCursor = false;
bool invertMouse = false;

Expand Down
7 changes: 4 additions & 3 deletions rts/Lua/LuaUnsyncedRead.cpp
Expand Up @@ -1984,11 +1984,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_LEFT ].pressed);
lua_pushboolean(L, mouse->buttons[SDL_BUTTON_MIDDLE].pressed);
lua_pushboolean(L, mouse->buttons[SDL_BUTTON_RIGHT].pressed);
lua_pushboolean(L, mouse->buttons[SDL_BUTTON_RIGHT ].pressed);
lua_pushboolean(L, mouse->offscreen);
return 6;
lua_pushboolean(L, mouse->mmbScroll);
return 7;
}


Expand Down

0 comments on commit 4d331bf

Please sign in to comment.