TDME2  1.9.200
GUIImageNode.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
8 #include <tdme/engine/Texture.h>
12 #include <tdme/engine/Engine.h>
24 #include <tdme/math/Math.h>
27 #include <tdme/utilities/Console.h>
30 
32 
33 using std::string;
34 using std::to_string;
35 
59 
60 int GUIImageNode::thumbnailTextureIdx = 0;
61 
62 GUIImageNode::GUIImageNode(
63  GUIScreenNode* screenNode,
64  GUIParentNode* parentNode,
65  const string& id,
66  GUINode_Flow* flow,
67  const GUINode_Alignments& alignments,
68  const GUINode_RequestedConstraints& requestedConstraints,
69  const GUIColor& backgroundColor,
70  const string& backgroundImage,
71  const GUINode_Scale9Grid& backgroundImageScale9Grid,
72  const GUIColor& backgroundImageEffectColorMul,
73  const GUIColor& backgroundImageEffectColorAdd,
74  const GUINode_Border& border,
75  const GUINode_Padding& padding,
76  const GUINodeConditions& showOn,
77  const GUINodeConditions& hideOn,
78  const string& tooltip,
79  const string& source,
80  const RequestedDimensionConstraints& requestedDimensionConstraints,
81  bool mirrorX,
82  bool mirrorY,
83  const GUIColor& effectColorMul,
84  const GUIColor& effectColorAdd,
85  const GUINode_Scale9Grid& scale9Grid,
86  const GUINode_Clipping& clipping,
87  const string& mask,
88  float maskMaxValue,
89  float rotation):
91  screenNode,
92  parentNode,
93  id,
94  flow,
95  alignments,
96  requestedConstraints,
97  backgroundColor,
98  backgroundImage,
99  backgroundImageScale9Grid,
100  backgroundImageEffectColorMul,
101  backgroundImageEffectColorAdd,
102  border,
103  padding,
104  showOn,
105  hideOn,
106  tooltip,
107  requestedDimensionConstraints,
108  mirrorX,
109  mirrorY,
110  effectColorMul,
111  effectColorAdd,
112  scale9Grid,
113  clipping,
114  mask,
115  maskMaxValue
116  )
117 {
118  this->setSource(source);
119  if (Math::abs(rotation) > Math::EPSILON) rotate(rotation);
120 }
121 
123  if (texture != nullptr) {
124  Engine::getInstance()->getTextureManager()->removeTexture(texture->getId());
125  if (releaseTextureReference == true) {
127  }
128  }
129  releaseTextureReference = false;
130  source.clear();
131  texture = nullptr;
132  frameBuffer = nullptr;
133 }
134 
136 {
137  return "image";
138 }
139 
141 {
142  disposeTexture();
144 }
145 
146 const string& GUIImageNode::getSource() {
147  return source;
148 }
149 
150 void GUIImageNode::setSource(const string& source) {
151  disposeTexture();
152  this->source = source;
153  if (source.empty() == false) {
154  // tdme mesh
155  if (StringTools::endsWith(StringTools::toLowerCase(source), ".tm") == true) {
156  try {
157  vector<uint8_t> thumbnailPNGData;
158  if (FileSystem::getInstance()->getThumbnailAttachment(
159  FileSystem::getInstance()->getPathName(source),
160  FileSystem::getInstance()->getFileName(source),
161  thumbnailPNGData
162  ) == true) {
163  //
164  auto thumbnailTexture = PNGTextureReader::read("tdme.gui.guiimagenode." + to_string(thumbnailTextureIdx++), thumbnailPNGData, true);
165  if (thumbnailTexture != nullptr) {
166  this->texture = thumbnailTexture;
167  this->releaseTextureReference = true;
168  } else {
169  this->texture = screenNode->getImage("resources/engine/images/mesh_big.png");
170  this->releaseTextureReference = false;
171  }
172  } else {
173  this->texture = screenNode->getImage("resources/engine/images/mesh_big.png");
174  this->releaseTextureReference = false;
175  }
176  } catch (Exception& exception) {
177  Console::println(string() + "GUIImageNode::setSource(): " + exception.what());
178  }
179  } else
180  // tmodel
181  if (StringTools::endsWith(StringTools::toLowerCase(source), ".tmodel") == true) {
182  try {
183  vector<uint8_t> thumbnailPNGData;
184  if (PrototypeReader::readThumbnail(
185  FileSystem::getInstance()->getPathName(source),
186  FileSystem::getInstance()->getFileName(source),
187  thumbnailPNGData
188  ) == true) {
189  //
190  auto thumbnailTexture = PNGTextureReader::read("tdme.gui.guiimagenode." + to_string(thumbnailTextureIdx++), thumbnailPNGData, true);
191  if (thumbnailTexture != nullptr) {
192  this->texture = thumbnailTexture;
193  this->releaseTextureReference = true;
194  } else {
195  this->texture = screenNode->getImage("resources/engine/images/tdme_big.png");
196  this->releaseTextureReference = false;
197  }
198  } else {
199  this->texture = screenNode->getImage("resources/engine/images/tdme_big.png");
200  this->releaseTextureReference = false;
201  }
202  } catch (Exception& exception) {
203  Console::println(string() + "GUIImageNode::setSource(): " + exception.what());
204  }
205  } else
206  // gui xml
207  if (StringTools::endsWith(StringTools::toLowerCase(source), ".xml") == true) {
208  try {
209  this->texture = screenNode->getImage("resources/engine/images/gui_big.png");
210  this->releaseTextureReference = false;
211  } catch (Exception& exception) {
212  Console::println(string() + "GUIImageNode::setSource(): " + exception.what());
213  }
214  } else {
215  // other model without thumbnail
216  for (const auto& extension: ModelReader::getModelExtensions()) {
217  if (StringTools::endsWith(StringTools::toLowerCase(source), "." + extension) == true) {
218  this->texture = screenNode->getImage("resources/engine/images/mesh_big.png");
219  this->releaseTextureReference = false;
220  // done
221  break;
222  }
223  }
224  // load it
225  if (this->texture == nullptr) {
226  this->texture = source.empty() == true?nullptr:screenNode->getImage(source);
227  this->releaseTextureReference = false;
228  }
229  }
230  }
231  this->textureId = texture == nullptr?0:Engine::getInstance()->getTextureManager()->addTexture(texture, 0);
232  this->textureWidth = texture == nullptr?0:texture->getWidth();
233  this->textureHeight = texture == nullptr?0:texture->getHeight();
234 }
235 
237  if (this->texture == texture) return;
238  disposeTexture();
239  this->texture = texture;
240  if (texture == nullptr) {
241  textureId = 0;
242  textureWidth = 0;
243  textureHeight = 0;
244  return;
245  }
248  textureId = Engine::getInstance()->getTextureManager()->addTexture(texture, 0);
251 }
252 
254  disposeTexture();
255  this->texture = nullptr;
256  if (texture == nullptr) {
257  textureId = 0;
258  textureWidth = 0;
259  textureHeight = 0;
260  return;
261  }
262  textureId = texture->getColorTextureId();
265  releaseTextureReference = false;
266 }
267 
268 void GUIImageNode::rotate(float rotation) {
269  if (texture == nullptr) return;
270  auto rotatedTexture = TextureReader::rotate(texture, rotation);
271  if (rotatedTexture == nullptr) return;
272  disposeTexture();
273  this->texture = rotatedTexture;
274  this->textureId = texture == nullptr?0:Engine::getInstance()->getTextureManager()->addTexture(texture, 0);
275  this->textureWidth = texture == nullptr?0:texture->getWidth();
276  this->textureHeight = texture == nullptr?0:texture->getHeight();
278 }
279 
281  return frameBuffer;
282 }
283 
285  disposeTexture();
286  if (frameBuffer == nullptr) {
287  textureId = 0;
288  textureWidth = 0;
289  textureHeight = 0;
290  return;
291  }
295 }
Engine main class.
Definition: Engine.h:131
Frame buffer class.
Definition: FrameBuffer.h:22
int32_t getColorBufferTextureId()
Definition: FrameBuffer.h:116
Texture entity.
Definition: Texture.h:24
uint16_t getWidth() const
Definition: Texture.h:197
const string & getId() const
Definition: Texture.h:172
uint16_t getHeight() const
Definition: Texture.h:204
void disposeTexture()
Release texture.
const string getNodeType() override
void dispose() override
Dispose node.
void setTexture(Texture *texture)
Set texture.
void setSource(const string &source)
Set image source.
static STATIC_DLL_IMPEXT int thumbnailTextureIdx
Definition: GUIImageNode.h:48
void setFrameBuffer(FrameBuffer *frameBuffer)
Set frame buffer.
void rotate(float rotation)
Rotate image around center.
GUI element node conditions.
GUIScreenNode * screenNode
Definition: GUINode.h:147
GUI parent node base class thats supporting child nodes.
Definition: GUIParentNode.h:42
GUI screen node that represents a screen that can be rendered via GUI system.
Definition: GUIScreenNode.h:72
Texture * getImage(const string &fileName)
Get image.
void dispose() override
Dispose node.
File system singleton class.
Definition: FileSystem.h:17
Console class.
Definition: Console.h:29
virtual void releaseReference()
Releases a reference, thus decrementing the counter and delete it if reference counter is zero.
Definition: Reference.h:38
virtual void acquireReference()
Acquires a reference, incrementing the counter.
Definition: Reference.h:31
String tools class.
Definition: StringTools.h:22
std::exception Exception
Exception base class.
Definition: Exception.h:18
GUI node border entity.
GUI node clipping entity.
GUI node padding entity.
GUI node scale 9 grid entity.