1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#include "ParticlePreview.h"
#include "ui/ieventmanager.h"
#include "ientity.h"
#include "ieclass.h"
#include "iparticles.h"
#include "iparticlestage.h"
#include "i18n.h"
#include "scene/Node.h"
#include "scene/BasicRootNode.h"
#include "entitylib.h"
#include "string/string.h"
#include "wxutil/GLWidget.h"
#include "wxutil/dialog/MessageBox.h"
#include "../Bitmap.h"
#include <fmt/format.h>
#include "string/predicate.h"
namespace wxutil
{
namespace
{
const char* const FUNC_EMITTER_CLASS = "func_emitter";
enum ToolItems
{
TOOL_SHOW_AXES = 100,
TOOL_SHOW_WIREFRAME,
TOOL_REFRESH,
TOOL_AUTO_LOOP
};
}
// Construct the widgets
ParticlePreview::ParticlePreview(wxWindow* parent) :
RenderPreview(parent, true)
{
// Add one additional toolbar for particle-related stuff
wxToolBar* toolbar = new wxToolBar(_mainPanel, wxID_ANY);
toolbar->SetToolBitmapSize(wxSize(24, 24));
_showAxesButton = toolbar->AddCheckTool(TOOL_SHOW_AXES, "",
wxutil::GetLocalBitmap("axes.png", wxART_TOOLBAR));
_showAxesButton->SetShortHelp(_("Show coordinate axes"));
toolbar->Connect(_showAxesButton->GetId(), wxEVT_TOOL,
wxCommandEventHandler(ParticlePreview::onToolItemClickRefresh), NULL, this);
_showWireFrameButton = toolbar->AddCheckTool(TOOL_SHOW_WIREFRAME, "",
wxutil::GetLocalBitmap("wireframe.png", wxART_TOOLBAR));
_showWireFrameButton->SetShortHelp(_("Show wireframe"));
toolbar->Connect(_showWireFrameButton->GetId(), wxEVT_TOOL,
wxCommandEventHandler(ParticlePreview::onToolItemClickRefresh), NULL, this);
_automaticLoopButton = toolbar->AddCheckTool(TOOL_AUTO_LOOP, _("Auto Loop"),
wxutil::GetLocalBitmap("loop.png", wxART_TOOLBAR));
_automaticLoopButton->SetShortHelp(_("Auto Loop"));
_reloadButton = toolbar->AddTool(TOOL_REFRESH, "",
wxutil::GetLocalBitmap("refresh.png", wxART_TOOLBAR));
_reloadButton->SetShortHelp(_("Reload Particle Defs"));
IEventPtr ev = GlobalEventManager().findEvent("ReloadDecls");
ev->connectToolItem(_reloadButton);
toolbar->Realize();
addToolbar(toolbar);
}
ParticlePreview::~ParticlePreview()
{
IEventPtr ev = GlobalEventManager().findEvent("ReloadDecls");
ev->disconnectToolItem(_reloadButton);
}
wxWindow* ParticlePreview::GetPreviewWidget()
{
return _mainPanel;
}
void ParticlePreview::ClearPreview()
{
SetPreviewDeclName({});
}
void ParticlePreview::SetPreviewDeclName(const std::string& declName)
{
std::string nameClean = declName;
if (string::ends_with(nameClean, ".prt"))
{
nameClean = nameClean.substr(0, nameClean.length() - 4);
}
// If the model name is empty, release the model
if (nameClean.empty())
{
if (_particleNode)
{
_entity->removeChildNode(_particleNode);
}
_particleNode.reset();
_lastParticle = "";
stopPlayback();
return;
}
// Set up the scene
if (!_entity)
{
setupSceneGraph();
}
if (!_entity) return; // FUNC_EMITTER_CLASS not found
if (_particleNode)
{
_entity->removeChildNode(_particleNode);
}
// Construct the particle emitter node
_particleNode = GlobalParticlesManager().createParticleNode(nameClean);
if (_particleNode && _lastParticle != nameClean)
{
_entity->addChildNode(_particleNode);
// Reset preview time
stopPlayback();
// Call update(0) once to enable the bounds calculation
_particleNode->getParticle()->update(_modelView, _particleNode->localToWorld(), _entity->getRenderEntity());
// Reset the model rotation
resetModelRotation();
// Use particle AABB to adjust camera distance
const AABB& particleBounds = _particleNode->getParticle()->getBounds();
if (particleBounds.isValid())
{
// Reset the default view, facing down to the model from diagonally above the bounding box
double distance = particleBounds.getRadius() * 2.0f;
setViewOrigin(Vector3(1, 1, 1) * distance);
}
else
{
// Bounds not valid, fall back to default
setViewOrigin(Vector3(1, 1, 1) * 40.0f);
}
setViewAngles(Vector3(34, 135, 0));
_lastParticle = nameClean;
// Start playback when switching particles
startPlayback();
}
// Redraw
queueDraw();
}
void ParticlePreview::setupSceneGraph()
{
RenderPreview::setupSceneGraph();
try
{
_rootNode = std::make_shared<scene::BasicRootNode>();
_entity = GlobalEntityModule().createEntity(
GlobalEntityClassManager().findClass(FUNC_EMITTER_CLASS));
_rootNode->addChildNode(_entity);
_entity->enable(scene::Node::eHidden);
// This entity is acting as our root node in the scene
getScene()->setRoot(_rootNode);
}
catch (std::runtime_error&)
{
wxutil::Messagebox::ShowError(fmt::format(_("Unable to setup the preview,\n"
"could not find the entity class {0}"), FUNC_EMITTER_CLASS));
}
}
AABB ParticlePreview::getSceneBounds()
{
if (!_particleNode)
{
return RenderPreview::getSceneBounds();
}
return _particleNode->getParticle()->getBounds();
}
Vector3 ParticlePreview::getGridOrigin()
{
return Vector3(0, 0, 0);
}
bool ParticlePreview::onPreRender()
{
return _particleNode != NULL;
}
void ParticlePreview::onPostRender()
{
if (_showWireFrameButton->IsToggled())
{
renderWireFrame();
}
// Draw coordinate axes for better orientation
if (_showAxesButton->IsToggled())
{
drawAxes();
}
const particles::IParticleDef::Ptr& def = _particleNode->getParticle()->getParticleDef();
// Calculate the total time of the particles
std::size_t totalTimeMsec = 0;
for (std::size_t i = 0; i < def->getNumStages(); ++i)
{
const auto& stage = def->getStage(i);
// For ever-repeating stages, set stuff to INT_MAX and break
if (stage->getCycles() == 0)
{
totalTimeMsec = INT_MAX;
break;
}
totalTimeMsec += static_cast<int>(stage->getCycleMsec() * stage->getCycles());
}
// Update the sensitivity of the auto-loop button
if (totalTimeMsec < INT_MAX)
{
_automaticLoopButton->GetToolBar()->EnableTool(TOOL_AUTO_LOOP, true);
// Auto-Loop is possible, check if we should reset the time
if (_automaticLoopButton->IsToggled() && _renderSystem->getTime() > totalTimeMsec)
{
_renderSystem->setTime(0);
}
}
else
{
_automaticLoopButton->GetToolBar()->EnableTool(TOOL_AUTO_LOOP, false);
}
}
void ParticlePreview::onModelRotationChanged()
{
if (_entity)
{
// Update the model rotation on the entity
std::ostringstream value;
value << _modelRotation.xx() << ' '
<< _modelRotation.xy() << ' '
<< _modelRotation.xz() << ' '
<< _modelRotation.yx() << ' '
<< _modelRotation.yy() << ' '
<< _modelRotation.yz() << ' '
<< _modelRotation.zx() << ' '
<< _modelRotation.zy() << ' '
<< _modelRotation.zz();
Node_getEntity(_entity)->setKeyValue("rotation", value.str());
}
}
void ParticlePreview::drawAxes()
{
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glLineWidth(2);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glBegin(GL_LINES);
glColor4f(1,0,0,0.6f);
glVertex3f(0,0,0);
glVertex3f(5,0,0);
glColor4f(0,1,0,0.6f);
glVertex3f(0,0,0);
glVertex3f(0,5,0);
glColor4f(0,0,1,0.6f);
glVertex3f(0,0,0);
glVertex3f(0,0,5);
glEnd();
}
void ParticlePreview::onToolItemClickRefresh(wxCommandEvent& ev)
{
queueDraw();
}
} // namespace ui
|