Skip to content

Commit

Permalink
Add Animation Chooser interface and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Dec 25, 2016
1 parent 1e5fb84 commit 39484f0
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 26 deletions.
37 changes: 37 additions & 0 deletions include/ianimationchooser.h
@@ -0,0 +1,37 @@
#pragma once

#include <string>

namespace ui
{

class IAnimationChooser
{
public:
virtual ~IAnimationChooser() {}

struct Result
{
// The selected model
std::string model;

// The selected animation
std::string anim;

bool cancelled()
{
return model.empty() && anim.empty();
}
};

// Run the dialog and return the selected model/anim combination.
// The dialog will enter a new event loop and block the UI until it's closed again.
// The returned Result will be empty if the user clicks cancel.
virtual Result runDialog(const std::string& preselectModel = std::string(),
const std::string& preselectAnim = std::string()) = 0;

// Destroys the window. Don't rely on the destructor, it won't call Destroy() for you.
virtual void destroyDialog() = 0;
};

}
10 changes: 8 additions & 2 deletions include/idialogmanager.h
Expand Up @@ -89,7 +89,8 @@ typedef std::shared_ptr<IFileChooser> IFileChooserPtr;
class IDirChooser;
typedef std::shared_ptr<IDirChooser> IDirChooserPtr;

class IResourceChooser;
class IResourceChooser; // defined in iresourcechooser.h
class IAnimationChooser; // defined in ianimationchooser.h

class IDialogManager
{
Expand Down Expand Up @@ -150,9 +151,14 @@ class IDialogManager
virtual IDirChooserPtr createDirChooser(const std::string& title) = 0;

// Creates and returns a new Dialog class for selecting a sound shader.
// It's the responsibility of the client code to call Destroy() on the returned object.
// It's the responsibility of the client code to call destroyDialog() on the returned object.
// Optionally specify a parent window the dialog should be a child of.
virtual IResourceChooser* createSoundShaderChooser(wxWindow* parent = nullptr) = 0;

// Creates and returns a new Dialog class for selecting an MD5 anim.
// It's the responsibility of the client code to call destroyDialog() on the returned object.
// Optionally specify a parent window the dialog should be a child of.
virtual IAnimationChooser* createAnimationChooser(wxWindow* parent = nullptr) = 0;
};

} // namespace ui
Expand Down
3 changes: 1 addition & 2 deletions include/iresourcechooser.h
Expand Up @@ -22,13 +22,12 @@ class IResourceChooser
virtual ~IResourceChooser() {}

// Run the dialog and return the selected shader. The dialog will enter a
// new event loop and block the UI until its closed again.
// new event loop and block the UI until it's closed again.
// The returned string will be empty if the user clicks cancel.
virtual std::string chooseResource(const std::string& preselected = std::string()) = 0;

// Destroys the window. Don't rely on the destructor, it won't call Destroy() for you.
virtual void destroyDialog() = 0;
};
typedef std::shared_ptr<IResourceChooser> IResourceChooserPtr;

}
6 changes: 6 additions & 0 deletions plugins/uimanager/DialogManager.cpp
Expand Up @@ -7,6 +7,7 @@
#include "wxutil/FileChooser.h"
#include "wxutil/DirChooser.h"
#include "SoundChooser.h"
#include "animationpreview/MD5AnimationChooser.h"

namespace ui
{
Expand Down Expand Up @@ -83,4 +84,9 @@ IResourceChooser* DialogManager::createSoundShaderChooser(wxWindow* parent)
return new SoundChooser(parent);
}

IAnimationChooser* DialogManager::createAnimationChooser(wxWindow* parent)
{
return new MD5AnimationChooser(parent);
}

} // namespace ui
1 change: 1 addition & 0 deletions plugins/uimanager/DialogManager.h
Expand Up @@ -33,6 +33,7 @@ class DialogManager :
IDirChooserPtr createDirChooser(const std::string& title) override;

IResourceChooser* createSoundShaderChooser(wxWindow* parent) override;
IAnimationChooser* createAnimationChooser(wxWindow* parent) override;

private:
void cleanupOldDialogs();
Expand Down
1 change: 1 addition & 0 deletions plugins/uimanager/Makefile.am
Expand Up @@ -16,6 +16,7 @@ uimanager_la_LIBADD = $(top_builddir)/libs/wxutil/libwxutil.la \
$(top_builddir)/libs/xmlutil/libxmlutil.la
uimanager_la_SOURCES = MenuItem.cpp \
GroupDialog.cpp \
animationpreview/MD5AnimationChooser.cpp \
animationpreview/MD5AnimationViewer.cpp \
animationpreview/AnimationPreview.cpp \
colourscheme/ColourScheme.cpp \
Expand Down
36 changes: 36 additions & 0 deletions plugins/uimanager/animationpreview/MD5AnimationChooser.cpp
@@ -0,0 +1,36 @@
#include "MD5AnimationChooser.h"

