-
Notifications
You must be signed in to change notification settings - Fork 47
/
ShaderSystemInterface.h
216 lines (179 loc) · 5.36 KB
/
ShaderSystemInterface.h
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
#pragma once
#include <pybind11/pybind11.h>
#include "ishaders.h"
#include "iscript.h"
#include "iscriptinterface.h"
namespace script
{
/**
* This class represents a single Material as seen by the Python script.
*/
class ScriptMaterial
{
// The contained shader (can be empty)
MaterialPtr _material;
public:
ScriptMaterial(const MaterialPtr& material) :
_material(material)
{}
operator const MaterialPtr&() const {
return _material;
}
std::string getName() {
return _material ? _material->getName() : "";
}
std::string getShaderFileName() {
return _material ? _material->getShaderFileName() : "";
}
void setShaderFileName(const std::string& filename)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setShaderFileName(filename);
}
std::string getDescription() {
return _material ? _material->getDescription() : "";
}
std::string getDefinition() {
return _material ? _material->getDefinition() : "";
}
bool isVisible() {
return _material ? _material->isVisible() : false;
}
bool isAmbientLight() {
return _material ? _material->isAmbientLight() : false;
}
bool isBlendLight() {
return _material ? _material->isBlendLight() : false;
}
bool isFogLight() {
return _material ? _material->isFogLight() : false;
}
bool isNull() const {
return _material == nullptr;
}
std::string getEditorImageExpressionString()
{
return _material && _material->getEditorImageExpression() ?
_material->getEditorImageExpression()->getExpressionString() : std::string();
}
void setEditorImageExpressionFromString(const std::string& editorImagePath)
{
throwIfMaterialCannotBeModified();
_material->setEditorImageExpressionFromString(editorImagePath);
}
float getSortRequest()
{
return _material ? _material->getSortRequest() : -1;
}
void setSortRequest(float sortRequest)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSortRequest(sortRequest);
}
void setSortRequest(Material::SortRequest sortRequest) // overload needed for python
{
throwIfMaterialCannotBeModified();
if (_material) _material->setSortRequest(static_cast<float>(sortRequest));
}
void resetSortRequest()
{
throwIfMaterialCannotBeModified();
if (_material) _material->resetSortRequest();
}
float getPolygonOffset()
{
return _material ? _material->getPolygonOffset() : 0;
}
void setPolygonOffset(float offset)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setPolygonOffset(offset);
}
void clearPolygonOffset()
{
throwIfMaterialCannotBeModified();
if (_material) _material->clearMaterialFlag(Material::FLAG_POLYGONOFFSET);
}
ClampType getClampType()
{
return _material ? _material->getClampType() : CLAMP_REPEAT;
}
void setClampType(ClampType type)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setClampType(type);
}
Material::CullType getCullType()
{
return _material ? _material->getCullType() : Material::CULL_BACK;
}
void setCullType(Material::CullType type)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setCullType(type);
}
int getMaterialFlags() const
{
return _material ? _material->getMaterialFlags() : 0;
}
void setMaterialFlag(Material::Flags flag)
{
throwIfMaterialCannotBeModified();
if (_material) _material->setMaterialFlag(flag);
}
void clearMaterialFlag(Material::Flags flag)
{
throwIfMaterialCannotBeModified();
if (_material) _material->clearMaterialFlag(flag);
}
private:
void throwIfMaterialCannotBeModified()
{
if (!_material || !GlobalMaterialManager().materialCanBeModified(_material->getName()))
{
throw std::runtime_error("Material cannot be modified");
}
}
};
class MaterialVisitor
{
public:
virtual ~MaterialVisitor() {}
virtual void visit(const MaterialPtr& shader) = 0;
};
// Wrap around the EntityClassVisitor interface
class MaterialVisitorWrapper :
public MaterialVisitor
{
public:
void visit(const MaterialPtr& shader) override
{
// Wrap this method to python
PYBIND11_OVERLOAD_PURE(
void, /* Return type */
MaterialVisitor, /* Parent class */
visit, /* Name of function in C++ (must match Python name) */
ScriptMaterial(shader) /* Argument(s) */
);
}
};
/**
* greebo: This class provides the script interface for the GlobalMaterialManager module.
*/
class ShaderSystemInterface :
public IScriptInterface
{
public:
void foreachMaterial(MaterialVisitor& visitor);
ScriptMaterial getMaterial(const std::string& name);
bool materialExists(const std::string& name);
bool materialCanBeModified(const std::string& name);
ScriptMaterial createEmptyMaterial(const std::string& name);
ScriptMaterial copyMaterial(const std::string& nameOfOriginal, const std::string& nameOfCopy);
bool renameMaterial(const std::string& oldName, const std::string& newName);
void removeMaterial(const std::string& name);
void saveMaterial(const std::string& name);
// IScriptInterface implementation
void registerInterface(py::module& scope, py::dict& globals) override;
};
} // namespace script