TDME2  1.9.200
GUIGradientNode.cpp
Go to the documentation of this file.
2 
3 #include <tdme/tdme.h>
4 #include <tdme/engine/Texture.h>
6 #include <tdme/engine/Engine.h>
16 #include <tdme/math/Matrix3x3.h>
17 #include <tdme/utilities/Console.h>
18 #include <tdme/utilities/Float.h>
19 #include <tdme/utilities/Integer.h>
21 
23 
41 
42 GUIGradientNode::Gradient GUIGradientNode::createGradient(const string& colors, const string& rotation) {
44  gradient.rotationAngle = Integer::parse(rotation);
45  auto colorsArray = StringTools::tokenize(colors, ",");
46  for (const auto& color: colorsArray) {
47  auto colorComponents = StringTools::tokenize(color, "=");
48  if (colorComponents.size() != 2) {
49  Console::println("GUIGradientNode::createGradient():color invalid: " + color);
50  continue;
51  }
52  if (StringTools::endsWith(colorComponents[0], "%") == false) {
53  Console::println("GUIGradientNode::createGradient():color start invalid: " + colorComponents[0]);
54  continue;
55  }
56  colorComponents[0] = StringTools::trim(colorComponents[0]);
57  colorComponents[0] = StringTools::substring(colorComponents[0], 0, colorComponents[0].size() - 1);
58  colorComponents[1] = StringTools::trim(colorComponents[1]);
59  auto start = Float::parse(colorComponents[0]) / 100.0f;
60  auto guiColor = GUIColor(colorComponents[1]);
61  gradient.colors[gradient.count] = guiColor;
63  gradient.count++;
64  }
65  return gradient;
66 }
67 
69  GUIScreenNode* screenNode,
70  GUIParentNode* parentNode,
71  const string& id,
72  GUINode_Flow* flow,
73  const GUINode_Alignments& alignments,
74  const GUINode_RequestedConstraints& requestedConstraints,
75  const GUIColor& backgroundColor,
76  const string& backgroundImage,
77  const GUINode_Scale9Grid& backgroundImageScale9Grid,
78  const GUIColor& backgroundImageEffectColorMul,
79  const GUIColor& backgroundImageEffectColorAdd,
80  const GUINode_Border& border,
81  const GUINode_Padding& padding,
82  const GUINodeConditions& showOn,
83  const GUINodeConditions& hideOn,
84  const string& tooltip,
85  const GUIColor& effectColorMul,
86  const GUIColor& effectColorAdd,
87  const GUINode_Clipping& clipping,
88  const string& mask,
89  float maskMaxValue,
90  const Gradient& gradient):
91  GUINode(screenNode, parentNode, id, flow, alignments, requestedConstraints, backgroundColor, backgroundImage, backgroundImageScale9Grid, backgroundImageEffectColorMul, backgroundImageEffectColorAdd, border, padding, showOn, hideOn, tooltip)
92 {
93  this->effectColorMul = effectColorMul;
94  this->effectColorAdd = effectColorAdd;
95  this->textureMatrix.identity();
96  this->clipping = clipping;
97  this->setMask(mask);
98  this->maskMaxValue = maskMaxValue;
99  this->gradient = gradient;
100 }
101 
103 {
104  return "gradient";
105 }
106 
108 {
109  return true;
110 }
111 
113 {
116  } else {
117  return computedConstraints.width;
118  }
119 }
120 
122 {
125  } else {
127  }
128 }
129 
131 {
132  if (maskTexture != nullptr) Engine::getInstance()->getTextureManager()->removeTexture(maskTexture->getId());
134 }
135 
137 {
138  if (shouldRender() == false) return;
139 
140  GUINode::render(guiRenderer);
141 
142  // clipping
143  {
144  auto renderOffsetXCurrent = guiRenderer->getRenderOffsetX();
145  auto renderOffsetYCurrent = guiRenderer->getRenderOffsetY();
146  auto screenWidth = screenNode->getScreenWidth();
147  auto screenHeight = screenNode->getScreenHeight();
152  auto renderAreaLeft = ((left) / (screenWidth / 2.0f)) - renderOffsetXCurrent - 1.0f;
153  auto renderAreaTop = ((screenHeight - top) / (screenHeight / 2.0f)) + renderOffsetYCurrent - 1.0f;
154  auto renderAreaRight = ((left + width) / (screenWidth / 2.0f)) - renderOffsetXCurrent - 1.0f;
155  auto renderAreaBottom = ((screenHeight - top - height) / (screenHeight / 2.0f)) + renderOffsetYCurrent - 1.0f;
156  guiRenderer->setSubRenderAreaLeft(renderAreaLeft);
157  guiRenderer->setSubRenderAreaTop(renderAreaTop);
158  guiRenderer->setSubRenderAreaRight(renderAreaRight);
159  guiRenderer->setSubRenderAreaBottom(renderAreaBottom);
160  }
161 
162  // render texture if required
163  if (maskTextureId != 0) {
164  guiRenderer->setMaskMaxValue(maskMaxValue);
165  guiRenderer->bindMask(maskTextureId);
166  }
168  //
169  auto screenWidth = screenNode->getScreenWidth();
170  auto screenHeight = screenNode->getScreenHeight();
171  guiRenderer->setTexureMatrix(textureMatrix);
172  guiRenderer->setEffectColorMul(effectColorMul);
173  guiRenderer->setEffectColorAdd(effectColorAdd);
176  float width = getContentWidth() - padding.left - padding.right;
177  float height = getContentHeight() - padding.top - padding.bottom;
178  guiRenderer->addQuad(
179  ((left) / (screenWidth / 2.0f)) - 1.0f,
180  ((screenHeight - top) / (screenHeight / 2.0f)) - 1.0f,
181  1.0f, 1.0f, 1.0f, 1.0f,
182  0.0f,
183  0.0f,
184  ((left + width) / (screenWidth / 2.0f)) - 1.0f,
185  ((screenHeight - top) / (screenHeight / 2.0f)) - 1.0f,
186  1.0f, 1.0f, 1.0f, 1.0f,
187  1.0f,
188  0.0f,
189  ((left + width) / (screenWidth / 2.0f)) - 1.0f,
190  ((screenHeight - top - height) / (screenHeight / 2.0f)) - 1.0f,
191  1.0f, 1.0f, 1.0f, 1.0f,
192  1.0f,
193  1.0f,
194  ((left) / (screenWidth / 2.0f)) - 1.0f,
195  ((screenHeight - top - height) / (screenHeight / 2.0f)) - 1.0f,
196  1.0f, 1.0f, 1.0f, 1.0f,
197  0.0f,
198  1.0f
199  );
200  guiRenderer->render();
201  // gradient
202  guiRenderer->unsetGradient();
203  //
204  guiRenderer->bindTexture(0);
205  if (maskTextureId != 0) {
206  guiRenderer->setMaskMaxValue(1.0f);
207  guiRenderer->bindMask(0);
208  }
209  guiRenderer->setTexureMatrix((Matrix3x3()).identity());
210 }
211 
212 void GUIGradientNode::setTextureMatrix(const Matrix3x3& textureMatrix) {
213  this->textureMatrix.set(textureMatrix);
214 }
215 
217  return effectColorMul;
218 }
219 
220 void GUIGradientNode::setEffectColorMul(const GUIColor& effectColorMul) {
221  this->effectColorMul = effectColorMul;
222 }
223 
225  return effectColorAdd;
226 }
227 
228 void GUIGradientNode::setEffectColorAdd(const GUIColor& effectColorAdd) {
229  this->effectColorAdd = effectColorAdd;
230 }
231 
233  return clipping;
234 }
235 
236 GUINode_Clipping GUIGradientNode::createClipping(const string& allClipping, const string& left, const string& top, const string& right, const string& bottom)
237 {
239  clipping.left = getRequestedPixelValue(allClipping, 0);
240  clipping.top = getRequestedPixelValue(allClipping, 0);
241  clipping.right = getRequestedPixelValue(allClipping, 0);
242  clipping.bottom = getRequestedPixelValue(allClipping, 0);
247  return clipping;
248 }
249 
250 const string& GUIGradientNode::getMask() {
251  return mask;
252 }
253 
254 void GUIGradientNode::setMask(const string& mask) {
255  if (maskTexture != nullptr) {
256  Engine::getInstance()->getTextureManager()->removeTexture(maskTexture->getId());
257  maskTexture = nullptr;
258  }
259  this->mask = mask;
260  this->maskTexture = mask.empty() == true?nullptr:screenNode->getImage(mask);
261  this->maskTextureId = maskTexture == nullptr?0:Engine::getInstance()->getTextureManager()->addTexture(maskTexture, 0);
262 }
Engine main class.
Definition: Engine.h:131
Texture entity.
Definition: Texture.h:24
const string & getId() const
Definition: Texture.h:172
static GUINode_Clipping createClipping(const string &allClipping, const string &left, const string &top, const string &right, const string &bottom)
Create clipping.
const string getNodeType() override
GUIGradientNode(GUIScreenNode *screenNode, GUIParentNode *parentNode, const string &id, GUINode_Flow *flow, const GUINode_Alignments &alignments, const GUINode_RequestedConstraints &requestedConstraints, const GUIColor &backgroundColor, const string &backgroundImage, const GUINode_Scale9Grid &backgroundImageScale9Grid, const GUIColor &backgroundImageEffectColorMul, const GUIColor &backgroundImageEffectColorAdd, const GUINode_Border &border, const GUINode_Padding &padding, const GUINodeConditions &showOn, const GUINodeConditions &hideOn, const string &tooltip, const GUIColor &effectColorMul, const GUIColor &effectColorAdd, const GUINode_Clipping &clipping, const string &mask, float maskMaxValue, const Gradient &gradient)
Constructor.
void setEffectColorMul(const GUIColor &effectColorMul)
Set effect color mul.
void dispose() override
Dispose node.
void setMask(const string &mask)
Set mask source.
void render(GUIRenderer *guiRenderer) override
Render.
void setEffectColorAdd(const GUIColor &effectColorAdd)
Set effect color add.
void setTextureMatrix(const Matrix3x3 &textureMatrix)
Set texture matrix.
GUI element node conditions.
static STATIC_DLL_IMPEXT GUINode_RequestedConstraints_RequestedConstraintsType * AUTO
GUI node base class.
Definition: GUINode.h:64
GUINode_Border border
Definition: GUINode.h:160
virtual void render(GUIRenderer *guiRenderer)
Render.
Definition: GUINode.cpp:509
static int getRequestedPixelValue(const string &value, int defaultValue)
Get requested pixel value.
Definition: GUINode.cpp:324
GUINode_ComputedConstraints computedConstraints
Definition: GUINode.h:152
GUINode_Padding padding
Definition: GUINode.h:159
GUIScreenNode * screenNode
Definition: GUINode.h:147
GUINode_RequestedConstraints requestedConstraints
Definition: GUINode.h:151
bool shouldRender()
Returns if to render.
Definition: GUINode.h:302
virtual void dispose()
Dispose node.
Definition: GUINode.cpp:462
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 setSubRenderAreaBottom(float renderAreaBottom)
Set sub render area bottom.
Definition: GUIRenderer.h:340
void setGradient(int count, array< GUIColor, 10 > &colors, array< float, 10 > &colorStarts, float rotationAngle)
Set gradient properties.
void setMaskMaxValue(float maskMaxValue)
Set mask max value.
void setTexureMatrix(const Matrix3x3 &textureMatrix)
Set texture matrix.
void unsetGradient()
Disable gradient.
void bindTexture(int32_t textureId)
Bind texture.
void setSubRenderAreaRight(float renderAreaRight)
Set sub render area right.
Definition: GUIRenderer.h:317
void setSubRenderAreaLeft(float renderAreaLeft)
Set sub render area left.
Definition: GUIRenderer.h:271
void setEffectColorAdd(const GUIColor &color)
Set effect color add.
Definition: GUIRenderer.h:192
void setSubRenderAreaTop(float renderAreaTop)
Set sub render area top.
Definition: GUIRenderer.h:294
void setEffectColorMul(const GUIColor &color)
Set effect color mul.
Definition: GUIRenderer.h:184
void bindMask(int32_t textureId)
Bind mask texture.
void addQuad(float x1, float y1, float colorR1, float colorG1, float colorB1, float colorA1, float tu1, float tv1, float x2, float y2, float colorR2, float colorG2, float colorB2, float colorA2, float tu2, float tv2, float x3, float y3, float colorR3, float colorG3, float colorB3, float colorA3, float tu3, float tv3, float x4, float y4, float colorR4, float colorG4, float colorB4, float colorA4, float tu4, float tv4, bool solidColor=false, bool rotated=false)
Add quad Note: quad vertices order 1 2 +-—+ | | | | +-—+ 4 3.
Definition: GUIRenderer.h:507
Matrix3x3 class representing matrix3x3 mathematical structure and operations for 2d space.
Definition: Matrix3x3.h:20
Matrix3x3 & identity()
Creates identity matrix.
Definition: Matrix3x3.h:128
Matrix3x3 & set(float r0c0, float r0c1, float r0c2, float r1c0, float r1c1, float r1c2, float r2c0, float r2c1, float r2c2)
Sets this matrix by its components.
Definition: Matrix3x3.h:88
Console class.
Definition: Console.h:29
Float class.
Definition: Float.h:27
Integer class.
Definition: Integer.h:25
String tools class.
Definition: StringTools.h:22
GUI node border entity.
GUI node clipping entity.
GUI node padding entity.
GUINode_RequestedConstraints_RequestedConstraintsType * widthType
GUINode_RequestedConstraints_RequestedConstraintsType * heightType
GUI node scale 9 grid entity.