Skip to content

Commit

Permalink
3156: "Reveal in texture browser" context menu item
Browse files Browse the repository at this point in the history
Fixes #3156
  • Loading branch information
ericwa committed May 28, 2020
1 parent 5c4be29 commit b8e9add
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 0 deletions.
6 changes: 6 additions & 0 deletions common/src/View/Actions.cpp
Expand Up @@ -620,6 +620,12 @@ namespace TrenchBroom {
context.view()->rotateTextures(false, MapViewBase::TextureActionMode::Fine);
},
[](ActionExecutionContext& context) { return context.hasDocument(); });
createAction(IO::Path("Controls/Map view/Reveal in texture browser"), QObject::tr("Reveal in texture browser"),
ActionContext::View3D | ActionContext::AnySelection, QKeySequence(),
[](ActionExecutionContext& context) {
context.frame()->revealTexture();
},
[](ActionExecutionContext& context) { return context.hasDocument(); });

/* ========== Tag Actions ========== */
createAction(IO::Path("Controls/Map view/Make structural"), QObject::tr("Make Structural"),
Expand Down
5 changes: 5 additions & 0 deletions common/src/View/CellView.cpp
Expand Up @@ -92,6 +92,11 @@ namespace TrenchBroom {
RenderView::resizeEvent(event);
}

void CellView::scrollToCellInternal(const Cell& cell) {
const float top = cell.cellBounds().top();
m_scrollBar->setSliderPosition(static_cast<int>(top));
}

void CellView::onScrollBarValueChanged() {
update();
}
Expand Down
23 changes: 23 additions & 0 deletions common/src/View/CellView.h
Expand Up @@ -60,6 +60,29 @@ namespace TrenchBroom {
void invalidate();
void clear();
void resizeEvent(QResizeEvent* event) override;

/**
* Scroll to a cell. Pass a visitor of type `const Cell& cell -> bool` that returns true
* for the cell that should be scrolled to.
*/
template <class L>
void scrollToCell(L&& visitor) {
for (size_t i = 0; i < m_layout.size(); ++i) {
const Group& group = m_layout[i];
for (size_t j = 0; j < group.size(); ++j) {
const Row& row = group[j];
for (const Cell& cell : row.cells()) {
const bool foundCell = visitor(cell);
if (foundCell) {
scrollToCellInternal(cell);
return;
}
}
}
}
}
private:
void scrollToCellInternal(const Cell& cell);
private:
void onScrollBarValueChanged();
void onScrollBarActionTriggered(int action);
Expand Down
4 changes: 4 additions & 0 deletions common/src/View/FaceInspector.cpp
Expand Up @@ -60,6 +60,10 @@ namespace TrenchBroom {
return m_faceAttribsEditor->cancelMouseDrag();
}

void FaceInspector::revealTexture(Assets::Texture* texture) {
m_textureBrowser->revealTexture(texture);
}

void FaceInspector::createGui(std::weak_ptr<MapDocument> document, GLContextManager& contextManager) {
m_splitter = new Splitter(Qt::Vertical);
m_splitter->setObjectName("FaceInspector_Splitter");
Expand Down
1 change: 1 addition & 0 deletions common/src/View/FaceInspector.h
Expand Up @@ -50,6 +50,7 @@ namespace TrenchBroom {
~FaceInspector() override;

bool cancelMouseDrag();
void revealTexture(Assets::Texture* texture);
private:
void createGui(std::weak_ptr<MapDocument> document, GLContextManager& contextManager);
QWidget* createFaceAttribsEditor(QWidget* parent, std::weak_ptr<MapDocument> document, GLContextManager& contextManager);
Expand Down
4 changes: 4 additions & 0 deletions common/src/View/Inspector.cpp
Expand Up @@ -69,5 +69,9 @@ namespace TrenchBroom {
bool Inspector::cancelMouseDrag() {
return m_faceInspector->cancelMouseDrag();
}

FaceInspector* Inspector::faceInspector() {
return m_faceInspector;
}
}
}
2 changes: 2 additions & 0 deletions common/src/View/Inspector.h
Expand Up @@ -55,6 +55,8 @@ namespace TrenchBroom {
void connectTopWidgets(MapViewBar* mapViewBar);
void switchToPage(InspectorPage page);
bool cancelMouseDrag();

FaceInspector* faceInspector();
};
}
}
Expand Down
25 changes: 25 additions & 0 deletions common/src/View/MapFrame.cpp
Expand Up @@ -46,6 +46,7 @@
#include "View/ColorButton.h"
#include "View/CompilationDialog.h"
#include "View/EdgeTool.h"
#include "View/FaceInspector.h"
#include "View/FaceTool.h"
#include "View/FrameManager.h"
#include "View/GLContextManager.h"
Expand Down Expand Up @@ -1506,6 +1507,30 @@ namespace TrenchBroom {
gameFactory.saveConfigs(gameName);
}

static std::optional<Assets::Texture*> textureToReveal(std::shared_ptr<MapDocument> document) {
kdl::vector_set<Assets::Texture*> selectedTextures;
for (const Model::BrushFaceHandle& face : document->allSelectedBrushFaces()) {
selectedTextures.insert(face.face().texture());
}
if (selectedTextures.size() == 1) {
return { *selectedTextures.begin() };
}
return std::nullopt;
}

bool MapFrame::canRevealTexture() const {
return textureToReveal(m_document).has_value();
}

void MapFrame::revealTexture() {
auto texture = textureToReveal(m_document);

if (texture) {
m_inspector->switchToPage(InspectorPage::Face);
m_inspector->faceInspector()->revealTexture(texture.value());
}
}

void MapFrame::debugPrintVertices() {
m_document->printVertices();
}
Expand Down
3 changes: 3 additions & 0 deletions common/src/View/MapFrame.h
Expand Up @@ -341,6 +341,9 @@ namespace TrenchBroom {

void showLaunchEngineDialog();

bool canRevealTexture() const;
void revealTexture();

void debugPrintVertices();
void debugCreateBrush();
void debugCreateCube();
Expand Down
6 changes: 6 additions & 0 deletions common/src/View/MapViewBase.cpp
Expand Up @@ -1062,6 +1062,12 @@ namespace TrenchBroom {

menu.addSeparator();

if (mapFrame->canRevealTexture()) {
menu.addAction(tr("Reveal Texture"), mapFrame, &MapFrame::revealTexture);

menu.addSeparator();
}

menu.addMenu(makeEntityGroupsMenu(Assets::EntityDefinitionType::PointEntity));
menu.addMenu(makeEntityGroupsMenu(Assets::EntityDefinitionType::BrushEntity));

Expand Down
4 changes: 4 additions & 0 deletions common/src/View/TextureBrowser.cpp
Expand Up @@ -69,6 +69,10 @@ namespace TrenchBroom {
m_view->setSelectedTexture(selectedTexture);
}

void TextureBrowser::revealTexture(Assets::Texture* texture) {
m_view->revealTexture(texture);
}

void TextureBrowser::setSortOrder(const TextureSortOrder sortOrder) {
m_view->setSortOrder(sortOrder);
switch (sortOrder) {
Expand Down
1 change: 1 addition & 0 deletions common/src/View/TextureBrowser.h
Expand Up @@ -67,6 +67,7 @@ namespace TrenchBroom {

Assets::Texture* selectedTexture() const;
void setSelectedTexture(Assets::Texture* selectedTexture);
void revealTexture(Assets::Texture* texture);

void setSortOrder(TextureSortOrder sortOrder);
void setGroup(bool group);
Expand Down
7 changes: 7 additions & 0 deletions common/src/View/TextureBrowserView.cpp
Expand Up @@ -124,6 +124,13 @@ namespace TrenchBroom {
update();
}

void TextureBrowserView::revealTexture(Assets::Texture* texture) {
scrollToCell([=](const Cell& cell) {
const Assets::Texture* cellTexture = cellData(cell).texture;
return cellTexture == texture;
});
}

void TextureBrowserView::usageCountDidChange() {
invalidate();
update();
Expand Down
2 changes: 2 additions & 0 deletions common/src/View/TextureBrowserView.h
Expand Up @@ -83,6 +83,8 @@ namespace TrenchBroom {

Assets::Texture* selectedTexture() const;
void setSelectedTexture(Assets::Texture* selectedTexture);

void revealTexture(Assets::Texture* texture);
private:
void usageCountDidChange();

Expand Down

0 comments on commit b8e9add

Please sign in to comment.