From 5ad06aac704bb55ff9ecd4b90f2d0d9663dc8079 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 22 Jan 2023 06:17:59 +0200 Subject: [PATCH 46/46] Ruledit: Add terrain details editing dialog Thre only values that one can edit in the initial version are movement cost and defense bonus. See osdn #46587 Signed-off-by: Marko Lindqvist --- common/terrain.c | 1 + common/terrain.h | 1 + meson.build | 2 + tools/ruledit/Makefile.am | 3 + tools/ruledit/edit_terrain.cpp | 110 +++++++++++++++++++++++++++++++++ tools/ruledit/edit_terrain.h | 50 +++++++++++++++ tools/ruledit/tab_terrains.cpp | 44 ++++++++++--- tools/ruledit/tab_terrains.h | 1 + 8 files changed, 204 insertions(+), 8 deletions(-) create mode 100644 tools/ruledit/edit_terrain.cpp create mode 100644 tools/ruledit/edit_terrain.h diff --git a/common/terrain.c b/common/terrain.c index 6b63c366b7..f931e712b3 100644 --- a/common/terrain.c +++ b/common/terrain.c @@ -49,6 +49,7 @@ void terrains_init(void) /* Can't use terrain_by_number here because it does a bounds check. */ civ_terrains[i].item_number = i; civ_terrains[i].ruledit_disabled = FALSE; + civ_terrains[i].ruledit_dlg = NULL; civ_terrains[i].rgb = NULL; civ_terrains[i].animal = NULL; diff --git a/common/terrain.h b/common/terrain.h index 395499cd4e..ca09b6fe88 100644 --- a/common/terrain.h +++ b/common/terrain.h @@ -183,6 +183,7 @@ struct terrain { int item_number; struct name_translation name; bool ruledit_disabled; /* Does not really exist - hole in terrain array */ + void *ruledit_dlg; char graphic_str[MAX_LEN_NAME]; /* add tile_ prefix */ char graphic_alt[MAX_LEN_NAME]; diff --git a/meson.build b/meson.build index 1767e0dc43..d00cad3b7d 100644 --- a/meson.build +++ b/meson.build @@ -3504,6 +3504,7 @@ mocced_ruledit = qt_mod.preprocess( moc_headers: [ 'tools/ruledit/conversion_log.h', 'tools/ruledit/edit_impr.h', + 'tools/ruledit/edit_terrain.h', 'tools/ruledit/edit_utype.h', 'tools/ruledit/effect_edit.h', 'tools/ruledit/req_edit.h', @@ -3526,6 +3527,7 @@ mocced_ruledit = qt_mod.preprocess( executable('freeciv-ruledit', 'tools/ruledit/conversion_log.cpp', 'tools/ruledit/edit_impr.cpp', + 'tools/ruledit/edit_terrain.cpp', 'tools/ruledit/edit_utype.cpp', 'tools/ruledit/effect_edit.cpp', 'tools/ruledit/req_edit.cpp', diff --git a/tools/ruledit/Makefile.am b/tools/ruledit/Makefile.am index 44d0288c1e..50364990bf 100644 --- a/tools/ruledit/Makefile.am +++ b/tools/ruledit/Makefile.am @@ -26,6 +26,7 @@ freeciv_ruledit_CXXFLAGS = $(ruledit_cxxflags) MOC_FILES = \ meta_conversion_log.cpp \ meta_edit_impr.cpp \ + meta_edit_terrain.cpp \ meta_edit_utype.cpp \ meta_effect_edit.cpp \ meta_req_edit.cpp \ @@ -49,6 +50,8 @@ freeciv_ruledit_SOURCES = \ conversion_log.h \ edit_impr.cpp \ edit_impr.h \ + edit_terrain.cpp \ + edit_terrain.h \ edit_utype.cpp \ edit_utype.h \ effect_edit.cpp \ diff --git a/tools/ruledit/edit_terrain.cpp b/tools/ruledit/edit_terrain.cpp new file mode 100644 index 0000000000..52da924d94 --- /dev/null +++ b/tools/ruledit/edit_terrain.cpp @@ -0,0 +1,110 @@ +/*********************************************************************** + Freeciv - Copyright (C) 2023 The Freeciv Team + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +***********************************************************************/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +// Qt +#include +#include +#include + +// common +#include "terrain.h" + +// ruledit +#include "ruledit.h" +#include "ruledit_qt.h" +#include "tab_tech.h" + +#include "edit_terrain.h" + +/**********************************************************************//** + Setup edit_terrain object +**************************************************************************/ +edit_terrain::edit_terrain(ruledit_gui *ui_in, struct terrain *ter_in) : QDialog() +{ + QVBoxLayout *main_layout = new QVBoxLayout(this); + QGridLayout *ter_layout = new QGridLayout(); + QLabel *label; + int row = 0; + + ui = ui_in; + ter = ter_in; + + setWindowTitle(QString::fromUtf8(terrain_rule_name(ter))); + + label = new QLabel(QString::fromUtf8(R__("Move Cost"))); + label->setParent(this); + + mcost = new QSpinBox(this); + mcost->setRange(0, 100); + connect(mcost, SIGNAL(valueChanged(int)), this, SLOT(set_mcost_value(int))); + + ter_layout->addWidget(label, row, 0); + ter_layout->addWidget(mcost, row++, 2); + + label = new QLabel(QString::fromUtf8(R__("Defense Bonus %"))); + label->setParent(this); + + defense = new QSpinBox(this); + defense->setRange(0, 1000); + connect(defense, SIGNAL(valueChanged(int)), this, SLOT(set_defense_value(int))); + + ter_layout->addWidget(label, row, 0); + ter_layout->addWidget(defense, row++, 2); + + refresh(); + + main_layout->addLayout(ter_layout); + + setLayout(main_layout); +} + +/**********************************************************************//** + User is closing dialog. +**************************************************************************/ +void edit_terrain::closeEvent(QCloseEvent *cevent) +{ + ter->ruledit_dlg = nullptr; +} + +/**********************************************************************//** + Refresh the information. +**************************************************************************/ +void edit_terrain::refresh() +{ + mcost->setValue(ter->movement_cost); + defense->setValue(ter->defense_bonus); +} + +/**********************************************************************//** + Read movement cost value from spinbox +**************************************************************************/ +void edit_terrain::set_mcost_value(int value) +{ + ter->movement_cost = value; + + refresh(); +} + +/**********************************************************************//** + Read defense value from spinbox +**************************************************************************/ +void edit_terrain::set_defense_value(int value) +{ + ter->defense_bonus = value; + + refresh(); +} diff --git a/tools/ruledit/edit_terrain.h b/tools/ruledit/edit_terrain.h new file mode 100644 index 0000000000..08fdd83e81 --- /dev/null +++ b/tools/ruledit/edit_terrain.h @@ -0,0 +1,50 @@ +/*********************************************************************** + Freeciv - Copyright (C) 2023 The Freeciv Team + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +***********************************************************************/ + +#ifndef FC__EDIT_TERRAIN_H +#define FC__EDIT_TERRAIN_H + +#ifdef HAVE_CONFIG_H +#include +#endif + +// Qt +#include + +class QSpinBox; + +class ruledit_gui; + +class edit_terrain : public QDialog +{ + Q_OBJECT + + public: + explicit edit_terrain(ruledit_gui *ui_in, struct terrain *ter_in); + void refresh(); + + private: + ruledit_gui *ui; + struct terrain *ter; + QSpinBox *mcost; + QSpinBox *defense; + + protected: + void closeEvent(QCloseEvent *cevent); + + private slots: + void set_mcost_value(int value); + void set_defense_value(int value); +}; + +#endif // FC__EDIT_TERRAIN_H diff --git a/tools/ruledit/tab_terrains.cpp b/tools/ruledit/tab_terrains.cpp index 9a32be881a..cd026feb96 100644 --- a/tools/ruledit/tab_terrains.cpp +++ b/tools/ruledit/tab_terrains.cpp @@ -33,6 +33,7 @@ #include "terrain.h" // ruledit +#include "edit_terrain.h" #include "req_edit.h" #include "ruledit.h" #include "ruledit_qt.h" @@ -51,6 +52,8 @@ tab_terrains::tab_terrains(ruledit_gui *ui_in) : QWidget() QPushButton *effects_button; QPushButton *add_button; QPushButton *delete_button; + QPushButton *edit_button; + int row = 0; ui = ui_in; selected = 0; @@ -67,8 +70,8 @@ tab_terrains::tab_terrains(ruledit_gui *ui_in) : QWidget() rname = new QLineEdit(this); rname->setText(R__("None")); connect(rname, SIGNAL(returnPressed()), this, SLOT(name_given())); - terrains_layout->addWidget(label, 0, 0); - terrains_layout->addWidget(rname, 0, 2); + terrains_layout->addWidget(label, row, 0); + terrains_layout->addWidget(rname, row++, 2); label = new QLabel(QString::fromUtf8(R__("Name"))); label->setParent(this); @@ -77,22 +80,26 @@ tab_terrains::tab_terrains(ruledit_gui *ui_in) : QWidget() name = new QLineEdit(this); name->setText(R__("None")); connect(name, SIGNAL(returnPressed()), this, SLOT(name_given())); - terrains_layout->addWidget(label, 1, 0); - terrains_layout->addWidget(same_name, 1, 1); - terrains_layout->addWidget(name, 1, 2); + terrains_layout->addWidget(label, row, 0); + terrains_layout->addWidget(same_name, row, 1); + terrains_layout->addWidget(name, row++, 2); + + edit_button = new QPushButton(QString::fromUtf8(R__("Edit Values")), this); + connect(edit_button, SIGNAL(pressed()), this, SLOT(edit_now())); + terrains_layout->addWidget(edit_button, row++, 2); effects_button = new QPushButton(QString::fromUtf8(R__("Effects")), this); connect(effects_button, SIGNAL(pressed()), this, SLOT(edit_effects())); - terrains_layout->addWidget(effects_button, 2, 2); + terrains_layout->addWidget(effects_button, row++, 2); add_button = new QPushButton(QString::fromUtf8(R__("Add Terrain")), this); connect(add_button, SIGNAL(pressed()), this, SLOT(add_now())); - terrains_layout->addWidget(add_button, 3, 0); + terrains_layout->addWidget(add_button, row, 0); show_experimental(add_button); delete_button = new QPushButton(QString::fromUtf8(R__("Remove this Terrain")), this); connect(delete_button, SIGNAL(pressed()), this, SLOT(delete_now())); - terrains_layout->addWidget(delete_button, 3, 2); + terrains_layout->addWidget(delete_button, row++, 2); show_experimental(delete_button); refresh(); @@ -210,6 +217,10 @@ void tab_terrains::delete_now() selected->ruledit_disabled = true; + if (selected->ruledit_dlg != nullptr) { + ((edit_terrain *)selected->ruledit_dlg)->done(0); + } + refresh(); update_terrain_info(nullptr); } @@ -292,3 +303,20 @@ void tab_terrains::edit_effects() &uni, EFMC_NORMAL); } } + +/**********************************************************************//** + User requested terrain edit dialog +**************************************************************************/ +void tab_terrains::edit_now() +{ + if (selected != nullptr) { + if (selected->ruledit_dlg == nullptr) { + edit_terrain *edit = new edit_terrain(ui, selected); + + edit->show(); + selected->ruledit_dlg = edit; + } else { + ((edit_terrain *)selected->ruledit_dlg)->raise(); + } + } +} diff --git a/tools/ruledit/tab_terrains.h b/tools/ruledit/tab_terrains.h index aa4c67f89d..8d0cefc4bd 100644 --- a/tools/ruledit/tab_terrains.h +++ b/tools/ruledit/tab_terrains.h @@ -52,6 +52,7 @@ class tab_terrains : public QWidget void select_terrain(); void add_now(); void delete_now(); + void edit_now(); void same_name_toggle(bool checked); void edit_effects(); }; -- 2.39.0