Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
Implement mouse grabbing (lock)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaffeine committed Feb 21, 2024
1 parent 23a494b commit 29a9418
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions gui/layout/options.dat
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ CB 6995 240 159

// Reverse mouse
CB 6994 240 179

// Grab mouse
CB 11002 240 199
2 changes: 2 additions & 0 deletions machstrg.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1494,4 +1494,6 @@ BELONGING TO</string>
>Scale Factor</string>
<string id="11001"
>The UI Scale Factor settings will not take effect until you re-run the game.</string>
<string id="11002"
>Grab Mouse</string>
</resources>
19 changes: 18 additions & 1 deletion src/libdev/machgui/ctxoptns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@
#define OPTIONS_AREA_MINX 95
#define OPTIONS_AREA_MINY 50
#define OPTIMISATIONS_AREA_MINX OPTIONS_AREA_MINX
#define OPTIMISATIONS_AREA_MINY 209
#define OPTIMISATIONS_AREA_MINY 239

const char c_ScaleFactorOptionKey[] = "Options\\Scale Factor";
const char c_GrabCursorOptionKey[] = "Options\\Grab Cursor";

class MachGuiCtxOptions;

Expand Down Expand Up @@ -154,6 +155,7 @@ MachGuiCtxOptions::MachGuiCtxOptions(MachGuiStartupScreens* pStartupScreens)
const MachGuiOptionsLayout::CheckBoxInfo& cursorType = screenLayout.checkBoxInfo(3);
const MachGuiOptionsLayout::CheckBoxInfo& reverseKeys = screenLayout.checkBoxInfo(4);
const MachGuiOptionsLayout::CheckBoxInfo& reverseMouse = screenLayout.checkBoxInfo(5);
const MachGuiOptionsLayout::CheckBoxInfo& grabMouse = screenLayout.checkBoxInfo(6);

// Create control labels
new MachGuiMenuText(
Expand Down Expand Up @@ -210,6 +212,9 @@ MachGuiCtxOptions::MachGuiCtxOptions(MachGuiStartupScreens* pStartupScreens)
pReverseMouse_
= new MachGuiCheckBox(pStartupScreens, pStartupScreens, reverseMouse.topLeft, reverseMouse.stringId);

pGrabMouse_
= new MachGuiCheckBox(pStartupScreens, pStartupScreens, grabMouse.topLeft, grabMouse.stringId);

// Create volume sliders
pMusicVolume_ = new MachGuiSlideBar(pStartupScreens, pStartupScreens, musicVolSl.topLeft, musicVolSl.range);
pMusicVolume_->minMax(0, 100);
Expand Down Expand Up @@ -445,6 +450,11 @@ MachGuiCtxOptions::MachGuiCtxOptions(MachGuiStartupScreens* pStartupScreens)

readFromConfig();

pGrabMouse_->setCallback([](MachGuiCheckBox* pCheckBox) {
RenDisplay* pDisplay_ = W4dManager::instance().sceneManager()->pDevice()->display();
pDisplay_->setCursorGrabEnabled(pCheckBox->isChecked());
});

TEST_INVARIANT;
}

Expand Down Expand Up @@ -558,6 +568,9 @@ void MachGuiCtxOptions::buttonEvent(MachGuiStartupScreens::ButtonEvent buttonEve
pGammaCorrection_->setValue(gammaCorrection_);
}
pStartupScreens_->buttonAction(MachGuiStartupScreens::EXIT);

const bool grabCursorEnabled = SysRegistry::instance().queryBooleanValue(c_GrabCursorOptionKey, "on", true);
pGrabMouse_->setChecked(grabCursorEnabled);
}
}

Expand Down Expand Up @@ -649,6 +662,7 @@ void MachGuiCtxOptions::writeToConfig()
// Store reverse direction of up/down keys/mouse
SysRegistry::instance().setIntegerValue("Options\\Reverse UpDown Keys", "on", pReverseKeys_->isChecked());
SysRegistry::instance().setIntegerValue("Options\\Reverse BackForward Mouse", "on", pReverseMouse_->isChecked());
SysRegistry::instance().setIntegerValue(c_GrabCursorOptionKey, "on", pGrabMouse_->isChecked());

// Access all the boolean optimisations
const MachPhysComplexityManager::BooleanItems& boolItems = MachPhysComplexityManager::instance().booleanItems();
Expand Down Expand Up @@ -726,6 +740,9 @@ void MachGuiCtxOptions::readFromConfig()
pReverseKeys_->setChecked(SysRegistry::instance().queryIntegerValue("Options\\Reverse UpDown Keys", "on"));
pReverseMouse_->setChecked(SysRegistry::instance().queryIntegerValue("Options\\Reverse BackForward Mouse", "on"));

const bool grabCursorEnabled = SysRegistry::instance().queryBooleanValue(c_GrabCursorOptionKey, "on", true);
pGrabMouse_->setChecked(grabCursorEnabled);

pTransitions_->setChecked(pStartupScreens_->startupData()->transitionFlicsOn());

// Access all the boolean optimisations
Expand Down
1 change: 1 addition & 0 deletions src/libdev/machgui/ctxoptns.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class MachGuiCtxOptions : public MachGuiStartupScreenContext
MachGuiCheckBox* pCursorType_ = nullptr;
MachGuiCheckBox* pReverseKeys_ = nullptr;
MachGuiCheckBox* pReverseMouse_ = nullptr;
MachGuiCheckBox* pGrabMouse_{};
MachGuiDropDownListBoxCreator* pScreenSize_ = nullptr;
BooleanOptimisations booleanOptimisations_;
ChoicesOptimisations choicesOptimisations_;
Expand Down
1 change: 1 addition & 0 deletions src/libdev/machgui/internal/strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -800,3 +800,4 @@

#define IDS_SCALE_FACTOR 11000
#define IDS_MENUMESSAGE_SCALE_FACTOR 11001
#define IDS_GRAB_MOUSE 11002
5 changes: 5 additions & 0 deletions src/libdev/render/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,11 @@ const RenCursor2d* RenDisplay::currentCursor() const
return pImpl_->currentCursor();
}

void RenDisplay::setCursorGrabEnabled(bool enabled)
{
SDL_SetWindowGrab(pImpl_->pWnd_, enabled ? SDL_TRUE : SDL_FALSE);
}

uint32_t RenDisplay::frameNumber() const
{
CB_RenDisplay_DEPIMPL();
Expand Down
2 changes: 2 additions & 0 deletions src/libdev/render/display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ class RenDisplay
void useCursor(const RenCursor2d*);
const RenCursor2d* currentCursor() const;

void setCursorGrabEnabled(bool enabled);

void supportsGammaCorrection(bool);
bool supportsGammaCorrection() const;
void gammaCorrection(const double& gammaCorrection);
Expand Down
4 changes: 4 additions & 0 deletions src/projects/machines/sdlapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ bool SDLApp::clientStartup()
}

DevMouse::instance().scaleCoordinates(mode.width(), mode.height());
{
const bool grabEnabled = SysRegistry::instance().queryBooleanValue("Options\\Grab Cursor", "on", true);
pDisplay_->setCursorGrabEnabled(grabEnabled);
}

spdlog::info("Initializing the rendering device...");
std::unique_ptr<RenDevice> pDevice = std::make_unique<RenDevice>(pDisplay_);
Expand Down

0 comments on commit 29a9418

Please sign in to comment.