Skip to content

Commit

Permalink
#5537: Move more preview-related code to the SkinSelector widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 18, 2022
1 parent ac26f79 commit af96a3f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 87 deletions.
1 change: 1 addition & 0 deletions install/user.xml
Expand Up @@ -340,6 +340,7 @@
<!-- Default Width/Height Fraction defines the proportion of the screen size
this window will take if no size information has been saved so far -->
<EntityClassChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
<SkinChooser defaultWidthFraction="0.7" defaultHeightFraction="0.8" />
</windowStates>
</ui>
</user>
41 changes: 41 additions & 0 deletions libs/wxutil/preview/SkinPreview.h
@@ -0,0 +1,41 @@
#pragma once

#include "ui/ideclpreview.h"
#include "ModelPreview.h"

namespace wxutil
{

class SkinPreview :
public ModelPreview,
public ui::IDeclarationPreview
{
private:
std::string _model;

public:
SkinPreview(wxWindow* parent, const std::string& model) :
ModelPreview(parent),
_model(model)
{}

// Returns the widget that can be packed into the selector container
wxWindow* GetPreviewWidget() override
{
return _mainPanel;
}

void ClearPreview() override
{
setModel({});
setSkin({});
}

void SetPreviewDeclName(const std::string& declName) override
{
setModel(_model);
setSkin(declName);
}
};

}
123 changes: 55 additions & 68 deletions radiant/ui/common/SkinChooser.cpp
Expand Up @@ -9,9 +9,10 @@
#include "ifavourites.h"
#include "debugging/ScopedDebugTimer.h"
#include "wxutil/dataview/ThreadedDeclarationTreePopulator.h"
#include "wxutil/dataview/TreeView.h"
#include "wxutil/dataview/VFSTreePopulator.h"
#include "wxutil/decl/DeclarationSelector.h"
#include "wxutil/preview/SkinPreview.h"
#include "ui/modelselector/MaterialsList.h"

namespace ui
{
Expand Down Expand Up @@ -127,21 +128,42 @@ class SkinSelector :
public wxutil::DeclarationSelector
{
private:
std::string _model;

wxDataViewItem _allSkinsItem;
wxDataViewItem _matchingSkinsItem;

std::unique_ptr<wxutil::SkinPreview> _preview;
MaterialsList* _materialsList;

public:
SkinSelector(wxWindow* parent) :
DeclarationSelector(parent, decl::Type::Skin)
SkinSelector(wxWindow* parent, const std::string& model) :
DeclarationSelector(parent, decl::Type::Skin),
_model(model),
_preview(new wxutil::SkinPreview(this, _model)),
_materialsList(new MaterialsList(this, _preview->getRenderSystem()))
{
// We want to control ourselves which items are expanded after population
GetTreeView()->SetExpandTopLevelItemsAfterPopulation(false);
GetTreeView()->Bind(wxutil::EV_TREEVIEW_POPULATION_FINISHED, &SkinSelector::onTreeViewPopulationFinished, this);

//AddPreviewToBottom(_materialsList); // TODO
AddPreviewToRightPane(_preview.get());

// Models are lazy-loaded, subscribe to the preview's event
_preview->signal_ModelLoaded().connect(sigc::mem_fun(this, &SkinSelector::onPreviewModelLoaded));

_materialsList->SetMinClientSize(wxSize(-1, 140));

// Refresh preview when material visibility changed
_materialsList->signal_visibilityChanged().connect(
sigc::mem_fun(*_preview, &wxutil::ModelPreview::queueDraw)
);
}

void Populate(const std::string& model)
void Populate()
{
PopulateTreeView(std::make_shared<ThreadedSkinLoader>(GetColumns(), model, _allSkinsItem, _matchingSkinsItem));
PopulateTreeView(std::make_shared<ThreadedSkinLoader>(GetColumns(), _model, _allSkinsItem, _matchingSkinsItem));
}

void SetSelectedDeclName(const std::string& skin) override
Expand All @@ -167,6 +189,25 @@ class SkinSelector :
}

private:
void onPreviewModelLoaded(const model::ModelNodePtr& model)
{
updateMaterialsList();
}

void updateMaterialsList()
{
// Update the material list
auto modelNode = Node_getModel(_preview->getModelNode());

if (!modelNode)
{
_materialsList->clear();
return;
}

_materialsList->updateFromModel(modelNode->getIModel());
}

void onTreeViewPopulationFinished(wxutil::ResourceTreeView::PopulationFinishedEvent& ev)
{
// Make sure the "matching skins" item is expanded
Expand All @@ -175,10 +216,10 @@ class SkinSelector :
}
};

