Skip to content

Commit

Permalink
Save difficulty name changes onto the worldspawn
Browse files Browse the repository at this point in the history
Setting a new difficulty name with the edit button in the DifficultyDialog now
results in the new name being written into the appropriate worldspawn property,
so the change is persistent and (hopefully) functional in game.
  • Loading branch information
Matthew Mott committed Apr 1, 2020
1 parent 84cbe47 commit adbf1b9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 37 deletions.
8 changes: 6 additions & 2 deletions libs/maplib.h
Expand Up @@ -10,9 +10,13 @@ namespace current
{

/// Convenience method to return the worldspawn entity pointer
inline Entity* getWorldspawn()
inline Entity* getWorldspawn(bool createIfNotFound = false)
{
return Node_getEntity(GlobalMapModule().getWorldspawn());
scene::INodePtr wsNode {
createIfNotFound ? GlobalMapModule().findOrInsertWorldspawn()
: GlobalMapModule().getWorldspawn()
};
return Node_getEntity(wsNode);
}

}
Expand Down
39 changes: 27 additions & 12 deletions plugins/dm.difficulty/DifficultyDialog.cpp
Expand Up @@ -66,15 +66,19 @@ namespace
// Simple dialog for editing a difficulty setting name
class EditNameDialog: public wxDialog
{
wxTextCtrl* _textCtrl = nullptr;

public:

// Construct and initialise with parent and initial text to edit
EditNameDialog(wxWindow* parent, const wxString& initialText)
: wxDialog(parent, wxID_ANY, _("Difficulty name"))
{
wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);

// Add the edit text box
wxTextCtrl* textBox = new wxTextCtrl(this, wxID_ANY, initialText);
mainSizer->Add(textBox, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 12);
_textCtrl= new wxTextCtrl(this, wxID_ANY, initialText);
mainSizer->Add(_textCtrl, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, 12);
mainSizer->AddSpacer(6);

// Add the buttons
Expand All @@ -86,8 +90,14 @@ namespace

// Start with the text selected and focussed to save an extra mouse
// click
textBox->SelectAll();
textBox->SetFocus();
_textCtrl->SelectAll();
_textCtrl->SetFocus();
}

// Get the result of editing
std::string editResult() const
{
return _textCtrl->GetValue().ToStdString();
}
};
}
Expand Down Expand Up @@ -137,11 +147,18 @@ void DifficultyDialog::editCurrentDifficultyName()
{
// Initialise an EditNameDialog with the current tab text as the initial
// name to edit
EditNameDialog dialog(this,
_notebook->GetPageText(_notebook->GetSelection()));
int curDiffLevel = _notebook->GetSelection(); // assume tabs numbered from 0
EditNameDialog dialog(this, _notebook->GetPageText(curDiffLevel));
if (dialog.ShowModal() == wxID_OK)
{
// OK, change the difficulty name
// Successful edit, get the changed name
std::string newName = dialog.editResult();

// Change the difficulty name in the map
_settingsManager.setDifficultyName(curDiffLevel, newName);

// Change the displayed name in the dialog
_notebook->SetPageText(curDiffLevel, newName);
}
}

Expand Down Expand Up @@ -171,11 +188,9 @@ int DifficultyDialog::ShowModal()
// Static command target
void DifficultyDialog::ShowDialog(const cmd::ArgumentList& args)
{
// Construct a new instance, this enters the main loop
DifficultyDialog* editor = new DifficultyDialog;

editor->ShowModal();
editor->Destroy();
// Construct a new instance and enter the main loop
DifficultyDialog editor;
editor.ShowModal();
}

} // namespace ui
43 changes: 39 additions & 4 deletions plugins/dm.difficulty/DifficultySettingsManager.cpp
Expand Up @@ -105,7 +105,8 @@ class ModDifficultyNames
}
}

