Skip to content

Commit

Permalink
Minor refactor of DifficultyEditor construction
Browse files Browse the repository at this point in the history
Merge the two separate loops into a single loop which constructs each
DifficultyEditor, adds it to the internal list and then inserts it as a
choicebook page. This means that DifficultyEditor no longer needs to store and
return its own label since the text is already known in the population loop.
  • Loading branch information
Matthew Mott committed Mar 18, 2020
1 parent 2a1fb1b commit 6b02dc4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 94 deletions.
32 changes: 12 additions & 20 deletions plugins/dm.difficulty/DifficultyDialog.cpp
Expand Up @@ -44,29 +44,21 @@ void DifficultyDialog::createDifficultyEditors()
{
// Acquire the settings object
difficulty::DifficultySettingsPtr settings = _settingsManager.getSettings(i);

if (settings != NULL)
if (settings)
{
_editors.push_back(
DifficultyEditorPtr(new DifficultyEditor(
_notebook, _settingsManager.getDifficultyName(i), settings)
)
);
// Construct the editor for this difficulty level and add it to our
// internal list of editors
std::string diffName = _settingsManager.getDifficultyName(i);
auto editor = std::make_shared<DifficultyEditor>(_notebook,
settings);
_editors.push_back(editor);

// Insert the editor's widget as a new page in the choicebook
wxWindow* editorWidget = editor->getWidget();
editorWidget->Reparent(_notebook);
_notebook->AddPage(editorWidget, diffName, false);
}
}

// Pack the editors into the notebook
for (std::size_t i = 0; i < _editors.size(); i++)
{
DifficultyEditor& editor = *_editors[i];

wxWindow* editorWidget = editor.getEditor();

// Reparent the DifficultyEditor's widget to the book control and then
// add it as a new page.
editorWidget->Reparent(_notebook);
_notebook->AddPage(editorWidget, editor.getNotebookLabel(), false);
}
}

