File: MissionReadmeDialog.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (150 lines) | stat: -rw-r--r-- 3,910 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "MissionReadmeDialog.h"

#include "igui.h"
#include "itextstream.h"
#include "i18n.h"
#include <sigc++/functors/mem_fun.h>

#include <functional>
#include <fmt/format.h>
#include <wx/button.h>
#include <wx/textctrl.h>
#include <wx/splitter.h>
#include "wxutil/preview/GuiView.h"
#include "wxutil/dialog/MessageBox.h"

#include "MissionInfoGuiView.h"

namespace ui
{

namespace
{
	const char* const WINDOW_TITLE = N_("Mission Readme Editor (readme.txt)");
}

MissionReadmeDialog::MissionReadmeDialog(wxWindow* parent) :
	DialogBase(_(WINDOW_TITLE), parent),
	_updateInProgress(false)
{
	populateWindow();

	try
	{
		_readmeFile = map::ReadmeTxt::LoadForCurrentMod();
	}
	catch (map::ReadmeTxt::ParseException& ex)
	{
		rError() << "Failed to parse readme.txt: " << ex.what() << std::endl;

		wxutil::Messagebox::ShowError(
			fmt::format(_("Failed to parse readme.txt:\n{0}"), ex.what()), this);

		// Reset the file to defaults
		_readmeFile.reset();
	}

	_guiView->setGui(GlobalGuiManager().getGui("guis/mainmenu.gui"));
	_guiView->setMissionInfoFile(_readmeFile);

	updateValuesFromReadmeFile();
}

void MissionReadmeDialog::updateValuesFromReadmeFile()
{
	assert(_readmeFile); // this should be non-NULL at all times

	if (!_readmeFile) return;

	_updateInProgress = true;

	findNamedObject<wxTextCtrl>(this, "MissionInfoReadmeContentsEntry")->SetValue(_readmeFile->getContents());
	findNamedObject<wxStaticText>(this, "MissionInfoReadmeOutputPath")->SetLabelText(_readmeFile->getFullOutputPath());

	_guiView->updateGuiState();

	_updateInProgress = false;
}

void MissionReadmeDialog::populateWindow()
{
	SetSizer(new wxBoxSizer(wxVERTICAL));

	wxPanel* panel = loadNamedPanel(this, "MissionInfoReadmeDialogMainPanel");
	GetSizer()->Add(panel, 1, wxEXPAND);

	// Add the preview widget
	wxPanel* previewPanel = findNamedObject<wxPanel>(this, "MissionInfoReadmeDialogPreviewPanel");

	_guiView = new ReadmeTxtGuiView(previewPanel);
	previewPanel->GetSizer()->Add(_guiView, 1, wxEXPAND);

	makeLabelBold(this, "MissionReadmeLabel");

	wxButton* saveButton = findNamedObject<wxButton>(this, "MissionInfoReadmeDialogSaveButton");
	wxButton* cancelButton = findNamedObject<wxButton>(this, "MissionInfoReadmeDialogCancelButton");

	saveButton->Bind(wxEVT_BUTTON, sigc::mem_fun(this, &MissionReadmeDialog::onSave));
	cancelButton->Bind(wxEVT_BUTTON, sigc::mem_fun(this, &MissionReadmeDialog::onCancel));

	// Wire up the text entry boxes to update the preview
	setupNamedEntryBox("MissionInfoReadmeContentsEntry");

	// Make this dialog LARGE
	Layout();
	FitToScreen(0.9f, 0.8f);

	// Set the sash to 50%
	wxSplitterWindow* splitter = findNamedObject<wxSplitterWindow>(this, "MissionInfoReadmeSplitter");
	splitter->SetSashPosition(this->GetSize().GetWidth() / 2);
}

void MissionReadmeDialog::setupNamedEntryBox(const std::string& ctrlName)
{
	wxTextCtrl* ctrl = findNamedObject<wxTextCtrl>(this, ctrlName);

	assert(ctrl != nullptr);
	if (ctrl == nullptr) return;

	ctrl->Bind(wxEVT_TEXT, [this](wxCommandEvent& ev)
	{
		if (_updateInProgress) return;

		// Load the values from the UI to the ReadmeTxt instance
		_readmeFile->setContents(findNamedObject<wxTextCtrl>(this, "MissionInfoReadmeContentsEntry")->GetValue().ToStdString());
		
		_guiView->updateGuiState();
	});
}

void MissionReadmeDialog::onSave(wxCommandEvent& ev)
{
	try
	{
		// ReadmeTxt is kept in sync all the time, no need to load anything, just save to disk
		_readmeFile->saveToCurrentMod();

		// Close the dialog
		EndModal(wxID_OK);
	}
	catch (std::runtime_error& err)
	{
		wxutil::Messagebox::ShowError(err.what(), this);
	}
}

void MissionReadmeDialog::onCancel(wxCommandEvent& ev)
{
	// destroy dialog without saving
	EndModal(wxID_CANCEL);
}

void MissionReadmeDialog::ShowDialog(const cmd::ArgumentList& args)
{
	MissionReadmeDialog* instance = new MissionReadmeDialog;

	instance->ShowModal();
	instance->Destroy();
}

}