Skip to content

Commit d7cd125

Browse files
committed
Add hotkeys and menu shortcuts to mute music and sound
This change introduces the ability to mute and unmute music and sound effects via the menu and hotkeys. - Added 'Music muted' and 'Sound muted' settings to the configuration. - Created an 'Audio' menu in the main window containing checkable actions for muting music (Ctrl+M) and sound (Ctrl+Shift+M). - Updated MusicManager and SfxManager to respect these muted states. - Chosen Ctrl+Shift+M for sound because Ctrl+S is already reserved for saving maps. - Integrated theme-based icons for the mute actions. - Synchronized the menu action states with the global configuration.
1 parent 4c2397f commit d7cd125

6 files changed

Lines changed: 64 additions & 6 deletions

File tree

src/configuration/configuration.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,9 @@ ConstString KEY_LINES_OF_SCROLLBACK = "Lines of scrollback";
268268
ConstString KEY_PROXY_LOCAL_PORT = "Local port number";
269269
ConstString KEY_MAP_MODE = "Map Mode";
270270
ConstString KEY_MUSIC_VOLUME = "Music volume";
271+
ConstString KEY_MUSIC_MUTED = "Music muted";
271272
ConstString KEY_SOUND_VOLUME = "Sound volume";
273+
ConstString KEY_SOUND_MUTED = "Sound muted";
272274
ConstString KEY_AUDIO_OUTPUT_DEVICE = "Audio output device";
273275
ConstString KEY_AUDIO_UNLOCKED = "Audio unlocked";
274276
ConstString KEY_MAXIMUM_NUMBER_OF_PATHS = "maximum number of paths";
@@ -777,7 +779,9 @@ void Configuration::AudioSettings::read(const QSettings &conf)
777779
? false
778780
: conf.value(KEY_AUDIO_UNLOCKED, false).toBool();
779781
m_musicVolume = std::clamp(conf.value(KEY_MUSIC_VOLUME, 50).toInt(), 0, 100);
782+
m_musicMuted = conf.value(KEY_MUSIC_MUTED, false).toBool();
780783
m_soundVolume = std::clamp(conf.value(KEY_SOUND_VOLUME, 50).toInt(), 0, 100);
784+
m_soundMuted = conf.value(KEY_SOUND_MUTED, false).toBool();
781785
m_outputDeviceId = conf.value(KEY_AUDIO_OUTPUT_DEVICE).toByteArray();
782786
}
783787

@@ -964,7 +968,9 @@ void Configuration::AudioSettings::write(QSettings &conf) const
964968
conf.setValue(KEY_AUDIO_UNLOCKED, m_unlocked);
965969
}
966970
conf.setValue(KEY_MUSIC_VOLUME, m_musicVolume);
971+
conf.setValue(KEY_MUSIC_MUTED, m_musicMuted);
967972
conf.setValue(KEY_SOUND_VOLUME, m_soundVolume);
973+
conf.setValue(KEY_SOUND_MUTED, m_soundMuted);
968974
conf.setValue(KEY_AUDIO_OUTPUT_DEVICE, m_outputDeviceId);
969975
}
970976

src/configuration/configuration.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ class NODISCARD Configuration final
357357
int m_soundVolume = 50;
358358
QByteArray m_outputDeviceId;
359359
bool m_unlocked = false;
360+
bool m_musicMuted = false;
361+
bool m_soundMuted = false;
360362

361363
public:
362364
explicit AudioSettings() = default;
@@ -371,13 +373,27 @@ class NODISCARD Configuration final
371373
m_changeMonitor.notifyAll();
372374
}
373375

376+
NODISCARD bool isMusicMuted() const { return m_musicMuted; }
377+
void setMusicMuted(const bool muted)
378+
{
379+
m_musicMuted = muted;
380+
m_changeMonitor.notifyAll();
381+
}
382+
374383
NODISCARD int getSoundVolume() const { return m_soundVolume; }
375384
void setSoundVolume(const int volume)
376385
{
377386
m_soundVolume = volume;
378387
m_changeMonitor.notifyAll();
379388
}
380389

390+
NODISCARD bool isSoundMuted() const { return m_soundMuted; }
391+
void setSoundMuted(const bool muted)
392+
{
393+
m_soundMuted = muted;
394+
m_changeMonitor.notifyAll();
395+
}
396+
381397
NODISCARD const QByteArray &getOutputDeviceId() const { return m_outputDeviceId; }
382398
void setOutputDeviceId(const QByteArray &id)
383399
{

src/mainwindow/mainwindow.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,12 @@ void MainWindow::wireConnections()
521521
&FindRoomsDlg::sig_editSelection,
522522
this,
523523
&MainWindow::slot_onEditRoomSelection);
524+
525+
setConfig().audio.registerChangeCallback(m_lifetime, [this]() {
526+
const auto &audio = getConfig().audio;
527+
muteMusicAct->setChecked(audio.isMusicMuted());
528+
muteSoundAct->setChecked(audio.isSoundMuted());
529+
});
524530
}
525531

