| 1 |
#include "BrushInterface.h" |
| 2 |
|
| 3 |
#include "../SceneNodeBuffer.h" |
| 4 |
#include <pybind11/stl_bind.h> |
| 5 |
|
| 6 |
PYBIND11_MAKE_OPAQUE(IWinding); |
| 7 |
|
| 8 |
namespace script |
| 9 |
{ |
| 10 |
|
| 11 |
ScriptFace::ScriptFace() : |
| 12 |
_face(NULL) |
| 13 |
{} |
| 14 |
|
| 15 |
ScriptFace::ScriptFace(IFace& face) : |
| 16 |
_face(&face) |
| 17 |
{} |
| 18 |
|
| 19 |
void ScriptFace::undoSave() |
| 20 |
{ |
| 21 |
if (_face == NULL) return; |
| 22 |
_face->undoSave(); |
| 23 |
} |
| 24 |
|
| 25 |
const std::string& ScriptFace::getShader() const |
| 26 |
{ |
| 27 |
if (_face == NULL) return _emptyShader; |
| 28 |
return _face->getShader(); |
| 29 |
} |
| 30 |
|
| 31 |
void ScriptFace::setShader(const std::string& name) |
| 32 |
{ |
| 33 |
if (_face == NULL) return; |
| 34 |
_face->setShader(name); |
| 35 |
} |
| 36 |
|
| 37 |
void ScriptFace::shiftTexdef(float s, float t) |
| 38 |
{ |
| 39 |
if (_face == NULL) return; |
| 40 |
_face->shiftTexdef(s, t); |
| 41 |
} |
| 42 |
|
| 43 |
void ScriptFace::scaleTexdef(float s, float t) |
| 44 |
{ |
| 45 |
if (_face == NULL) return; |
| 46 |
_face->scaleTexdef(s, t); |
| 47 |
} |
| 48 |
|
| 49 |
void ScriptFace::rotateTexdef(float angle) |
| 50 |
{ |
| 51 |
if (_face == NULL) return; |
| 52 |
_face->rotateTexdef(angle); |
| 53 |
} |
| 54 |
|
| 55 |
void ScriptFace::fitTexture(float s_repeat, float t_repeat) |
| 56 |
{ |
| 57 |
if (_face == NULL) return; |
| 58 |
_face->fitTexture(s_repeat, t_repeat); |
| 59 |
} |
| 60 |
|
| 61 |
void ScriptFace::flipTexture(unsigned int flipAxis) |
| 62 |
{ |
| 63 |
if (_face == NULL) return; |
| 64 |
_face->flipTexture(flipAxis); |
| 65 |
} |
| 66 |
|
| 67 |
void ScriptFace::normaliseTexture() |
| 68 |
{ |
| 69 |
if (_face == NULL) return; |
| 70 |
_face->normaliseTexture(); |
| 71 |
} |
| 72 |
|
| 73 |
IWinding& ScriptFace::getWinding() |
| 74 |
{ |
| 75 |
if (_face == NULL) return _emptyWinding; |
| 76 |
return _face->getWinding(); |
| 77 |
} |
| 78 |
|
| 79 |
std::string ScriptFace::_emptyShader; |
| 80 |
IWinding ScriptFace::_emptyWinding; |
| 81 |
|
| 82 |
ScriptBrushNode::ScriptBrushNode(const scene::INodePtr& node) : |
| 83 |
ScriptSceneNode((node != NULL && Node_isBrush(node)) ? node : scene::INodePtr()) |
| 84 |
{} |
| 85 |
|
| 86 |
std::size_t ScriptBrushNode::getNumFaces() { |
| 87 |
// Sanity check |
| 88 |
scene::INodePtr node = _node.lock(); |
| 89 |
if (node == NULL) return 0; |
| 90 |
|
| 91 |
IBrush* brush = Node_getIBrush(node); |
| 92 |
|
| 93 |
return (brush != NULL) ? brush->getNumFaces() : 0; |
| 94 |
} |
| 95 |
|
| 96 |
ScriptFace ScriptBrushNode::getFace(std::size_t index) |
| 97 |
{ |
| 98 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 99 |
if (brushNode == NULL) return ScriptFace(); |
| 100 |
|
| 101 |
IBrush& brush = brushNode->getIBrush(); |
| 102 |
return (index < brush.getNumFaces()) ? ScriptFace(brush.getFace(index)) : ScriptFace(); |
| 103 |
} |
| 104 |
|
| 105 |
bool ScriptBrushNode::empty() const |
| 106 |
{ |
| 107 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 108 |
if (brushNode == NULL) return true; |
| 109 |
|
| 110 |
return brushNode->getIBrush().empty(); |
| 111 |
} |
| 112 |
|
| 113 |
bool ScriptBrushNode::hasContributingFaces() const |
| 114 |
{ |
| 115 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 116 |
if (brushNode == NULL) return true; |
| 117 |
|
| 118 |
return brushNode->getIBrush().hasContributingFaces(); |
| 119 |
} |
| 120 |
|
| 121 |
void ScriptBrushNode::removeEmptyFaces() |
| 122 |
{ |
| 123 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 124 |
if (brushNode == NULL) return; |
| 125 |
|
| 126 |
brushNode->getIBrush().removeEmptyFaces(); |
| 127 |
} |
| 128 |
|
| 129 |
void ScriptBrushNode::setShader(const std::string& newShader) |
| 130 |
{ |
| 131 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 132 |
if (brushNode == NULL) return; |
| 133 |
|
| 134 |
brushNode->getIBrush().setShader(newShader); |
| 135 |
} |
| 136 |
|
| 137 |
bool ScriptBrushNode::hasShader(const std::string& name) |
| 138 |
{ |
| 139 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 140 |
if (brushNode == NULL) return false; |
| 141 |
|
| 142 |
return brushNode->getIBrush().hasShader(name); |
| 143 |
} |
| 144 |
|
| 145 |
bool ScriptBrushNode::hasVisibleMaterial() |
| 146 |
{ |
| 147 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 148 |
if (brushNode == NULL) return false; |
| 149 |
|
| 150 |
return brushNode->getIBrush().hasVisibleMaterial(); |
| 151 |
} |
| 152 |
|
| 153 |
ScriptBrushNode::DetailFlag ScriptBrushNode::getDetailFlag() |
| 154 |
{ |
| 155 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 156 |
if (brushNode == NULL) return Structural; |
| 157 |
|
| 158 |
return static_cast<DetailFlag>(brushNode->getIBrush().getDetailFlag()); |
| 159 |
} |
| 160 |
|
| 161 |
void ScriptBrushNode::setDetailFlag(DetailFlag detailFlag) |
| 162 |
{ |
| 163 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 164 |
if (brushNode == NULL) return; |
| 165 |
|
| 166 |
brushNode->getIBrush().setDetailFlag(static_cast<IBrush::DetailFlag>(detailFlag)); |
| 167 |
} |
| 168 |
|
| 169 |
void ScriptBrushNode::undoSave() |
| 170 |
{ |
| 171 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>(_node.lock()); |
| 172 |
if (brushNode == NULL) return; |
| 173 |
|
| 174 |
brushNode->getIBrush().undoSave(); |
| 175 |
} |
| 176 |
|
| 177 |
// Checks if the given SceneNode structure is a BrushNode |
| 178 |
bool ScriptBrushNode::isBrush(const ScriptSceneNode& node) |
| 179 |
{ |
| 180 |
return Node_isBrush(node); |
| 181 |
} |
| 182 |
|
| 183 |
ScriptBrushNode ScriptBrushNode::getBrush(const ScriptSceneNode& node) |
| 184 |
{ |
| 185 |
// Try to cast the node onto a brush |
| 186 |
IBrushNodePtr brushNode = std::dynamic_pointer_cast<IBrushNode>( |
| 187 |
static_cast<scene::INodePtr>(node) |
| 188 |
); |
| 189 |
|
| 190 |
// Construct a brushnode (contained node may be NULL) |
| 191 |
return (brushNode != NULL) ? ScriptBrushNode(node) : ScriptBrushNode(scene::INodePtr()); |
| 192 |
} |
| 193 |
|
| 194 |
ScriptSceneNode BrushInterface::createBrush() |
| 195 |
{ |
| 196 |
// Create a new brush and return the script scene node |
| 197 |
scene::INodePtr node = GlobalBrushCreator().createBrush(); |
| 198 |
|
| 199 |
// Add the node to the buffer otherwise it will be deleted immediately, |
| 200 |
// as ScriptSceneNodes are using weak_ptrs. |
| 201 |
SceneNodeBuffer::Instance().push_back(node); |
| 202 |
|
| 203 |
return ScriptSceneNode(node); |
| 204 |
} |
| 205 |
|
| 206 |
void BrushInterface::registerInterface(py::module& scope, py::dict& globals) |
| 207 |
{ |
| 208 |
// Define a WindingVertex structure |
| 209 |
py::class_<WindingVertex> vertex(scope, "WindingVertex"); |
| 210 |
vertex.def(py::init<>()); |
| 211 |
vertex.def_readonly("vertex", &WindingVertex::vertex); |
| 212 |
vertex.def_readonly("texcoord", &WindingVertex::texcoord); |
| 213 |
vertex.def_readonly("tangent", &WindingVertex::tangent); |
| 214 |
vertex.def_readonly("bitangent", &WindingVertex::bitangent); |
| 215 |
vertex.def_readonly("normal", &WindingVertex::normal); |
| 216 |
vertex.def_readonly("adjacent", &WindingVertex::adjacent); |
| 217 |
|
| 218 |
// Declare the IWinding vector |
| 219 |
py::bind_vector<IWinding>(scope, "Winding"); |
| 220 |
|
| 221 |
// Define a "Face" interface |
| 222 |
py::class_<ScriptFace> face(scope, "Face"); |
| 223 |
face.def(py::init<>()); |
| 224 |
face.def(py::init<IFace&>()); |
| 225 |
face.def("undoSave", &ScriptFace::undoSave); |
| 226 |
face.def("getShader", &ScriptFace::getShader, py::return_value_policy::reference); |
| 227 |
face.def("setShader", &ScriptFace::setShader); |
| 228 |
face.def("shiftTexdef", &ScriptFace::shiftTexdef); |
| 229 |
face.def("scaleTexdef", &ScriptFace::scaleTexdef); |
| 230 |
face.def("rotateTexdef", &ScriptFace::rotateTexdef); |
| 231 |
face.def("fitTexture", &ScriptFace::fitTexture); |
| 232 |
face.def("flipTexture", &ScriptFace::flipTexture); |
| 233 |
face.def("normaliseTexture", &ScriptFace::normaliseTexture); |
| 234 |
face.def("getWinding", &ScriptFace::getWinding, py::return_value_policy::reference); |
| 235 |
|
| 236 |
// Define a BrushNode interface |
| 237 |
py::class_<ScriptBrushNode, ScriptSceneNode> brush(scope, "BrushNode"); |
| 238 |
brush.def(py::init<const scene::INodePtr&>()); |
| 239 |
brush.def("getNumFaces", &ScriptBrushNode::getNumFaces); |
| 240 |
brush.def("empty", &ScriptBrushNode::empty); |
| 241 |
brush.def("hasContributingFaces", &ScriptBrushNode::hasContributingFaces); |
| 242 |
brush.def("removeEmptyFaces", &ScriptBrushNode::getNumFaces); |
| 243 |
brush.def("setShader", &ScriptBrushNode::setShader); |
| 244 |
brush.def("hasShader", &ScriptBrushNode::hasShader); |
| 245 |
brush.def("hasVisibleMaterial", &ScriptBrushNode::hasVisibleMaterial); |
| 246 |
brush.def("undoSave", &ScriptBrushNode::undoSave); |
| 247 |
brush.def("getFace", &ScriptBrushNode::getFace); |
| 248 |
brush.def("getDetailFlag", &ScriptBrushNode::getDetailFlag); |
| 249 |
brush.def("setDetailFlag", &ScriptBrushNode::setDetailFlag); |
| 250 |
|
| 251 |
// Define the BrushCreator interface |
| 252 |
py::class_<BrushInterface> brushCreator(scope, "BrushCreator"); |
| 253 |
brushCreator.def("createBrush", &BrushInterface::createBrush); |
| 254 |
|
| 255 |
// Now point the Python variable "GlobalBrushCreator" to this instance |
| 256 |
globals["GlobalBrushCreator"] = this; |
| 257 |
|
| 258 |
py::enum_<ScriptBrushNode::DetailFlag>(scope, "BrushDetailFlag") |
| 259 |
.value("Structural", ScriptBrushNode::Structural) |
| 260 |
.value("Detail", ScriptBrushNode::Detail) |
| 261 |
.export_values(); |
| 262 |
} |
| 263 |
|
| 264 |
} // namespace script |