diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b12a90..27829f0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,6 +123,9 @@ set(PROJECT_SOURCES game/Start.h game/Start.Paint.cpp game/Start.ui + game/Settings.cpp + game/Settings.h + game/Settings.ui game/StartLayout.h game/localplayer/KeyBindings.cpp game/localplayer/KeyBindings.h diff --git a/game/Settings.cpp b/game/Settings.cpp new file mode 100644 index 0000000..e9a04e8 --- /dev/null +++ b/game/Settings.cpp @@ -0,0 +1,349 @@ +// This file is part of c-evo-evo. +// +// This software is governed by the CeCILL license under French law and +// abiding by the rules of distribution of free software. You can use, +// modify and/or redistribute the software under the terms of the CeCILL +// license as circulated by CEA, CNRS and INRIA at the following URL +// "http://www.cecill.info". +// +// As a counterpart to the access to the source code and rights to copy, +// modify and redistribute granted by the license, users are provided only +// with a limited warranty and the software's author, the holder of the +// economic rights, and the successive licensors have only limited +// liability. +// +// In this respect, the user's attention is drawn to the risks associated +// with loading, using, modifying and/or developing or reproducing the +// software by the user in light of its specific status of free software, +// that may mean that it is complicated to manipulate, and that also +// therefore means that it is reserved for developers and experienced +// professionals having in-depth computer knowledge. Users are therefore +// encouraged to load and test the software's suitability as regards their +// requirements in conditions enabling the security of their systems and/or +// data to be ensured and, more generally, to use and operate it in the +// same conditions as regards security. +// +// The fact that you are presently reading this means that you have had +// knowledge of the CeCILL license and that you accept its terms. + +#include "Settings.h" +#include "ui_Settings.h" + +#include "Cevo/CevoButtonA.h" +#include "Cevo/CevoButtonC.h" +#include "Cevo/CevoListBoxEx.h" +#include "Cevo/CevoScreenTools.h" +#include "Cevo/CevoTexture.h" +#include "Global.h" +#include "Messg.h" + +#include +#include +#include + +#include + +namespace Cevo { + +namespace { + +constexpr int MusicVolumeMin = 0; +constexpr int MusicVolumeMax = 100; +constexpr int MusicVolumeStep = 10; +constexpr int GammaMin = 50; +constexpr int GammaMax = 150; + +QString settingsText(int index) +{ + return phrases.lookup(QStringLiteral("SETTINGS"), index); +} + +} // namespace + +SettingsDlg::SettingsDlg(Languages &languages, QWidget *parent) + : DrawDlg(parent), m_languages(languages), ui(new Ui::SettingsDlg) +{ + ui->setupUi(this); + // A modal window of its own, even though it has a parent (the window it is transient for). + setWindowFlags(Qt::Window | Qt::FramelessWindowHint); + + ui->buttonOk->setCaption(phrases.lookup(QStringLiteral("BTN_OK"))); + ui->buttonCancel->setCaption(phrases.lookup(QStringLiteral("BTN_CANCEL"))); + ui->buttonReset->setCaption(phrases.lookup(QStringLiteral("BTN_RESET"))); + ui->buttonFullscreen->setButtonIndex(0); + ui->buttonMusic->setButtonIndex(0); + ui->buttonGammaUp->setButtonIndex(1); + ui->buttonGammaDown->setButtonIndex(0); + ui->buttonMusicVolumeUp->setButtonIndex(1); + ui->buttonMusicVolumeDown->setButtonIndex(0); + initButtons(); + + ui->buttonOk->onClick = [this] { + saveData(); + setModalResult(Dpi::ModalResult::Ok); + close(); + }; + ui->buttonCancel->onClick = [this] { + setModalResult(Dpi::ModalResult::Cancel); + close(); + }; + ui->buttonReset->onClick = [this] { resetClicked(); }; + ui->buttonFullscreen->onClick = [this] { + ui->buttonFullscreen->setButtonIndex(ui->buttonFullscreen->buttonIndex() ^ 1); + }; + ui->buttonMusic->onClick = [this] { + ui->buttonMusic->setButtonIndex(ui->buttonMusic->buttonIndex() ^ 1); + updateInterface(); + }; + ui->buttonMusicVolumeDown->onClick = [this] { + m_localMusicVolume = std::max(m_localMusicVolume - MusicVolumeStep, MusicVolumeMin); + update(); + }; + ui->buttonMusicVolumeUp->onClick = [this] { + m_localMusicVolume = std::min(m_localMusicVolume + MusicVolumeStep, MusicVolumeMax); + update(); + }; + ui->buttonGammaDown->onClick = [this] { + if (m_localGamma > GammaMin) { + --m_localGamma; + update(); + } + }; + ui->buttonGammaUp->onClick = [this] { + if (m_localGamma < GammaMax) { + ++m_localGamma; + update(); + } + }; + + ui->editShortCutPrimary->installEventFilter(this); + ui->editShortCutSecondary->installEventFilter(this); + connect(ui->listKeyBindings, &QListWidget::currentRowChanged, this, [this] { keyBindingSelectionChanged(); }); + + onShow = [this] { + centerToScreen(); + setWindowTitle(phrases2.lookup(QStringLiteral("ACTIONHEADER_CONFIG"))); + m_fullScreenText = settingsText(0); + m_gammaText = settingsText(1); + m_restartText = settingsText(2); + m_primaryText = settingsText(3); + m_secondaryText = settingsText(4); + m_languagesText = settingsText(5); + m_keyBindingsText = settingsText(6); + m_musicText = settingsText(9); + m_musicVolumeText = settingsText(10); + + m_languages.detectAvailable(); + ui->listLanguages->clear(); + for (int i = 0; i < m_languages.count(); ++i) { + const Language &language = m_languages[i]; + if (!language.available) + continue; + QString text = language.name; + if (!language.code.isEmpty()) + text += QStringLiteral(" (") + language.code + QLatin1Char(')'); + auto *item = new QListWidgetItem(text, ui->listLanguages); + item->setData(Qt::UserRole, language.code); + } + loadData(); + for (QLineEdit *edit : {ui->editShortCutPrimary, ui->editShortCutSecondary}) { + QPalette pal = edit->palette(); + pal.setColor(QPalette::Base, Qt::black); + pal.setColor(QPalette::Text, MainTexture->colorMark); + edit->setPalette(pal); + } + updateInterface(); + }; + onClose = [this] { ui->listKeyBindings->setCurrentRow(-1); }; +} + +SettingsDlg::~SettingsDlg() +{ + delete ui; +} + +bool SettingsDlg::localMusicEnabled() const +{ + return (ui->buttonMusic->buttonIndex() & 1) == 1; +} + +void SettingsDlg::updateInterface() +{ + ui->buttonMusicVolumeDown->setEnabled(localMusicEnabled()); + ui->buttonMusicVolumeUp->setEnabled(localMusicEnabled()); +} + +void SettingsDlg::loadData() +{ + int languageRow = 0; + for (int i = 0; i < ui->listLanguages->count(); ++i) { + if (ui->listLanguages->item(i)->data(Qt::UserRole).toString() == localeCode) + languageRow = i; + } + ui->listLanguages->setCurrentRow(languageRow); + ui->buttonFullscreen->setButtonIndex(fullScreen ? 3 : 2); + ui->buttonMusic->setButtonIndex(musicEnabled ? 3 : 2); + m_localMusicVolume = qRound(musicVolume * 100); + m_localGamma = gamma; + m_localKeyBindings.assign(KeyBindings::keyBindings); + m_currentKeyBinding = nullptr; + reloadKeyBindingList(); + keyBindingSelectionChanged(); +} + +void SettingsDlg::saveData() +{ + const bool needRestart = gamma != m_localGamma; + if (ui->listLanguages->currentRow() >= 0) + localeCode = ui->listLanguages->currentItem()->data(Qt::UserRole).toString(); + else + localeCode.clear(); + fullScreen = (ui->buttonFullscreen->buttonIndex() & 1) == 1; + musicEnabled = localMusicEnabled(); + musicVolume = static_cast(m_localMusicVolume) / 100.0f; + gamma = m_localGamma; + if (needRestart) + simpleMessage(m_restartText); + // Take over a shortcut still being edited. + keyBindingSelectionChanged(); + KeyBindings::keyBindings.assign(m_localKeyBindings); +} + +void SettingsDlg::resetClicked() +{ + // TODO (as in the original): the default values should live in one place. + ui->listLanguages->setCurrentRow(0); + ui->buttonFullscreen->setButtonIndex(3); + ui->buttonMusic->setButtonIndex(3); + m_localMusicVolume = 50; + m_localGamma = 100; + ui->listKeyBindings->setCurrentRow(-1); + m_currentKeyBinding = nullptr; + keyBindingSelectionChanged(); + m_localKeyBindings.resetToDefault(); + reloadKeyBindingList(); + updateInterface(); + update(); +} + +void SettingsDlg::reloadKeyBindingList() +{ + const int row = ui->listKeyBindings->currentRow(); + ui->listKeyBindings->blockSignals(true); + ui->listKeyBindings->clear(); + ui->listKeyBindings->addItems(m_localKeyBindings.toDisplayStrings()); + ui->listKeyBindings->setCurrentRow(row); + ui->listKeyBindings->blockSignals(false); +} + +void SettingsDlg::keyBindingSelectionChanged() +{ + if (m_currentKeyBinding) { + m_currentKeyBinding->shortCut = KeyBindings::textToShortCut(ui->editShortCutPrimary->text()); + m_currentKeyBinding->shortCut2 = KeyBindings::textToShortCut(ui->editShortCutSecondary->text()); + } + const int row = ui->listKeyBindings->currentRow(); + m_currentKeyBinding = row >= 0 && row < m_localKeyBindings.count() ? &m_localKeyBindings[row] : nullptr; + if (m_currentKeyBinding) { + ui->editShortCutPrimary->setText(m_currentKeyBinding->shortCut.isEmpty() + ? QString() + : KeyBindings::shortCutToText(m_currentKeyBinding->shortCut)); + ui->editShortCutSecondary->setText(m_currentKeyBinding->shortCut2.isEmpty() + ? QString() + : KeyBindings::shortCutToText(m_currentKeyBinding->shortCut2)); + } else { + ui->editShortCutPrimary->clear(); + ui->editShortCutSecondary->clear(); + } + ui->editShortCutPrimary->setEnabled(m_currentKeyBinding != nullptr); + ui->editShortCutSecondary->setEnabled(m_currentKeyBinding != nullptr); +} + +void SettingsDlg::updateShortCutItem() +{ + if (!m_currentKeyBinding) + return; + if (!m_currentKeyBinding->shortCut.isEmpty()) + m_localKeyBindings.removeShortCut(m_currentKeyBinding->shortCut); + if (!m_currentKeyBinding->shortCut2.isEmpty()) + m_localKeyBindings.removeShortCut(m_currentKeyBinding->shortCut2); + m_currentKeyBinding->shortCut = KeyBindings::textToShortCut(ui->editShortCutPrimary->text()); + m_currentKeyBinding->shortCut2 = KeyBindings::textToShortCut(ui->editShortCutSecondary->text()); + reloadKeyBindingList(); +} + +bool SettingsDlg::eventFilter(QObject *watched, QEvent *event) +{ + const bool primary = watched == ui->editShortCutPrimary; + if ((primary || watched == ui->editShortCutSecondary) && m_currentKeyBinding && + (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)) { + // The edit shows the pressed combination instead of receiving text. + if (event->type() == QEvent::KeyRelease) { + auto *keyEvent = static_cast(event); + const int key = keyEvent->key(); + if (key != Qt::Key_Shift && key != Qt::Key_Control && key != Qt::Key_Alt && key != Qt::Key_Meta && + key != Qt::Key_AltGr) { + const QKeySequence shortcut(keyEvent->modifiers() | key); + if (primary) { + m_currentKeyBinding->shortCut = shortcut; + ui->editShortCutPrimary->setText(KeyBindings::shortCutToText(shortcut)); + } else { + m_currentKeyBinding->shortCut2 = shortcut; + ui->editShortCutSecondary->setText(KeyBindings::shortCutToText(shortcut)); + } + updateShortCutItem(); + } + } + return true; + } + return DrawDlg::eventFilter(watched, event); +} + +void SettingsDlg::paintEvent(QPaintEvent *) +{ + constexpr int TextDistanceX = 20; + constexpr int UpDownTextWidth = 120; + QPainter painter(this); + Dpi::Canvas canvas; + canvas.bind(&painter); + canvas.font().assign(UniFont[static_cast(FontType::Normal)]); + canvas.brush().setStyle(Qt::NoBrush); + const QColor light = MainTexture->colorBevelLight; + const QColor shade = MainTexture->colorBevelShade; + + paintBackground(canvas, 3, 3, width() - 6, height() - 6, width(), height()); + frame(canvas, 0, 0, width() - 1, height() - 1, Qt::black, Qt::black); + frame(canvas, 1, 1, width() - 2, height() - 2, light, shade); + frame(canvas, 2, 2, width() - 3, height() - 3, light, shade); + editFrame(canvas, ui->editShortCutPrimary->geometry(), *MainTexture); + editFrame(canvas, ui->editShortCutSecondary->geometry(), *MainTexture); + editFrame(canvas, ui->listLanguages->geometry(), *MainTexture); + btnFrame(canvas, ui->buttonOk->geometry(), *MainTexture); + btnFrame(canvas, ui->buttonCancel->geometry(), *MainTexture); + + const auto checkFrame = [&](const QWidget *button) { + rframe(canvas, button->x() - 1, button->y() - 1, button->x() + 12, button->y() + 12, shade, light); + }; + checkFrame(ui->buttonFullscreen); + checkFrame(ui->buttonMusic); + loweredTextOut(canvas, -2, *MainTexture, ui->buttonMusic->x() + TextDistanceX, ui->buttonMusic->y() - 4, + m_musicText); + underlinedTitleValue(canvas, m_musicVolumeText, QString::number(m_localMusicVolume) + QLatin1Char('%'), + ui->buttonMusicVolumeUp->x() - UpDownTextWidth - 4, ui->buttonMusicVolumeUp->y() + 2, + UpDownTextWidth); + + loweredTextOut(canvas, -2, *MainTexture, ui->listLanguages->x(), ui->listLanguages->y() - 26, m_languagesText); + loweredTextOut(canvas, -2, *MainTexture, ui->listKeyBindings->x(), ui->listKeyBindings->y() - 26, + m_keyBindingsText); + loweredTextOut(canvas, -2, *MainTexture, ui->buttonFullscreen->x() + TextDistanceX, ui->buttonFullscreen->y() - 4, + m_fullScreenText); + underlinedTitleValue(canvas, m_gammaText, QString::number(m_localGamma) + QLatin1Char('%'), + ui->buttonGammaUp->x() - UpDownTextWidth - 4, ui->buttonGammaUp->y() + 2, UpDownTextWidth); + loweredTextOut(canvas, -2, *MainTexture, ui->editShortCutPrimary->x(), ui->editShortCutPrimary->y() - 26, + m_primaryText); + loweredTextOut(canvas, -2, *MainTexture, ui->editShortCutSecondary->x(), ui->editShortCutSecondary->y() - 26, + m_secondaryText); + canvas.unbind(); +} + +} // namespace Cevo diff --git a/game/Settings.h b/game/Settings.h new file mode 100644 index 0000000..265f29d --- /dev/null +++ b/game/Settings.h @@ -0,0 +1,78 @@ +// This file is part of c-evo-evo. +// +// This software is governed by the CeCILL license under French law and +// abiding by the rules of distribution of free software. You can use, +// modify and/or redistribute the software under the terms of the CeCILL +// license as circulated by CEA, CNRS and INRIA at the following URL +// "http://www.cecill.info". +// +// As a counterpart to the access to the source code and rights to copy, +// modify and redistribute granted by the license, users are provided only +// with a limited warranty and the software's author, the holder of the +// economic rights, and the successive licensors have only limited +// liability. +// +// In this respect, the user's attention is drawn to the risks associated +// with loading, using, modifying and/or developing or reproducing the +// software by the user in light of its specific status of free software, +// that may mean that it is complicated to manipulate, and that also +// therefore means that it is reserved for developers and experienced +// professionals having in-depth computer knowledge. Users are therefore +// encouraged to load and test the software's suitability as regards their +// requirements in conditions enabling the security of their systems and/or +// data to be ensured and, more generally, to use and operate it in the +// same conditions as regards security. +// +// The fact that you are presently reading this means that you have had +// knowledge of the CeCILL license and that you accept its terms. + +#pragma once + +#include "Cevo/CevoDrawDlg.h" +#include "Languages.h" +#include "localplayer/KeyBindings.h" + +QT_BEGIN_NAMESPACE +namespace Ui { +class SettingsDlg; +} +QT_END_NAMESPACE + +namespace Cevo { + +// Equivalent of Settings.pas's TSettingsDlg: language, full screen, music, +// gamma and key binding settings. Editing happens on local copies; OK +// applies them to the global settings. The original's custom DPI controls +// (DPI builds only) are not ported: Qt scales the interface by itself. +class SettingsDlg : public DrawDlg { + Q_OBJECT +public: + explicit SettingsDlg(Languages &languages, QWidget *parent = nullptr); + ~SettingsDlg() override; + +protected: + void paintEvent(QPaintEvent *event) override; + bool eventFilter(QObject *watched, QEvent *event) override; + +private: + void loadData(); + void saveData(); + void updateInterface(); + void updateShortCutItem(); + void reloadKeyBindingList(); + void keyBindingSelectionChanged(); + void resetClicked(); + bool localMusicEnabled() const; + + Languages &m_languages; + int m_localGamma = 100; + int m_localMusicVolume = 50; + KeyBindings::KeyBindingList m_localKeyBindings; + KeyBindings::KeyBinding *m_currentKeyBinding = nullptr; + // Phrases['SETTINGS', n], loaded when the dialog is shown. + QString m_fullScreenText, m_gammaText, m_restartText, m_primaryText, m_secondaryText, m_languagesText, + m_keyBindingsText, m_musicText, m_musicVolumeText; + Ui::SettingsDlg *ui; +}; + +} // namespace Cevo diff --git a/game/Settings.ui b/game/Settings.ui new file mode 100644 index 0000000..39c831d --- /dev/null +++ b/game/Settings.ui @@ -0,0 +1,135 @@ + + + + SettingsDlg + + + 00483448 + + + Settings + + + + 1632144280 + + + + + 36440010025 + + + + + 24440010025 + + + + + 1763661818 + + + + + 4523601212 + + + + + 4523721212 + + + + + 17632288232 + + + + + 1640010025 + + + + + 1443721212 + + + + + 1443601212 + + + + + 163341818 + + + + + 17629613627 + + + false + + + false + + + + + 32829613627 + + + false + + + false + + + + + + Cevo::ButtonA + QWidget +
Cevo/CevoButtonA.h
+
+ + Cevo::ButtonC + QWidget +
Cevo/CevoButtonC.h
+
+ + Cevo::ListBoxEx + QListWidget +
Cevo/CevoListBoxEx.h
+
+
+ +
diff --git a/game/Start.cpp b/game/Start.cpp index 14c00d6..1b91a5c 100644 --- a/game/Start.cpp +++ b/game/Start.cpp @@ -47,6 +47,7 @@ #include "Global.h" #include "Inp.h" #include "Messg.h" +#include "Settings.h" #include "StartLayout.h" #include "localplayer/KeyBindings.h" #include "localplayer/Term.h" @@ -393,7 +394,14 @@ void StartDlg::updateInterface() void StartDlg::showSettings() { - // TODO: Settings.pas (the configuration dialog) isn't ported yet. + SettingsDlg settingsDlg(m_languages, this); + if (settingsDlg.showModal() == Dpi::ModalResult::Ok) { + loadAssets(); + changePage(m_page); // texts of the (possibly new) language + updateInterface(); + updateMusic(); + update(); + } } void StartDlg::updateMusic() diff --git a/main.cpp b/main.cpp index 455ac7a..575bd40 100644 --- a/main.cpp +++ b/main.cpp @@ -29,6 +29,7 @@ #include "game/Direct.h" #include "game/Global.h" #include "game/Start.h" +#include "game/localplayer/KeyBindings.h" #include #include @@ -41,6 +42,7 @@ int main(int argc, char *argv[]) QCoreApplication::setApplicationName(QStringLiteral("c-evo-evo")); Cevo::initDirectories(QCoreApplication::arguments()); Cevo::loadConfig(); + Cevo::KeyBindings::init(); Cevo::loadAssets(); QTranslator translator; diff --git a/widget/src/CevoListBoxEx.cpp b/widget/src/CevoListBoxEx.cpp index c1e396c..f4bb025 100644 --- a/widget/src/CevoListBoxEx.cpp +++ b/widget/src/CevoListBoxEx.cpp @@ -57,6 +57,12 @@ void ListBoxExDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt ListBoxEx::ListBoxEx(QWidget *parent) : Dpi::ListBox(parent) { setItemDelegate(new ListBoxExDelegate(this)); + // Black list area without a frame, like the original (Color = clBlack, BorderStyle = bsNone). + QPalette pal = palette(); + pal.setColor(QPalette::Base, Qt::black); + setPalette(pal); + setFrameShape(QFrame::NoFrame); + setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } } // namespace Cevo