From 8b75de0213ce223df9cea52fd68a06f890b32d2b Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 6 Nov 2022 13:50:59 +0100 Subject: [PATCH] #4764: Move scene-related logic to MapTextureBrowser, TextureThumbnailBrowser serves as platform --- .../ui/texturebrowser/MapTextureBrowser.cpp | 78 +++++++++- radiant/ui/texturebrowser/MapTextureBrowser.h | 28 ++++ .../TextureThumbnailBrowser.cpp | 145 +++++------------- .../texturebrowser/TextureThumbnailBrowser.h | 34 ++-- 4 files changed, 151 insertions(+), 134 deletions(-) diff --git a/radiant/ui/texturebrowser/MapTextureBrowser.cpp b/radiant/ui/texturebrowser/MapTextureBrowser.cpp index ae9405a77b..8e5914b080 100644 --- a/radiant/ui/texturebrowser/MapTextureBrowser.cpp +++ b/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(RKEY_TEXTURES_HIDE_UNUSED); + _showFavouritesOnly = registry::getValue(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY); + _showOtherMaterials = registry::getValue(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(); +} } diff --git a/radiant/ui/texturebrowser/MapTextureBrowser.h b/radiant/ui/texturebrowser/MapTextureBrowser.h index 8953198109..965bc9bc58 100644 --- a/radiant/ui/texturebrowser/MapTextureBrowser.h +++ b/radiant/ui/texturebrowser/MapTextureBrowser.h @@ -1,5 +1,6 @@ #pragma once +#include #include "TextureThumbnailBrowser.h" namespace ui @@ -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 _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(); }; } diff --git a/radiant/ui/texturebrowser/TextureThumbnailBrowser.cpp b/radiant/ui/texturebrowser/TextureThumbnailBrowser.cpp index 11b693e029..b432485dd8 100644 --- a/radiant/ui/texturebrowser/TextureThumbnailBrowser.cpp +++ b/radiant/ui/texturebrowser/TextureThumbnailBrowser.cpp @@ -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; @@ -222,18 +212,13 @@ TextureThumbnailBrowser::TextureThumbnailBrowser(wxWindow* parent) : _mouseWheelScrollIncrement(registry::getValue(RKEY_TEXTURE_MOUSE_WHEEL_INCR)), _showTextureFilter(registry::getValue(RKEY_TEXTURE_SHOW_FILTER)), _showTextureScrollbar(registry::getValue(RKEY_TEXTURE_SHOW_SCROLLBAR)), - _hideUnused(registry::getValue(RKEY_TEXTURES_HIDE_UNUSED)), - _showFavouritesOnly(registry::getValue(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY)), _showNamesKey(RKEY_TEXTURES_SHOW_NAMES), _textureScale(50), _useUniformScale(registry::getValue(RKEY_TEXTURE_USE_UNIFORM_SCALE)), - _showOtherMaterials(registry::getValue(RKEY_TEXTURES_SHOW_OTHER_MATERIALS)), _uniformTextureSize(registry::getValue(RKEY_TEXTURE_UNIFORM_SIZE)), _maxNameLength(registry::getValue(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); @@ -241,7 +226,6 @@ TextureThumbnailBrowser::TextureThumbnailBrowser(wxWindow* parent) : 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(); @@ -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); @@ -405,9 +386,6 @@ void TextureThumbnailBrowser::filterChanged() void TextureThumbnailBrowser::keyChanged() { - _hideUnused = registry::getValue(RKEY_TEXTURES_HIDE_UNUSED); - _showFavouritesOnly = registry::getValue(RKEY_TEXTURES_SHOW_FAVOURITES_ONLY); - _showOtherMaterials = registry::getValue(RKEY_TEXTURES_SHOW_OTHER_MATERIALS); _showTextureFilter = registry::getValue(RKEY_TEXTURE_SHOW_FILTER); _uniformTextureSize = registry::getValue(RKEY_TEXTURE_UNIFORM_SIZE); _useUniformScale = registry::getValue(RKEY_TEXTURE_USE_UNIFORM_SCALE); @@ -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 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; @@ -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 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; @@ -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 @@ -705,11 +641,6 @@ void TextureThumbnailBrowser::refreshTiles() _currentPopulationPosition.reset(); } -void TextureThumbnailBrowser::onActiveShadersChanged() -{ - queueUpdate(); -} - void TextureThumbnailBrowser::focus(const std::string& name) { if (name.empty()) @@ -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()); diff --git a/radiant/ui/texturebrowser/TextureThumbnailBrowser.h b/radiant/ui/texturebrowser/TextureThumbnailBrowser.h index 3a8c723253..b594fc3c12 100644 --- a/radiant/ui/texturebrowser/TextureThumbnailBrowser.h +++ b/radiant/ui/texturebrowser/TextureThumbnailBrowser.h @@ -87,20 +87,11 @@ class TextureThumbnailBrowser : bool _showTextureFilter; // make the texture increments match the grid changes bool _showTextureScrollbar; - // if true, the texture window will only display in-use shaders - // if false, all the shaders in memory are displayed - bool _hideUnused; - bool _showFavouritesOnly; + registry::CachedKey _showNamesKey; int _textureScale; bool _useUniformScale; - // Cached set of material favourites - std::set _favourites; - - // Whether materials not starting with "textures/" should be visible - bool _showOtherMaterials; - // The uniform size (in pixels) that textures are resized to when m_resizeTextures is true. int _uniformTextureSize; @@ -147,13 +138,19 @@ class TextureThumbnailBrowser : // To be implemented by subclasses, this method should iterate over all // materials that should be shown in the view, calling createTile on each material. // (All previous tiles will already have been removed from the view.) - virtual void populateTiles(); + virtual void populateTiles() = 0; void createTileForMaterial(const MaterialPtr& material); -private: + // Returns the currently active filter string or "" if not active. + std::string getFilter(); + + // Returns true if the given material name is filtered out (should be invisible) + bool materialIsFiltered(const std::string& materialName); + void clearFilter(); +private: int getViewportHeight(); // Callback needed for DeferredAdjustment @@ -162,9 +159,6 @@ class TextureThumbnailBrowser : // Repopulates the texture tiles void refreshTiles(); - // This gets called by the ShaderSystem - void onActiveShadersChanged(); - // Return the display width/height of a texture in the texture browser int getTextureWidth(const Texture& tex) const; int getTextureHeight(const Texture& tex) const; @@ -193,11 +187,6 @@ class TextureThumbnailBrowser : void updateScroll(); - /** greebo: Returns the currently active filter string or "" if - * the filter is not active. - */ - std::string getFilter(); - /** * Callback run when filter text was changed. */ @@ -231,11 +220,6 @@ class TextureThumbnailBrowser : */ void selectTextureAt(int mx, int my); - /** greebo: Returns true if the given material is visible, - * taking filter and showUnused into account. - */ - bool materialIsVisible(const MaterialPtr& material); - // wx callbacks bool onRender(); void onScrollChanged(wxScrollEvent& ev);