Skip to content

Commit

Permalink
#1646: Set selected texture when the user clicks on a texture in the …
Browse files Browse the repository at this point in the history
…texture browser.
  • Loading branch information
kduske committed Jan 14, 2017
1 parent a88d111 commit bf48b3f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
4 changes: 4 additions & 0 deletions common/src/View/CellLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ namespace TrenchBroom {
return m_scale;
}

const LayoutBounds& bounds() const {
return cellBounds();
}

const LayoutBounds& cellBounds() const {
return m_cellBounds;
}
Expand Down
33 changes: 18 additions & 15 deletions common/src/View/CellView.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ namespace TrenchBroom {
class CellView : public RenderView {
protected:
typedef CellLayout<CellData, GroupData> Layout;
typedef typename Layout::Group Group;
typedef typename Group::Row Row;
typedef typename Row::Cell Cell;
private:
Layout m_layout;
typename Layout::Group::Row::Cell* m_selectedCell;
Cell* m_selectedCell;
bool m_layoutInitialized;

bool m_valid;
Expand All @@ -53,7 +56,7 @@ namespace TrenchBroom {
wxPoint m_lastMousePos;

void updateScrollBar() {
if (m_scrollBar != NULL) {
if (m_scrollBar != nullptr) {
int position = m_scrollBar->GetThumbPosition();
int thumbSize = GetClientSize().y;
int range = static_cast<int>(m_layout.height());
Expand Down Expand Up @@ -81,7 +84,7 @@ namespace TrenchBroom {
reloadLayout();
}
public:
CellView(wxWindow* parent, GLContextManager& contextManager, wxGLAttributes attribs, wxScrollBar* scrollBar = NULL) :
CellView(wxWindow* parent, GLContextManager& contextManager, wxGLAttributes attribs, wxScrollBar* scrollBar = nullptr) :
RenderView(parent, contextManager, attribs),
m_layoutInitialized(false),
m_valid(false),
Expand All @@ -93,7 +96,7 @@ namespace TrenchBroom {
Bind(wxEVT_MOTION, &CellView::OnMouseMove, this);
Bind(wxEVT_MOUSE_CAPTURE_LOST, &CellView::OnMouseCaptureLost, this);

if (m_scrollBar != NULL) {
if (m_scrollBar != nullptr) {
m_scrollBar->Bind(wxEVT_SCROLL_LINEUP, &CellView::OnScrollBarLineUp, this);
m_scrollBar->Bind(wxEVT_SCROLL_LINEDOWN, &CellView::OnScrollBarLineDown, this);
m_scrollBar->Bind(wxEVT_SCROLL_PAGEUP, &CellView::OnScrollBarPageUp, this);
Expand Down Expand Up @@ -184,10 +187,10 @@ namespace TrenchBroom {

void startDrag(const wxMouseEvent& event) {
if (dndEnabled()) {
int top = m_scrollBar != NULL ? m_scrollBar->GetThumbPosition() : 0;
int top = m_scrollBar != nullptr ? m_scrollBar->GetThumbPosition() : 0;
float x = static_cast<float>(event.GetX());
float y = static_cast<float>(event.GetY() + top);
const typename Layout::Group::Row::Cell* cell = NULL;
const Cell* cell = nullptr;
if (m_layout.cellAt(x, y, &cell)) {
/*
wxImage* feedbackImage = dndImage(*cell);
Expand All @@ -204,7 +207,7 @@ namespace TrenchBroom {
}

void scroll(const wxMouseEvent& event) {
if (m_scrollBar != NULL) {
if (m_scrollBar != nullptr) {
const wxPoint mousePosition = event.GetPosition();
const wxCoord delta = mousePosition.y - m_lastMousePos.y;
const wxCoord newThumbPosition = m_scrollBar->GetThumbPosition() - delta;
Expand All @@ -214,10 +217,10 @@ namespace TrenchBroom {
}

void updateTooltip(const wxMouseEvent& event) {
int top = m_scrollBar != NULL ? m_scrollBar->GetThumbPosition() : 0;
int top = m_scrollBar != nullptr ? m_scrollBar->GetThumbPosition() : 0;
float x = static_cast<float>(event.GetX());
float y = static_cast<float>(event.GetY() + top);
const typename Layout::Group::Row::Cell* cell = NULL;
const typename Layout::Group::Row::Cell* cell = nullptr;
if (m_layout.cellAt(x, y, &cell))
SetToolTip(tooltip(*cell));
else
Expand All @@ -240,14 +243,14 @@ namespace TrenchBroom {
}

void OnMouseLeftUp(wxMouseEvent& event) {
int top = m_scrollBar != NULL ? m_scrollBar->GetThumbPosition() : 0;
int top = m_scrollBar != nullptr ? m_scrollBar->GetThumbPosition() : 0;
float x = static_cast<float>(event.GetX());
float y = static_cast<float>(event.GetY() + top);
doLeftClick(m_layout, x, y);
}

void OnMouseWheel(wxMouseEvent& event) {
if (m_scrollBar != NULL) {
if (m_scrollBar != nullptr) {
const float top = static_cast<float>(m_scrollBar->GetThumbPosition());
float newTop = event.GetWheelRotation() < 0 ? m_layout.rowPosition(top, 1) : m_layout.rowPosition(top, -1);
newTop = std::max(0.0f, std::ceil(newTop - m_layout.rowMargin()));
Expand All @@ -263,7 +266,7 @@ namespace TrenchBroom {
if (!m_layoutInitialized)
initLayout();

const int top = m_scrollBar != NULL ? m_scrollBar->GetThumbPosition() : 0;
const int top = m_scrollBar != nullptr ? m_scrollBar->GetThumbPosition() : 0;
const wxRect visibleRect = wxRect(wxPoint(0, top), GetClientSize());

const float y = static_cast<float>(visibleRect.GetY());
Expand Down Expand Up @@ -298,9 +301,9 @@ namespace TrenchBroom {
virtual bool dndEnabled() { return false; }
virtual void dndWillStart() {}
virtual void dndDidEnd() {}
virtual wxImage dndImage(const typename Layout::Group::Row::Cell& cell) { assert(false); return wxImage(); }
virtual wxString dndData(const typename Layout::Group::Row::Cell& cell) { assert(false); return ""; }
virtual wxString tooltip(const typename Layout::Group::Row::Cell& cell) { return ""; }
virtual wxImage dndImage(const Cell& cell) { assert(false); return wxImage(); }
virtual wxString dndData(const Cell& cell) { assert(false); return ""; }
virtual wxString tooltip(const Cell& cell) { return ""; }
};
}
}
Expand Down
13 changes: 7 additions & 6 deletions common/src/View/ReplaceTextureDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ namespace TrenchBroom {
ReplaceTextureDialog::ReplaceTextureDialog(wxWindow* parent, MapDocumentWPtr document, GLContextManager& contextManager) :
wxDialog(parent, wxID_ANY, "Replace Texture", wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
m_document(document),
m_subjectBrowser(NULL),
m_replacementBrowser(NULL) {
m_subjectBrowser(nullptr),
m_replacementBrowser(nullptr) {
createGui(contextManager);
}

void ReplaceTextureDialog::OnReplace(wxCommandEvent& event) {
if (IsBeingDeleted()) return;

const Assets::Texture* subject = m_subjectBrowser->selectedTexture();
ensure(subject != NULL, "subject is null");
ensure(subject != nullptr, "subject is null");

Assets::Texture* replacement = m_replacementBrowser->selectedTexture();
ensure(replacement != NULL, "replacement is null");
ensure(replacement != nullptr, "replacement is null");

MapDocumentSPtr document = lock(m_document);
const Model::BrushFaceList faces = getApplicableFaces();
Expand Down Expand Up @@ -83,7 +83,7 @@ namespace TrenchBroom {
}

const Assets::Texture* subject = m_subjectBrowser->selectedTexture();
ensure(subject != NULL, "subject is null");
ensure(subject != nullptr, "subject is null");

Model::BrushFaceList result;
std::copy_if(std::begin(faces), std::end(faces), std::back_inserter(result), [&subject](const Model::BrushFace* face) { return face->texture() == subject; });
Expand All @@ -95,7 +95,7 @@ namespace TrenchBroom {

const Assets::Texture* subject = m_subjectBrowser->selectedTexture();
const Assets::Texture* replacement = m_replacementBrowser->selectedTexture();
event.Enable(subject != NULL && replacement != NULL);
event.Enable(subject != nullptr && replacement != nullptr);
}

void ReplaceTextureDialog::createGui(GLContextManager& contextManager) {
Expand All @@ -111,6 +111,7 @@ namespace TrenchBroom {

TitledPanel* replacementPanel = new TitledPanel(this, "Replace with");
m_replacementBrowser = new TextureBrowser(replacementPanel->getPanel(), m_document, contextManager);
m_replacementBrowser->setSelectedTexture(nullptr); // Override the current texture.

wxSizer* replacementPanelSizer = new wxBoxSizer(wxVERTICAL);
replacementPanelSizer->Add(m_replacementBrowser, 1, wxEXPAND);
Expand Down
8 changes: 7 additions & 1 deletion common/src/View/TextureBrowserView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,17 @@ namespace TrenchBroom {
const Layout::Group::Row::Cell* result = NULL;
if (layout.cellAt(x, y, &result)) {
if (!result->item().texture->overridden()) {
Assets::Texture* texture = result->item().texture;

TextureSelectedCommand command;
command.SetEventObject(this);
command.SetId(GetId());
command.setTexture(result->item().texture);
command.setTexture(texture);
ProcessEvent(command);

if (command.IsAllowed())
setSelectedTexture(texture);

Refresh();
}
}
Expand Down

0 comments on commit bf48b3f

Please sign in to comment.