SkinChooser::SkinChooser() :
DialogBase(_(WINDOW_TITLE)),
_selector(nullptr),
_materialsList(nullptr)
SkinChooser::SkinChooser(const std::string& model) :
DialogBase(_(WINDOW_TITLE), "SkinChooser"),
_model(model),
_selector(nullptr)
{
populateWindow();
}
Expand All @@ -190,7 +231,7 @@ void SkinChooser::populateWindow()
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);

_selector = new SkinSelector(this);
_selector = new SkinSelector(this, _model);

#if 0
wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition,
Expand All @@ -215,14 +256,7 @@ void SkinChooser::populateWindow()
_treeView->Bind(wxutil::EV_TREEVIEW_POPULATION_FINISHED, &SkinChooser::_onTreeViewPopulationFinished, this);

_treeViewToolbar = new wxutil::ResourceTreeViewToolbar(leftPanel, _treeView);
#endif
// Preview
_preview.reset(new wxutil::ModelPreview(this));
_preview->getWidget()->SetMinClientSize(wxSize(GetSize().GetWidth() / 3, -1));

_materialsList = new MaterialsList(this, _preview->getRenderSystem());
_materialsList->SetMinClientSize(wxSize(-1, 140));
#if 0
// Refresh preview when material visibility changed
_materialsList->signal_visibilityChanged().connect(
sigc::mem_fun(*_preview, &wxutil::ModelPreview::queueDraw)
Expand All @@ -237,10 +271,6 @@ void SkinChooser::populateWindow()

// Pack treeview and preview
splitter->SplitVertically(leftPanel, _preview->getWidget());
#endif
FitToScreen(0.8f, 0.8f);

#if 0
// Set the default size of the window
splitter->SetSashPosition(static_cast<int>(GetSize().GetWidth() * 0.3f));

Expand All @@ -251,6 +281,8 @@ void SkinChooser::populateWindow()
vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxTOP, 12);

Bind( wxEVT_DATAVIEW_ITEM_ACTIVATED, &SkinChooser::_onItemActivated, this );

RegisterPersistableObject(_selector);
}

int SkinChooser::ShowModal()
Expand All @@ -260,11 +292,6 @@ int SkinChooser::ShowModal()
// Display the model in the window title
SetTitle(std::string(_(WINDOW_TITLE)) + ": " + _model);

// Models are lazy-loaded, subscribe to the preview's event
_modelLoadedConn.disconnect();
_modelLoadedConn = _preview->signal_ModelLoaded().connect(
sigc::mem_fun(this, &SkinChooser::_onPreviewModelLoaded));

int returnCode = DialogBase::ShowModal();

if (returnCode == wxID_OK)
Expand All @@ -278,9 +305,6 @@ int SkinChooser::ShowModal()
_lastSkin = _prevSkin;
}

_modelLoadedConn.disconnect();
_preview->setModel(""); // release model

return returnCode;
}

Expand All @@ -293,7 +317,7 @@ void SkinChooser::_onItemActivated(wxDataViewEvent& ev)
// Populate the list of skins
void SkinChooser::populateSkins()
{
_selector->Populate(_model);
_selector->Populate();
}

std::string SkinChooser::getSelectedSkin()
Expand All @@ -304,15 +328,13 @@ std::string SkinChooser::getSelectedSkin()
void SkinChooser::setSelectedSkin(const std::string& skin)
{
_selector->SetSelectedDeclName(skin);

handleSelectionChange();
}

