Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tileset options #2251

Merged
merged 14 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ target_sources(
themes.cpp
themes_common.cpp
tileset_debugger.cpp
tileset_options.cpp
tooltips.cpp
top_bar.cpp
tradecalculation.cpp
Expand Down
27 changes: 27 additions & 0 deletions client/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
#include <QApplication>
#include <QFileDialog>
#include <QMainWindow>
#include <QMenuBar>
#include <QMessageBox>
#include <QStandardPaths>
#include <QVBoxLayout>

// utility
#include "fcintl.h"
// common
Expand All @@ -29,6 +31,7 @@
#include "map.h"
#include "multipliers.h"
#include "road.h"
#include "tileset_options.h"
#include "unit.h"
// client
#include "audio/audio.h"
Expand Down Expand Up @@ -577,6 +580,10 @@ void mr_menu::setup_menus()
connect(act, &QAction::triggered, this, &mr_menu::tileset_custom_load);
act = menu->addAction(_("Add Modpacks"));
connect(act, &QAction::triggered, this, &mr_menu::add_modpacks);
tileset_options = menu->addAction(_("Tileset Options"));
connect(tileset_options, &QAction::triggered, this,
&mr_menu::show_tileset_options);
tileset_options->setEnabled(tileset_has_options(tileset));
act = menu->addAction(_("Tileset Debugger"));
connect(act, &QAction::triggered, queen()->mapview_wdg,
&map_view::show_debugger);
Expand Down Expand Up @@ -2662,6 +2669,15 @@ void mr_menu::tileset_custom_load()
dialog->show();
}

/**
* Slot for loading modpack installer
*/
void mr_menu::show_tileset_options()
{
auto dialog = new freeciv::tileset_options_dialog(tileset, this);
dialog->show();
}

/**
* Slot for loading modpack installer
*/
Expand Down Expand Up @@ -2735,6 +2751,17 @@ void mr_menu::slot_build_base(int id)
}
}

/**
* Reimplemented virtual function.
*/
bool mr_menu::event(QEvent *event)
{
if (event->type() == TilesetChanged) {
tileset_options->setEnabled(tileset_has_options(tileset));
}
return QMenuBar::event(event);
}

/**
Invoke dialog with interface (local) options
*/
Expand Down
6 changes: 6 additions & 0 deletions client/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class mr_menu : public QMenuBar {
void update_bases_menu();
void set_tile_for_order(struct tile *ptile);
bool shortcut_exists(const fc_shortcut &fcs, QString &where);
QAction *tileset_options = nullptr;
QAction *minimap_status = nullptr;
QAction *scale_fonts_status = nullptr;
QAction *lock_status = nullptr;
Expand All @@ -191,6 +192,10 @@ class mr_menu : public QMenuBar {
bool delayed_order = false;
bool quick_airlifting = false;
Unit_type_id airlift_type_id = 0;

protected:
bool event(QEvent *event) override;

private slots:
// game menu
void local_options();
Expand All @@ -204,6 +209,7 @@ private slots:
void tileset_custom_load();
void load_new_tileset();
void add_modpacks();
void show_tileset_options();
void back_to_menu();
void quit_game();

Expand Down
93 changes: 93 additions & 0 deletions client/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <fc_config.h>

#include <cstring>
#include <qglobal.h>
#include <sys/stat.h>

// Qt
Expand Down Expand Up @@ -65,6 +66,8 @@
#include "views/view_map_common.h"
#include "views/view_nations_data.h"

const char *const TILESET_OPTIONS_PREFIX = "tileset_";

typedef QHash<QString, QString> optionsHash;
typedef QHash<QString, intptr_t> dialOptionsHash;

Expand Down Expand Up @@ -3870,6 +3873,91 @@ static const char *get_last_option_file_name(bool *allow_digital_boolean)

Q_GLOBAL_STATIC(optionsHash, settable_options)

/**
* Migrate players using cimpletoon/toonhex to amplio2/hexemplio with the
* cimpletoon option enabled.
*
* \since 3.1
*/
static void
tileset_options_migrate_cimpletoon(struct client_options *options)
{
for (auto &name : {options->default_tileset_iso_name,
options->default_tileset_isohex_name,
options->default_tileset_square_name}) {
if (name == QStringLiteral("cimpletoon")) {
fc_strlcpy(name, "amplio2", sizeof(name));
options->tileset_options[QStringLiteral("amplio2")]
[QStringLiteral("cimpletoon")] = true;
} else if (name == QStringLiteral("toonhex")) {
fc_strlcpy(name, "hexemplio", sizeof(name));
options->tileset_options[QStringLiteral("hexemplio")]
[QStringLiteral("cimpletoon")] = true;
}
}
}

/**
* Load tileset options.
*
* Every tileset has its own section called tileset_xxx. The options are
* saved as name=value pairs.
* \see tileset_options_save
*/
static void tileset_options_load(struct section_file *sf,
struct client_options *options)
{
// Gather all tileset_xxx sections
auto sections =
secfile_sections_by_name_prefix(sf, TILESET_OPTIONS_PREFIX);
if (!sections) {
return;
}

section_list_iterate(sections, psection)
{
// Extract the tileset name from the name of the section.
auto tileset_name =
section_name(psection) + strlen(TILESET_OPTIONS_PREFIX);

// Get all values from the section and fill a map with them.
auto entries = section_entries(psection);
auto settings = std::map<QString, bool>();
entry_list_iterate(entries, pentry)
{
bool value = true;
if (entry_bool_get(pentry, &value)) {
settings[entry_name(pentry)] = value;
} else {
// Ignore options we can't convert to a bool, but warn the user.
qWarning("Could not load option %s for tileset %s",
entry_name(pentry), tileset_name);
}
}
entry_list_iterate_end;

// Store the loaded options for later use.
options->tileset_options[tileset_name] = settings;
}
section_list_iterate_end;
}

/**
* Save tileset options.
*
* \see tileset_options_load
*/
static void tileset_options_save(struct section_file *sf,
const struct client_options *options)
{
for (const auto &[tileset, settings] : options->tileset_options) {
for (const auto &[name, value] : settings) {
secfile_insert_bool(sf, value, "%s%s.%s", TILESET_OPTIONS_PREFIX,
qUtf8Printable(tileset), qUtf8Printable(name));
}
}
}

/**
Load the server options.
*/
Expand Down Expand Up @@ -4381,6 +4469,8 @@ void options_load()
create_default_cma_presets();
}

tileset_options_load(sf, gui_options);
tileset_options_migrate_cimpletoon(gui_options);
settable_options_load(sf);
global_worklists_load(sf);

Expand Down Expand Up @@ -4435,6 +4525,9 @@ void options_save(option_save_log_callback log_cb)
message_options_save(sf, "client");
options_dialogs_save(sf);

// Tileset options
tileset_options_save(sf, gui_options);

// server settings
save_cma_presets(sf);
settable_options_save(sf);
Expand Down
4 changes: 4 additions & 0 deletions client/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ struct client_options {
bool gui_qt_show_titlebar = true;

struct overview overview = {};

/// Saved tileset options. The first index is the tileset name, the second
/// is the option name.
std::map<QString, std::map<QString, bool>> tileset_options;
};

extern client_options *gui_options;
Expand Down
Loading
Loading