Skip to content

Commit

Permalink
#5537: Merge common code from ParticleSelector into DeclarationSelector.
Browse files Browse the repository at this point in the history
Expand base class for added flexibility.
  • Loading branch information
codereader committed Sep 16, 2022
1 parent 2a3ee16 commit 072f99f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 79 deletions.
46 changes: 40 additions & 6 deletions radiant/ui/common/DeclarationSelector.cpp
@@ -1,5 +1,8 @@
#include "DeclarationSelector.h"

#include <wx/sizer.h>
#include "wxutil/dataview/ResourceTreeViewToolbar.h"

namespace ui
{

Expand All @@ -12,28 +15,59 @@ DeclarationSelector::DeclarationSelector(wxWindow* parent, decl::Type declType,
wxPanel(parent),
_declType(declType),
_columns(columns),
_treeView(nullptr)
_treeView(nullptr),
_horizontalSizer(nullptr),
_treeViewSizerItem(nullptr)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
createTreeView();

auto* toolbar = new wxutil::ResourceTreeViewToolbar(this, _treeView);

auto treeVbox = new wxBoxSizer(wxVERTICAL);
treeVbox->Add(toolbar, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 6);
treeVbox->Add(_treeView, 1, wxEXPAND);

_horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
_treeViewSizerItem = _horizontalSizer->Add(treeVbox, 1, wxEXPAND);
// the horizontal sizer has room for a preview widget => AddPreviewToRightPane

GetSizer()->Add(_horizontalSizer, 1, wxEXPAND);

// a preview widget can be appended to the vertical sizer => AddPreviewToBottom
}

void DeclarationSelector::AddPreviewToRightPane(wxWindow* preview, int sizerProportion)
{
// Tree view no longer takes full proportion after a full-size preview has been added
if (sizerProportion == 1)
{
_treeViewSizerItem->SetProportion(0);
}

preview->Reparent(this);
_horizontalSizer->Add(preview, sizerProportion, wxEXPAND | wxLEFT, 6);
}

void DeclarationSelector::AddPreviewToBottom(wxWindow* preview, int sizerProportion)
{
preview->Reparent(this);
GetSizer()->Add(preview, sizerProportion, wxEXPAND | wxTOP, 3);
}

void DeclarationSelector::createTreeView()
{
_treeView = new wxutil::DeclarationTreeView(this, decl::Type::Material,
_columns, wxDV_NO_HEADER | wxDV_SINGLE);
_treeView = new wxutil::DeclarationTreeView(this, _declType, _columns, wxDV_NO_HEADER | wxDV_SINGLE);

// Single visible column, containing the directory/decl name and the icon
_treeView->AppendIconTextColumn(_("Value"), _columns.iconAndName.getColumnIndex(),
_treeView->AppendIconTextColumn(decl::getTypeName(_declType), _columns.iconAndName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);

// Use the TreeModel's full string search function
_treeView->AddSearchColumn(_columns.leafName);

// Get selection and connect the changed callback
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &DeclarationSelector::onTreeViewSelectionChanged, this);

GetSizer()->Add(_treeView, 1, wxEXPAND);
}

wxutil::DeclarationTreeView* DeclarationSelector::GetTreeView() const
Expand Down
8 changes: 8 additions & 0 deletions radiant/ui/common/DeclarationSelector.h
Expand Up @@ -20,6 +20,9 @@ class DeclarationSelector :
const wxutil::DeclarationTreeView::Columns& _columns;
wxutil::DeclarationTreeView* _treeView;

wxSizer* _horizontalSizer;
wxSizerItem* _treeViewSizerItem;

public:
// Construct a selector widget with the default set of tree view columns
DeclarationSelector(wxWindow* parent, decl::Type declType);
Expand All @@ -45,6 +48,11 @@ class DeclarationSelector :

protected:
wxutil::DeclarationTreeView* GetTreeView() const;

// Adds a preview widget to the right of the tree view
void AddPreviewToRightPane(wxWindow* preview, int sizerProportion = 1);
void AddPreviewToBottom(wxWindow* preview, int sizerProportion = 0);

const wxutil::DeclarationTreeView::Columns& GetColumns() const;

void PopulateTreeView(const wxutil::IResourceTreePopulator::Ptr& populator);
Expand Down
10 changes: 2 additions & 8 deletions radiant/ui/common/ShaderSelector.cpp
Expand Up @@ -81,18 +81,12 @@ ShaderSelector::ShaderSelector(wxWindow* parent, const std::function<void()>& se
_textureFilter(textureFilter),
_selectionChanged(selectionChanged)
{
// Pack in info panel
createPreview();
_previewCombo = new TexturePreviewCombo(this);
AddPreviewToBottom(_previewCombo);

PopulateTreeView(std::make_shared<ThreadedMaterialLoader>(GetColumns(), _textureFilter));
}

void ShaderSelector::createPreview()
{
_previewCombo = new TexturePreviewCombo(this);
GetSizer()->Add(_previewCombo, 0, wxEXPAND | wxTOP, 3);
}

MaterialPtr ShaderSelector::getSelectedShader()
{
return GlobalMaterialManager().getMaterial(GetSelectedDeclName());
Expand Down
3 changes: 0 additions & 3 deletions radiant/ui/common/ShaderSelector.h
Expand Up @@ -56,9 +56,6 @@ class ShaderSelector :

protected:
void onTreeViewSelectionChanged() override;

private:
void createPreview();
};

} // namespace ui
4 changes: 2 additions & 2 deletions radiant/ui/particles/ParticleChooserDialog.cpp
Expand Up @@ -77,15 +77,15 @@ ParticleChooserDialog::SelectionResult ParticleChooserDialog::RunDialog(bool sho
{
auto* dialog = new ParticleChooserDialog(showClassnameSelector);

dialog->_selector->setSelectedParticle(currentParticle);
dialog->_selector->SetSelectedParticle(currentParticle);

auto result = dialog->ShowModal();

SelectionResult selectionResult;

if (result == wxID_OK)
{
selectionResult.selectedParticle = result == wxID_OK ? dialog->_selector->getSelectedParticle() : "";
selectionResult.selectedParticle = result == wxID_OK ? dialog->_selector->GetSelectedParticle() : "";
selectionResult.selectedClassname = showClassnameSelector ? dialog->getSelectedClassname() : "";
}

Expand Down
58 changes: 11 additions & 47 deletions radiant/ui/particles/ParticleSelector.cpp
Expand Up @@ -7,81 +7,45 @@ namespace ui
{

ParticleSelector::ParticleSelector(wxWindow* parent) :
wxPanel(parent, wxID_ANY),
DeclarationSelector(parent, decl::Type::Particle),
_preview(new wxutil::ParticlePreview(this))
{
SetSizer(new wxBoxSizer(wxVERTICAL));

// Tree view plus toolbar
auto* treeView = createTreeView(this);
auto* toolbar = new wxutil::ResourceTreeViewToolbar(this, treeView);

auto treeVbox = new wxBoxSizer(wxVERTICAL);
treeVbox->Add(toolbar, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 6);
treeVbox->Add(treeView, 1, wxEXPAND);

// Treeview to the left, preview to the right
auto hbox = new wxBoxSizer(wxHORIZONTAL);

hbox->Add(treeVbox, 1, wxEXPAND);
hbox->Add(_preview->getWidget(), 0, wxEXPAND | wxLEFT, 6);

GetSizer()->Add(hbox, 1, wxEXPAND | wxALL, 12);
AddPreviewToRightPane(_preview->getWidget());

GlobalParticlesManager().signal_particlesReloaded().connect(
sigc::mem_fun(this, &ParticleSelector::reloadParticles)
);
}

wxutil::ResourceTreeView* ParticleSelector::createTreeView(wxWindow* parent)
{
_treeView = new wxutil::DeclarationTreeView(parent, decl::Type::Particle, _columns, wxDV_NO_HEADER);
_treeView->SetSize(300, -1);

_treeView->AppendIconTextColumn(_("Particle"), _columns.iconAndName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);

// Apply full-text search to the column
_treeView->AddSearchColumn(_columns.leafName);

// Start loading particles into the view
populateParticleList();

// Connect up the selection changed callback
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ParticleSelector::_onSelChanged, this);

return _treeView;
}

void ParticleSelector::populateParticleList()
{
_treeView->Populate(std::make_shared<ThreadedParticlesLoader>(_columns));
PopulateTreeView(std::make_shared<ThreadedParticlesLoader>(GetColumns()));
}

void ParticleSelector::reloadParticles()
{
populateParticleList();
}

std::string ParticleSelector::getSelectedParticle()
std::string ParticleSelector::GetSelectedParticle()
{
return _treeView->GetSelectedFullname();
return GetTreeView()->GetSelectedFullname();
}

void ParticleSelector::setSelectedParticle(const std::string& particleName)
void ParticleSelector::SetSelectedParticle(const std::string& particleName)
{
_treeView->SetSelectedFullname(particleName);
GetTreeView()->SetSelectedFullname(particleName);
}

void ParticleSelector::_onSelChanged(wxDataViewEvent& ev)
void ParticleSelector::onTreeViewSelectionChanged()
{
// Get the selection and store it
auto item = _treeView->GetSelection();
auto selectedParticle = GetSelectedParticle();

if (item.IsOk())
if (!selectedParticle.empty())
{
wxutil::TreeModel::Row row(item, *_treeView->GetTreeModel());
_preview->setParticle(row[_columns.leafName]);
_preview->setParticle(selectedParticle);
}
}

Expand Down
20 changes: 7 additions & 13 deletions radiant/ui/particles/ParticleSelector.h
@@ -1,8 +1,8 @@
#pragma once

#include <sigc++/trackable.h>
#include <wx/panel.h>

#include "../common/DeclarationSelector.h"
#include "wxutil/dataview/DeclarationTreeView.h"
#include "wxutil/preview/ParticlePreview.h"

Expand All @@ -15,32 +15,26 @@ namespace ui
* Features a resource tree view on the left and a render preview on the right.
*/
class ParticleSelector :
public wxPanel,
public DeclarationSelector,
public sigc::trackable
{
private:
wxutil::DeclarationTreeView::Columns _columns;

// Tree view listing all the particles
wxutil::DeclarationTreeView* _treeView;

// The preview widget
wxutil::ParticlePreviewPtr _preview;

public:
ParticleSelector(wxWindow* parent);

std::string getSelectedParticle();
void setSelectedParticle(const std::string& particleName);
std::string GetSelectedParticle();
void SetSelectedParticle(const std::string& particleName);

private:
wxutil::ResourceTreeView* createTreeView(wxWindow* parent);
protected:
void onTreeViewSelectionChanged() override;

private:
// Populate the list of particles
void populateParticleList();
void reloadParticles();

void _onSelChanged(wxDataViewEvent& ev);
};

}

0 comments on commit 072f99f

Please sign in to comment.