// Return the translated name for a key, such as "diff0Default"
// Return the translated name for the difficulty level identified by the
// given key (e.g. the translated name for "diff0Default" might be "Easy").
std::string getNameForKey(const std::string& nameKey) const
{
if (_menuEclass)
Expand All @@ -127,6 +128,12 @@ class ModDifficultyNames
}
};

// Return key for a difficulty level name
std::string diffNameKeyForLevel(int level)
{
return "diff" + std::to_string(level) + "default";
}

}

void DifficultySettingsManager::loadDifficultyNames()
Expand All @@ -143,7 +150,7 @@ void DifficultySettingsManager::loadDifficultyNames()
int numLevels = game::current::getValue<int>(GKEY_DIFFICULTY_LEVELS);
for (int i = 0; i < numLevels; i++)
{
std::string nameKey = "diff" + std::to_string(i) + "default";
std::string nameKey = diffNameKeyForLevel(i);

// First, try to find a map-specific name
if (worldspawn)
Expand Down Expand Up @@ -215,12 +222,40 @@ void DifficultySettingsManager::saveSettings()
}
}

std::string DifficultySettingsManager::getDifficultyName(int level) {
if (level < 0 || level >= static_cast<int>(_difficultyNames.size())) {
std::string DifficultySettingsManager::getDifficultyName(int level)
{
if (level < 0 || level >= static_cast<int>(_difficultyNames.size()))
{
return "";
}

return _difficultyNames[level];
}

void DifficultySettingsManager::setDifficultyName(int level, const std::string& name)
{
if (level < 0 || level >= static_cast<int>(_difficultyNames.size()))
{
throw std::logic_error(
"Invalid difficulty level (" + std::to_string(level) + ")"
);
}

_difficultyNames[level] = name;

// Write the new name into the worldspawn
Entity* worldspawn = map::current::getWorldspawn(true);
if (worldspawn)
{
worldspawn->setKeyValue(diffNameKeyForLevel(level), name);
}
else
{
throw std::logic_error(
"DifficultySettingsManager::setDifficultyName():"
" could not find or create worldspawn"
);
}
}

} // namespace difficulty
38 changes: 19 additions & 19 deletions plugins/dm.difficulty/DifficultySettingsManager.h
@@ -1,5 +1,4 @@
#ifndef DIFFICULTY_SETTINGS_MANAGER_H_
#define DIFFICULTY_SETTINGS_MANAGER_H_
#pragma once

#include "DifficultySettings.h"

Expand All @@ -13,31 +12,32 @@ namespace difficulty {
**/
class DifficultySettingsManager
{
// This contains all the settings of all the difficulty levels
std::vector<DifficultySettingsPtr> _settings;
// This contains all the settings of all the difficulty levels
std::vector<DifficultySettingsPtr> _settings;

// The names of the difficulty levels
std::vector<std::string> _difficultyNames;
// The names of the difficulty levels
std::vector<std::string> _difficultyNames;

public:
// Loads all settings from the entityDefs and the currently loaded map.
void loadSettings();
// Loads all settings from the entityDefs and the currently loaded map.
void loadSettings();

// Saves the current working set into one map entity
void saveSettings();
// Saves the current working set into one map entity
void saveSettings();

// Get the settings object for the given difficulty <level>
DifficultySettingsPtr getSettings(int level);
// Get the settings object for the given difficulty <level>
DifficultySettingsPtr getSettings(int level);

// Get the display name of the given difficulty level
std::string getDifficultyName(int level);
// Get the display name of the given difficulty level
std::string getDifficultyName(int level);

/// Set the name for the given difficulty level, and save it into the map
void setDifficultyName(int level, const std::string& name);

private:
void loadDefaultSettings();
void loadMapSettings();
void loadDifficultyNames();
void loadDefaultSettings();
void loadMapSettings();
void loadDifficultyNames();
};

} // namespace difficulty

#endif /* DIFFICULTY_SETTINGS_MANAGER_H_ */

0 comments on commit adbf1b9

Please sign in to comment.