Skip to content

Commit

Permalink
#6021: Use a VFSTreePopulator to load the textures into the MaterialT…
Browse files Browse the repository at this point in the history
…reeView.
  • Loading branch information
codereader committed Jul 29, 2022
1 parent e9bc061 commit 724d4ec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 38 deletions.
77 changes: 47 additions & 30 deletions radiant/ui/materials/MaterialPopulator.cpp
@@ -1,15 +1,13 @@
#include "MaterialPopulator.h"

#include <map>
#include "ifavourites.h"
#include "i18n.h"
#include "ishaders.h"

#include "string/split.h"
#include "shaderlib.h"

#include "wxutil/Bitmap.h"
#include "wxutil/dataview/TreeViewItemStyle.h"
#include "wxutil/dataview/VFSTreePopulator.h"

namespace ui
{
Expand All @@ -24,6 +22,7 @@ namespace
MaterialPopulator::MaterialPopulator(const MaterialTreeView::TreeColumns& columns) :
ThreadedDeclarationTreePopulator(decl::Type::Material, columns, TEXTURE_ICON),
_columns(columns),
_texturePrefix(GlobalMaterialManager().getTexturePrefix()),
_otherMaterialsPath(_(OTHER_MATERIALS_FOLDER))
{}

Expand All @@ -38,7 +37,7 @@ void MaterialPopulator::AddSingleMaterial(const wxutil::TreeModel::Ptr& model, c
std::vector<std::string> parts;
string::split(parts, materialName, "/");

bool isOtherMaterial = parts.size() > 1 && !string::istarts_with(materialName, GlobalTexturePrefix_get());
bool isOtherMaterial = parts.size() > 1 && !string::istarts_with(materialName, _texturePrefix);
auto otherMaterialsFolder = _(OTHER_MATERIALS_FOLDER);

if (isOtherMaterial)
Expand Down Expand Up @@ -121,10 +120,53 @@ void MaterialPopulator::PopulateModel(const wxutil::TreeModel::Ptr& model)
{
model->SetHasDefaultCompare(false);

wxutil::VFSTreePopulator populator(model);

// Insert the "Other Materials" folder in any case
populator.addPath(_otherMaterialsPath, [&](wxutil::TreeModel::Row& row,
const std::string& path, const std::string& leafName, bool isFolder)
{
row[_columns.isOtherMaterialsFolder] = true;
AssignValuesToRow(row, path, path, path, true);
});

GlobalMaterialManager().foreachShaderName([&](const std::string& name)
{
ThrowIfCancellationRequested();
insert(model, name);

// Determine the folder this texture will be sorted into
auto texturePath = string::istarts_with(name, _texturePrefix) ?
name : _otherMaterialsPath + "/" + name;

populator.addPath(texturePath, [&](wxutil::TreeModel::Row& row,
const std::string& path, const std::string& leafName, bool isFolder)
{
row[_columns.isOtherMaterialsFolder] = false;
AssignValuesToRow(row, path, isFolder ? path : name, leafName, isFolder);
});
#if 0
// Find rightmost slash
auto slashPos = name.rfind("/");

wxDataViewItem parent;

if (string::istarts_with(name, _texturePrefix))
{
// Regular texture, ensure parent folder
parent = slashPos != std::string::npos ? addRecursive(model, name.substr(0, slashPos), false) : model->GetRoot();
}
else
{
// Put it under "other materials", ensure parent folder
parent = slashPos != std::string::npos ?
addRecursive(model, _otherMaterialsPath + "/" + name.substr(0, slashPos), true) :
addRecursive(model, _otherMaterialsPath, true);
}

// Insert the actual leaf
auto leafName = slashPos != std::string::npos ? name.substr(slashPos + 1) : name;
InsertTexture(model, name, leafName, parent);
#endif
});
}

Expand Down Expand Up @@ -186,31 +228,6 @@ wxDataViewItem& MaterialPopulator::addRecursive(const wxutil::TreeModel::Ptr& mo
return result.first->second;
}

void MaterialPopulator::insert(const wxutil::TreeModel::Ptr& model, const std::string& name)
{
// Find rightmost slash
auto slashPos = name.rfind("/");

wxDataViewItem parent;

if (string::istarts_with(name, GlobalTexturePrefix_get()))
{
// Regular texture, ensure parent folder
parent = slashPos != std::string::npos ? addRecursive(model, name.substr(0, slashPos), false) : model->GetRoot();
}
else
{
// Put it under "other materials", ensure parent folder
parent = slashPos != std::string::npos ?
addRecursive(model, _otherMaterialsPath + "/" + name.substr(0, slashPos), true) :
addRecursive(model, _otherMaterialsPath, true);
}

// Insert the actual leaf
auto leafName = slashPos != std::string::npos ? name.substr(slashPos + 1) : name;
InsertTexture(model, name, leafName, parent);
}

void MaterialPopulator::SortModel(const wxutil::TreeModel::Ptr& model)
{
// Sort the model while we're still in the worker thread
Expand Down
2 changes: 1 addition & 1 deletion radiant/ui/materials/MaterialPopulator.h
Expand Up @@ -13,6 +13,7 @@ class MaterialPopulator :
private:
const MaterialTreeView::TreeColumns& _columns;

std::string _texturePrefix;
std::string _otherMaterialsPath;

// Maps of names to corresponding treemodel items, for both intermediate
Expand Down Expand Up @@ -44,7 +45,6 @@ class MaterialPopulator :
void InsertTexture(const wxutil::TreeModel::Ptr& model, const std::string& path,
const std::string& leafName, const wxDataViewItem& parentItem);
wxDataViewItem& addRecursive(const wxutil::TreeModel::Ptr& model, const std::string& path, bool isOtherMaterial);
void insert(const wxutil::TreeModel::Ptr& model, const std::string& name);
};

}
4 changes: 2 additions & 2 deletions radiant/ui/mediabrowser/MediaBrowser.cpp
Expand Up @@ -122,13 +122,13 @@ void MediaBrowser::onMapEvent(IMap::MapEvent ev)

std::string MediaBrowser::getSelection()
{
return _treeView->GetSelectedFullname();
return _treeView->GetSelectedDeclName();
}

// Set the selection in the treeview
void MediaBrowser::setSelection(const std::string& selection)
{
_treeView->SetSelectedFullname(selection);
_treeView->SetSelectedDeclName(selection);
}

void MediaBrowser::onMaterialDefsLoaded()
Expand Down
10 changes: 5 additions & 5 deletions radiant/ui/mediabrowser/MediaBrowserTreeView.cpp
Expand Up @@ -50,7 +50,7 @@ void MediaBrowserTreeView::PopulateContextMenu(wxutil::PopupMenu& popupMenu)
new wxutil::IconTextMenuItem(_(OPEN_IN_MATERIAL_EDITOR_TEXT), OPEN_IN_MATERIAL_EDITOR_ICON),
[this]()
{
GlobalCommandSystem().executeCommand("MaterialEditor", cmd::ArgumentList{ GetSelectedFullname() });
GlobalCommandSystem().executeCommand("MaterialEditor", cmd::ArgumentList{ GetSelectedDeclName() });
},
std::bind(&MediaBrowserTreeView::_testSingleTexSel, this)
);
Expand Down Expand Up @@ -96,17 +96,17 @@ bool MediaBrowserTreeView::_testLoadInTexView()
void MediaBrowserTreeView::_onApplyToSel()
{
// Pass shader name to the selection system
GlobalCommandSystem().executeCommand("SetShaderOnSelection", GetSelectedFullname());
GlobalCommandSystem().executeCommand("SetShaderOnSelection", GetSelectedDeclName());
}

bool MediaBrowserTreeView::_testSingleTexSel()
{
return !IsDirectorySelected() && !GetSelectedFullname().empty();
return !IsDirectorySelected() && !GetSelectedDeclName().empty();
}

void MediaBrowserTreeView::_onSelectItems(bool select)
{
std::string shaderName = GetSelectedFullname();
std::string shaderName = GetSelectedDeclName();

if (select)
{
Expand All @@ -120,7 +120,7 @@ void MediaBrowserTreeView::_onSelectItems(bool select)

void MediaBrowserTreeView::_onTreeViewItemActivated(wxDataViewEvent& ev)
{
std::string selection = GetSelectedFullname();
std::string selection = GetSelectedDeclName();

if (!IsDirectorySelected() && !selection.empty())
{
Expand Down

0 comments on commit 724d4ec

Please sign in to comment.