TDME2 1.9.121
UIEditorTabView.cpp
Go to the documentation of this file.
2
3#include <string>
4#include <unordered_set>
5
6#include <tdme/tdme.h>
17#include <tdme/engine/Engine.h>
21#include <tdme/gui/GUI.h>
22#include <tdme/math/Vector3.h>
31
32using std::string;
33using std::unordered_set;
34
36
51using tdme::gui::GUI;
59
60UIEditorTabView::UIEditorTabView(EditorView* editorView, const string& tabId, GUIScreenNode* screenNode)
61{
62 this->editorView = editorView;
63 this->tabId = tabId;
64 this->popUps = editorView->getPopUps();
65 screenNodes.push_back(screenNode);
66 screenDimensions.push_back({ screenNode->getSizeConstraints().maxWidth, screenNode->getSizeConstraints().maxHeight });
67 guiEngine = Engine::createOffScreenInstance(1920, 1080, false, false, false);
68 guiEngine->setSceneColor(Color4(125.0f / 255.0f, 125.0f / 255.0f, 125.0f / 255.0f, 1.0f));
71}
72
74 delete uiTabController;
75 delete guiEngine;
76 if (projectedUi == true) {
78 delete engine;
79 }
80}
81
83 // no op
84}
85
87 // no op
88}
89
91{
92 if (projectedUi == true) {
93 //
94 auto modelEntity = dynamic_cast<Object3D*>(engine->getEntity("model"));
95 if (modelEntity != nullptr && modelMeshNode.empty() == false && modelEntity->getModel()->getNodeById(modelMeshNode) != nullptr) {
96 auto modelEntityWorldMatrix = modelEntity->getNodeTransformationsMatrix(modelMeshNode);
97 auto modelEntityModelImportMatrixInverted = modelEntity->getModel()->getImportTransformationsMatrix().clone().invert();
98 auto modelEntityWorldMatrixInverted = modelEntityWorldMatrix.clone().multiply(modelEntity->getTransformationsMatrix()).multiply(modelEntityModelImportMatrixInverted).invert();
99 // handle mouse events
100 for (auto& event: engine->getGUI()->getMouseEvents()) {
101 if (event.isProcessed() == true) continue;
102 // push event to gui engine if in book space
103 Vector3 mouseWorldCoordinate = engine->computeWorldCoordinateByMousePosition(event.getXUnscaled(), event.getYUnscaled());
104 auto bookLocalCoordinate = modelEntityWorldMatrixInverted.multiply(mouseWorldCoordinate);
105 auto clonedEvent = event;
106 clonedEvent.setX((bookLocalCoordinate.getX() - projectedUiMinX) * (guiEngine->getWidth() / (projectedUiMaxX - projectedUiMinX)));
107 clonedEvent.setY((bookLocalCoordinate.getZ() - projectedUiMinZ) * (guiEngine->getHeight() / (projectedUiMaxZ - projectedUiMinZ)));
108 clonedEvent.setXUnscaled(clonedEvent.getX());
109 clonedEvent.setYUnscaled(clonedEvent.getY());
110 if (clonedEvent.getX() >= 0 && clonedEvent.getX() < guiEngine->getWidth() &&
111 clonedEvent.getY() >= 0 && clonedEvent.getY() < guiEngine->getHeight()) {
112 guiEngine->getGUI()->getMouseEvents().push_back(clonedEvent);
113 event.setProcessed(true);
114 }
115 }
116 }
119 engine->getGUI()->getMouseEvents().clear();
120 engine->getGUI()->getKeyboardEvents().clear();
121 } else {
123 }
124}
125
127{
129 guiEngine->getGUI()->render();
130 if (projectedUi == true) engine->display();
131}
132
134{
135 try {
138 } catch (Exception& exception) {
139 Console::print(string("UIEditorTabView::initialize(): An error occurred: "));
140 Console::println(string(exception.what()));
141 }
142 // TODO: load settings
143}
144
146{
148 if (projectedUi == true) {
149 engine->dispose();
150 }
151}
152
154}
155
157 return projectedUi == false;
158}
159
161 return projectedUi == true?engine:guiEngine;
162}
163
169}
170
173}
174
178}
179
181 screenNodes.push_back(nullptr);
182 screenDimensions.push_back({ -1, -1});
183}
184
185void UIEditorTabView::setScreen(int screenIdx, GUIScreenNode* screenNode) {
186 if (screenIdx < 0 || screenIdx >= screenNodes.size()) return;
187 screenNodes[screenIdx] = screenNode;
188 screenDimensions[screenIdx] = { screenNode->getSizeConstraints().maxWidth, screenNode->getSizeConstraints().maxHeight };
189}
190
191void UIEditorTabView::unsetScreen(int screenIdx) {
192 if (screenIdx < 0 || screenIdx >= screenNodes.size()) return;
193 auto screenNode = screenNodes[screenIdx];
194 if (screenNode != nullptr) {
195 guiEngine->getGUI()->removeScreen(screenNode->getId());
196 screenNodes[screenIdx] = nullptr;
197 screenDimensions[screenIdx] = { -1, -1 };
198 }
199}
200
201void UIEditorTabView::removeScreen(int screenIdx) {
202 if (screenIdx < 0 || screenIdx >= screenNodes.size()) return;
203 auto screenNode = screenNodes[screenIdx];
204 if (screenNode != nullptr) {
205 guiEngine->getGUI()->removeScreen(screenNode->getId());
206 screenNodes.erase(screenNodes.begin() + screenIdx);
207 screenDimensions.erase(screenDimensions.begin() + screenIdx);
208 }
209}
210
213 auto screensMaxWidth = -1;
214 auto screensMaxHeight = -1;
215 for (auto i = 0; i < screenNodes.size(); i++) {
216 auto screenNode = screenNodes[i];
217 auto& screenDimensionsEntity = screenDimensions[i];
218 if (screenNode == nullptr) continue;
219 auto screenMaxWidth = screenDimensionsEntity[0];
220 auto screenMaxHeight = screenDimensionsEntity[1];
221 if (screenMaxWidth > screensMaxWidth) screensMaxWidth = screenMaxWidth;
222 if (screenMaxHeight > screensMaxHeight) screensMaxHeight = screenMaxHeight;
223 screenNode->getSizeConstraints().minWidth = -1;
224 screenNode->getSizeConstraints().minHeight = -1;
225 screenNode->getSizeConstraints().maxWidth = -1;
226 screenNode->getSizeConstraints().maxHeight = -1;
227 guiEngine->getGUI()->addScreen(screenNode->getId(), screenNode);
228 guiEngine->getGUI()->addRenderScreen(screenNode->getId());
229 }
230 if (screensMaxWidth == -1) screensMaxWidth = 1920;
231 if (screensMaxHeight == -1) screensMaxHeight = 1080;
232 if (guiEngine->getWidth() != screensMaxWidth || guiEngine->getHeight() != screensMaxHeight) {
233 guiEngine->reshape(screensMaxWidth, screensMaxHeight);
234 }
235}
236
238 return prototype;
239}
240
241Prototype* UIEditorTabView::loadPrototype(const string& pathName, const string& fileName, const string& modelMeshNode, const string& modelMeshAnimation) {
242 //
243 if (projectedUi == true) engine->reset();
244 if (prototype != nullptr) delete prototype;
245 prototype = nullptr;
246
247 //
248 try {
249 prototype = PrototypeReader::read(pathName, fileName);
250 } catch (Exception& exception) {
251 Console::print(string("UIEditorTabView::loadPrototype(): An error occurred: "));
252 Console::println(string(exception.what()));
253 }
254
255 //
256 auto projectedUiLast = projectedUi;
257 if (projectedUi == false) {
258 engine = Engine::createOffScreenInstance(512, 512, true, true, false);
261 engine->setSceneColor(Color4(125.0f / 255.0f, 125.0f / 255.0f, 125.0f / 255.0f, 1.0f));
262 guiEngine->setSceneColor(Color4(0.0f, 0.0f, 0.0f, 0.0f));
264 projectedUi = true;
265 }
266 Vector3 objectScale;
267 Tools::setupPrototype(prototype, engine, cameraRotationInputHandler->getLookFromRotations(), 1, objectScale, cameraRotationInputHandler, 1.5f, projectedUiLast == true);
268
269 //
270 auto modelEntity = dynamic_cast<Object3D*>(engine->getEntity("model"));
271 if (modelEntity != nullptr) {
273 modelEntity->setAnimation(modelMeshAnimation);
274 }
275
276 //
278
279 //
280 return prototype;
281}
282
283void UIEditorTabView::setModelMeshNode(const string& modelMeshNode) {
284 if (projectedUi == false) return;
285 //
286 auto modelEntity = dynamic_cast<Object3D*>(engine->getEntity("model"));
287 if (modelEntity != nullptr) {
288 modelEntity->unbindDiffuseTexture();
289 if (modelMeshNode.empty() == false) modelEntity->bindDiffuseTexture(guiEngine->getFrameBuffer(), modelMeshNode);
290 }
291
292 this->modelMeshNode = modelMeshNode;
293 projectedUiMinX = Float::MAX_VALUE;
294 projectedUiMinZ = Float::MAX_VALUE;
295 projectedUiMaxX = Float::MIN_VALUE;
296 projectedUiMaxZ = Float::MIN_VALUE;
297
298 auto model = prototype->getModel();
299 if (model == nullptr || model->getNodeById(modelMeshNode) == nullptr) return;
300
301 //
302 auto& modelMeshNodeFacesEntities = model->getNodeById(modelMeshNode)->getFacesEntities();
303 unordered_set<string> materialIds;
304 for (auto& facesEntity: modelMeshNodeFacesEntities) {
305 if (facesEntity.getMaterial() != nullptr) materialIds.insert(facesEntity.getMaterial()->getId());
306 for (auto& face: facesEntity.getFaces()) {
307 for (auto i = 0; i < 3; i++) {
308 projectedUiMinZ = Math::min(projectedUiMinZ, face.getNode()->getVertices()[face.getVertexIndices()[i]].getZ());
309 projectedUiMinX = Math::min(projectedUiMinX, face.getNode()->getVertices()[face.getVertexIndices()[i]].getX());
310 projectedUiMaxZ = Math::max(projectedUiMaxZ, face.getNode()->getVertices()[face.getVertexIndices()[i]].getZ());
311 projectedUiMaxX = Math::max(projectedUiMaxX, face.getNode()->getVertices()[face.getVertexIndices()[i]].getX());
312 }
313 }
314 }
315
316 //
317 for (auto& materialId: materialIds) {
318 auto materialIt = model->getMaterials().find(materialId);
319 auto material = materialIt != model->getMaterials().end()?materialIt->second:nullptr;
320 if (material == nullptr) continue;
321 material->getSpecularMaterialProperties()->setDiffuseColor(Color4(0.8f, 0.8f, 0.8f, 0.9999f));
322 if (material->getPBRMaterialProperties() != nullptr) material->getPBRMaterialProperties()->setBaseColorFactor(Color4(1.0f, 1.0f, 1.0f, 0.9999f));
323 }
324}
325
326void UIEditorTabView::setModelMeshAnimation(const string& modelMeshAnimation) {
327 if (projectedUi == false) return;
328 //
329 auto modelEntity = dynamic_cast<Object3D*>(engine->getEntity("model"));
330 if (modelEntity != nullptr) {
331 modelEntity->setAnimation(modelMeshAnimation);
332 }
333}
334
336 //
337 if (projectedUi == true) {
338 engine->dispose();
339 delete engine;
341 engine = nullptr;
343 projectedUi = false;
344 }
345 if (prototype != nullptr) delete prototype;
346 prototype = nullptr;
347
348 //
349 engine->setSceneColor(Color4(125.0f / 255.0f, 125.0f / 255.0f, 125.0f / 255.0f, 1.0f));
350}
Engine main class.
Definition: Engine.h:122
FrameBuffer * getFrameBuffer()
Definition: Engine.h:927
void reshape(int32_t width, int32_t height)
Reshape.
Definition: Engine.cpp:885
void display()
Renders the scene.
Definition: Engine.cpp:1269
int32_t getWidth()
Definition: Engine.h:865
Vector3 computeWorldCoordinateByMousePosition(int32_t mouseX, int32_t mouseY, float z)
Compute world coordinate from mouse position and z value.
Definition: Engine.cpp:1469
void setPartition(Partition *partition)
Set partition.
Definition: Engine.cpp:358
int32_t getHeight()
Definition: Engine.h:872
void setShadowMapLightEyeDistanceScale(float shadowMapLightEyeDistanceScale)
Set shadow map light eye distance scale.
Definition: Engine.h:632
void dispose()
Shutdown the engine.
Definition: Engine.cpp:1907
Entity * getEntity(const string &id)
Returns a entity by given id.
Definition: Engine.h:981
void reset()
Removes all entities and caches.
Definition: Engine.cpp:652
void setSceneColor(const Color4 &sceneColor)
Set scene color.
Definition: Engine.h:965
Object 3D to be used with engine class.
Definition: Object3D.h:60
Bogus/Simple partition implementation.
Color 4 definition.
Definition: Color4.h:20
Represents a model face, consisting of vertex, normal, tangent and bitangent vectors,...
Definition: Face.h:19
Node faces entity A node can have multiple entities containing faces and a applied material.
Definition: FacesEntity.h:28
Represents a material.
Definition: Material.h:21
Representation of a 3d model.
Definition: Model.h:32
Node * getNodeById(const string &id)
Returns a node by given name or null.
Definition: Model.cpp:78
Model node.
Definition: Node.h:31
const vector< FacesEntity > & getFacesEntities() const
Definition: Node.h:256
Represents specular material properties.
Represents specular material properties.
Prototype definition.
Definition: Prototype.h:49
void setAnimation(const string &id, float speed=1.0f)
Sets up a base animation to play.
Definition: Object3DBase.h:175
const Matrix4x4 getNodeTransformationsMatrix(const string &id)
Returns transformation matrix for given node.
Definition: Object3DBase.h:255
void unbindDiffuseTexture(const string &nodeId=string(), const string &facesEntityId=string())
Unbind dynamic texture to a node and faces entity of this object.
void bindDiffuseTexture(int32_t textureId, const string &nodeId=string(), const string &facesEntityId=string())
Bind a texture to a node and faces entity.
GUI module class.
Definition: GUI.h:66
void handleEvents()
Handle screen events.
Definition: GUI.cpp:588
void render()
Render GUIs.
Definition: GUI.cpp:414
void addRenderScreen(const string &screenId)
Add render screen.
Definition: GUI.cpp:254
vector< GUIMouseEvent > & getMouseEvents()
Definition: GUI.h:212
void removeScreen(const string &id)
Removes an screen.
Definition: GUI.cpp:196
void addScreen(const string &id, GUIScreenNode *screen)
Add screen.
Definition: GUI.cpp:186
vector< GUIKeyboardEvent > & getKeyboardEvents()
Definition: GUI.h:219
void resetRenderScreens()
Reset render screens.
Definition: GUI.cpp:235
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:57
GUIScreenNode_SizeConstraints & getSizeConstraints()
Matrix4x4 clone()
Clones this matrix.
Definition: Matrix4x4.h:624
Matrix4x4 & invert()
Inverts the matrix.
Definition: Matrix4x4.h:536
Vector3 multiply(const Vector3 &v) const
Multiplies a vector3 with this matrix into destination vector.
Definition: Matrix4x4.h:351
3D vector 3 class
Definition: Vector3.h:22
Vector3 & setX(float x)
Set X.
Definition: Vector3.h:111
Vector3 & setY(float y)
Set Y.
Definition: Vector3.h:128
void restoreOutlinerState(const TabView::OutlinerState &outlinerState)
Restore outliner state.
void storeOutlinerState(TabView::OutlinerState &outlinerState)
Store outliner state.
void setDetailsContent(const string &xml)
Set details content.
void handleInputEvents() override
Handle input events that have not yet been processed.
void setOutlinerAddDropDownContent()
Set outliner add drop down content.
void initialize(GUIScreenNode *screenNode) override
Init.
void dispose() override
Disposes the view.
bool hasFixedSize() override
If this viewport framebuffer has a fixed size.
void display() override
Renders the view.
void reloadOutliner() override
Reload outliner.
void onCameraRotation() override
On rotation event to be overloaded.
Prototype * loadPrototype(const string &pathName, const string &fileName, const string &modelMeshNode, const string &modelMeshAnimation)
Load prototype.
void initialize() override
Initiates the view.
void handleInputEvents() override
Handle input events that have not yet been processed.
void setModelMeshAnimation(const string &modelMeshAnimation)
Set model mesh animation.
CameraRotationInputHandler * cameraRotationInputHandler
void removeScreen(int screenIdx)
Remove screen.
void setScreen(int screenIdx, GUIScreenNode *screenNode)
Set screen.
void unsetScreen(int screenIdx)
Unset screen.
void updateRendering() override
Update rendering.
void setModelMeshNode(const string &modelMeshNode)
Set model mesh node.
void onCameraScale() override
On scale event to be overloaded.
EditorScreenController * getScreenController()
Definition: EditorView.h:57
Console class.
Definition: Console.h:26
std::exception Exception
Exception base class.
Definition: Exception.h:19