526532
void MainWindow::slot_log(const QString &mod, const QString &message)
@@ -1027,6 +1033,26 @@ void MainWindow::createActions()
10271033
rebuildMeshesAct->setStatusTip(tr("Reconstruct the world mesh to fix graphical rendering bugs"));
10281034
rebuildMeshesAct->setCheckable(false);
10291035
connect(rebuildMeshesAct, &QAction::triggered, getCanvas(), &MapCanvas::slot_rebuildMeshes);
1036+
1037+
muteMusicAct = new QAction(QIcon::fromTheme("audio-volume-muted", QIcon(":/icons/audiocfg.png")),
1038+
tr("Mute Music"),
1039+
this);
1040+
muteMusicAct->setShortcut(tr("Ctrl+M"));
1041+
muteMusicAct->setCheckable(true);
1042+
muteMusicAct->setStatusTip(tr("Mute/Unmute Music"));
1043+
connect(muteMusicAct, &QAction::triggered, this, [](bool checked) {
1044+
setConfig().audio.setMusicMuted(checked);
1045+
});
1046+
1047+
muteSoundAct = new QAction(QIcon::fromTheme("audio-volume-muted", QIcon(":/icons/audiocfg.png")),
1048+
tr("Mute Sound"),
1049+
this);
1050+
muteSoundAct->setShortcut(tr("Ctrl+Shift+M"));
1051+
muteSoundAct->setCheckable(true);
1052+
muteSoundAct->setStatusTip(tr("Mute/Unmute Sound"));
1053+
connect(muteSoundAct, &QAction::triggered, this, [](bool checked) {
1054+
setConfig().audio.setSoundMuted(checked);
1055+
});
10301056
}
10311057

10321058
static void setConfigMapMode(const MapModeEnum mode)
@@ -1211,6 +1237,10 @@ void MainWindow::setupMenuBar()
12111237
}
12121238
viewMenu->addAction(alwaysOnTopAct);
12131239

1240+
QMenu *audioMenu = menuBar()->addMenu(tr("&Audio"));
1241+
audioMenu->addAction(muteMusicAct);
1242+
audioMenu->addAction(muteSoundAct);
1243+
12141244
settingsMenu = menuBar()->addMenu(tr("&Tools"));
12151245
QMenu *clientMenu = settingsMenu->addMenu(QIcon(":/icons/terminal.png"),
12161246
tr("&Integrated Mud Client"));

src/mainwindow/mainwindow.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ class NODISCARD_QOBJECT MainWindow final : public QMainWindow
238238
QAction *forceRoomAct = nullptr;
239239
QAction *releaseAllPathsAct = nullptr;
240240
QAction *rebuildMeshesAct = nullptr;
241+
QAction *muteMusicAct = nullptr;
242+
QAction *muteSoundAct = nullptr;
241243

242244
std::unique_ptr<ConfigDialog> m_configDialog;
243245

src/media/MusicManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void MusicManager::updateVolumes()
242242

243243
auto &cfg = getConfig().audio;
244244
float masterVol = static_cast<float>(cfg.getMusicVolume()) / 100.0f;
245-
bool canPlay = cfg.isUnlocked() && masterVol > 0;
245+
bool canPlay = cfg.isUnlocked() && masterVol > 0 && !cfg.isMusicMuted();
246246
auto &active = m_channels[m_activeChannel];
247247
if (canPlay && active.player->playbackState() == QMediaPlayer::StoppedState
248248
&& !active.file.isEmpty()) {
@@ -251,7 +251,8 @@ void MusicManager::updateVolumes()
251251
}
252252
active.player->play();
253253
applyPendingPosition(m_activeChannel);
254-
} else if (masterVol <= 0 && active.player->playbackState() == QMediaPlayer::PlayingState) {
254+
} else if ((masterVol <= 0 || cfg.isMusicMuted())
255+
&& active.player->playbackState() == QMediaPlayer::PlayingState) {
255256
if (!active.file.isEmpty()) {
256257
m_cachedPositions.insert(active.file, new qint64(active.player->position()));
257258
}
@@ -293,7 +294,8 @@ void MusicManager::applyPendingPosition(int channelIndex)
293294

294295
void MusicManager::updateChannelVolume(int channelIndex)
295296
{
296-
float masterVol = static_cast<float>(getConfig().audio.getMusicVolume()) / 100.0f;
297+
auto &cfg = getConfig().audio;
298+
float masterVol = cfg.isMusicMuted() ? 0.0f : static_cast<float>(cfg.getMusicVolume()) / 100.0f;
297299
m_channels[channelIndex].audioOutput->setVolume(masterVol * m_channels[channelIndex].fadeVolume);
298300
}
299301

src/media/SfxManager.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ SfxManager::SfxManager(MediaLibrary &library, QObject *const parent)
3232
void SfxManager::playSound(MAYBE_UNUSED const QString &soundName)
3333
{
3434
auto &cfg = getConfig().audio;
35-
if (!cfg.isUnlocked() || cfg.getSoundVolume() <= 0) {
35+
if (!cfg.isUnlocked() || cfg.getSoundVolume() <= 0 || cfg.isSoundMuted()) {
3636
return;
3737
}
3838

@@ -60,7 +60,7 @@ void SfxManager::playFromData(const QByteArray &data, const QString &soundName)
6060
}
6161

6262
auto &cfg = getConfig().audio;
63-
if (!cfg.isUnlocked() || cfg.getSoundVolume() <= 0) {
63+
if (!cfg.isUnlocked() || cfg.getSoundVolume() <= 0 || cfg.isSoundMuted()) {
6464
return;
6565
}
6666

@@ -106,7 +106,9 @@ void SfxManager::startEffect(const QUrl &url, const QString &tmpToDelete)
106106
void SfxManager::updateVolume()
107107
{
108108
#ifndef MMAPPER_NO_AUDIO
109-
m_output->setVolume(static_cast<float>(getConfig().audio.getSoundVolume()) / 100.0f);
109+
auto &cfg = getConfig().audio;
110+
float volume = cfg.isSoundMuted() ? 0.0f : static_cast<float>(cfg.getSoundVolume()) / 100.0f;
111+
m_output->setVolume(volume);
110112
#endif
111113
}
112114

0 commit comments

Comments
 (0)