// Static method to display singleton instance and choose a skin
std::string SkinChooser::chooseSkin(const std::string& model,
const std::string& prev)
{
auto dialog = new SkinChooser();
auto dialog = new SkinChooser(model);

dialog->_model = model;
dialog->_prevSkin = prev;
Expand All @@ -326,41 +348,6 @@ std::string SkinChooser::chooseSkin(const std::string& model,
return selectedSkin;
}

void SkinChooser::handleSelectionChange()
{
auto selectedSkin = getSelectedSkin();

// Set the model preview to show the model with the selected skin
_preview->setModel(_model);
_preview->setSkin(selectedSkin);

updateMaterialsList();
}

void SkinChooser::updateMaterialsList()
{
// Update the material list
auto modelNode = Node_getModel(_preview->getModelNode());

if (!modelNode)
{
_materialsList->clear();
return;
}

_materialsList->updateFromModel(modelNode->getIModel());
}

void SkinChooser::_onPreviewModelLoaded(const model::ModelNodePtr& model)
{
updateMaterialsList();
}

void SkinChooser::_onSelChanged(wxDataViewEvent& ev)
{
handleSelectionChange();
}

void SkinChooser::_onTreeViewPopulationFinished(wxutil::ResourceTreeView::PopulationFinishedEvent& ev)
{
// Select the active skin
Expand Down
23 changes: 4 additions & 19 deletions radiant/ui/common/SkinChooser.h
Expand Up @@ -4,10 +4,8 @@
#include "imodel.h"

#include "wxutil/dialog/DialogBase.h"
#include "wxutil/preview/ModelPreview.h"
#include <string>

#include "ui/modelselector/MaterialsList.h"
#include "wxutil/dataview/ResourceTreeView.h"

namespace ui
Expand All @@ -23,24 +21,18 @@ class SkinChooser :
public wxutil::DialogBase
{
private:
SkinSelector* _selector;
MaterialsList* _materialsList;

// The model name to use for skin matching
std::string _model;

SkinSelector* _selector;

// The last skin selected, and the original (previous) skin
std::string _lastSkin;
std::string _prevSkin;

// Model preview widget
wxutil::ModelPreviewPtr _preview;

sigc::connection _modelLoadedConn;

private:
// Constructor creates widgets
SkinChooser();
SkinChooser(const std::string& model);

// Widget creation functions
void populateWindow();
Expand All @@ -49,23 +41,16 @@ class SkinChooser :
void populateSkins();

// callbacks
void _onSelChanged(wxDataViewEvent& ev);
void _onTreeViewPopulationFinished(wxutil::ResourceTreeView::PopulationFinishedEvent& ev);

// Retrieve the currently selected skin
std::string getSelectedSkin();
void setSelectedSkin(const std::string& skin);

void handleSelectionChange();
void updateMaterialsList();

void _onItemActivated( wxDataViewEvent& ev );
void _onPreviewModelLoaded(const model::ModelNodePtr& model);

public:

// Override Dialogbase
int ShowModal();
int ShowModal() override;

/** Display the dialog and return the skin chosen by the user, or an empty
* string if no selection was made. This static method enters are recursive
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/wxutillib.vcxproj
Expand Up @@ -192,6 +192,7 @@
<ClInclude Include="..\..\libs\wxutil\preview\ModelPreview.h" />
<ClInclude Include="..\..\libs\wxutil\preview\ParticlePreview.h" />
<ClInclude Include="..\..\libs\wxutil\preview\RenderPreview.h" />
<ClInclude Include="..\..\libs\wxutil\preview\SkinPreview.h" />
<ClInclude Include="..\..\libs\wxutil\ScopeTimer.h" />
<ClInclude Include="..\..\libs\wxutil\ScrollWindow.h" />
<ClInclude Include="..\..\libs\wxutil\SerialisableWidgets.h" />
Expand Down
3 changes: 3 additions & 0 deletions tools/msvc/wxutillib.vcxproj.filters
Expand Up @@ -182,6 +182,9 @@
<Filter>preview</Filter>
</ClInclude>
<ClInclude Include="..\..\libs\wxutil\WindowState.h" />
<ClInclude Include="..\..\libs\wxutil\preview\SkinPreview.h">
<Filter>preview</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\libs\wxutil\dialog\MessageBox.cpp">
Expand Down

0 comments on commit af96a3f

Please sign in to comment.