File: SelectionGroupInfoFileModule.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 (358 lines) | stat: -rw-r--r-- 8,774 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "SelectionGroupInfoFileModule.h"

#include <limits>
#include "iselectiongroup.h"
#include "ientity.h"
#include "string/convert.h"
#include "string/replace.h"
#include "parser/DefTokeniser.h"

#include "scenelib.h"
#include "debugging/ScenegraphUtils.h"
#include "SelectionGroupManager.h"

namespace selection
{

namespace
{
	const char* const SELECTION_GROUPS = "SelectionGroups";
	const char* const SELECTION_GROUP = "SelectionGroup";
	const char* const NODE_MAPPING = "SelectionGroupNodeMapping";
	const char* const NODE = "Node";
	std::size_t EMPTY_PRIMITVE_NUM = std::numeric_limits<std::size_t>::max();
}

std::string SelectionGroupInfoFileModule::getName()
{
	return "Selection Groups";
}

void SelectionGroupInfoFileModule::clear()
{
	_groupInfo.clear();
	_nodeMapping.clear();

	_output.str(std::string());
	_output.clear();
	_selectionGroupBuffer.str(std::string());
	_selectionGroupBuffer.clear();
	_nodeInfoCount = 0;
}

void SelectionGroupInfoFileModule::onInfoFileSaveStart()
{
	clear();
}

void SelectionGroupInfoFileModule::onBeginSaveMap(const scene::IMapRootNodePtr& root)
{
	// Selection Group output
	_selectionGroupBuffer << "\t" << SELECTION_GROUPS << std::endl;

	_selectionGroupBuffer << "\t{" << std::endl;

	// SelectionGroup 0 { Name of this group }

	std::size_t selectionGroupCount = 0;

	root->getSelectionGroupManager().foreachSelectionGroup([&](ISelectionGroup& group)
	{
		// Ignore empty groups
		if (group.size() == 0) return;

		// Make sure to escape the quotes of the set name, use the XML quote entity
		_selectionGroupBuffer << "\t\t" << SELECTION_GROUP << " " << group.getId()
			<< " { \"" << string::replace_all_copy(group.getName(), "\"", "&quot;") << "\" }"
			<< std::endl;

		selectionGroupCount++;
	});

	_selectionGroupBuffer << "\t}" << std::endl;

	rMessage() << selectionGroupCount << " selection groups collected." << std::endl;
}

void SelectionGroupInfoFileModule::onFinishSaveMap(const scene::IMapRootNodePtr& root)
{}

void SelectionGroupInfoFileModule::onSavePrimitive(const scene::INodePtr& node, std::size_t entityNum, std::size_t primitiveNum)
{
	saveNode(node, entityNum, primitiveNum);
}

void SelectionGroupInfoFileModule::onSaveEntity(const scene::INodePtr& node, std::size_t entityNum)
{
	saveNode(node, entityNum, EMPTY_PRIMITVE_NUM);
}

void SelectionGroupInfoFileModule::saveNode(const scene::INodePtr& node, std::size_t entityNum, std::size_t primitiveNum)
{
	// Don't export the group settings for models and particles, as they are not there
	// at map load/parse time - these shouldn't even be passed in here
	assert(Node_isEntity(node) || Node_isPrimitive(node));

	std::shared_ptr<IGroupSelectable> selectable = std::dynamic_pointer_cast<IGroupSelectable>(node);

	if (!selectable) return;

	const IGroupSelectable::GroupIds& ids = selectable->getGroupIds();

	// Ignore nodes that are not part of any group
	if (ids.empty()) return;

	// Node { ( EntityNum [PrimitiveNum] ) ( GroupId1 GroupId2 ... )
	// e.g.
	// Node { ( 3 78 ) ( 1 2 8 ) }

	// Open a Node block
	_output << "\t\t" << NODE << " { ";

	// Write the node coordinates
	_output << "( " << entityNum;

	if (primitiveNum != EMPTY_PRIMITVE_NUM)
	{
		_output << " " << primitiveNum;
	}
	
	_output << " )";

	_output << " ( ";

	// Write a space-separated list of group IDs
	for (const IGroupSelectable::GroupIds::value_type& i : ids)
	{
		_output << i << " ";
	}

	_output << ") ";

	// Close the Node block
	_output << "}";

	// Write additional node info, for easier debugging in case of issues
	_output << " // " << getNodeInfo(node);

	_output << std::endl;

	_nodeInfoCount++;
}

void SelectionGroupInfoFileModule::writeBlocks(std::ostream& stream)
{
	// Selection Group output
	stream << _selectionGroupBuffer.str();

	// Write the NodeToLayerMapping block
	stream << "\t" << NODE_MAPPING << std::endl;
	stream << "\t{" << std::endl;

	// Write the output buffer to the stream
	stream << _output.str();

	// Closing braces of NodeToLayerMapping block
	stream << "\t}" << std::endl;

	rMessage() << _nodeInfoCount << " selection group member mappings written." << std::endl;
}

void SelectionGroupInfoFileModule::onInfoFileSaveFinished()
{
	clear();
}

void SelectionGroupInfoFileModule::onInfoFileLoadStart()
{
	clear();
}

bool SelectionGroupInfoFileModule::canParseBlock(const std::string& blockName)
{
	return blockName == SELECTION_GROUPS || blockName == NODE_MAPPING;
}

void SelectionGroupInfoFileModule::parseBlock(const std::string& blockName, parser::DefTokeniser& tok)
{
	assert(canParseBlock(blockName));

	if (blockName == SELECTION_GROUPS)
	{
		parseSelectionGroups(tok);
	}
	else if (blockName == NODE_MAPPING)
	{
		parseNodeMappings(tok);
	}
}

void SelectionGroupInfoFileModule::parseSelectionGroups(parser::DefTokeniser& tok)
{
	// The opening brace
	tok.assertNextToken("{");

	while (tok.hasMoreTokens())
	{
		std::string token = tok.nextToken();

		if (token == SELECTION_GROUP)
		{
			// SelectionGroup 0 { Name of this group }

			// Get the ID
			std::string idStr = tok.nextToken();
			std::size_t id = string::convert<std::size_t>(idStr);

			tok.assertNextToken("{");

			// Parse the name, replacing the &quot; placeholder with a proper quote
			std::string name = string::replace_all_copy(tok.nextToken(), "&quot;", "\"");

			tok.assertNextToken("}");

			_groupInfo.push_back(SelectionGroupImportInfo());
			_groupInfo.back().id = id;
			_groupInfo.back().name = name;

			rDebug() << "[InfoFile]: Parsed group #" << id << " with name \"" << name << "\"" << std::endl;

			continue;
		}

		if (token == "}")
		{
			break;
		}
	}
}

void SelectionGroupInfoFileModule::parseNodeMappings(parser::DefTokeniser& tok)
{
	// The opening brace
	tok.assertNextToken("{");

	while (tok.hasMoreTokens())
	{
		std::string token = tok.nextToken();

		if (token == NODE)
		{
			// Node { ( 3 78 ) ( 1 2 8 ) }

			// Get the node coordinates
			tok.assertNextToken("{");

			tok.assertNextToken("(");

			// Entity number is always there
			std::size_t entityNum = string::convert<std::size_t>(tok.nextToken());
			std::size_t primitiveNum = EMPTY_PRIMITVE_NUM;

			token = tok.nextToken();

			// If we hit the closing parenthesis, we don't have a primitive number
			if (token != ")")
			{
				// We have a primitive number
				primitiveNum = string::convert<std::size_t>(token);
				tok.assertNextToken(")");
			}

			// Initialise the node mapping with an empty list
			NodeMapping::iterator mapped = _nodeMapping.insert(NodeMapping::value_type(
				map::NodeIndexPair(entityNum, primitiveNum),
				IGroupSelectable::GroupIds())).first;

			tok.assertNextToken("(");

			// Parse the group IDs until we hit the closing parenthesis
			for (token = tok.nextToken(); token != ")"; token = tok.nextToken())
			{
				std::size_t groupId = string::convert<std::size_t>(token);

				mapped->second.push_back(groupId);
			}

			// Node closed
			tok.assertNextToken("}");

			continue;
		}

		if (token == "}")
		{
			break;
		}
	}
}

void SelectionGroupInfoFileModule::applyInfoToScene(const scene::IMapRootNodePtr& root, const map::NodeIndexMap& nodeMap)
{
	// Remove all selection sets, there shouldn't be any left at this point
	root->getSelectionGroupManager().deleteAllSelectionGroups();

	typedef std::map<std::size_t, ISelectionGroupPtr> GroupMap;
	GroupMap groups;

	// Re-construct the groups first
	for (const SelectionGroupImportInfo& info : _groupInfo)
	{
		try
		{
			// Create the group by ID
			ISelectionGroupPtr group = root->getSelectionGroupManager().createSelectionGroup(info.id);
			group->setName(info.name);

			// Store for later retrieval
			groups[info.id] = group;
		}
		catch (std::runtime_error& ex)
		{
			rError() << ex.what() << std::endl;
		}
	}

	// Assign the nodes, as found in the mapping, keeping the group ID order intact
	std::size_t failedNodes = 0;

	for (const NodeMapping::value_type& mapping : _nodeMapping)
	{
		map::NodeIndexMap::const_iterator foundNode = nodeMap.find(mapping.first);

		if (foundNode != nodeMap.end())
		{
			// Assign this node to its groups, following the order
			for (const IGroupSelectable::GroupIds::value_type& id : mapping.second)
			{
				// Get the group and assign the node
				GroupMap::iterator found = groups.find(id);

				if (found == groups.end())
				{
					rWarning() << "Invalid group ID " << id << " encountered for node (" << 
						mapping.first.first << "," << mapping.first.second << ")" << std::endl;
					continue;
				}

				found->second->addNode(foundNode->second);
			}
		}
		else
		{
			failedNodes++;
		}
	}
		
	if (failedNodes > 0)
	{
		rWarning() << "Couldn't resolve " << failedNodes << " nodes in group mapping " << std::endl;
	}
}

void SelectionGroupInfoFileModule::onInfoFileLoadFinished()
{
	clear();
}

}