| 1 |
#include "DifficultyPanel.h" |
| 2 |
|
| 3 |
#include "Objective.h" |
| 4 |
|
| 5 |
#include "i18n.h" |
| 6 |
#include <algorithm> |
| 7 |
|
| 8 |
#include "string/convert.h" |
| 9 |
#include "string/split.h" |
| 10 |
|
| 11 |
#include <wx/checkbox.h> |
| 12 |
#include <wx/sizer.h> |
| 13 |
#include <wx/panel.h> |
| 14 |
|
| 15 |
namespace objectives |
| 16 |
{ |
| 17 |
|
| 18 |
DifficultyPanel::DifficultyPanel(wxPanel* container) |
| 19 |
{ |
| 20 |
// Create the main widget |
| 21 |
_allLevels = new wxCheckBox(container, wxID_ANY, _("All Levels")); |
| 22 |
_allLevels->Connect( |
| 23 |
wxEVT_CHECKBOX, wxCommandEventHandler(DifficultyPanel::_onCheckBoxToggle), NULL, this); |
| 24 |
|
| 25 |
// First pack the "All difficulty levels" toggle |
| 26 |
container->GetSizer()->Add(_allLevels, 0, wxALIGN_CENTER_VERTICAL); |
| 27 |
|
| 28 |
// Create the various toggles |
| 29 |
// TODO: Connect to optional Difficulty plugin |
| 30 |
_toggles.push_back(new wxCheckBox(container, wxID_ANY, _("Level 1: Easy"))); |
| 31 |
_toggles.push_back(new wxCheckBox(container, wxID_ANY, _("Level 2: Hard"))); |
| 32 |
_toggles.push_back(new wxCheckBox(container, wxID_ANY, _("Level 3: Expert"))); |
| 33 |
|
| 34 |
// The hbox for the difficulty levels 1..N |
| 35 |
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL); |
| 36 |
|
| 37 |
// Then all the other ones |
| 38 |
for (std::size_t i = 0; i < _toggles.size(); i++) |
| 39 |
{ |
| 40 |
hbox->Add(_toggles[i], 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 6); |
| 41 |
|
| 42 |
// Connect the checkbox |
| 43 |
_toggles[i]->Connect( |
| 44 |
wxEVT_CHECKBOX, wxCommandEventHandler(DifficultyPanel::_onCheckBoxToggle), NULL, this); |
| 45 |
} |
| 46 |
|
| 47 |
container->GetSizer()->Add(hbox, 1, wxALIGN_CENTER_VERTICAL); |
| 48 |
} |
| 49 |
|
| 50 |
void DifficultyPanel::populateFromObjective(const Objective& obj) |
| 51 |
{ |
| 52 |
// De-serialise the difficulty level string |
| 53 |
std::vector<std::string> parts; |
| 54 |
string::split(parts, obj.difficultyLevels, " "); |
| 55 |
|
| 56 |
// Set the "applies to all difficulty" toggle |
| 57 |
_allLevels->SetValue(obj.difficultyLevels.empty()); |
| 58 |
|
| 59 |
// Set all levels to deactivated |
| 60 |
for (std::size_t i = 0; i < _toggles.size(); i++) |
| 61 |
{ |
| 62 |
// See if this level appears in the difficulty string, if yes => toggled |
| 63 |
_toggles[i]->SetValue( |
| 64 |
std::find(parts.begin(), parts.end(), string::to_string(i)) != parts.end() |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
updateSensitivity(); |
| 69 |
} |
| 70 |
|
| 71 |
void DifficultyPanel::updateSensitivity() |
| 72 |
{ |
| 73 |
// The "All levels" toggle has been changed, set the 1..N checkboxes |
| 74 |
// to the inverse of the togglebutton's status |
| 75 |
for (std::size_t i = 0; i < _toggles.size(); i++) |
| 76 |
{ |
| 77 |
_toggles[i]->Enable(!_allLevels->GetValue()); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
void DifficultyPanel::_onCheckBoxToggle(wxCommandEvent& ev) |
| 82 |
{ |
| 83 |
// Update the sensitivity of the other toggles |
| 84 |
|
| 85 |
if (ev.GetEventObject() == _allLevels) |
| 86 |
{ |
| 87 |
// The "All levels" toggle has been changed, set the 1..N checkboxes |
| 88 |
// to the inverse of the togglebutton's status |
| 89 |
for (std::size_t i = 0; i < _toggles.size(); i++) |
| 90 |
{ |
| 91 |
_toggles[i]->Enable(!_allLevels->GetValue()); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void DifficultyPanel::writeToObjective(Objective& obj) |
| 97 |
{ |
| 98 |
// Set the difficulty to "all levels" per default |
| 99 |
obj.difficultyLevels = ""; |
| 100 |
|
| 101 |
if (!_allLevels->GetValue()) |
| 102 |
{ |
| 103 |
// Not applicable to all difficulty levels, form the string |
| 104 |
for (std::size_t i = 0; i < _toggles.size(); i++) |
| 105 |
{ |
| 106 |
// Check each toggle button |
| 107 |
if (_toggles[i]->GetValue()) |
| 108 |
{ |
| 109 |
std::string prefix = (!obj.difficultyLevels.empty()) ? " " : ""; |
| 110 |
obj.difficultyLevels += prefix + string::to_string(i); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
} // namespace objectives |