Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelSpinner - use middle mouse button to spin model #5594

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions data/lang/ui-core/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,14 @@
"description": "",
"message": "Invert Mouse Y"
},
"NO_MIDDLE_MOUSE_BUTTON": {
"description": "",
"message": "No middle mouse button available"
},
"NO_MIDDLE_MOUSE_BUTTON_DESC": {
"description": "",
"message": "Use left mouse button to grab/spin objects when there is no middle mouse button available"
},
"IN_CARGO_HOLD": {
"description": "",
"message": "In cargo hold"
Expand Down
4 changes: 4 additions & 0 deletions data/pigui/modules/settings-window.lua
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,17 @@ local function showControlsOptions()
ui.text(lui.CONTROL_OPTIONS)

local mouseYInvert = Input.GetMouseYInverted()
local middleMouseButton = Input.EmulateMiddleMouseButton()
local joystickEnabled = Input.GetJoystickEnabled()
binding_pages = Input.GetBindingPages()
local c

c,mouseYInvert = checkbox(lui.INVERT_MOUSE_Y, mouseYInvert)
if c then Input.SetMouseYInverted(mouseYInvert) end

c,middleMouseButton = checkbox(lui.NO_MIDDLE_MOUSE_BUTTON, middleMouseButton, lui.NO_MIDDLE_MOUSE_BUTTON_DESC)
if c then Input.SetMiddleMouseButton(middleMouseButton) end

c,joystickEnabled = checkbox(lui.ENABLE_JOYSTICK, joystickEnabled)
if c then Input.SetJoystickEnabled(joystickEnabled) end

Expand Down
1 change: 1 addition & 0 deletions src/GameConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ GameConfig::GameConfig(const map_string &override_)
map["SfxVolume"] = "0.8";
map["EnableJoystick"] = "1";
map["InvertMouseY"] = "0";
map["noMiddleMouseButton"] = "0";
map["FOVVertical"] = "65";
map["DisplayNavTunnel"] = "0";
map["CompactRadar"] = "1";
Expand Down
11 changes: 11 additions & 0 deletions src/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,13 @@ Manager::Manager(IniConfig *config, SDL_Window *window) :
m_capturingMouse(false),
joystickEnabled(true),
mouseYInvert(false),
noMiddleMouseButton(false), // Which means a middle mouse button is expected by default
m_enableBindings(true),
m_frameListChanged(false)
{
joystickEnabled = (m_config->Int("EnableJoystick")) ? true : false;
mouseYInvert = (m_config->Int("InvertMouseY")) ? true : false;
noMiddleMouseButton = (m_config->Int("noMiddleMouseButton")) ? true : false;

Input::InitJoysticks(m_config);
}
Expand Down Expand Up @@ -509,6 +511,15 @@ void Manager::SetMouseYInvert(bool state)
}
}

void Manager::SetMiddleMouseButton(bool state)
{
noMiddleMouseButton = state;
if (m_enableConfigSaving) {
m_config->SetInt("noMiddleMouseButton", noMiddleMouseButton);
m_config->Save();
}
}

