| 1 |
#include "MissionReadmeDialog.h" |
| 2 |
|
| 3 |
#include "igui.h" |
| 4 |
#include "itextstream.h" |
| 5 |
#include "i18n.h" |
| 6 |
#include <sigc++/functors/mem_fun.h> |
| 7 |
|
| 8 |
#include <functional> |
| 9 |
#include <fmt/format.h> |
| 10 |
#include <wx/button.h> |
| 11 |
#include <wx/textctrl.h> |
| 12 |
#include <wx/splitter.h> |
| 13 |
#include "wxutil/preview/GuiView.h" |
| 14 |
#include "wxutil/dialog/MessageBox.h" |
| 15 |
|
| 16 |
#include "MissionInfoGuiView.h" |
| 17 |
|
| 18 |
namespace ui |
| 19 |
{ |
| 20 |
|
| 21 |
namespace |
| 22 |
{ |
| 23 |
const char* const WINDOW_TITLE = N_("Mission Readme Editor (readme.txt)"); |
| 24 |
} |
| 25 |
|
| 26 |
MissionReadmeDialog::MissionReadmeDialog(wxWindow* parent) : |
| 27 |
DialogBase(_(WINDOW_TITLE), parent), |
| 28 |
_updateInProgress(false) |
| 29 |
{ |
| 30 |
populateWindow(); |
| 31 |
|
| 32 |
try |
| 33 |
{ |
| 34 |
_readmeFile = map::ReadmeTxt::LoadForCurrentMod(); |
| 35 |
} |
| 36 |
catch (map::ReadmeTxt::ParseException& ex) |
| 37 |
{ |
| 38 |
rError() << "Failed to parse readme.txt: " << ex.what() << std::endl; |
| 39 |
|
| 40 |
wxutil::Messagebox::ShowError( |
| 41 |
fmt::format(_("Failed to parse readme.txt:\n{0}"), ex.what()), this); |
| 42 |
|
| 43 |
// Reset the file to defaults |
| 44 |
_readmeFile.reset(); |
| 45 |
} |
| 46 |
|
| 47 |
_guiView->setGui(GlobalGuiManager().getGui("guis/mainmenu.gui")); |
| 48 |
_guiView->setMissionInfoFile(_readmeFile); |
| 49 |
|
| 50 |
updateValuesFromReadmeFile(); |
| 51 |
} |
| 52 |
|
| 53 |
void MissionReadmeDialog::updateValuesFromReadmeFile() |
| 54 |
{ |
| 55 |
assert(_readmeFile); // this should be non-NULL at all times |
| 56 |
|
| 57 |
if (!_readmeFile) return; |
| 58 |
|
| 59 |
_updateInProgress = true; |
| 60 |
|
| 61 |
findNamedObject<wxTextCtrl>(this, "MissionInfoReadmeContentsEntry")->SetValue(_readmeFile->getContents()); |
| 62 |
findNamedObject<wxStaticText>(this, "MissionInfoReadmeOutputPath")->SetLabelText(_readmeFile->getFullOutputPath()); |
| 63 |
|
| 64 |
_guiView->updateGuiState(); |
| 65 |
|
| 66 |
_updateInProgress = false; |
| 67 |
} |
| 68 |
|
| 69 |
void MissionReadmeDialog::populateWindow() |
| 70 |
{ |
| 71 |
SetSizer(new wxBoxSizer(wxVERTICAL)); |
| 72 |
|
| 73 |
wxPanel* panel = loadNamedPanel(this, "MissionInfoReadmeDialogMainPanel"); |
| 74 |
GetSizer()->Add(panel, 1, wxEXPAND); |
| 75 |
|
| 76 |
// Add the preview widget |
| 77 |
wxPanel* previewPanel = findNamedObject<wxPanel>(this, "MissionInfoReadmeDialogPreviewPanel"); |
| 78 |
|
| 79 |
_guiView = new ReadmeTxtGuiView(previewPanel); |
| 80 |
previewPanel->GetSizer()->Add(_guiView, 1, wxEXPAND); |
| 81 |
|
| 82 |
makeLabelBold(this, "MissionReadmeLabel"); |
| 83 |
|
| 84 |
wxButton* saveButton = findNamedObject<wxButton>(this, "MissionInfoReadmeDialogSaveButton"); |
| 85 |
wxButton* cancelButton = findNamedObject<wxButton>(this, "MissionInfoReadmeDialogCancelButton"); |
| 86 |
|
| 87 |
saveButton->Bind(wxEVT_BUTTON, sigc::mem_fun(this, &MissionReadmeDialog::onSave)); |
| 88 |
cancelButton->Bind(wxEVT_BUTTON, sigc::mem_fun(this, &MissionReadmeDialog::onCancel)); |
| 89 |
|
| 90 |
// Wire up the text entry boxes to update the preview |
| 91 |
setupNamedEntryBox("MissionInfoReadmeContentsEntry"); |
| 92 |
|
| 93 |
// Make this dialog LARGE |
| 94 |
Layout(); |
| 95 |
FitToScreen(0.9f, 0.8f); |
| 96 |
|
| 97 |
// Set the sash to 50% |
| 98 |
wxSplitterWindow* splitter = findNamedObject<wxSplitterWindow>(this, "MissionInfoReadmeSplitter"); |
| 99 |
splitter->SetSashPosition(this->GetSize().GetWidth() / 2); |
| 100 |
} |
| 101 |
|
| 102 |
void MissionReadmeDialog::setupNamedEntryBox(const std::string& ctrlName) |
| 103 |
{ |
| 104 |
wxTextCtrl* ctrl = findNamedObject<wxTextCtrl>(this, ctrlName); |
| 105 |
|
| 106 |
assert(ctrl != nullptr); |
| 107 |
if (ctrl == nullptr) return; |
| 108 |
|
| 109 |
ctrl->Bind(wxEVT_TEXT, [this](wxCommandEvent& ev) |
| 110 |
{ |
| 111 |
if (_updateInProgress) return; |
| 112 |
|
| 113 |
// Load the values from the UI to the ReadmeTxt instance |
| 114 |
_readmeFile->setContents(findNamedObject<wxTextCtrl>(this, "MissionInfoReadmeContentsEntry")->GetValue().ToStdString()); |
| 115 |
|
| 116 |
_guiView->updateGuiState(); |
| 117 |
}); |
| 118 |
} |
| 119 |
|
| 120 |
void MissionReadmeDialog::onSave(wxCommandEvent& ev) |
| 121 |
{ |
| 122 |
try |
| 123 |
{ |
| 124 |
// ReadmeTxt is kept in sync all the time, no need to load anything, just save to disk |
| 125 |
_readmeFile->saveToCurrentMod(); |
| 126 |
|
| 127 |
// Close the dialog |
| 128 |
EndModal(wxID_OK); |
| 129 |
} |
| 130 |
catch (std::runtime_error& err) |
| 131 |
{ |
| 132 |
wxutil::Messagebox::ShowError(err.what(), this); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void MissionReadmeDialog::onCancel(wxCommandEvent& ev) |
| 137 |
{ |
| 138 |
// destroy dialog without saving |
| 139 |
EndModal(wxID_CANCEL); |
| 140 |
} |
| 141 |
|
| 142 |
void MissionReadmeDialog::ShowDialog(const cmd::ArgumentList& args) |
| 143 |
{ |
| 144 |
MissionReadmeDialog* instance = new MissionReadmeDialog; |
| 145 |
|
| 146 |
instance->ShowModal(); |
| 147 |
instance->Destroy(); |
| 148 |
} |
| 149 |
|
| 150 |
} |