File: ParticleDef.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 (201 lines) | stat: -rw-r--r-- 4,520 bytes parent folder | download | duplicates (2)
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
#include "ParticleDef.h"
#include "string/convert.h"
#include "util/ScopedBoolLock.h"

namespace particles
{

sigc::signal<void>& ParticleDef::signal_changed()
{ 
	return _changedSignal;
}

float ParticleDef::getDepthHack()
{
    ensureParsed();
	return _depthHack;
}

void ParticleDef::setDepthHack(float value)
{
    ensureParsed();
    _depthHack = value;
    onParsedContentsChanged();
}

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

const std::shared_ptr<IStageDef>& ParticleDef::getStage(std::size_t stageNum)
{
    ensureParsed();
	return _stages[stageNum].first;
}

std::size_t ParticleDef::addParticleStage()
{
    ensureParsed();

    // Create and append a new stage
    appendStage(std::make_shared<StageDef>());

    onParticleChanged();

    return _stages.size() - 1;
}

void ParticleDef::removeParticleStage(std::size_t index)
{
    ensureParsed();

    if (index < _stages.size())
    {
        // Disconnect the change signal before removal
        _stages[index].second.disconnect();
        _stages.erase(_stages.begin() + index);
    }

    onParticleChanged();
}

void ParticleDef::swapParticleStages(std::size_t index, std::size_t index2)
{
    ensureParsed();

    if (index >= _stages.size() || index2 >= _stages.size() || index == index2)
    {
        return;
    }

    std::swap(_stages[index], _stages[index2]);
    onParticleChanged();
}

void ParticleDef::appendStage(const StageDef::Ptr& stage)
{
    // Add to list and connect the incoming stage's changed signal
    _stages.emplace_back(std::make_pair(
        stage,
        stage->signal_changed().connect(sigc::mem_fun(this, &ParticleDef::onParticleChanged)))
    );

    // This method is only used internally, no signal emission
}

void ParticleDef::copyFrom(const Ptr& other)
{
    ensureParsed();

    {
        // Suppress signals until we're done copying
        util::ScopedBoolLock changeLock(_blockChangedSignal);

        setDepthHack(other->getDepthHack());

        _stages.clear();

        // Copy each stage
        for (std::size_t i = 0; i < other->getNumStages(); ++i)
        {
            auto stage = std::make_shared<StageDef>();
            stage->copyFrom(other->getStage(i));

            appendStage(stage);
        }
    }

	// We've changed all the stages, so emit the changed signal now (#4411)
	onParticleChanged();
}

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

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

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

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

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.

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

        if (token == "depthHack")
        {
            setDepthHack(string::convert<float>(tokeniser.nextToken()));
        }
        else if (token == "{")
        {
            // Construct/Parse the stage from the tokens
            // Append to the ParticleDef
            appendStage(StageDef::Parse(tokeniser));
        }
    }
}

void ParticleDef::onSyntaxBlockAssigned(const decl::DeclarationBlockSyntax& block)
{
    EditableDeclaration<IParticleDef>::onSyntaxBlockAssigned(block);

    _changedSignal.emit();
}

std::string ParticleDef::generateSyntax()
{
    std::stringstream stream;

    // Don't use scientific notation when exporting floats
    stream << std::fixed;
    stream.precision(3); // Three post-comma digits precision

    stream << "\n"; // initial line break after the opening brace

    if (_depthHack > 0)
    {
        stream << "\tdepthHack\t" << _depthHack << std::endl;
    }

    // Write stages, one by one
    for (const auto& stage : _stages)
    {
        stream << *std::static_pointer_cast<StageDef>(stage.first);
    }

    stream << "\n"; // final line break before the closing brace

    return stream.str();
}

void ParticleDef::onParticleChanged()
{
    if (_blockChangedSignal) return;

    onParsedContentsChanged();
    _changedSignal.emit();
}

}