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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/* Copyright (C) 2018 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "MapDialog.h"
#include "GameInterface/MessagePasser.h"
#include "GameInterface/Messages.h"
#include <wx/filename.h>
#include <wx/notebook.h>
enum {
ID_MapDialogFilename = 1,
ID_MapDialogNotebook,
ID_ScenarioPage,
ID_SkirmishPage,
ID_TutorialPage
};
static const wxString scenarioPath(L"maps/scenarios/");
static const wxString skirmishPath(L"maps/skirmishes/");
static const wxString tutorialPath(L"maps/tutorials/");
MapDialog::MapDialog(wxWindow* parent, MapDialogType type, const wxIcon& icon)
: wxDialog(parent, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(600,400), wxCAPTION|wxRESIZE_BORDER|wxCLOSE_BOX|wxSYSTEM_MENU),
m_Type(type)
{
Freeze();
SetIcon(icon);
if (m_Type == MAPDIALOG_OPEN)
SetTitle(_("Choose map to open"));
else // MAPDIALOG_SAVE
SetTitle(_("Choose map to save"));
AtlasMessage::qGetMapList qry;
qry.Post();
wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
wxNotebook* notebook = new wxNotebook(this, ID_MapDialogNotebook);
{
wxPanel* page = new wxPanel(notebook, ID_ScenarioPage);
wxSizer* pageSizer = new wxBoxSizer(wxVERTICAL);
// TODO: Should display something nicer than raw VFS paths
wxListBox* listBox = new wxListBox(page, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE|wxLB_HSCROLL);
for (const std::wstring& filename : *qry.scenarioFilenames)
{
wxString name = filename.substr(scenarioPath.Length());
listBox->Append(name, new wxStringClientData(filename));
}
pageSizer->Add(listBox, wxSizerFlags().Proportion(1).Expand());
page->SetSizer(pageSizer);
notebook->AddPage(page, _("Scenarios"));
}
{
wxPanel* page = new wxPanel(notebook, ID_SkirmishPage);
wxSizer* pageSizer = new wxBoxSizer(wxVERTICAL);
// TODO: Should display something nicer than raw VFS paths
wxListBox* listBox = new wxListBox(page, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE|wxLB_HSCROLL);
for (const std::wstring& filename : *qry.skirmishFilenames)
{
wxString name = filename.substr(skirmishPath.Length());
listBox->Append(name, new wxStringClientData(filename));
}
pageSizer->Add(listBox, wxSizerFlags().Proportion(1).Expand());
page->SetSizer(pageSizer);
notebook->AddPage(page, _("Skirmishes"));
}
{
wxPanel* page = new wxPanel(notebook, ID_TutorialPage);
wxSizer* pageSizer = new wxBoxSizer(wxVERTICAL);
// TODO: Should display something nicer than raw VFS paths
wxListBox* listBox = new wxListBox(page, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, NULL, wxLB_SINGLE | wxLB_HSCROLL);
for (const std::wstring& filename : *qry.tutorialFilenames)
{
wxString name = filename.substr(tutorialPath.Length());
listBox->Append(name, new wxStringClientData(filename));
}
pageSizer->Add(listBox, wxSizerFlags().Proportion(1).Expand());
page->SetSizer(pageSizer);
notebook->AddPage(page, _("Tutorials"));
}
notebook->SetSelection(0);
sizer->Add(notebook, wxSizerFlags().Proportion(1).Expand());
sizer->AddSpacer(5);
wxSizer* filenameSizer = new wxBoxSizer(wxHORIZONTAL);
filenameSizer->AddSpacer(10);
filenameSizer->Add(new wxStaticText(this, wxID_ANY, _(m_Type == MAPDIALOG_SAVE ? "Map name: " : "Map path: ")), wxSizerFlags().Align(wxALIGN_CENTER_VERTICAL));
wxTextCtrl* filename = new wxTextCtrl(this, ID_MapDialogFilename, wxEmptyString, wxDefaultPosition, wxDefaultSize, m_Type == MAPDIALOG_OPEN ? wxTE_READONLY : 0L);
filenameSizer->Add(filename, wxSizerFlags().Proportion(1).Expand());
sizer->Add(filenameSizer, wxSizerFlags().Expand());
sizer->AddSpacer(10);
wxSizer* buttonSizer = new wxBoxSizer(wxHORIZONTAL);
if (m_Type == MAPDIALOG_OPEN)
buttonSizer->Add(new wxButton(this, wxID_OPEN, _("Open")));
else // MAPDIALOG_SAVE
buttonSizer->Add(new wxButton(this, wxID_SAVE, _("Save")));
buttonSizer->AddSpacer(5);
buttonSizer->Add(new wxButton(this, wxID_CANCEL, _("Cancel")));
sizer->Add(buttonSizer, wxSizerFlags().Align(wxALIGN_RIGHT).Border(wxRIGHT|wxBOTTOM, 10));
SetSizer(sizer);
Layout();
Thaw();
}
wxString MapDialog::GetSelectedFilePath() const
{
wxNotebook* notebook = wxDynamicCast(FindWindow(ID_MapDialogNotebook), wxNotebook);
if (!notebook)
return wxEmptyString;
wxFileName fileName(m_FileName, wxPATH_UNIX);
fileName.SetExt(L"xml");
switch (notebook->GetSelection())
{
case 0:
return scenarioPath + fileName.GetFullPath(wxPATH_UNIX);
case 1:
return skirmishPath + fileName.GetFullPath(wxPATH_UNIX);
case 2:
return tutorialPath + fileName.GetFullPath(wxPATH_UNIX);
default:
return wxEmptyString;
}
}
void MapDialog::OnListBox(wxCommandEvent& evt)
{
if (evt.GetInt() < 0)
m_FileName = wxEmptyString;
else
m_FileName = evt.GetString();
if (m_Type == MAPDIALOG_SAVE)
wxDynamicCast(FindWindow(ID_MapDialogFilename), wxTextCtrl)->ChangeValue(m_FileName);
else
{
wxString filePath = GetSelectedFilePath();
AtlasMessage::qVFSFileExists qry(filePath.wc_str());
qry.Post();
if (!filePath.IsEmpty() && qry.exists)
{
AtlasMessage::qVFSFileRealPath qry(filePath.wc_str());
qry.Post();
wxDynamicCast(FindWindow(ID_MapDialogFilename), wxTextCtrl)->ChangeValue(*qry.realPath);
}
}
if (evt.GetEventType() == wxEVT_COMMAND_LISTBOX_DOUBLECLICKED)
{
if (m_Type == MAPDIALOG_OPEN)
OpenFile();
else
SaveFile();
}
}
void MapDialog::OnCancel(wxCommandEvent& WXUNUSED(evt))
{
EndModal(wxID_CANCEL);
}
void MapDialog::OnOpen(wxCommandEvent& WXUNUSED(evt))
{
OpenFile();
}
void MapDialog::OnSave(wxCommandEvent& WXUNUSED(evt))
{
SaveFile();
}
void MapDialog::OnFilename(wxCommandEvent& evt)
{
m_FileName = evt.GetString();
}
void MapDialog::OnNotebookChanged(wxBookCtrlEvent& WXUNUSED(evt))
{
if (m_Type != MAPDIALOG_OPEN)
return;
wxWindow* window = FindWindow(ID_MapDialogFilename);
if (window)
wxDynamicCast(window, wxTextCtrl)->ChangeValue(wxEmptyString);
}
void MapDialog::OpenFile()
{
wxString filePath = GetSelectedFilePath();
if (filePath.empty())
return;
AtlasMessage::qVFSFileExists qry(filePath.wc_str());
qry.Post();
if (!qry.exists)
return;
EndModal(wxID_OK);
}
void MapDialog::SaveFile()
{
wxString filePath = GetSelectedFilePath();
if (filePath.empty())
return;
// TODO: this test would work better outside the VFS
AtlasMessage::qVFSFileExists qry(filePath.wc_str());
qry.Post();
if (qry.exists)
{
if (wxMessageBox(_("WARNING: '") + filePath + _("' already exists, it may be overwritten. Continue?"), _("Overwrite map confirmation"), wxICON_EXCLAMATION | wxYES_NO) != wxYES)
return;
}
EndModal(wxID_OK);
}
BEGIN_EVENT_TABLE(MapDialog, wxDialog)
EVT_BUTTON (wxID_CANCEL, MapDialog::OnCancel)
EVT_BUTTON (wxID_OPEN, MapDialog::OnOpen)
EVT_BUTTON (wxID_SAVE, MapDialog::OnSave)
EVT_LISTBOX (wxID_ANY, MapDialog::OnListBox)
EVT_LISTBOX_DCLICK (wxID_ANY, MapDialog::OnListBox)
EVT_TEXT (ID_MapDialogFilename, MapDialog::OnFilename)
EVT_NOTEBOOK_PAGE_CHANGED (ID_MapDialogNotebook, MapDialog::OnNotebookChanged)
END_EVENT_TABLE()
|