Skip to content

Commit

Permalink
#6158: EntityClassPreview is inheriting from EntityPreview
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Nov 12, 2022
1 parent 44a1566 commit 285c685
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 15 deletions.
22 changes: 16 additions & 6 deletions libs/wxutil/preview/EntityClassPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
#include "ieclass.h"
#include "ui/ideclpreview.h"
#include "ModelPreview.h"
#include "../dialog/MessageBox.h"

namespace wxutil
{

class EntityClassPreview :
public ModelPreview,
public EntityPreview,
public ui::IDeclarationPreview
{
public:
EntityClassPreview(wxWindow* parent) :
ModelPreview(parent)
EntityPreview(parent)
{}

// Returns the widget that can be packed into the selector container
Expand All @@ -24,8 +25,7 @@ class EntityClassPreview :

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

void SetPreviewDeclName(const std::string& declName) override
Expand All @@ -38,8 +38,18 @@ class EntityClassPreview :
return;
}

setModel(eclass->getAttributeValue("model"));
setSkin(eclass->getAttributeValue("skin"));
try
{
// Create an entity of the selected type
auto entity = GlobalEntityModule().createEntity(eclass);
setEntity(entity);
}
catch (const std::runtime_error&)
{
Messagebox::ShowError(fmt::format(
_("Unable to setup the preview,\ncould not find the entity class '{0}'"),
declName));
}
}
};

Expand Down
35 changes: 29 additions & 6 deletions libs/wxutil/preview/EntityPreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "i18n.h"
#include "ieclass.h"
#include "ifilter.h"
#include "scene/BasicRootNode.h"

#include "../dialog/MessageBox.h"
Expand Down Expand Up @@ -40,7 +41,16 @@ void EntityPreview::setEntity(const IEntityNodePtr& entity)
if (_entity)
{
_rootNode->addChildNode(_entity);

// Remember the entity bounds including children
_untransformedEntityBounds = _entity->worldAABB();
}
else
{
_untransformedEntityBounds = AABB({ 0,0,0 }, { 64,64,64 });
}

queueSceneUpdate();
}

void EntityPreview::setupSceneGraph()
Expand Down Expand Up @@ -71,23 +81,36 @@ void EntityPreview::setupSceneGraph()

AABB EntityPreview::getSceneBounds()
{
if (!_entity)
{
return RenderPreview::getSceneBounds();
}

return _entity->localAABB();
return _untransformedEntityBounds;
}

void EntityPreview::prepareScene()
{
if (_sceneIsReady) return;

// Clear the flag
_sceneIsReady = true;

// Reset the model rotation
resetModelRotation();

if (_entity)
{
// Reset the default view, facing down to the model from diagonally above the bounding box
double distance = _entity->worldAABB().getRadius() * _defaultCamDistanceFactor;

setViewOrigin(Vector3(1, 1, 1) * distance);
setViewAngles(Vector3(34, 135, 0));
}

// Trigger an initial update of the subgraph
GlobalFilterSystem().updateSubgraph(getScene()->root());
}

void EntityPreview::queueSceneUpdate()
{
_sceneIsReady = false;
queueDraw();
}

bool EntityPreview::onPreRender()
Expand Down
2 changes: 2 additions & 0 deletions libs/wxutil/preview/EntityPreview.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class EntityPreview :
// The previewed entity
IEntityNodePtr _entity;

AABB _untransformedEntityBounds;

// The light
scene::INodePtr _light;

Expand Down
3 changes: 0 additions & 3 deletions libs/wxutil/preview/ModelPreview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,6 @@ void ModelPreview::prepareScene()
scene::applyIdlePose(_modelNode, modelDef);
}

// Trigger an initial update of the subgraph
GlobalFilterSystem().updateSubgraph(getScene()->root());

if (_lastModel != _model)
{
// Reset the model rotation
Expand Down

0 comments on commit 285c685

Please sign in to comment.