void Manager::GetMousePosition(int position[2])
{
SDL_GetMouseState(&position[0], &position[1]);
Expand Down
4 changes: 4 additions & 0 deletions src/Input.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ class Input::Manager {
bool IsMouseYInvert() { return mouseYInvert; }
void SetMouseYInvert(bool state);

bool EmulateMiddleMouseButton() { return noMiddleMouseButton; }
void SetMiddleMouseButton(bool state);

bool IsMouseButtonPressed(int button) { return mouseButton[button] == 1; }
bool IsMouseButtonReleased(int button) { return mouseButton[button] == 4; }

Expand Down Expand Up @@ -248,6 +251,7 @@ class Input::Manager {

bool joystickEnabled;
bool mouseYInvert;
bool noMiddleMouseButton;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adopted to surrounding code here but shouldn't they be member variables, m_mouseYInvert, m_noMiddleMouseButton... etc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically yes, they're this way for legacy reasons. Let me review the PR further before I make a decision as to whether it's worth converting them.


std::map<std::string, BindingPage> bindingPages;
std::map<std::string, InputBindings::Action> actionBindings;
Expand Down
3 changes: 2 additions & 1 deletion src/SectorMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,8 @@ void SectorMap::Update(float frameTime)
if (InputBindings.mapViewPitch->IsActive()) m_rotXMovingTo += 0.5f * moveSpeed * InputBindings.mapViewPitch->GetValue();

// to capture mouse when button was pressed and release when released
if (input->MouseButtonState(SDL_BUTTON_MIDDLE) != m_rotateWithMouseButton) {
const int mouseButton = (input->EmulateMiddleMouseButton() ? SDL_BUTTON_LEFT : SDL_BUTTON_MIDDLE);
if (input->MouseButtonState(mouseButton) != m_rotateWithMouseButton) {
m_rotateWithMouseButton = !m_rotateWithMouseButton;
input->SetCapturingMouse(m_rotateWithMouseButton);
}
Expand Down
3 changes: 2 additions & 1 deletion src/SystemView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ void SystemMapViewport::HandleInput(float ft)
Input::Manager *inputMgr = m_app->GetInput();

// to capture mouse when button was pressed and release when released
if (inputMgr->MouseButtonState(SDL_BUTTON_MIDDLE) != m_rotateWithMouseButton) {
const int mouseButton = (inputMgr->EmulateMiddleMouseButton() ? SDL_BUTTON_LEFT : SDL_BUTTON_MIDDLE);
if (inputMgr->MouseButtonState(mouseButton) != m_rotateWithMouseButton) {
m_rotateWithMouseButton = !m_rotateWithMouseButton;
inputMgr->SetCapturingMouse(m_rotateWithMouseButton);
}
Expand Down
20 changes: 20 additions & 0 deletions src/lua/LuaInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,24 @@ static int l_input_set_mouse_y_inverted(lua_State *l)
return 0;
}


static int l_input_emulate_middle_mouse_button(lua_State *l)
{
lua_pushboolean(l, Pi::input->EmulateMiddleMouseButton());
return 1;
}

static int l_input_set_middle_mouse_button(lua_State *l)
{
if (lua_isnone(l, 1))
return luaL_error(l, "SetMiddleMouseButton takes one boolean argument");
const bool mousebutton = lua_toboolean(l, 1);
Pi::config->SetInt("noMiddleMouseButton", (mousebutton ? 1 : 0));
Pi::config->Save();
Pi::input->SetMiddleMouseButton(mousebutton);
return 0;
}

static int l_input_get_mouse_captured(lua_State *l)
{
LuaPush<bool>(l, Pi::input->IsCapturingMouse());
Expand Down Expand Up @@ -716,6 +734,8 @@ void LuaInput::Register()
{ "SaveBinding", l_input_save_binding },
{ "GetMouseYInverted", l_input_get_mouse_y_inverted },
{ "SetMouseYInverted", l_input_set_mouse_y_inverted },
{ "EmulateMiddleMouseButton", l_input_emulate_middle_mouse_button },
{ "SetMiddleMouseButton", l_input_set_middle_mouse_button },
{ "GetJoystickEnabled", l_input_get_joystick_enabled },
{ "SetJoystickEnabled", l_input_set_joystick_enabled },

Expand Down
5 changes: 4 additions & 1 deletion src/pigui/ModelSpinner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "graphics/RenderTarget.h"
#include "graphics/Renderer.h"
#include "graphics/Texture.h"
#include "Input.h"
#include "scenegraph/Tag.h"

#include <algorithm>
Expand All @@ -17,6 +18,7 @@ using namespace PiGui;

ModelSpinner::ModelSpinner() :
m_spinning(true),
m_middleMouseButton(false),
m_pauseTime(.0f),
m_rot(vector2f(DEG2RAD(-15.0), DEG2RAD(120.0))),
m_zoom(1.0f),
Expand Down Expand Up @@ -136,7 +138,8 @@ void ModelSpinner::DrawPiGui()

const ImGuiIO &io = ImGui::GetIO();
bool hovered = ImGui::IsItemHovered();
if (hovered && ImGui::IsMouseDown(0)) {
const int mouseButton = (Pi::input->EmulateMiddleMouseButton() ? 0 : 2); // 0 : 2 = ImGui mouse button Left and Middle.
if (hovered && ImGui::IsMouseDown(mouseButton)) {
m_rot.x -= 0.005 * io.MouseDelta.y;
m_rot.y -= 0.005 * io.MouseDelta.x;
m_pauseTime = 1.0f;
Expand Down
3 changes: 3 additions & 0 deletions src/pigui/ModelSpinner.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ namespace PiGui {
// Shoulde we spinne?
bool m_spinning;

// Is there a middle mouse button?
bool m_middleMouseButton;

// After the user manually rotates the model, hold that orientation for
// a second to let them look at it. Assumes Update() is called every
// frame while visible.
Expand Down
3 changes: 2 additions & 1 deletion src/ship/ShipViewController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ void ShipViewController::Update()
Pi::input->GetMouseMotion(mouseMotion);

// external camera mouselook
bool mouse_down = Pi::input->MouseButtonState(SDL_BUTTON_MIDDLE);
const int mouseButton = (Pi::input->EmulateMiddleMouseButton() ? SDL_BUTTON_LEFT : SDL_BUTTON_MIDDLE);
bool mouse_down = Pi::input->MouseButtonState(mouseButton);
if (mouse_down && !headtracker_input_priority) {
if (!m_mouseActive) {
m_mouseActive = true;
Expand Down