Skip to content

Commit

Permalink
Add possibility to turn the music on/off
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkatrina committed Jun 4, 2022
1 parent 184a381 commit f0e41b6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
57 changes: 45 additions & 12 deletions src/Client/View/MainMenu/client_main_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ ClientMainMenu::ClientMainMenu(QWidget* parent) :
QWidget(parent),
setting_(new QSettings(this)),
background_(new BackgroundWidget(this)),
sound_on_(new QPushButton(this)),
background_layout_(new QGridLayout),
game_name_(new QLabel("SpaceGate", this)),
interface_layout_(new QGridLayout),
Expand Down Expand Up @@ -143,8 +144,7 @@ void ClientMainMenu::Connect() {
&ClientMainMenu::BackToGameOption);
connect(exit_,
&QPushButton::clicked,
[this]() { emit Close();
setting_->setValue("sound_value", set_sound_->value());});
[this]() { emit Close();});
connect(back_to_start_,
&QPushButton::clicked,
this,
Expand All @@ -154,11 +154,35 @@ void ClientMainMenu::Connect() {
&QPushButton::clicked, this, &ClientMainMenu::ReadyButtonPressEvent);
connect(set_sound_, QOverload<int>::of(&QSpinBox::valueChanged),
this, [&](int value){
if (value > 0) {
sound_on_->setText("Turn Off");
sound_->setMuted(0);
} else {
sound_on_->setText("Turn On");
sound_->setMuted(1);
}
sound_->setVolume(double(value) / 100);
setting_->setValue("sound_value", sound_->volume());
setting_->setValue("spin_box_value", value);
setting_->setValue("muted", sound_->isMuted());
});
connect(sound_on_, &QPushButton::clicked, this, [&](){
if (sound_->isMuted()) {
sound_->setMuted(0);
sound_on_->setText("Turn Off");
} else {
sound_->setMuted(1);
sound_on_->setText("Turn On");
}
setting_->setValue("muted", sound_->isMuted());
});
connect(sound_, &QSoundEffect::mutedChanged, this, [&](){
if (sound_->isMuted()) {
sound_on_->setText("Turn On");
} else {
sound_on_->setText("Turn Off");
}
});

}

void ClientMainMenu::ChooseRoomOption() {
Expand Down Expand Up @@ -205,6 +229,7 @@ void ClientMainMenu::RemoveAllWidgets() {
rooms_list_->setVisible(false);
nothing_here_->setVisible(false);
set_sound_->setVisible(false);
sound_on_->setVisible(false);

interface_layout_->removeWidget(create_room_);
interface_layout_->removeWidget(join_room_);
Expand Down Expand Up @@ -240,24 +265,33 @@ void ClientMainMenu::Settings() {
set_sound_->setFixedSize(400, 100);
set_sound_->setSpecialValueText("Volume");
set_sound_->setFont(font_);
set_sound_->setStyleSheet("color : #88bcff; font-size: 60px;");
set_sound_->setStyleSheet("color : #88bcff; font-size: 10px; aligment : Qt::AlignCenter");
set_sound_->setRange(0, 100);

set_sound_->setValue((setting_->value("spin_box_value")).toInt());
sound_->setMuted((setting_->value("muted").toBool()));
set_sound_->setVisible(true);

nothing_here_->setVisible(true);
back_to_start_->setVisible(true);
set_sound_->setVisible(true);

sound_on_->setVisible(true);
sound_on_->setFixedSize(400, 100);
sound_on_->setText("Turn Off");
sound_on_->setFont(font_);
sound_on_->setStyleSheet("color : #88bcff; font-size: 40px;");

nothing_here_->setText("Adjust Sound");
nothing_here_->setFont(font_);
nothing_here_->setStyleSheet("QLabel {color : #88bcff; font-size: 40px;}");
nothing_here_->setStyleSheet("color : #88bcff; font-size: 40px;");

interface_layout_->addWidget(nothing_here_, 1, 0, 1, 2,
Qt::AlignHCenter | Qt::AlignVCenter);
interface_layout_->addWidget(back_to_start_, 3, 0, 1, 2,
interface_layout_->addWidget(back_to_start_, 4, 0, 1, 2,
Qt::AlignHCenter | Qt::AlignVCenter);
interface_layout_->addWidget(set_sound_, 2, 0, 1, 2,
Qt::AlignHCenter | Qt::AlignVCenter);
interface_layout_->addWidget(sound_on_, 3, 0, 1, 2,
Qt::AlignHCenter | Qt::AlignVCenter);
}

void ClientMainMenu::UpdateRoomsList(
Expand Down Expand Up @@ -411,12 +445,11 @@ void ClientMainMenu::SetSound() {
sound_->setSource(QUrl::fromLocalFile(":sound.wav"));
auto volume = setting_->value("sound_value");
if (!volume.isNull()) {
sound_->setVolume(volume.toInt());
sound_->setVolume(volume.toDouble());
} else {
sound_->setVolume(0.5);
}
sound_->setMuted((setting_->value("muted")).toBool());
sound_->setLoopCount(1000);
sound_->play();
}
void ClientMainMenu::SoundValueChangedEvent() {
emit SoundValueChanged();
}
1 change: 1 addition & 0 deletions src/Client/View/MainMenu/client_main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ClientMainMenu : public QWidget {
QSoundEffect* sound_;
QSpinBox* set_sound_;
QSettings* setting_;
QPushButton* sound_on_;

bool is_chief_{true};
static QColor StatusToColor(server_events::RoomUser::Status status);
Expand Down

0 comments on commit f0e41b6

Please sign in to comment.