Skip to content

Commit

Permalink
#4764: Move scene-related logic to MapTextureBrowser, TextureThumbnai…
Browse files Browse the repository at this point in the history
…lBrowser serves as platform
  • Loading branch information
codereader committed Nov 6, 2022
1 parent a076142 commit 8b75de0
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 134 deletions.
78 changes: 77 additions & 1 deletion radiant/ui/texturebrowser/MapTextureBrowser.cpp
@@ -1,10 +1,86 @@
#include "MapTextureBrowser.h"

#include "ifavourites.h"

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

#include "TextureBrowserManager.h"

namespace ui
{

MapTextureBrowser::MapTextureBrowser(wxWindow* parent) :
TextureThumbnailBrowser(parent)
{}
{
observeRegistryKey(RKEY_TEXTURES_HIDE_UNUSED);
observeRegistryKey(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY);
observeRegistryKey(RKEY_TEXTURES_SHOW_OTHER_MATERIALS);

onSettingsChanged();

GlobalMaterialManager().signal_activeShadersChanged().connect(
sigc::mem_fun(this, &MapTextureBrowser::onActiveShadersChanged));
}

void MapTextureBrowser::observeRegistryKey(const std::string& key)
{
GlobalRegistry().signalForKey(key).connect(
sigc::mem_fun(this, &MapTextureBrowser::onSettingsChanged)
);
}

void MapTextureBrowser::onSettingsChanged()
{
_hideUnused = registry::getValue<bool>(RKEY_TEXTURES_HIDE_UNUSED);
_showFavouritesOnly = registry::getValue<bool>(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY);
_showOtherMaterials = registry::getValue<bool>(RKEY_TEXTURES_SHOW_OTHER_MATERIALS);
}

void MapTextureBrowser::populateTiles()
{
// Update the favourites
_favourites = GlobalFavouritesManager().getFavourites(decl::getTypeName(decl::Type::Material));

GlobalMaterialManager().foreachMaterial([&](const MaterialPtr& mat)
{
if (!materialIsVisible(mat))
{
return;
}

createTileForMaterial(mat);
});
}

bool MapTextureBrowser::materialIsVisible(const MaterialPtr& material)
{
if (!material) return false;

auto materialName = material->getName();

if (!_showOtherMaterials && !string::istarts_with(material->getName(), GlobalTexturePrefix_get()))
{
return false;
}

if (_hideUnused && !material->IsInUse())
{
return false;
}

if (_showFavouritesOnly && _favourites.count(materialName) == 0)
{
return false;
}

return !materialIsFiltered(materialName);
}

void MapTextureBrowser::onActiveShadersChanged()
{
queueUpdate();
}

}
28 changes: 28 additions & 0 deletions radiant/ui/texturebrowser/MapTextureBrowser.h
@@ -1,5 +1,6 @@
#pragma once

#include <set>
#include "TextureThumbnailBrowser.h"

namespace ui
Expand All @@ -11,8 +12,35 @@ namespace ui
class MapTextureBrowser :
public TextureThumbnailBrowser
{
private:
// if true, the texture window will only display in-use shaders
// if false, all the shaders in memory are displayed
bool _hideUnused;
bool _showFavouritesOnly;

// Whether materials not starting with "textures/" should be visible
bool _showOtherMaterials;

// Cached set of material favourites
std::set<std::string> _favourites;

public:
MapTextureBrowser(wxWindow* parent);

protected:
void populateTiles() override;

private:
/**
* Returns true if the given material is visible,
* taking filter and showUnused into account.
*/
bool materialIsVisible(const MaterialPtr& material);

void onSettingsChanged();
void observeRegistryKey(const std::string& key);

void onActiveShadersChanged();
};

}
145 changes: 37 additions & 108 deletions radiant/ui/texturebrowser/TextureThumbnailBrowser.cpp
Expand Up @@ -64,18 +64,8 @@ class TextureThumbnailBrowser::TextureTile
_owner(owner)
{}