namespace ui
{

MD5AnimationChooser::MD5AnimationChooser(wxWindow* parent) :
MD5AnimationViewer(parent, RunMode::Selection)
{}

MD5AnimationChooser::Result MD5AnimationChooser::runDialog(const std::string& preselectModel,
const std::string& preselectAnim)
{
MD5AnimationChooser::Result result;

// TODO: Pre-select

if (MD5AnimationViewer::ShowModal() == wxID_OK)
{
result.model = getSelectedModel();
result.anim = getSelectedAnim();
}
else
{
result.model.clear();
result.anim.clear();
}

return result;
}

void MD5AnimationChooser::destroyDialog()
{
Destroy();
}

}
22 changes: 22 additions & 0 deletions plugins/uimanager/animationpreview/MD5AnimationChooser.h
@@ -0,0 +1,22 @@
#pragma once

#include "ianimationchooser.h"
#include "MD5AnimationViewer.h"

namespace ui
{

class MD5AnimationChooser :
public MD5AnimationViewer,
public IAnimationChooser
{
public:
MD5AnimationChooser(wxWindow* parent = nullptr);

Result runDialog(const std::string& preselectModel = std::string(),
const std::string& preselectAnim = std::string()) override;

void destroyDialog() override;
};

}
67 changes: 51 additions & 16 deletions plugins/uimanager/animationpreview/MD5AnimationViewer.cpp
Expand Up @@ -12,39 +12,49 @@
namespace ui
{

MD5AnimationViewer::MD5AnimationViewer() :
wxutil::DialogBase(_("MD5 Animation Viewer")),
MD5AnimationViewer::MD5AnimationViewer(wxWindow* parent, RunMode runMode) :
wxutil::DialogBase(_("MD5 Animation Viewer"), parent),
_runMode(runMode),
_modelList(new wxutil::TreeModel(_modelColumns)),
_modelPopulator(_modelList),
_animList(new wxutil::TreeModel(_animColumns, true))
{
SetSizer(new wxBoxSizer(wxVERTICAL));

wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition,
wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSP_3D | wxSP_LIVE_UPDATE);
splitter->SetMinimumPaneSize(10); // disallow unsplitting
splitter->SetMinimumPaneSize(10); // disallow unsplitting

// Preview goes to the right
// Preview goes to the right
_preview.reset(new AnimationPreview(splitter));

splitter->SplitVertically(createListPane(splitter), _preview->getWidget());

GetSizer()->Add(splitter, 1, wxEXPAND | wxALL, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxCLOSE), 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
SetAffirmativeId(wxID_CLOSE);

FitToScreen(0.8f, 0.6f);
if (_runMode == RunMode::Selection)
{
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
SetAffirmativeId(wxID_OK);
}
else
{
GetSizer()->Add(CreateStdDialogButtonSizer(wxCLOSE), 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
SetAffirmativeId(wxID_CLOSE);
}

FitToScreen(0.8f, 0.7f);

// Set the default size of the window
splitter->SetSashPosition(static_cast<int>(GetSize().GetWidth() * 0.2f));
splitter->SetSashPosition(static_cast<int>(GetSize().GetWidth() * 0.25f));

// Populate with model names
populateModelList();
}

void MD5AnimationViewer::Show(const cmd::ArgumentList& args)
{
MD5AnimationViewer* viewer = new MD5AnimationViewer;
MD5AnimationViewer* viewer = new MD5AnimationViewer(nullptr, RunMode::ViewOnly);

viewer->ShowModal();
viewer->Destroy();
Expand Down Expand Up @@ -114,7 +124,7 @@ wxWindow* MD5AnimationViewer::createAnimTreeView(wxWindow* parent)

void MD5AnimationViewer::_onModelSelChanged(wxDataViewEvent& ev)
{
IModelDefPtr modelDef = getSelectedModel();
IModelDefPtr modelDef = getSelectedModelDef();

if (!modelDef)
{
Expand All @@ -131,23 +141,48 @@ void MD5AnimationViewer::_onModelSelChanged(wxDataViewEvent& ev)
populateAnimationList();
}

IModelDefPtr MD5AnimationViewer::getSelectedModel()
std::string MD5AnimationViewer::getSelectedModel()
{
wxDataViewItem item = _modelTreeView->GetSelection();

if (!item.IsOk())
{
return IModelDefPtr();
return std::string();
}

wxutil::TreeModel::Row row(item, *_modelList);

return GlobalEntityClassManager().findModel(row[_modelColumns.name]);
return static_cast<std::string>(row[_modelColumns.name]);
}

std::string MD5AnimationViewer::getSelectedAnim()
{
wxDataViewItem item = _animTreeView->GetSelection();

if (!item.IsOk())
{
return std::string();
}

wxutil::TreeModel::Row row(item, *_animList);
return static_cast<std::string>(row[_animColumns.name]);
}

IModelDefPtr MD5AnimationViewer::getSelectedModelDef()
{
std::string modelDefName = getSelectedModel();

if (modelDefName.empty())
{
return IModelDefPtr();
}

return GlobalEntityClassManager().findModel(modelDefName);
}

void MD5AnimationViewer::_onAnimSelChanged(wxDataViewEvent& ev)
{
IModelDefPtr modelDef = getSelectedModel();
IModelDefPtr modelDef = getSelectedModelDef();

if (!modelDef)
{
Expand Down Expand Up @@ -198,7 +233,7 @@ void MD5AnimationViewer::populateAnimationList()
{
_animList->Clear();

IModelDefPtr modelDef = getSelectedModel();
IModelDefPtr modelDef = getSelectedModelDef();

if (!modelDef) return;

Expand Down
20 changes: 14 additions & 6 deletions plugins/uimanager/animationpreview/MD5AnimationViewer.h
Expand Up @@ -42,7 +42,15 @@ class MD5AnimationViewer :
{}
};

enum class RunMode
{
ViewOnly, // viewing purposes, provides just an OK button
Selection, // selection purposes
};

private:
RunMode _runMode;

ModelListColumns _modelColumns;

// Liststore for the model list, and its selection object
Expand All @@ -60,9 +68,10 @@ class MD5AnimationViewer :
// Animation preview widget
AnimationPreviewPtr _preview;

public:
MD5AnimationViewer();
protected:
MD5AnimationViewer(wxWindow* parent, RunMode runMode);

public:
static void Show(const cmd::ArgumentList& args);

void visit(const IModelDefPtr& modelDef);
Expand All @@ -72,9 +81,8 @@ class MD5AnimationViewer :
const std::string& path,
bool isExplicit);

protected:
// Override BlockingTransientWindow::_postShow()
void _postShow();
std::string getSelectedModel();
std::string getSelectedAnim();

private:
// callbacks
Expand All @@ -89,7 +97,7 @@ class MD5AnimationViewer :
void populateModelList();
void populateAnimationList();

IModelDefPtr getSelectedModel();
IModelDefPtr getSelectedModelDef();
};

}
1 change: 1 addition & 0 deletions tools/msvc2015/include.vcxproj
Expand Up @@ -109,6 +109,7 @@
<ClInclude Include="..\..\include\GLProgramAttributes.h" />
<ClInclude Include="..\..\include\i18n.h" />
<ClInclude Include="..\..\include\iaasfile.h" />
<ClInclude Include="..\..\include\ianimationchooser.h" />
<ClInclude Include="..\..\include\iarchive.h" />
<ClInclude Include="..\..\include\ibrush.h" />
<ClInclude Include="..\..\include\icamera.h" />
Expand Down
2 changes: 2 additions & 0 deletions tools/msvc2015/uimanager.vcxproj
Expand Up @@ -316,6 +316,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\..\plugins\uimanager\animationpreview\AnimationPreview.cpp" />
<ClCompile Include="..\..\plugins\uimanager\animationpreview\MD5AnimationChooser.cpp" />
<ClCompile Include="..\..\plugins\uimanager\animationpreview\MD5AnimationViewer.cpp" />
<ClCompile Include="..\..\plugins\uimanager\DialogManager.cpp" />
<ClCompile Include="..\..\plugins\uimanager\FilterMenu.cpp" />
Expand All @@ -333,6 +334,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\plugins\uimanager\animationpreview\AnimationPreview.h" />
<ClInclude Include="..\..\plugins\uimanager\animationpreview\MD5AnimationChooser.h" />
<ClInclude Include="..\..\plugins\uimanager\animationpreview\MD5AnimationViewer.h" />
<ClInclude Include="..\..\plugins\uimanager\DialogManager.h" />
<ClInclude Include="..\..\plugins\uimanager\FilterMenu.h" />
Expand Down
6 changes: 6 additions & 0 deletions tools/msvc2015/uimanager.vcxproj.filters
Expand Up @@ -58,6 +58,9 @@
<ClCompile Include="..\..\plugins\uimanager\animationpreview\MD5AnimationViewer.cpp">
<Filter>src\animationpreview</Filter>
</ClCompile>
<ClCompile Include="..\..\plugins\uimanager\animationpreview\MD5AnimationChooser.cpp">
<Filter>src\animationpreview</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\plugins\uimanager\DialogManager.h">
Expand Down Expand Up @@ -108,6 +111,9 @@
<ClInclude Include="..\..\plugins\uimanager\animationpreview\MD5AnimationViewer.h">
<Filter>src\animationpreview</Filter>
</ClInclude>
<ClInclude Include="..\..\plugins\uimanager\animationpreview\MD5AnimationChooser.h">
<Filter>src\animationpreview</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\plugins\uimanager\uimanager.def">
Expand Down

0 comments on commit 39484f0

Please sign in to comment.