Skip to content

Commit

Permalink
#5977: Migrate ParticlesManager to use the declaration infrastructure…
Browse files Browse the repository at this point in the history
… (WIP).
  • Loading branch information
codereader committed Jul 9, 2022
1 parent b954865 commit dd373b0
Show file tree
Hide file tree
Showing 17 changed files with 431 additions and 368 deletions.
18 changes: 7 additions & 11 deletions include/iparticles.h
Expand Up @@ -59,17 +59,14 @@ class IParticleDef :
/**
* Set/get the depth hack flag
*/
virtual float getDepthHack() const = 0;
virtual float getDepthHack() = 0;
virtual void setDepthHack(float value) = 0;

/// Returns the number of stages for this particle system.
virtual std::size_t getNumStages() const = 0;

/// Get a const stage definition from the particle definition
virtual const IStageDef& getStage(std::size_t stageNum) const = 0;
virtual std::size_t getNumStages() = 0;

/// Get a stage definition from the particle definition
virtual IStageDef& getStage(std::size_t stageNum) = 0;
virtual const std::shared_ptr<IStageDef>& getStage(std::size_t stageNum) = 0;

/**
* Add a new stage to this particle, returns the index of the new stage.
Expand All @@ -91,14 +88,13 @@ class IParticleDef :
/// Signal emitted when some aspect of the particle def has changed
virtual sigc::signal<void>& signal_changed() = 0;

// Comparison operators - particle defs are considered equal if all properties (except the name!),
// Comparison operator - particle defs are considered equal if all properties (except the name!),
// number of stages and stage contents are the equal
virtual bool operator==(const IParticleDef& other) const = 0;
virtual bool operator!=(const IParticleDef& other) const = 0;
virtual bool isEqualTo(Ptr& other) = 0;

// Copies all properties from the other particle, overwriting this one
// Note: Name, filename and observers are not copied
virtual void copyFrom(const IParticleDef& other) = 0;
virtual void copyFrom(const Ptr& other) = 0;
};

/**
Expand Down Expand Up @@ -163,7 +159,7 @@ typedef std::function< void (const IParticleDef&) > ParticleDefVisitor;

/* CONSTANTS */
constexpr const char* const PARTICLES_DIR = "particles/";
constexpr const char* const PARTICLES_EXT = "prt";
constexpr const char* const PARTICLES_EXT = ".prt";

/// Inteface for the particles manager
class IParticlesManager :
Expand Down
4 changes: 3 additions & 1 deletion include/iparticlestage.h
@@ -1,6 +1,7 @@
#pragma once

#include <string>
#include <memory>

