GlistEngine
Loading...
Searching...
No Matches
gGUITextbox.h
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2014 Nitra Games Ltd., Istanbul, Turkey *
3 * *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
11 * *
12 * The above copyright notice and this permission notice should not be *
13 * deleted from the source form of the Software. *
14 * *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
22 * *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
26 * authorization. *
27 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Noyan Culum, 2021-09-02, Utku Sarialan, 2022-on *
31 ****************************************************************************//*
32 * gTextbox.h
33 *
34 * Created on: Sep 2, 2021
35 * Author: noyan
36 */
37
38#ifndef UI_GGUITEXTBOX_H_
39#define UI_GGUITEXTBOX_H_
40
41#include "gGUIControl.h"
42#include "gGUIText.h"
43#include "gGUIBBCodeParser.h"
44#include <map>
45#include <memory>
46#include <stack>
47#include <utility>
48class gGUIManager;
49
50
51class gGUITextbox: public gGUIControl {
52public:
53
55 virtual ~gGUITextbox();
56
57 void set(gBaseApp* root, gBaseGUIObject* topParentGUIObject, gBaseGUIObject* parentGUIObject, int parentSlotLineNo, int parentSlotColumnNo, int x, int y, int w, int h) override;
58
64 void setText(const std::string& text);
65
71 std::string getText();
72
79 void setSelection(int startChar, int endChar);
80
84 void selectAll();
85
91 std::string getSelectedText();
92
94 void copyText();
95 void cutText();
96 void pasteText();
99 void longPressed(int x, int y);
100
109
116
127 void setLineCount(int linecount);
128
135
139 int getCursor(int x, int y) override;
140
149 void setSize(int width, int height);
150
159
166 bool isNumeric();
167
177
186 void addTopMargin(int top);
187
193 void setLineTopMargin(int linetopmargin);
194
203
210
217 /*
218 * Enables/disables the vertical margin between textbox and its background
219 *
220 */
221 void enableVerticalMargin(bool isAligned);
223
224 void update() override;
225 void draw() override;
226
227 void keyPressed(int key) override;
228 void keyReleased(int key) override;
229 void charPressed(unsigned int codepoint) override;
230 void mousePressed(int x, int y, int button) override;
231 void mouseReleased(int x, int y, int button) override;
232 void mouseDragged(int x, int y, int button) override;
233 void unfocus() override;
234
236 void cleanText();
237
238 void setFirstX(int firstx);
239 void setFirstY(int firsty);
240
241 int getInitX();
242
243 void setTextFont(gFont* textFont);
245
246 void setTextAlignment(int textAlignment, float cellW, int initX);
247
248 void setTextColor(gColor* textColor);
249
251 void setCursorPosX(int cursorPosX, int length);
255
256 void setDisabled(bool isDisabled);
257
258 void setEditMode(bool editMode);
259
261
267 void setHintText(const std::string& hintText);
268
275
281 void setHintTextColor(const gColor& color);
282
289
302 void enableBBCode(bool isEnabled = true);
303
309 bool isBBCodeEnabled() const;
310
317 std::string getPlainText();
318
324 void setBBCodeCodeTextColor(const gColor& color);
325
326private:
327 static const int KEY_NONE = 0, KEY_BACKSPACE = 1, KEY_LEFT = 2, KEY_RIGHT = 4, KEY_DELETE = 8, KEY_ENTER = 16, KEY_UP = 32, KEY_DOWN = 64;
328
329 int boxw, boxh;
330 int cursorposx, cursorposy, cursorposchar, cursorposutf, cursorpreviousposutf;
331 std::string text;
332 int cursorshowcounter, cursorshowcounterlimit, cursorshowlimit;
333 bool editmode;
334 bool isselectionmenushown;
335 std::vector<short> letterlength;
336 std::vector<int> letterpos;
337 int keystate;
338 int keypresstime, keypresstimelimit1, keypresstimelimit2;
339 int initx;
340 int firstchar, firstutf, firstposx;
341 int lastutf;
342 void handleKeys();
343 void pressKey();
344 std::vector<int> clickTextbox(int x, int y);
345 std::vector<int> calculateClickPosition(int x, int y);
346 std::vector<int> calculateClickPositionMultiline(int x, int y);
347 std::vector<int> calculateCursorPositionMultiline(int x, int y);
348 void calculateLinePositionMultiline(int x, int y);
349 std::vector<int> calculateLetterPosition(int letterCharNo);
350 std::vector<int> calculateAllLetterPositions();
351 void calculateLines();
352 int calculateLastUtf();
353 int calculateCharNoFromUtf(int letterUtfNo);
354 int calculateCharNum(std::string str);
355 int findFirstSpace(int lineend);
356 void startSelection();
357 std::vector<short> readString(const std::string& str);
358 bool isLetter(char c);
359 bool isNumber(char c);
360 void findCursorPosition();
361 void findCursorPositionPassword();
362 void calculateLineCount();
363 void resetCursorPosition();
364 void syncTextLayout();
365 void clampCaretAndScroll();
366 void scrollCaretIntoView();
367 void deleteSelectedText();
368 int getPreviousWordCharIndex(int charIdx);
369 int getNextWordCharIndex(int charIdx);
370 bool selectionmode;
371 int selectionposchar1, selectionposchar2;
372 int selectionposx1, selectionposx2;
373 int selectionposutf1, selectionposutf2;
374 int selectionboxx1, selectionboxx2, selectionboxw;
375 bool shiftpressed, ctrlpressed, commandpressed;
376 bool ctrlcpressed, ctrlvpressed, ctrlxpressed, ctrlapressed, ctrlzpressed;
377 bool commandcpressed, commandvpressed, commandxpressed, commandapressed, commandzpressed;
378 bool isdragging;
379 long clicktime, previousclicktime, firstclicktime, clicktimediff;
380 bool isdoubleclicked, istripleclicked;
381 bool iseditable;
382 bool isselectedall;
383 int rowsnum;
384 int lineheight;
385 bool ismultiline;
386 bool rowsnumexceeded;
387 int currentline, linecount, lastdrawnline;
388 int linetopmargin;
389 int hdiff;
390 std::vector<std::string> lines;
391 std::vector<int> lineendchar;
392 int leftlimit, rightlimit, toplimit, bottomlimit;
393 bool isnumeric;
394 bool ispassword;
395 int dotradius;
396 bool isbackgroundenabled;
397 int totalh;
398 int firstx, firsty;
399 bool boxshrinked, boxexpanded;
400 bool arrowkeypressed;
401 gFont* textfont;
402 gGUIManager* manager;
403 int textalignment;
404 int textalignmentamount;
405 gColor* textcolor;
406 bool colorset;
407 bool isdisabled;
408 int charawidth;
409 float cursoroffset;
410
411 bool widthexceeded;
412 int widthAdjusmentDelay;
413
414 gGUIText hinttext;
415 gColor hinttextcolor;
416
417 //undo stacks
418 std::stack<std::string> undostack;
419 std::stack<int> cursorposxstack, cursorposystack, cursorposcharstack, cursorposutfstack;
420 std::stack<int> firstutfstack, firstcharstack, firstposxstack;
421 std::stack<int> currentlinestack, lastdrawnlinestack,linecountstack;
422
423 void pushToStack();
424
425 // BBCode rich-text state and layout cache
426 bool isbbcodeenabled;
427 bool bbcodedocdirty;
428 bool bbcodelayoutdirty;
429 int bbcodelayoutwidth;
430 int bbcodelayoutfontsize;
431 bool bbcodelayoutmultiline;
432 int bbcodelayoutlinetopmargin;
433 gGUIBBCodeDocument bbcodedoc;
434 std::vector<gGUIBBCodeLayoutLine> bbcodelines;
435 gFont* bbcodeboldfont;
436 gFont* bbcodeitalicfont;
437 gFont* bbcodebolditalicfont;
438 gFont* bbcodecodefont;
439 gColor bbcodecodebgcolor;
440 bool bbcodecodebgcolorset;
441 gColor bbcodecodetextcolor;
442 bool bbcodecodetextcolorset;
443 std::map<std::pair<std::string, int>, std::shared_ptr<gFont>> bbcodesizedfonts;
444
445 bool shouldRenderBBCode() const;
446 void invalidateBBCodeDoc();
447 void invalidateBBCodeLayout();
448 void ensureBBCodeDoc();
449 void ensureBBCodeLayout();
450 gFont* resolveBBCodeFont(const gGUIBBCodeStyle& style, bool& outFauxBold);
451 float measureBBCodeText(const std::string& str, const gGUIBBCodeStyle& style);
452 gGUIBBCodeFontMetrics measureBBCodeMetrics(const gGUIBBCodeStyle& style);
453 float getBBCodeSingleLineDrawX() const;
454 void drawBBCode();
455 gGUIBBCodeHitResult hitTestBBCode(int x, int y);
456 void moveRawCursorToByteOffset(int rawByte);
457};
458
459#endif /* UI_GGUITEXTBOX_H_ */
Definition gBaseApp.h:16
int top
Definition gBaseGUIObject.h:124
gBaseApp * root
Definition gBaseGUIObject.h:151
static gFont * font
Definition gBaseGUIObject.h:137
int width
Definition gBaseGUIObject.h:124
int height
Definition gBaseGUIObject.h:124
int left
Definition gBaseGUIObject.h:124
Definition gColor.h:17
Definition gFont.h:47
Definition gGUIManager.h:37
Definition gGUIText.h:20
bool isBBCodeEnabled() const
void mouseReleased(int x, int y, int button) override
void update() override
int calculateContentHeight() override
int getCursorPreviousPosUTF()
void setHintText(const std::string &hintText)
void charPressed(unsigned int codepoint) override
void setFont(gFont *font)
void setSize(int width, int height)
bool isNumeric()
void showAndroidSelectionMenu()
int getCursorPosX()
std::string getText()
std::string getPlainText()
void selectAll()
void setLineCount(int linecount)
void set(gBaseApp *root, gBaseGUIObject *topParentGUIObject, gBaseGUIObject *parentGUIObject, int parentSlotLineNo, int parentSlotColumnNo, int x, int y, int w, int h) override
void setEditMode(bool editMode)
void setDisabled(bool isDisabled)
void enableBBCode(bool isEnabled=true)
bool isBackgroundEnabled()
void setSelection(int startChar, int endChar)
void mousePressed(int x, int y, int button) override
bool isEditable()
void pasteText()
void setLineTopMargin(int linetopmargin)
gGUIText * getHintText()
int getLineCount()
void setBBCodeBoldFont(gFont *font)
void setBBCodeBoldItalicFont(gFont *font)
void setPassword(bool isPassword)
gGUIText * getHintTextObject()
bool isPassword()
void setText(const std::string &text)
int getCursorPosUTF()
void setBBCodeCodeBackgroundColor(const gColor &color)
void copyText()
void setNumeric(bool isNumeric)
void cutText()
int getCursor(int x, int y) override
void keyReleased(int key) override
void setBBCodeCodeFont(gFont *font)
void cleanText()
void setEditable(bool isEditable)
int getInitX()
virtual ~gGUITextbox()
void setTextFont(gFont *textFont)
void setTextAlignment(int textAlignment, float cellW, int initX)
void enableBackground(bool isEnabled)
int getCursorPosChar()
void draw() override
void keyPressed(int key) override
void addLeftMargin(int left)
void setTextColor(gColor *textColor)
void hideAndroidSelectionMenu()
void longPressed(int x, int y)
void setHintTextColor(const gColor &color)
int getTextboxh()
std::string getSelectedText()
void enableVerticalMargin(bool isAligned)
void setBBCodeItalicFont(gFont *font)
void setFirstY(int firsty)
void unfocus() override
void setFirstX(int firstx)
void setBBCodeCodeTextColor(const gColor &color)
void addTopMargin(int top)
static gGUITextbox * focusedtextbox
Definition gGUITextbox.h:93
void mouseDragged(int x, int y, int button) override
void setCursorPosX(int cursorPosX, int length)
Definition gGUIBBCodeParser.h:100
Definition gGUIBBCodeParser.h:109
Definition gGUIBBCodeParser.h:142
Definition gGUIBBCodeParser.h:56