File: SelectionSetInterface.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 (152 lines) | stat: -rw-r--r-- 4,063 bytes parent folder | download | duplicates (4)
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
#include "SelectionSetInterface.h"

#include "imap.h"
#include "iselection.h"

namespace script
{

ScriptSelectionSet::ScriptSelectionSet(const selection::ISelectionSetPtr& set) :
	_set(set)
{}

const std::string& ScriptSelectionSet::getName()
{
	return (_set) ? _set->getName() : _emptyStr;
}

bool ScriptSelectionSet::empty()
{
	return (_set) ? _set->empty() : true;
}

void ScriptSelectionSet::select()
{
	if (_set) _set->select();
}

void ScriptSelectionSet::deselect()
{
	if (_set) _set->deselect();
}

void ScriptSelectionSet::clear()
{
	if (_set) _set->clear();
}

void ScriptSelectionSet::assignFromCurrentScene()
{
	if (_set) _set->assignFromCurrentScene();
}

std::string ScriptSelectionSet::_emptyStr;

// ---------------------------

inline selection::ISelectionSetManager& GetMapSelectionSetManager()
{
	if (!GlobalMapModule().getRoot())
	{
		throw std::runtime_error("No map loaded.");
	}

	return GlobalMapModule().getRoot()->getSelectionSetManager();
}

void SelectionSetInterface::foreachSelectionSet(selection::ISelectionSetManager::Visitor& visitor)
{
	try
	{
		GetMapSelectionSetManager().foreachSelectionSet(visitor);
	}
	catch (const std::runtime_error& ex)
	{
		rError() << ex.what() << std::endl;
	}
}

ScriptSelectionSet SelectionSetInterface::createSelectionSet(const std::string& name)
{
	try
	{
		return ScriptSelectionSet(GetMapSelectionSetManager().createSelectionSet(name));
	}
	catch (const std::runtime_error & ex)
	{
		rError() << ex.what() << std::endl;
		return ScriptSelectionSet(selection::ISelectionSetPtr());
	}
}

void SelectionSetInterface::deleteSelectionSet(const std::string& name)
{
	try
	{
		GetMapSelectionSetManager().deleteSelectionSet(name);
	}
	catch (const std::runtime_error & ex)
	{
		rError() << ex.what() << std::endl;
	}
}

void SelectionSetInterface::deleteAllSelectionSets()
{
	try
	{
		GetMapSelectionSetManager().deleteAllSelectionSets();
	}
	catch (const std::runtime_error & ex)
	{
		rError() << ex.what() << std::endl;
	}
}

ScriptSelectionSet SelectionSetInterface::findSelectionSet(const std::string& name)
{
	try
	{
		return ScriptSelectionSet(GetMapSelectionSetManager().findSelectionSet(name));
	}
	catch (const std::runtime_error & ex)
	{
		rError() << ex.what() << std::endl;
		return ScriptSelectionSet(selection::ISelectionSetPtr());
	}
}

// IScriptInterface implementation
void SelectionSetInterface::registerInterface(py::module& scope, py::dict& globals)
{
	// Expose the SelectionSystem::Visitor interface
	py::class_<selection::ISelectionSetManager::Visitor, SelectionSetVisitorWrapper> visitor(scope, "SelectionSetVisitor");

	visitor.def(py::init<>());
	visitor.def("visit", &selection::ISelectionSetManager::Visitor::visit);

	// Add SelectionSet declaration
	py::class_<ScriptSelectionSet> selectionSet(scope, "SelectionSet");

	selectionSet.def(py::init<const selection::ISelectionSetPtr&>());
	selectionSet.def("getName", &ScriptSelectionSet::getName, py::return_value_policy::reference);
	selectionSet.def("empty", &ScriptSelectionSet::empty);
	selectionSet.def("clear", &ScriptSelectionSet::clear);
	selectionSet.def("select", &ScriptSelectionSet::select);
	selectionSet.def("deselect", &ScriptSelectionSet::deselect);
	selectionSet.def("assignFromCurrentScene", &ScriptSelectionSet::assignFromCurrentScene);

	// Add the module declaration to the given python namespace
	py::class_<SelectionSetInterface> selectionSetManager(scope, "SelectionSetManager");

	selectionSetManager.def("foreachSelectionSet", &SelectionSetInterface::foreachSelectionSet);
	selectionSetManager.def("createSelectionSet", &SelectionSetInterface::createSelectionSet);
	selectionSetManager.def("deleteSelectionSet", &SelectionSetInterface::deleteSelectionSet);
	selectionSetManager.def("deleteAllSelectionSets", &SelectionSetInterface::deleteAllSelectionSets);
	selectionSetManager.def("findSelectionSet", &SelectionSetInterface::findSelectionSet);

	// Now point the Python variable "GlobalSelectionSetManager" to this instance
	globals["GlobalSelectionSetManager"] = this;
}

} // namespace script