bool isVisible()
{
return _owner.materialIsVisible(material);
}

void render(bool drawName)
{
if (!isVisible())
{
return;
}

TexturePtr texture = material->getEditorImage();
if (!texture) return;

Expand Down Expand Up @@ -222,26 +212,20 @@ TextureThumbnailBrowser::TextureThumbnailBrowser(wxWindow* parent) :
_mouseWheelScrollIncrement(registry::getValue<int>(RKEY_TEXTURE_MOUSE_WHEEL_INCR)),
_showTextureFilter(registry::getValue<bool>(RKEY_TEXTURE_SHOW_FILTER)),
_showTextureScrollbar(registry::getValue<bool>(RKEY_TEXTURE_SHOW_SCROLLBAR)),
_hideUnused(registry::getValue<bool>(RKEY_TEXTURES_HIDE_UNUSED)),
_showFavouritesOnly(registry::getValue<bool>(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY)),
_showNamesKey(RKEY_TEXTURES_SHOW_NAMES),
_textureScale(50),
_useUniformScale(registry::getValue<bool>(RKEY_TEXTURE_USE_UNIFORM_SCALE)),
_showOtherMaterials(registry::getValue<bool>(RKEY_TEXTURES_SHOW_OTHER_MATERIALS)),
_uniformTextureSize(registry::getValue<int>(RKEY_TEXTURE_UNIFORM_SIZE)),
_maxNameLength(registry::getValue<int>(RKEY_TEXTURE_MAX_NAME_LENGTH)),
_updateNeeded(true)
{
observeKey(RKEY_TEXTURES_HIDE_UNUSED);
observeKey(RKEY_TEXTURES_SHOW_OTHER_MATERIALS);
observeKey(RKEY_TEXTURE_UNIFORM_SIZE);
observeKey(RKEY_TEXTURE_USE_UNIFORM_SCALE);
observeKey(RKEY_TEXTURE_SCALE);
observeKey(RKEY_TEXTURE_SHOW_SCROLLBAR);
observeKey(RKEY_TEXTURE_MOUSE_WHEEL_INCR);
observeKey(RKEY_TEXTURE_SHOW_FILTER);
observeKey(RKEY_TEXTURE_MAX_NAME_LENGTH);
observeKey(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY);
observeKey(RKEY_TEXTURES_SHOW_NAMES);

loadScaleFromRegistry();
Expand All @@ -265,9 +249,6 @@ TextureThumbnailBrowser::TextureThumbnailBrowser(wxWindow* parent) :
wxutil::FreezePointer::MouseEventFunction(),
std::bind(&TextureThumbnailBrowser::onGLMouseButtonRelease, this, std::placeholders::_1));

GlobalMaterialManager().signal_activeShadersChanged().connect(
sigc::mem_fun(this, &TextureThumbnailBrowser::onActiveShadersChanged));

SetSizer(new wxBoxSizer(wxHORIZONTAL));

wxPanel* texbox = new wxPanel(this, wxID_ANY);
Expand Down Expand Up @@ -405,9 +386,6 @@ void TextureThumbnailBrowser::filterChanged()

void TextureThumbnailBrowser::keyChanged()
{
_hideUnused = registry::getValue<bool>(RKEY_TEXTURES_HIDE_UNUSED);
_showFavouritesOnly = registry::getValue<bool>(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY);
_showOtherMaterials = registry::getValue<bool>(RKEY_TEXTURES_SHOW_OTHER_MATERIALS);
_showTextureFilter = registry::getValue<bool>(RKEY_TEXTURE_SHOW_FILTER);
_uniformTextureSize = registry::getValue<int>(RKEY_TEXTURE_UNIFORM_SIZE);
_useUniformScale = registry::getValue<bool>(RKEY_TEXTURE_USE_UNIFORM_SCALE);
Expand Down Expand Up @@ -495,6 +473,42 @@ std::string TextureThumbnailBrowser::getFilter()
return _filter->GetValue().ToStdString();
}

bool TextureThumbnailBrowser::materialIsFiltered(const std::string& materialName)
{
auto filterText = getFilter();

if (filterText.empty()) return false; // not filtered

std::string textureName = shader_get_textureName(materialName.c_str());

if (_filterIgnoresTexturePath)
{
std::size_t lastSlash = textureName.find_last_of('/');

if (lastSlash != std::string::npos)
{
textureName.erase(0, lastSlash + 1);
}
}

string::to_lower(textureName);

// Split the filter text into words, every word must match (#5738)
std::vector<std::string> filters;
string::split(filters, string::to_lower_copy(filterText), " ");

// case insensitive substring match (all must match for the name to be visible)
for (const auto& filter : filters)
{
if (textureName.find(filter) == std::string::npos)
{
return true; // no match, texture name is filtered out
}
}

return false; // not filtered
}

void TextureThumbnailBrowser::setSelectedShader(const std::string& newShader)
{
_shader = newShader;
Expand Down Expand Up @@ -538,68 +552,6 @@ Vector2i TextureThumbnailBrowser::getNextPositionForTexture(const Texture& tex)
return texPos;
}

// if texture_showinuse jump over non in-use textures
bool TextureThumbnailBrowser::materialIsVisible(const MaterialPtr& material)
{
if (!material)
{
return false;
}

auto materialName = material->getName();

if (!_showOtherMaterials && !string::istarts_with(material->getName(), GlobalTexturePrefix_get()))
{
return false;
}

if (_hideUnused && !material->IsInUse())
{
return false;
}

if (_showFavouritesOnly && _favourites.count(materialName) == 0)
{
return false;
}

auto filterText = getFilter();

if (!filterText.empty())
{
std::string textureName = shader_get_textureName(materialName.c_str());

if (_filterIgnoresTexturePath)
{
std::size_t lastSlash = textureName.find_last_of('/');

if (lastSlash != std::string::npos)
{
textureName.erase(0, lastSlash + 1);
}
}

string::to_lower(textureName);

// Split the filter text into words, every word must match (#5738)
std::vector<std::string> filters;
string::split(filters, string::to_lower_copy(filterText), " ");

// case insensitive substring match
for (const auto& filter : filters)
{
if (textureName.find(filter) == std::string::npos)
{
return false;
}
}

return true;
}

return true;
}

int TextureThumbnailBrowser::getTotalHeight()
{
return _entireSpaceHeight;
Expand Down Expand Up @@ -646,22 +598,6 @@ void TextureThumbnailBrowser::queueUpdate()
requestIdleCallback();
}

void TextureThumbnailBrowser::populateTiles()
{
// Update the favourites
_favourites = GlobalFavouritesManager().getFavourites(decl::getTypeName(decl::Type::Material));

GlobalMaterialManager().foreachMaterial([&](const MaterialPtr& mat)
{
if (!materialIsVisible(mat))
{
return;
}

createTileForMaterial(mat);
});
}

void TextureThumbnailBrowser::createTileForMaterial(const MaterialPtr& material)
{
// Create a new tile for this material
Expand Down Expand Up @@ -705,11 +641,6 @@ void TextureThumbnailBrowser::refreshTiles()
_currentPopulationPosition.reset();
}

void TextureThumbnailBrowser::onActiveShadersChanged()
{
queueUpdate();
}

void TextureThumbnailBrowser::focus(const std::string& name)
{
if (name.empty())
Expand Down Expand Up @@ -759,9 +690,7 @@ MaterialPtr TextureThumbnailBrowser::getShaderAtCoords(int x, int y)

void TextureThumbnailBrowser::selectTextureAt(int mx, int my)
{
auto shader = getShaderAtCoords(mx, my);

if (shader)
if (auto shader = getShaderAtCoords(mx, my); shader)
{
setSelectedShader(shader->getName());

Expand Down

0 comments on commit 8b75de0

Please sign in to comment.