| 1 |
#include "EntityInterface.h" |
| 2 |
|
| 3 |
#include <pybind11/stl_bind.h> |
| 4 |
|
| 5 |
#include "ientity.h" |
| 6 |
#include "ieclass.h" |
| 7 |
#include "itextstream.h" |
| 8 |
|
| 9 |
#include "../SceneNodeBuffer.h" |
| 10 |
|
| 11 |
namespace script |
| 12 |
{ |
| 13 |
|
| 14 |
// Constructor, checks if the passed node is actually an entity |
| 15 |
ScriptEntityNode::ScriptEntityNode(const scene::INodePtr& node) : |
| 16 |
ScriptSceneNode((node != NULL && Node_isEntity(node)) ? node : scene::INodePtr()) |
| 17 |
{} |
| 18 |
|
| 19 |
// Methods wrapping to Entity class |
| 20 |
std::string ScriptEntityNode::getKeyValue(const std::string& key) { |
| 21 |
Entity* entity = Node_getEntity(*this); |
| 22 |
return (entity != NULL) ? entity->getKeyValue(key) : ""; |
| 23 |
} |
| 24 |
|
| 25 |
void ScriptEntityNode::setKeyValue(const std::string& key, const std::string& value) { |
| 26 |
Entity* entity = Node_getEntity(*this); |
| 27 |
|
| 28 |
if (entity != NULL) { |
| 29 |
entity->setKeyValue(key, value); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
bool ScriptEntityNode::isInherited(const std::string& key) { |
| 34 |
Entity* entity = Node_getEntity(*this); |
| 35 |
|
| 36 |
return (entity != NULL) ? entity->isInherited(key) : false; |
| 37 |
} |
| 38 |
|
| 39 |
ScriptEntityClass ScriptEntityNode::getEntityClass() { |
| 40 |
Entity* entity = Node_getEntity(*this); |
| 41 |
return ScriptEntityClass(entity != NULL ? entity->getEntityClass() : IEntityClassPtr()); |
| 42 |
} |
| 43 |
|
| 44 |
bool ScriptEntityNode::isModel() { |
| 45 |
Entity* entity = Node_getEntity(*this); |
| 46 |
return (entity != NULL) ? entity->isModel() : false; |
| 47 |
} |
| 48 |
|
| 49 |
bool ScriptEntityNode::isOfType(const std::string& className) |
| 50 |
{ |
| 51 |
Entity* entity = Node_getEntity(*this); |
| 52 |
return entity == NULL ? false : entity->isOfType(className); |
| 53 |
} |
| 54 |
|
| 55 |
Entity::KeyValuePairs ScriptEntityNode::getKeyValuePairs(const std::string& prefix) { |
| 56 |
Entity* entity = Node_getEntity(*this); |
| 57 |
return (entity != NULL) ? entity->getKeyValuePairs(prefix) : Entity::KeyValuePairs(); |
| 58 |
} |
| 59 |
|
| 60 |
void ScriptEntityNode::forEachKeyValue(EntityVisitor& visitor) { |
| 61 |
Entity* entity = Node_getEntity(*this); |
| 62 |
|
| 63 |
if (entity) |
| 64 |
{ |
| 65 |
entity->forEachKeyValue([&](const std::string& key, const std::string& value) |
| 66 |
{ |
| 67 |
visitor.visit(key, value); |
| 68 |
}); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// Checks if the given SceneNode structure is a BrushNode |
| 73 |
bool ScriptEntityNode::isEntity(const ScriptSceneNode& node) { |
| 74 |
return Node_isEntity(node); |
| 75 |
} |
| 76 |
|
| 77 |
// "Cast" service for Python, returns a ScriptEntityNode. |
| 78 |
// The returned node is non-NULL if the cast succeeded |
| 79 |
ScriptEntityNode ScriptEntityNode::getEntity(const ScriptSceneNode& node) { |
| 80 |
// Try to cast the node onto a brush |
| 81 |
IEntityNodePtr entityNode = std::dynamic_pointer_cast<IEntityNode>( |
| 82 |
static_cast<scene::INodePtr>(node) |
| 83 |
); |
| 84 |
|
| 85 |
// Construct a entityNode (contained node is NULL if not an entity) |
| 86 |
return ScriptEntityNode(entityNode != NULL |
| 87 |
? node |
| 88 |
: ScriptSceneNode(scene::INodePtr())); |
| 89 |
} |
| 90 |
|
| 91 |
// Creates a new entity for the given entityclass |
| 92 |
ScriptSceneNode EntityInterface::createEntity(const ScriptEntityClass& eclass) |
| 93 |
{ |
| 94 |
scene::INodePtr node = GlobalEntityModule().createEntity(eclass); |
| 95 |
|
| 96 |
// Add the node to the buffer otherwise it will be deleted immediately, |
| 97 |
// as ScriptSceneNodes are using weak_ptrs. |
| 98 |
SceneNodeBuffer::Instance().push_back(node); |
| 99 |
|
| 100 |
return ScriptSceneNode(node); |
| 101 |
} |
| 102 |
|
| 103 |
// Creates a new entity for the given entityclass |
| 104 |
ScriptSceneNode EntityInterface::createEntity(const std::string& eclassName) { |
| 105 |
// Find the eclass |
| 106 |
IEntityClassPtr eclass = GlobalEntityClassManager().findClass(eclassName); |
| 107 |
|
| 108 |
if (eclass == NULL) { |
| 109 |
rMessage() << "Could not find entity class: " << eclassName << std::endl; |
| 110 |
return ScriptSceneNode(scene::INodePtr()); |
| 111 |
} |
| 112 |
|
| 113 |
scene::INodePtr node = GlobalEntityModule().createEntity(eclass); |
| 114 |
|
| 115 |
// Add the node to the buffer otherwise it will be deleted immediately, |
| 116 |
// as ScriptSceneNodes are using weak_ptrs. |
| 117 |
SceneNodeBuffer::Instance().push_back(node); |
| 118 |
|
| 119 |
return ScriptSceneNode(node); |
| 120 |
} |
| 121 |
|
| 122 |
struct EntityKeyValuePair : |
| 123 |
public std::pair<std::string, std::string> |
| 124 |
{ |
| 125 |
using std::pair<std::string, std::string>::pair; |
| 126 |
|
| 127 |
std::string left() |
| 128 |
{ |
| 129 |
return first; |
| 130 |
} |
| 131 |
|
| 132 |
std::string right() |
| 133 |
{ |
| 134 |
return second; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
void EntityInterface::registerInterface(py::module& scope, py::dict& globals) |
| 139 |
{ |
| 140 |
// Add the EntityNode interface |
| 141 |
py::class_<ScriptEntityNode, ScriptSceneNode> entityNode(scope, "EntityNode"); |
| 142 |
entityNode.def(py::init<const scene::INodePtr&>()); |
| 143 |
entityNode.def("getKeyValue", &ScriptEntityNode::getKeyValue); |
| 144 |
entityNode.def("setKeyValue", &ScriptEntityNode::setKeyValue); |
| 145 |
entityNode.def("forEachKeyValue", &ScriptEntityNode::forEachKeyValue); |
| 146 |
entityNode.def("isInherited", &ScriptEntityNode::isInherited); |
| 147 |
entityNode.def("getEntityClass", &ScriptEntityNode::getEntityClass); |
| 148 |
entityNode.def("isModel", &ScriptEntityNode::isModel); |
| 149 |
entityNode.def("isOfType", &ScriptEntityNode::isOfType); |
| 150 |
entityNode.def("getKeyValuePairs", &ScriptEntityNode::getKeyValuePairs); |
| 151 |
|
| 152 |
// Declare the KeyValuePairs vector |
| 153 |
py::bind_vector<Entity::KeyValuePairs>(scope, "EntityKeyValuePairs"); |
| 154 |
|
| 155 |
// Expose the Entity::Visitor interface |
| 156 |
py::class_<EntityVisitor, EntityVisitorWrapper> visitor(scope, "EntityVisitor"); |
| 157 |
visitor.def(py::init<>()); |
| 158 |
visitor.def("visit", &EntityVisitor::visit); |
| 159 |
|
| 160 |
// Add the EntityCreator module declaration to the given python namespace |
| 161 |
py::class_<EntityInterface> entityCreator(scope, "EntityCreator"); |
| 162 |
|
| 163 |
// Add both overloads to createEntity |
| 164 |
entityCreator.def("createEntity", static_cast<ScriptSceneNode(EntityInterface::*)(const std::string&)>(&EntityInterface::createEntity)); |
| 165 |
entityCreator.def("createEntity", static_cast<ScriptSceneNode(EntityInterface::*)(const ScriptEntityClass&)>(&EntityInterface::createEntity)); |
| 166 |
|
| 167 |
// Now point the Python variable "GlobalEntityCreator" to this instance |
| 168 |
globals["GlobalEntityCreator"] = this; |
| 169 |
} |
| 170 |
|
| 171 |
} // namespace script |