Skip to content

Commit

Permalink
#5565: Attempt at keeping the material tree view up to date when a ma…
Browse files Browse the repository at this point in the history
…terials get added, removed or renamed. Not quite working yet.
  • Loading branch information
codereader committed Mar 27, 2021
1 parent b903217 commit c52928d
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 12 deletions.
14 changes: 14 additions & 0 deletions radiant/ui/materials/MaterialPopulator.cpp
Expand Up @@ -146,6 +146,20 @@ MaterialPopulator::~MaterialPopulator()
EnsureStopped();
}

void MaterialPopulator::AddSingleMaterial(const wxutil::TreeModel::Ptr& model, const std::string& materialName)
{
ShaderNameFunctor functor(*model, _columns, _favourites);
functor.visit(materialName);
}

void MaterialPopulator::RemoveSingleMaterial(const wxutil::TreeModel::Ptr& model, const std::string& materialName)
{
auto item = model->FindString(materialName, _columns.fullName);
if (!item.IsOk()) return;

model->RemoveItem(item);
}

void MaterialPopulator::PopulateModel(const wxutil::TreeModel::Ptr& model)
{
model->SetHasDefaultCompare(false);
Expand Down
6 changes: 6 additions & 0 deletions radiant/ui/materials/MaterialPopulator.h
Expand Up @@ -23,6 +23,12 @@ class MaterialPopulator :

virtual ~MaterialPopulator();

// Add the given named material to the tree (assuming it was not present before)
void AddSingleMaterial(const wxutil::TreeModel::Ptr& model, const std::string& materialName);

// Remove the given named material from the tree (assuming it is present)
void RemoveSingleMaterial(const wxutil::TreeModel::Ptr& model, const std::string& materialName);

protected:
virtual void PopulateModel(const wxutil::TreeModel::Ptr& model) override;

Expand Down
34 changes: 34 additions & 0 deletions radiant/ui/materials/MaterialTreeView.cpp
@@ -1,5 +1,6 @@
#include "MaterialTreeView.h"

#include "ishaders.h"
#include "i18n.h"
#include "MaterialPopulator.h"

Expand All @@ -21,6 +22,20 @@ MaterialTreeView::MaterialTreeView(wxWindow* parent) :
// The wxWidgets algorithm sucks at sorting large flat lists of strings,
// so we do that ourselves
GetTreeModel()->SetHasDefaultCompare(false);

_materialCreated = GlobalMaterialManager().signal_materialCreated().connect(
sigc::mem_fun(this, &MaterialTreeView::onMaterialCreated));
_materialRenamed = GlobalMaterialManager().signal_materialRenamed().connect(
sigc::mem_fun(this, &MaterialTreeView::onMaterialRenamed));
_materialRemoved = GlobalMaterialManager().signal_materialRemoved().connect(
sigc::mem_fun(this, &MaterialTreeView::onMaterialRemoved));
}

MaterialTreeView::~MaterialTreeView()
{
_materialCreated.disconnect();
_materialRenamed.disconnect();
_materialRemoved.disconnect();
}

const MaterialTreeView::TreeColumns& MaterialTreeView::Columns() const
Expand All @@ -44,4 +59,23 @@ void MaterialTreeView::SetTreeMode(MaterialTreeView::TreeMode mode)
SetSelectedFullname(previouslySelectedItem);
}

void MaterialTreeView::onMaterialCreated(const std::string& name)
{
auto populator = MaterialPopulator(Columns());
populator.AddSingleMaterial(GetTreeModel(), name);
}

void MaterialTreeView::onMaterialRenamed(const std::string& oldName, const std::string& newName)
{
auto populator = MaterialPopulator(Columns());
populator.RemoveSingleMaterial(GetTreeModel(), oldName);
populator.AddSingleMaterial(GetTreeModel(), newName);
}

void MaterialTreeView::onMaterialRemoved(const std::string& name)
{
auto populator = MaterialPopulator(Columns());
populator.RemoveSingleMaterial(GetTreeModel(), name);
}

}
12 changes: 12 additions & 0 deletions radiant/ui/materials/MaterialTreeView.h
@@ -1,5 +1,6 @@
#pragma once

#include <sigc++/connection.h>
#include "wxutil/dataview/ResourceTreeView.h"

namespace ui
Expand All @@ -23,15 +24,26 @@ class MaterialTreeView :
wxutil::TreeModel::Column isOtherMaterialsFolder;
};

// Subscriptions to the material manager
sigc::connection _materialCreated;
sigc::connection _materialRenamed;
sigc::connection _materialRemoved;

public:
MaterialTreeView(wxWindow* parent);
virtual ~MaterialTreeView();

const TreeColumns& Columns() const;

virtual void SetTreeMode(MaterialTreeView::TreeMode mode) override;

// Loads all the materials
virtual void Populate();

private:
void onMaterialCreated(const std::string& name);
void onMaterialRenamed(const std::string& oldName, const std::string& newName);
void onMaterialRemoved(const std::string& name);
};

}
20 changes: 10 additions & 10 deletions radiant/ui/materials/editor/MaterialEditor.cpp
Expand Up @@ -1069,6 +1069,7 @@ void MaterialEditor::revertCurrentMaterial()

bool MaterialEditor::askUserAboutModifiedMaterial()
{
#if 0
// Get the original name
std::string origName = _material->getName();

Expand Down Expand Up @@ -1098,17 +1099,13 @@ bool MaterialEditor::askUserAboutModifiedMaterial()
return false; // user cancelled
}

#endif
// User doesn't want to save
return true;
}

bool MaterialEditor::isAllowedToChangeMaterial()
{
if (_material && _material->isModified())
{
return askUserAboutModifiedMaterial();
}

return true;
}

Expand Down Expand Up @@ -1153,14 +1150,17 @@ void MaterialEditor::_onSaveMaterial(wxCommandEvent& ev)

void MaterialEditor::_onNewMaterial(wxCommandEvent& ev)
{
if (!_material) return;
auto materialName = "textures/darkmod/map_specific/unnamed";

if (_material->isModified())
auto newMaterial = GlobalMaterialManager().createEmptyMaterial(materialName);

auto newItem = _treeView->GetTreeModel()->FindString(newMaterial->getName(), _treeView->Columns().fullName);

if (newItem.IsOk())
{

_treeView->Select(newItem);
updateMaterialTreeItem();
}

// TODO
}

void MaterialEditor::_onCopyMaterial(wxCommandEvent& ev)
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/shaders/Doom3ShaderSystem.cpp
Expand Up @@ -353,11 +353,11 @@ MaterialPtr Doom3ShaderSystem::createEmptyMaterial(const std::string& name)

_library->addDefinition(candidate, def);

_sigMaterialCreated.emit(candidate);

auto material = _library->findShader(candidate);
material->setIsModified();

_sigMaterialCreated.emit(candidate);

return material;
}

Expand Down

0 comments on commit c52928d

Please sign in to comment.