void DifficultyDialog::populateWindow()
Expand Down
36 changes: 15 additions & 21 deletions plugins/dm.difficulty/DifficultyEditor.cpp
Expand Up @@ -19,21 +19,20 @@
namespace ui
{

DifficultyEditor::DifficultyEditor(wxWindow* parent, const std::string& label,
const difficulty::DifficultySettingsPtr& settings) :
_settings(settings),
_label(label),
_settingsView(nullptr),
_classCombo(nullptr),
_spawnArgEntry(nullptr),
_argumentEntry(nullptr),
_appTypeCombo(nullptr),
_saveSettingButton(nullptr),
_deleteSettingButton(nullptr),
_createSettingButton(nullptr),
_refreshButton(nullptr),
_noteText(nullptr),
_updateActive(false)
DifficultyEditor::DifficultyEditor(wxWindow* parent,
const difficulty::DifficultySettingsPtr& settings)
: _settings(settings),
_settingsView(nullptr),
_classCombo(nullptr),
_spawnArgEntry(nullptr),
_argumentEntry(nullptr),
_appTypeCombo(nullptr),
_saveSettingButton(nullptr),
_deleteSettingButton(nullptr),
_createSettingButton(nullptr),
_refreshButton(nullptr),
_noteText(nullptr),
_updateActive(false)
{
// The actual editor pane
_editor = loadNamedPanel(parent, "DifficultyEditorMainPanel");
Expand All @@ -44,16 +43,11 @@ DifficultyEditor::DifficultyEditor(wxWindow* parent, const std::string& label,
updateEditorWidgets();
}

wxWindow* DifficultyEditor::getEditor()
wxWindow* DifficultyEditor::getWidget()
{
return _editor;
}

std::string DifficultyEditor::getNotebookLabel()
{
return _label;
}

void DifficultyEditor::populateWindow()
{
wxPanel* viewPanel = findNamedObject<wxPanel>(_editor, "DifficultyEditorTreeViewPanel");
Expand Down
102 changes: 49 additions & 53 deletions plugins/dm.difficulty/DifficultyEditor.h
Expand Up @@ -24,80 +24,76 @@ namespace ui
* data is stored in a DifficultySettings instance.
*/
class DifficultyEditor :
public wxEvtHandler,
private wxutil::XmlResourceBasedWidget
public wxEvtHandler,
private wxutil::XmlResourceBasedWidget
{
private:
// The actual settings we're working with
difficulty::DifficultySettingsPtr _settings;
// The actual settings we're working with
difficulty::DifficultySettingsPtr _settings;

// GtkNotebook-related widgets
wxPanel* _editor;
std::string _label; // the actual label
// GtkNotebook-related widgets
wxPanel* _editor;

wxutil::TreeView* _settingsView;
wxutil::TreeView* _settingsView;

// The classname dropdown entry field
wxComboBox* _classCombo;
wxTextCtrl* _spawnArgEntry;
wxTextCtrl* _argumentEntry;
wxChoice* _appTypeCombo;
// The classname dropdown entry field
wxComboBox* _classCombo;
wxTextCtrl* _spawnArgEntry;
wxTextCtrl* _argumentEntry;
wxChoice* _appTypeCombo;

wxButton* _saveSettingButton;
wxButton* _deleteSettingButton;
wxButton* _createSettingButton;
wxButton* _refreshButton;
wxButton* _saveSettingButton;
wxButton* _deleteSettingButton;
wxButton* _createSettingButton;
wxButton* _refreshButton;

// A label containing notes to the user
wxStaticText* _noteText;
// A label containing notes to the user
wxStaticText* _noteText;

// Mutex for avoiding loopbacks
bool _updateActive;
// Mutex for avoiding loopbacks
bool _updateActive;

public:
/**
* greebo: Pass the label string and the difficulty settings object to the
* constructor. The DifficultySettings should be populated first.
*/
DifficultyEditor(wxWindow* parent, const std::string& label, const difficulty::DifficultySettingsPtr& settings);

// Returns the actual editor widget (contains all controls and views)
wxWindow* getEditor();
/**
* greebo: Pass the label string and the difficulty settings object to the
* constructor. The DifficultySettings should be populated first.
*/
DifficultyEditor(wxWindow* parent,
const difficulty::DifficultySettingsPtr& settings);

// Returns the label for packing into a GtkNotebook tab.
std::string getNotebookLabel();
// Returns the actual editor widget (contains all controls and views)
wxWindow* getWidget();

private:
// Creates the widgets
void populateWindow();
// Creates the widgets
void populateWindow();

// Returns the ID of the selected setting (or -1) if no valid setting is selected
int getSelectedSettingId();
// Returns the ID of the selected setting (or -1) if no valid setting is selected
int getSelectedSettingId();

// Loads the data from the treeview selection into the editor widgets
void updateEditorWidgets();
// Loads the data from the treeview selection into the editor widgets
void updateEditorWidgets();

// Prepares the widgets for addition of a new setting
void createSetting();
// Prepares the widgets for addition of a new setting
void createSetting();

// Saves the setting data from the widgets to the DifficultySettings object
void saveSetting();
// Saves the setting data from the widgets to the DifficultySettings object
void saveSetting();

// Removes the setting selected in the treeview
void deleteSetting();
// Removes the setting selected in the treeview
void deleteSetting();

// Highlights the setting (according to the given <id>) in the treeview
void selectSettingById(int id);
// Highlights the setting (according to the given <id>) in the treeview
void selectSettingById(int id);

// Callback for treeview selection changes
void onSettingSelectionChange(wxDataViewEvent& ev);
// Callback for treeview selection changes
void onSettingSelectionChange(wxDataViewEvent& ev);

void onSettingCreate(wxCommandEvent& ev);
void onSettingSave(wxCommandEvent& ev);
void onSettingDelete(wxCommandEvent& ev);
void onRefresh(wxCommandEvent& ev);
void onSettingCreate(wxCommandEvent& ev);
void onSettingSave(wxCommandEvent& ev);
void onSettingDelete(wxCommandEvent& ev);
void onRefresh(wxCommandEvent& ev);

void onAppTypeChange(wxCommandEvent& ev);
void onAppTypeChange(wxCommandEvent& ev);
};
typedef std::shared_ptr<DifficultyEditor> DifficultyEditorPtr;

Expand Down

0 comments on commit 6b02dc4

Please sign in to comment.