template<typename Element> class BasicVector3;
typedef BasicVector3<double> Vector3;
Expand Down Expand Up @@ -57,6 +58,7 @@ class IParticleParameter
class IStageDef
{
public:
using Ptr = std::shared_ptr<IStageDef>;

// Particle orientation
enum OrientationType
Expand Down Expand Up @@ -415,7 +417,7 @@ class IStageDef
/**
* Copy operator, copies all properties from the other stage into this one.
*/
virtual void copyFrom(const IStageDef& other) = 0;
virtual void copyFrom(const Ptr& other) = 0;

/**
* Returns the stage visibility. This flag is used in the Particle Editor context only,
Expand Down
6 changes: 3 additions & 3 deletions libs/wxutil/preview/ParticlePreview.cpp
Expand Up @@ -223,16 +223,16 @@ void ParticlePreview::onPostRender()

for (std::size_t i = 0; i < def->getNumStages(); ++i)
{
const particles::IStageDef& stage = def->getStage(i);
const auto& stage = def->getStage(i);

// For ever-repeating stages, set stuff to INT_MAX and break
if (stage.getCycles() == 0)
if (stage->getCycles() == 0)
{
totalTimeMsec = INT_MAX;
break;
}

totalTimeMsec += static_cast<int>(stage.getCycleMsec() * stage.getCycles());
totalTimeMsec += static_cast<int>(stage->getCycleMsec() * stage->getCycles());
}

// Update the sensitivity of the auto-loop button
Expand Down
308 changes: 154 additions & 154 deletions radiant/ui/particles/ParticleEditor.cpp

Large diffs are not rendered by default.

38 changes: 21 additions & 17 deletions radiantcore/particles/ParticleDef.cpp
Expand Up @@ -7,7 +7,7 @@ namespace particles
std::size_t ParticleDef::addParticleStage()
{
// Create a new stage and relay its changed signal
StageDefPtr stage = std::make_shared<StageDef>();
auto stage = std::make_shared<StageDef>();
stage->signal_changed().connect(_changedSignal.make_slot());
_stages.push_back(stage);

Expand Down Expand Up @@ -37,25 +37,27 @@ void ParticleDef::swapParticleStages(std::size_t index, std::size_t index2)
_changedSignal.emit();
}

void ParticleDef::appendStage(const StageDefPtr& stage)
void ParticleDef::appendStage(const StageDef::Ptr& stage)
{
// Relay the incoming stage's changed signal then add to list
stage->signal_changed().connect(_changedSignal.make_slot());
_stages.push_back(stage);
_changedSignal.emit();
}

void ParticleDef::copyFrom(const IParticleDef& other)
void ParticleDef::copyFrom(const Ptr& other)
{
setDepthHack(other.getDepthHack());
ensureParsed();

setDepthHack(other->getDepthHack());

_stages.clear();

// Copy each stage
for (std::size_t i = 0; i < other.getNumStages(); ++i)
for (std::size_t i = 0; i < other->getNumStages(); ++i)
{
StageDefPtr stage = std::make_shared<StageDef>();
stage->copyFrom(other.getStage(i));
auto stage = std::make_shared<StageDef>();
stage->copyFrom(other->getStage(i));
stage->signal_changed().connect(_changedSignal.make_slot());
_stages.push_back(stage);
}
Expand All @@ -64,34 +66,36 @@ void ParticleDef::copyFrom(const IParticleDef& other)
_changedSignal.emit();
}

void ParticleDef::parseFromTokens(parser::DefTokeniser& tok)
void ParticleDef::onBeginParsing()
{
// Clear out the particle def (except the name) before parsing
clear();
}

void ParticleDef::parseFromTokens(parser::DefTokeniser& tokeniser)
{
// Any global keywords will come first, after which we get a series of
// brace-delimited stages.
std::string token = tok.nextToken();

while (token != "}")
while (tokeniser.hasMoreTokens())
{
auto token = tokeniser.nextToken();

if (token == "depthHack")
{
setDepthHack(string::convert<float>(tok.nextToken()));
setDepthHack(string::convert<float>(tokeniser.nextToken()));
}
else if (token == "{")
{
// Construct/Parse the stage from the tokens
StageDefPtr stage = std::make_shared<StageDef>(std::ref(tok));

// Append to the ParticleDef
appendStage(stage);
appendStage(StageDef::Parse(tokeniser));
}

// Get next token
token = tok.nextToken();
}
}

void ParticleDef::onParsingFinished()
{
_changedSignal.emit();
}

Expand Down
43 changes: 20 additions & 23 deletions radiantcore/particles/ParticleDef.h
Expand Up @@ -26,7 +26,7 @@ class ParticleDef :
float _depthHack;

// Vector of stages
typedef std::vector<StageDefPtr> StageList;
typedef std::vector<StageDef::Ptr> StageList;
StageList _stages;

// Changed signal
Expand Down Expand Up @@ -73,29 +73,28 @@ class ParticleDef :
return _changedSignal;
}

float getDepthHack() const override
float getDepthHack() override
{
ensureParsed();
return _depthHack;
}

void setDepthHack(float value) override
{
ensureParsed();
_depthHack = value;
}

std::size_t getNumStages() const override
std::size_t getNumStages() override
{
ensureParsed();
return _stages.size();
}

const IStageDef& getStage(std::size_t stageNum) const override
const std::shared_ptr<IStageDef>& getStage(std::size_t stageNum) override
{
return *_stages[stageNum];
}

IStageDef& getStage(std::size_t stageNum) override
{
return *_stages[stageNum];
ensureParsed();
return _stages[stageNum];
}

std::size_t addParticleStage() override;
Expand All @@ -104,37 +103,35 @@ class ParticleDef :

void swapParticleStages(std::size_t index, std::size_t index2) override;

void appendStage(const StageDefPtr& stage);
void appendStage(const StageDef::Ptr& stage);

bool operator==(const IParticleDef& other) const override
bool isEqualTo(IParticleDef::Ptr& other) override
{
// Compare depth hack flag
if (getDepthHack() != other.getDepthHack()) return false;
if (getDepthHack() != other->getDepthHack()) return false;

// Compare number of stages
if (getNumStages() != other.getNumStages()) return false;
if (getNumStages() != other->getNumStages()) return false;

// Compare each stage
for (std::size_t i = 0; i < getNumStages(); ++i)
{
if (getStage(i) != other.getStage(i)) return false;
if (getStage(i) != other->getStage(i)) return false;
}

// All checks passed => equal
return true;
}

bool operator!=(const IParticleDef& other) const override
{
return !operator==(other);
}

void copyFrom(const IParticleDef& other) override;

void parseFromTokens(parser::DefTokeniser& tok);
void copyFrom(const Ptr& other) override;

// Stream insertion operator, writing the entire particle def to the given stream
friend std::ostream& operator<< (std::ostream& stream, const ParticleDef& def);

protected:
void onBeginParsing() override;
void parseFromTokens(parser::DefTokeniser& tok) override;
void onParsingFinished() override;
};
typedef std::shared_ptr<ParticleDef> ParticleDefPtr;

Expand Down
24 changes: 24 additions & 0 deletions radiantcore/particles/ParticleDefCreator.h
@@ -0,0 +1,24 @@
#pragma once

#include "ideclmanager.h"
#include "ParticleDef.h"

namespace particles
{

class ParticleDefCreator :
public decl::IDeclarationCreator
{
public:
decl::Type getDeclType() const override
{
return decl::Type::Particle;
}

decl::IDeclaration::Ptr createDeclaration(const std::string& name) override
{
return std::make_shared<ParticleDef>(name);
}
};

}
2 changes: 2 additions & 0 deletions radiantcore/particles/ParticleLoader.cpp
@@ -1,3 +1,4 @@
#if 0
#include "ParticleLoader.h"

#include "ifilesystem.h"
Expand Down Expand Up @@ -83,3 +84,4 @@ void ParticleLoader::onFinishParsing()
}

}
#endif
2 changes: 2 additions & 0 deletions radiantcore/particles/ParticleLoader.h
@@ -1,5 +1,6 @@
#pragma once

#if 0
#include <functional>
#include <iosfwd>
#include "ParticleDef.h"
Expand Down Expand Up @@ -38,3 +39,4 @@ class ParticleLoader :
};

}
#endif

0 comments on commit dd373b0

Please sign in to comment.