File: EClassManager.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-- 4,157 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 "EClassManager.h"

#include "ideclmanager.h"
#include "ieclasscolours.h"
#include "i18n.h"
#include "iregistry.h"
#include "icommandsystem.h"
#include "messages/ScopedLongRunningOperation.h"

#include "decl/DeclarationCreator.h"

#include "module/StaticModule.h"

namespace eclass
{

// Get a named entity class, creating if necessary
IEntityClassPtr EClassManager::findOrInsert(const std::string& name, bool has_brushes)
{
    return std::static_pointer_cast<IEntityClass>(
        GlobalDeclarationManager().findOrCreateDeclaration(decl::Type::EntityDef, name)
    );
}

IEntityClassPtr EClassManager::findClass(const std::string& className)
{
    return std::static_pointer_cast<IEntityClass>(
        GlobalDeclarationManager().findDeclaration(decl::Type::EntityDef, className)
    );
}

void EClassManager::forEachEntityClass(EntityClassVisitor& visitor)
{
    forEachEntityClass([&](const IEntityClassPtr& eclass)
    {
        visitor.visit(eclass);
    });
}

void EClassManager::forEachEntityClass(const std::function<void(const IEntityClassPtr&)>& functor)
{
    GlobalDeclarationManager().foreachDeclaration(decl::Type::EntityDef, [&](const decl::IDeclaration::Ptr& decl)
    {
        functor(std::static_pointer_cast<IEntityClass>(decl));
    });
}

IModelDef::Ptr EClassManager::findModel(const std::string& name)
{
    return std::static_pointer_cast<IModelDef>(
        GlobalDeclarationManager().findDeclaration(decl::Type::ModelDef, name)
    );
}

void EClassManager::forEachModelDef(const std::function<void(const IModelDef::Ptr&)>& functor)
{
    GlobalDeclarationManager().foreachDeclaration(decl::Type::ModelDef, [&](const decl::IDeclaration::Ptr& decl)
    {
        functor(std::static_pointer_cast<IModelDef>(decl));
    });
}

void EClassManager::reloadDefs()
{
    GlobalDeclarationManager().reloadDeclarations();
}

// RegisterableModule implementation
const std::string& EClassManager::getName() const
{
	static std::string _name(MODULE_ECLASSMANAGER);
	return _name;
}

const StringSet& EClassManager::getDependencies() const
{
    static StringSet _dependencies
    {
        MODULE_DECLMANAGER,
        MODULE_XMLREGISTRY,
        MODULE_COMMANDSYSTEM,
        MODULE_ECLASS_COLOUR_MANAGER,
    };

	return _dependencies;
}

void EClassManager::initialiseModule(const IApplicationContext& ctx)
{
    GlobalDeclarationManager().registerDeclType("entityDef", std::make_shared<decl::DeclarationCreator<EntityClass>>(decl::Type::EntityDef));
    GlobalDeclarationManager().registerDeclType("model", std::make_shared<decl::DeclarationCreator<Doom3ModelDef>>(decl::Type::ModelDef));
    GlobalDeclarationManager().registerDeclFolder(decl::Type::EntityDef, "def/", ".def");

	GlobalCommandSystem().addCommand("ReloadDefs", std::bind(&EClassManager::reloadDefsCmd, this, std::placeholders::_1));

    _eclassColoursChanged = GlobalEclassColourManager().sig_overrideColourChanged().connect(
        sigc::mem_fun(this, &EClassManager::onEclassOverrideColourChanged));
}

void EClassManager::shutdownModule()
{
	rMessage() << getName() << "::shutdownModule called." << std::endl;

    _eclassColoursChanged.disconnect();
}

void EClassManager::onEclassOverrideColourChanged(const std::string& eclass, bool overrideRemoved)
{
    // An override colour in the IColourManager instance has changed
    // Do we have an affected eclass with that name?
    auto foundEclass = std::static_pointer_cast<EntityClass>(findClass(eclass));

    if (!foundEclass)
    {
        return;
    }

    // If the override was removed, we just reset the colour
    // We perform this switch to avoid firing the eclass changed signal twice
    if (overrideRemoved)
    {
        foundEclass->resetColour();
    }
    else
    {
        GlobalEclassColourManager().applyColours(*foundEclass);
    }
}

// This takes care of relading the entityDefs and refreshing the scenegraph
void EClassManager::reloadDefsCmd(const cmd::ArgumentList& args)
{
	radiant::ScopedLongRunningOperation operation(_("Reloading Defs"));

    reloadDefs();
}

// Static module instance
module::StaticModuleRegistration<EClassManager> eclassModule;

} // namespace eclass