Skip to content

Commit

Permalink
Migrate ShaderSystemInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 21, 2017
1 parent 8826153 commit 33bd614
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
51 changes: 27 additions & 24 deletions plugins/script/interfaces/ShaderSystemInterface.cpp
Expand Up @@ -31,43 +31,46 @@ void ShaderSystemInterface::foreachShader(ShaderVisitor& visitor)
// Note: foreachShader only traverses the loaded materials, use a small adaptor to traverse all known
ShaderNameToShaderWrapper adaptor(visitor);

GlobalMaterialManager().foreachShaderName(boost::bind(&ShaderNameToShaderWrapper::visit, &adaptor, _1));
GlobalMaterialManager().foreachShaderName(
std::bind(&ShaderNameToShaderWrapper::visit, &adaptor, std::placeholders::_1));
}

ScriptShader ShaderSystemInterface::getMaterialForName(const std::string& name) {
ScriptShader ShaderSystemInterface::getMaterialForName(const std::string& name)
{
return ScriptShader(GlobalMaterialManager().getMaterialForName(name));
}

// IScriptInterface implementation
void ShaderSystemInterface::registerInterface(boost::python::object& nspace) {
void ShaderSystemInterface::registerInterface(py::module& scope, py::dict& globals)
{
// Add the declaration for a Shader object
nspace["Shader"] = boost::python::class_<ScriptShader>(
"Shader", boost::python::init<const MaterialPtr&>())
.def("getName", &ScriptShader::getName)
.def("getShaderFileName", &ScriptShader::getShaderFileName)
.def("getDescription", &ScriptShader::getDescription)
.def("getDefinition", &ScriptShader::getDefinition)
.def("isVisible", &ScriptShader::isVisible)
.def("isAmbientLight", &ScriptShader::isAmbientLight)
.def("isBlendLight", &ScriptShader::isBlendLight)
.def("isFogLight", &ScriptShader::isFogLight)
.def("isNull", &ScriptShader::isNull)
;
py::class_<ScriptShader> shader(scope, "Shader");

shader.def(py::init<const MaterialPtr&>());
shader.def("getName", &ScriptShader::getName);
shader.def("getShaderFileName", &ScriptShader::getShaderFileName);
shader.def("getDescription", &ScriptShader::getDescription);
shader.def("getDefinition", &ScriptShader::getDefinition);
shader.def("isVisible", &ScriptShader::isVisible);
shader.def("isAmbientLight", &ScriptShader::isAmbientLight);
shader.def("isBlendLight", &ScriptShader::isBlendLight);
shader.def("isFogLight", &ScriptShader::isFogLight);
shader.def("isNull", &ScriptShader::isNull);

// Expose the ShaderVisitor interface
nspace["ShaderVisitor"] =
boost::python::class_<ShaderVisitorWrapper, boost::noncopyable>("ShaderVisitor")
.def("visit", boost::python::pure_virtual(&ShaderVisitorWrapper::visit))
;

py::class_<ShaderVisitor, ShaderVisitorWrapper> visitor(scope, "ShaderVisitor");
visitor.def(py::init<>());
visitor.def("visit", &ShaderVisitor::visit);

// Add the module declaration to the given python namespace
nspace["GlobalMaterialManager"] = boost::python::class_<ShaderSystemInterface>("GlobalMaterialManager")
.def("foreachShader", &ShaderSystemInterface::foreachShader)
.def("getMaterialForName", &ShaderSystemInterface::getMaterialForName)
;
py::class_<ShaderSystemInterface> materialManager(scope, "MaterialManager");

materialManager.def("foreachShader", &ShaderSystemInterface::foreachShader);
materialManager.def("getMaterialForName", &ShaderSystemInterface::getMaterialForName);

// Now point the Python variable "GlobalMaterialManager" to this instance
nspace["GlobalMaterialManager"] = boost::python::ptr(this);
globals["GlobalMaterialManager"] = this;
}

} // namespace script
20 changes: 12 additions & 8 deletions plugins/script/interfaces/ShaderSystemInterface.h
@@ -1,11 +1,12 @@
#pragma once

#include <boost/python.hpp>
#include <pybind11/pybind11.h>

#include "ishaders.h"
#include "iscript.h"

namespace script {
namespace script
{

/**
* This class represents a single Shader as seen by the Python script.
Expand Down Expand Up @@ -69,14 +70,18 @@ class ShaderVisitor

// Wrap around the EntityClassVisitor interface
class ShaderVisitorWrapper :
public ShaderVisitor,
public boost::python::wrapper<ShaderVisitor>
public ShaderVisitor
{
public:
void visit(const MaterialPtr& shader)
void visit(const MaterialPtr& shader) override
{
// Wrap this method to python
this->get_override("visit")(ScriptShader(shader));
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
ShaderVisitor, /* Parent class */
visit, /* Name of function in C++ (must match Python name) */
ScriptShader(shader) /* Argument(s) */
);
}
};

Expand All @@ -91,8 +96,7 @@ class ShaderSystemInterface :
ScriptShader getMaterialForName(const std::string& name);

// IScriptInterface implementation
void registerInterface(boost::python::object& nspace);
void registerInterface(py::module& scope, py::dict& globals) override;
};
typedef std::shared_ptr<ShaderSystemInterface> ShaderSystemInterfacePtr;

} // namespace script

0 comments on commit 33bd614

Please sign in to comment.