Skip to content

Commit

Permalink
Use middle mouse button to spin models
Browse files Browse the repository at this point in the history
* Make grab/spin action consistent and default to middle mouse button.
* Option to use left mouse button in cases where no middle mouse button
  is available such as on a laptop.
  • Loading branch information
zonkmachine committed Aug 16, 2024
1 parent 4ef9b91 commit fe53e62
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 4 deletions.
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.IsMiddleMouseButton()
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 IsMiddleMouseButton() { 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;

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->IsMiddleMouseButton() ? 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->IsMiddleMouseButton() ? 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_is_middle_mouse_button(lua_State *l)
{
lua_pushboolean(l, Pi::input->IsMiddleMouseButton());
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 },
{ "IsMiddleMouseButton", l_input_is_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->IsMiddleMouseButton() ? 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->IsMiddleMouseButton() ? 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

0 comments on commit fe53e62

Please sign in to comment.