TDME2  1.9.200
GUIInputInternalNode.cpp
Go to the documentation of this file.
2 
3 #include <tdme/tdme.h>
17 #include <tdme/gui/GUI.h>
18 #include <tdme/math/Math.h>
19 #include <tdme/utilities/Console.h>
21 #include <tdme/utilities/Integer.h>
23 
38 using tdme::gui::GUI;
39 using tdme::math::Math;
44 
45 GUIInputInternalNode::GUIInputInternalNode(
46  GUIScreenNode* screenNode,
47  GUIParentNode* parentNode,
48  const string& id,
49  GUINode_Flow* flow,
50  const GUINode_Alignments& alignments,
51  const GUINode_RequestedConstraints& requestedConstraints,
52  const GUIColor& backgroundColor,
53  const string& backgroundImage,
54  const GUINode_Scale9Grid& backgroundImageScale9Grid,
55  const GUIColor& backgroundImageEffectColorMul,
56  const GUIColor& backgroundImageEffectColorAdd,
57  const GUINode_Border& border,
58  const GUINode_Padding& padding,
59  const GUINodeConditions& showOn,
60  const GUINodeConditions& hideOn,
61  const string& tooltip,
62  const string& font,
63  int size,
64  const string& color,
65  const string& colorDisabled,
66  const MutableString& text,
67  int maxLength
68  ):
69  GUINode(screenNode, parentNode, id, flow, alignments, requestedConstraints, backgroundColor, backgroundImage, backgroundImageScale9Grid, backgroundImageEffectColorMul, backgroundImageEffectColorAdd, border, padding, showOn, hideOn, tooltip)
70 {
71  this->font = font.empty() == true?nullptr:screenNode->getFont(font, size);
72  this->color = color.empty() == true || color.length() == 0 ? GUIColor() : GUIColor(color);
73  this->colorDisabled = colorDisabled.empty() == true || colorDisabled.length() == 0 ? GUIColor() : GUIColor(colorDisabled);
74  this->text = text;
75  this->maxLength = maxLength;
76  if (this->font != nullptr) this->font->initialize();
77  //
78  auto controller = new GUIInputInternalController(this);
79  controller->initialize();
81 }
82 
84 {
85  try {
86  auto maxLength = Integer::parse(s);
87  return maxLength;
88  } catch (Exception& exception) {
89  Console::print(string("GUIInputInternalNode::createMaxLength(): An error occurred: "));
90  Console::println(string(exception.what()));
91  return 0;
92  }
93 }
94 
96 {
97  return "text";
98 }
99 
101 {
102  return true;
103 }
104 
106 {
107  return font == nullptr?0:font->getTextWidth(text) + border.left + border.right + padding.left + padding.right;
108 }
109 
111 {
112  return font == nullptr?0:font->getLineHeight() + border.top + border.bottom + padding.top + padding.bottom;
113 }
114 
116 {
117  if (font != nullptr) font->dispose();
119 }
120 
122 {
123  if (shouldRender() == false) return;
124 
125  GUINode::render(guiRenderer);
126  auto inputInternalController = required_dynamic_cast<GUIInputInternalController*>(this->getController());
127  auto inputNode = this->getParentControllerNode();
128  auto inputNodeController = required_dynamic_cast<GUIInputController*>(inputNode->getController());
129  auto disable = inputNodeController->isDisabled();
130  auto index = inputInternalController->getIndex();
131  auto selectionIndex = inputInternalController->getSelectionIndex();
132  if (font != nullptr) {
133  font->drawString(
134  guiRenderer,
137  text,
138  inputInternalController->getOffset(),
139  0,
140  disable == false?color:colorDisabled,
141  selectionIndex == -1 || selectionIndex == index?-1:Math::min(selectionIndex, index),
142  selectionIndex == -1 || selectionIndex == index?-1:Math::max(selectionIndex, index),
143  inputNode->getBackgroundColor()
144  );
145  }
146  if (screenNode->getGUI() != nullptr &&
147  static_cast<GUIParentNode*>(screenNode->getGUI()->getFocussedNode()) == inputNode &&
148  inputInternalController->isShowCursor() == true &&
149  inputInternalController->getCursorMode() == GUIInputInternalController::CURSORMODE_SHOW &&
150  (selectionIndex == -1 || selectionIndex == index)) {
151  auto screenWidth = screenNode->getScreenWidth();
152  auto screenHeight = screenNode->getScreenHeight();
153  float left = computedConstraints.left + computedConstraints.alignmentLeft + border.left + padding.left + (font != nullptr?font->getTextIndexX(text, inputInternalController->getOffset(), 0, inputInternalController->getIndex()):0);
155  float width = 2;
157  const auto& cursorColor = disable == false?color:colorDisabled;
158  guiRenderer->bindTexture(0);
159  guiRenderer->addQuad(
160  ((left) / (screenWidth / 2.0f)) - 1.0f,
161  ((screenHeight - top) / (screenHeight / 2.0f)) - 1.0f,
162  cursorColor[0],
163  cursorColor[1],
164  cursorColor[2],
165  cursorColor[3],
166  0.0f,
167  1.0f,
168  ((left + width) / (screenWidth / 2.0f)) - 1.0f,
169  ((screenHeight - top) / (screenHeight / 2.0f)) - 1.0f,
170  cursorColor[0],
171  cursorColor[1],
172  cursorColor[2],
173  cursorColor[3],
174  1.0f,
175  1.0f,
176  ((left + width) / (screenWidth / 2.0f)) - 1.0f,
177  ((screenHeight - top - height) / (screenHeight / 2.0f)) - 1.0f,
178  cursorColor[0],
179  cursorColor[1],
180  cursorColor[2],
181  cursorColor[3],
182  1.0f,
183  0.0f,
184  ((left) / (screenWidth / 2.0f)) - 1.0f,
185  ((screenHeight - top - height) / (screenHeight / 2.0f)) - 1.0f,
186  cursorColor[0],
187  cursorColor[1],
188  cursorColor[2],
189  cursorColor[3],
190  0.0f,
191  0.0f
192  );
193  guiRenderer->render();
194  }
195 }
196 
GUI module class.
Definition: GUI.h:64
GUIElementNode * getFocussedNode()
Definition: GUI.cpp:224
static int createMaxLength(const string &s)
Create max length.
void render(GUIRenderer *guiRenderer) override
Render.
GUI element node conditions.
GUI node controller base class.
GUI node base class.
Definition: GUINode.h:64
GUINode_Border border
Definition: GUINode.h:160
friend class GUIInputInternalController
Definition: GUINode.h:77
virtual void render(GUIRenderer *guiRenderer)
Render.
Definition: GUINode.cpp:509
unique_ptr< GUINodeController > controller
Definition: GUINode.h:163
GUINode_ComputedConstraints computedConstraints
Definition: GUINode.h:152
GUINode_Padding padding
Definition: GUINode.h:159
GUINodeController * getController()
Definition: GUINode.h:661
GUIScreenNode * screenNode
Definition: GUINode.h:147
bool shouldRender()
Returns if to render.
Definition: GUINode.h:302
GUIParentNode * getParentControllerNode()
Definition: GUINode.cpp:1011
virtual void dispose()
Dispose node.
Definition: GUINode.cpp:462
void setController(GUINodeController *controller)
Set up node controller.
Definition: GUINode.cpp:1046
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
GUIFont * getFont(const string &fileName, int size)
Get font.
GUI font class.
Definition: GUIFont.h:41
void drawString(GUIRenderer *guiRenderer, int x, int y, const MutableString &text, int offset, int length, const GUIColor &color, int selectionStartIndex=-1, int selectionEndIndex=-1, const GUIColor &backgroundColor=GUIColor::GUICOLOR_TRANSPARENT)
Draw string.
Definition: GUIFont.cpp:344
int getTextIndexX(const MutableString &text, int offset, int length, int index)
Get text index X of given text and index.
Definition: GUIFont.cpp:213
int getTextWidth(const MutableString &text)
Text width.
Definition: GUIFont.cpp:249
void bindTexture(int32_t textureId)
Bind 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
Standard math functions.
Definition: Math.h:19
Console class.
Definition: Console.h:29
Integer class.
Definition: Integer.h:25
Mutable utf8 aware string class.
Definition: MutableString.h:23
std::exception Exception
Exception base class.
Definition: Exception.h:18
GUI node border entity.
GUI node padding entity.
GUI node scale 9 grid entity.