23 #include "gamerulespanel.h" 24 #include "ui_gamerulespanel.h" 27 #include "plugins/engineplugin.h" 28 #include "serverapi/gamecreateparams.h" 34 DClass<GameRulesPanel> :
public Ui::GameRulesPanel
45 bool anythingAvailable;
47 GameCreateParams::HostMode hostMode;
49 QList<GameCVar> gameModifiers;
50 QList<GameLimitWidget *> limitWidgets;
51 QMap<QString, QMap<QString, int> > memorizedLimits;
60 d->anythingAvailable =
true;
62 d->hostMode = GameCreateParams::Host;
65 GameRulesPanel::~GameRulesPanel()
67 qDeleteAll(d->limitWidgets);
70 void GameRulesPanel::applyModeToUi()
72 bool engineAllowsSlotsLimits =
false;
73 bool engineHasModifiers =
false;
74 bool engineHasMapList =
false;
76 if (d->engine !=
nullptr)
78 setupModifiers(d->engine);
79 d->mapListPanel->setupForEngine(d->engine);
80 setupLimitWidgets(d->engine, d->gameMode);
82 auto ecfg = d->engine->data();
84 d->labelMaxClients->setVisible(ecfg->allowsClientSlots);
85 d->spinMaxClients->setVisible(ecfg->allowsClientSlots);
86 d->labelMaxPlayers->setVisible(ecfg->allowsPlayerSlots);
87 d->spinMaxPlayers->setVisible(ecfg->allowsPlayerSlots);
91 d->hostingLimitsLayout->removeWidget(d->labelMaxClients);
92 d->hostingLimitsLayout->removeWidget(d->spinMaxClients);
93 d->hostingLimitsLayout->removeWidget(d->labelMaxPlayers);
94 d->hostingLimitsLayout->removeWidget(d->spinMaxPlayers);
96 if (ecfg->allowsClientSlots)
97 d->hostingLimitsLayout->addRow(d->labelMaxClients, d->spinMaxClients);
98 if (ecfg->allowsPlayerSlots)
99 d->hostingLimitsLayout->addRow(d->labelMaxPlayers, d->spinMaxPlayers);
101 engineAllowsSlotsLimits = ecfg->allowsClientSlots || ecfg->allowsPlayerSlots;
102 engineHasMapList = ecfg->hasMapList;
103 engineHasModifiers = !d->engine->gameModifiers().isEmpty();
106 d->gameLimitsBox->setVisible(!d->limitWidgets.isEmpty());
108 bool slotLimitsBoxAvailable = d->hostMode == GameCreateParams::Host && engineAllowsSlotsLimits;
109 d->hostLimitsBox->setVisible(slotLimitsBoxAvailable);
110 d->mapListBox->setVisible(engineHasMapList);
112 d->anythingAvailable = !d->limitWidgets.isEmpty()
113 || engineHasModifiers
115 || slotLimitsBoxAvailable;
120 params.setMaxClients(d->spinMaxClients->value());
121 params.setMaxPlayers(d->spinMaxPlayers->value());
123 fillInLimits(params);
124 fillInModifiers(params);
126 d->mapListPanel->fillInParams(params);
133 p->limit.setValue(p->spinBox->value());
134 params.
cvars() << p->limit;
140 int modIndex = d->cboModifier->currentIndex();
144 d->gameModifiers[modIndex].setValue(1);
145 params.
cvars() << d->gameModifiers[modIndex];
149 bool GameRulesPanel::isAnythingAvailable()
const 151 return d->anythingAvailable;
156 return d->mapListPanel;
159 void GameRulesPanel::memorizeLimits()
161 if (d->engine !=
nullptr)
163 if (!d->memorizedLimits.contains(d->engine->nameCanonical()))
164 d->memorizedLimits[d->engine->nameCanonical()] = QMap<QString, int>();
165 QMap<QString, int> &limits = d->memorizedLimits[d->engine->nameCanonical()];
168 limits[limitWidget->limit.command()] = limitWidget->spinBox->value();
173 void GameRulesPanel::loadMemorizedLimits(
const EnginePlugin *engine)
177 QMap<QString, int> &limits = d->memorizedLimits[engine->
nameCanonical()];
180 if (limits.contains(limitWidget->limit.command()))
181 limitWidget->spinBox->setValue(limits[limitWidget->limit.command()]);
186 void GameRulesPanel::loadConfig(
Ini &config)
190 d->cboModifier->setCurrentIndex(section[
"modifier"]);
191 d->spinMaxClients->
setValue(section[
"maxClients"]);
192 d->spinMaxPlayers->setValue(section[
"maxPlayers"]);
195 widget->spinBox->setValue(section[widget->limit.command()]);
198 d->mapListPanel->loadConfig(config);
201 void GameRulesPanel::saveConfig(
Ini &config)
205 section[
"modifier"] = d->cboModifier->currentIndex();
206 section[
"maxClients"] = d->spinMaxClients->
value();
207 section[
"maxPlayers"] = d->spinMaxPlayers->
value();
210 section[widget->limit.command()] = widget->spinBox->value();
213 d->mapListPanel->saveConfig(config);
218 d->mapListPanel->setCreateServerDialog(dialog);
224 d->gameMode = gameMode;
228 void GameRulesPanel::setupForHostMode(GameCreateParams::HostMode hostMode)
230 d->hostMode = hostMode;
234 void GameRulesPanel::setupModifiers(
const EnginePlugin *engine)
236 QString selectedModifier = d->cboModifier->currentText();
237 d->cboModifier->clear();
238 d->gameModifiers.clear();
242 if (!modifiers.isEmpty())
244 d->modifierBox->show();
246 d->cboModifier->addItem(tr(
"< NONE >"));
248 for (
const GameCVar &cvar : modifiers)
250 d->cboModifier->addItem(cvar.name());
251 d->gameModifiers << cvar;
253 if (!selectedModifier.isEmpty())
255 int modifierIndex = d->cboModifier->findText(selectedModifier);
256 if (modifierIndex >= 0)
257 d->cboModifier->setCurrentIndex(modifierIndex);
262 d->modifierBox->hide();
266 void GameRulesPanel::removeLimitWidgets()
270 delete widget->label;
271 delete widget->spinBox;
275 d->limitWidgets.clear();
281 removeLimitWidgets();
282 QList<GameCVar> limits = engine->
limits(gameMode);
284 for (
const GameCVar &limit : limits)
286 QLabel *label =
new QLabel(
this);
287 label->setText(limit.
name());
288 auto spinBox =
new QSpinBox(
this);
289 spinBox->setMaximum(INT_MAX);
290 spinBox->setMinimum(INT_MIN);
291 spinBox->setCorrectionMode(QAbstractSpinBox::CorrectToNearestValue);
292 spinBox->setValue(limit.
value().toInt());
294 d->limitsLayout->addRow(label, spinBox);
298 glw->spinBox = spinBox;
300 d->limitWidgets << glw;
303 loadMemorizedLimits(engine);
const QString & name() const
Nice name to display to user in Create Game dialog and in other widgets.
Game parametrization data used when creating new games.
const QVariant & value() const
Passed as the second argument, following command().
QVariant value(const QString &key) const
Retrieves a variable directly; omits the IniVariable system.
Dialog window allowing user to create a game.
Game mode representation.
void setValue(const QString &key, const QVariant &value)
Sets a variable directly. Omits the IniVariable system.
virtual QList< GameCVar > gameModifiers() const
Modifier that apply to all game modes (ex. instagib).
QList< GameCVar > & cvars()
Contents of this list will be passed as "+consoleCommand value" to the command line.
INI section representation.
IniSection section(const QString &name)
Access configuration file section.
virtual QList< GameCVar > limits(const GameMode &mode) const
Returns a list of limits (like fraglimit) supported by passed gamemode.
QString nameCanonical() const
Either specified explicitly by plugin or derived from the actual plugin name.
A general game setting or variable (like fraglimit).