File: TargetableNode.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 (182 lines) | stat: -rw-r--r-- 4,630 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
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
#include "TargetableNode.h"

#include "TargetManager.h"
#include "../EntityNode.h"
#include "TargetLineNode.h"

namespace entity {

TargetableNode::TargetableNode(SpawnArgs& entity, EntityNode& node) :
	_d3entity(entity),
    _targetKeys(*this),
	_node(node),
    _targetManager(nullptr)
{
	// Note: don't do anything with _d3Entity here,
	// the structure is not fully constructed yet at this point.
	// Execute initialisation code in construct()
}

ITargetManager* TargetableNode::getTargetManager()
{
    return _targetManager;
}

void TargetableNode::construct()
{
	_d3entity.attachObserver(this);
	_d3entity.attachObserver(&_targetKeys);
}

// Disconnect this class from the entity
void TargetableNode::destruct()
{
	_d3entity.detachObserver(&_targetKeys);
	_d3entity.detachObserver(this);
}

TargetKeyCollection& TargetableNode::getTargetKeys()
{
    return _targetKeys;
}

// Gets called as soon as the "name" keyvalue changes
void TargetableNode::onKeyValueChanged(const std::string& name)
{
	// Check if we were registered before
    if (!_targetName.empty() && _targetManager)
    {
		// Old name is not empty
		// Tell the Manager to disassociate us from the target
        _targetManager->clearTarget(_targetName, _node);
	}

	// Store the new name, in any case
	_targetName = name;

	if (_targetName.empty())
    {
		// New name is empty, do not associate
		return;
	}

	// Tell the TargetManager to associate the name with this scene::INode here
    if (_targetManager)
    {
        _targetManager->associateTarget(_targetName, _node);
    }
}

// Entity::Observer implementation, gets called on key insert
void TargetableNode::onKeyInsert(const std::string& key, EntityKeyValue& value) 
{
	if (key == "name")
	{
		// Subscribe to this keyvalue to get notified about "name" changes
		value.attach(*this);
	}
}

void TargetableNode::onTransformationChanged()
{
    if (_targetManager)
    {
        // Notify the target manager that our position has changed
        _targetManager->onTargetPositionChanged(_targetName, _node);
    }
}

void TargetableNode::onKeyChange(const std::string& key, const std::string& value)
{
    if (_targetManager && key == "origin")
    {
        // Notify the target manager that our position has changed
        _targetManager->onTargetPositionChanged(_targetName, _node);
    }
}

// Entity::Observer implementation, gets called on key erase
void TargetableNode::onKeyErase(const std::string& key, EntityKeyValue& value)
{
	if (key == "name")
    {
		// Unsubscribe from this keyvalue
		value.detach(*this);
	}
}

void TargetableNode::onInsertIntoScene(scene::IMapRootNode& root)
{
    _targetManager = &root.getTargetManager();

    // Now that we're in the scene, register this name if we have one already
    if (!_targetName.empty() && _targetManager)
    {
        _targetManager->associateTarget(_targetName, _node);
    }

    // Notify the underlying key collection to reacquire their targets
    _targetKeys.onTargetManagerChanged();
}

void TargetableNode::onRemoveFromScene(scene::IMapRootNode& root)
{
    // On scene removal, unregister this name if we have one
    if (!_targetName.empty() && _targetManager)
    {
        _targetManager->clearTarget(_targetName, _node);
    }

    _targetManager = nullptr;

    // Notify the underlying key collection to clear their target references
    _targetKeys.onTargetManagerChanged();
}

void TargetableNode::onVisibilityChanged(bool isVisibleNow)
{
    if (!_targetManager) return;

    // Notify the target manager that our position has changed
    _targetManager->onTargetVisibilityChanged(_targetName, _node);
}

void TargetableNode::onTargetKeyCollectionChanged()
{
    if (!_targetKeys.empty())
    {
        // Add TargetLineNode as child
        if (!_targetLineNode)
        {
            _targetLineNode.reset(new TargetLineNode(_node));
			
			// Fix #4373: Move the target lines to the same layers as the owning node
			_targetLineNode->assignToLayers(_node.getLayers());

			// Add the target node as child to the owning node,
			// this also updates its layer visibility flags
            scene::addNodeToContainer(_targetLineNode, _node.shared_from_this());
        }

        _targetLineNode->queueRenderableUpdate();
    }
    else // No more targets
    {
        // Clear child TargetLineNode
        if (_targetLineNode)
        {
            scene::removeNodeFromParent(_targetLineNode);
            _targetLineNode.reset();
        }
    }
}

void TargetableNode::onRenderSystemChanged()
{
    if (_targetLineNode)
    {
        _targetLineNode->onRenderSystemChanged();
    }
}

} // namespace entity