File: ParticlesManager.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (141 lines) | stat: -rw-r--r-- 3,818 bytes parent folder | download | duplicates (3)
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
#include "ParticlesManager.h"

#include "ParticleDef.h"
#include "ParticleNode.h"
#include "RenderableParticle.h"

#include "icommandsystem.h"
#include "itextstream.h"
#include "ifiletypes.h"
#include "ideclmanager.h"
#include "i18n.h"

#include <functional>

#include "decl/DeclarationCreator.h"
#include "string/predicate.h"
#include "module/StaticModule.h"

namespace particles
{

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

sigc::signal<void>& ParticlesManager::signal_particlesReloaded()
{
    return _particlesReloadedSignal;
}

// Visit all of the particle defs
void ParticlesManager::forEachParticleDef(const ParticleDefVisitor& visitor)
{
    GlobalDeclarationManager().foreachDeclaration(decl::Type::Particle, [&](const decl::IDeclaration::Ptr& decl)
    {
        visitor(*std::static_pointer_cast<IParticleDef>(decl));
    });
}

IParticleDef::Ptr ParticlesManager::getDefByName(const std::string& name)
{
    return std::static_pointer_cast<IParticleDef>(
        GlobalDeclarationManager().findDeclaration(decl::Type::Particle, name)
    );
}

IParticleNodePtr ParticlesManager::createParticleNode(const std::string& name)
{
	std::string nameCleaned = name;

	// Cut off the ".prt" from the end of the particle name
	if (string::ends_with(nameCleaned, ".prt"))
	{
		nameCleaned = nameCleaned.substr(0, nameCleaned.length() - 4);
	}

    auto def = getDefByName(nameCleaned);

    if (!def)
    {
        return IParticleNodePtr();
    }

	return std::make_shared<ParticleNode>(std::make_shared<RenderableParticle>(def));
}

IRenderableParticlePtr ParticlesManager::getRenderableParticle(const std::string& name)
{
    auto def = getDefByName(name);

    return def ? std::make_shared<RenderableParticle>(def) : IRenderableParticlePtr();
}

IParticleDef::Ptr ParticlesManager::findOrInsertParticleDef(const std::string& name)
{
    return findOrInsertParticleDefInternal(name);
}

ParticleDefPtr ParticlesManager::findOrInsertParticleDefInternal(const std::string& name)
{
    return std::static_pointer_cast<ParticleDef>(
        GlobalDeclarationManager().findOrCreateDeclaration(decl::Type::Particle, name)
    );
}

void ParticlesManager::removeParticleDef(const std::string& name)
{
    GlobalDeclarationManager().removeDeclaration(decl::Type::Particle, name);
}

const std::string& ParticlesManager::getName() const
{
	static std::string _name(MODULE_PARTICLESMANAGER);
	return _name;
}

const StringSet& ParticlesManager::getDependencies() const
{
    static StringSet _dependencies
    {
        MODULE_DECLMANAGER,
        MODULE_COMMANDSYSTEM,
        MODULE_FILETYPES,
    };

	return _dependencies;
}

void ParticlesManager::initialiseModule(const IApplicationContext& ctx)
{
    GlobalDeclarationManager().registerDeclType("particle", std::make_shared<decl::DeclarationCreator<ParticleDef>>(decl::Type::Particle));
    GlobalDeclarationManager().registerDeclFolder(decl::Type::Particle, PARTICLES_DIR, PARTICLES_EXT);

	// Register the particle file extension
	GlobalFiletypes().registerPattern("particle", FileTypePattern(_("Particle File"), "prt", "*.prt"));

    _defsReloadedConn = GlobalDeclarationManager().signal_DeclsReloaded(decl::Type::Particle).connect(
        [this]() { _particlesReloadedSignal.emit(); }
    );
}

void ParticlesManager::shutdownModule()
{
    _defsReloadedConn.disconnect();
}

void ParticlesManager::saveParticleDef(const std::string& particleName)
{
    auto decl = getDefByName(particleName);

    if (!decl)
    {
        throw std::runtime_error(_("Cannot save particle, it has not been registered yet."));
    }

    GlobalDeclarationManager().saveDeclaration(decl);
}

module::StaticModuleRegistration<ParticlesManager> particlesManagerModule;

} // namespace particles