From 57e3a4e4350a65c78ae740cd0fee035d5b03ad7b Mon Sep 17 00:00:00 2001 From: codereader Date: Fri, 20 Nov 2020 14:01:01 +0100 Subject: [PATCH] #5108: Add MapSelector::Result structure to contain more info about the selected map file --- libs/wxutil/fsview/FileSystemView.cpp | 11 ++++++++++ libs/wxutil/fsview/FileSystemView.h | 3 +++ radiant/ui/mapselector/MapSelector.cpp | 30 ++++++++++++++++++++------ radiant/ui/mapselector/MapSelector.h | 10 ++++++++- 4 files changed, 47 insertions(+), 7 deletions(-) diff --git a/libs/wxutil/fsview/FileSystemView.cpp b/libs/wxutil/fsview/FileSystemView.cpp index e8c216e81e..c62c1b9981 100644 --- a/libs/wxutil/fsview/FileSystemView.cpp +++ b/libs/wxutil/fsview/FileSystemView.cpp @@ -168,6 +168,17 @@ bool FileSystemView::GetIsFolderSelected() return row[Columns().isFolder].getBool(); } +std::string FileSystemView::GetArchivePathOfSelection() +{ + wxDataViewItem item = GetSelection(); + + if (!item.IsOk()) return ""; + + wxutil::TreeModel::Row row(item, *GetModel()); + + return row[Columns().archivePath]; +} + void FileSystemView::SelectItem(const wxDataViewItem& item) { if (!item.IsOk()) return; diff --git a/libs/wxutil/fsview/FileSystemView.h b/libs/wxutil/fsview/FileSystemView.h index 349eeb66b5..9a10609774 100644 --- a/libs/wxutil/fsview/FileSystemView.h +++ b/libs/wxutil/fsview/FileSystemView.h @@ -85,6 +85,9 @@ class FileSystemView : std::string GetSelectedPath(); bool GetIsFolderSelected(); + // Returns the path to the archive (directory/PK4) of the selected path + std::string GetArchivePathOfSelection(); + // Expands the given path, which is relative to the base path (e.g. "maps/") void ExpandPath(const std::string& relativePath); diff --git a/radiant/ui/mapselector/MapSelector.cpp b/radiant/ui/mapselector/MapSelector.cpp index c0b94098e9..b9366c6305 100644 --- a/radiant/ui/mapselector/MapSelector.cpp +++ b/radiant/ui/mapselector/MapSelector.cpp @@ -34,6 +34,7 @@ MapSelector::MapSelector() : vbox->Add(_treeView, 1, wxEXPAND); _buttons = CreateStdDialogButtonSizer(wxOK | wxCANCEL); + _buttons->GetAffirmativeButton()->SetLabel(_("Open")); wxButton* reloadButton = new wxButton(this, wxID_ANY, _("Refresh")); reloadButton->Bind(wxEVT_BUTTON, &MapSelector::onRescanPath, this); @@ -58,14 +59,15 @@ int MapSelector::ShowModal() return DialogBase::ShowModal(); } -std::string MapSelector::ChooseMapFile() +MapSelector::Result MapSelector::ChooseMapFile() { auto* dialog = new MapSelector(); - std::string returnValue = ""; + Result returnValue; if (dialog->ShowModal() == wxID_OK) { - returnValue = dialog->getSelectedPath(); + returnValue.selectedPath = dialog->getSelectedPath(); + returnValue.archivePath = dialog->getArchivePath(); } // Use the instance to select a model. @@ -74,11 +76,22 @@ std::string MapSelector::ChooseMapFile() void MapSelector::OpenMapFromProject(const cmd::ArgumentList& args) { - auto mapPath = ChooseMapFile(); + auto result = ChooseMapFile(); - if (!mapPath.empty()) + if (result.selectedPath.empty()) { - GlobalCommandSystem().executeCommand("OpenMap", mapPath); + return; + } + + auto extension = string::to_lower_copy(os::getExtension(result.archivePath)); + + if (!os::isDirectory(result.archivePath) && GlobalFileSystem().getArchiveExtensions().count(extension) > 0) + { + GlobalCommandSystem().executeCommand("OpenMapFromArchive", result.archivePath, result.selectedPath); + } + else + { + GlobalCommandSystem().executeCommand("OpenMap", result.selectedPath); } } @@ -231,4 +244,9 @@ std::string MapSelector::getSelectedPath() return _treeView->GetSelectedPath(); } +std::string MapSelector::getArchivePath() +{ + return _treeView->GetArchivePathOfSelection(); +} + } diff --git a/radiant/ui/mapselector/MapSelector.h b/radiant/ui/mapselector/MapSelector.h index e3977f2c70..4736fd4244 100644 --- a/radiant/ui/mapselector/MapSelector.h +++ b/radiant/ui/mapselector/MapSelector.h @@ -50,6 +50,8 @@ class MapSelector : // Return the selected map path std::string getSelectedPath(); + // Return the archive path of the selection + std::string getArchivePath(); void onRescanPath(wxCommandEvent& ev); void onItemActivated(wxDataViewEvent& ev); @@ -61,10 +63,16 @@ class MapSelector : public: int ShowModal() override; + struct Result + { + std::string archivePath; // path to containing archive + std::string selectedPath; // relative path within the archive + }; + /** * Display the Selector return the path of the file selected by the user. */ - static std::string ChooseMapFile(); + static Result ChooseMapFile(); static void OpenMapFromProject(const cmd::ArgumentList& args); };