[go: up one dir, main page]

Menu

[r119]: / lpi_gui_text.h  Maximize  Restore  History

Download this file

304 lines (227 with data), 9.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
Copyright (c) 2005-2008 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
/*
text related GUI elements for lpi_gui
NOTE: some of these elements may currently be broken due to refactoring and will be fixed later
TODO: fix possible broken elements
*/
#ifndef LPI_GUI_TEXT_H_INCLUDED
#define LPI_GUI_TEXT_H_INCLUDED
#include "lpi_gui.h"
namespace lpi
{
namespace gui
{
class InputLine : public Element //input text line
{
private:
//unsigned long titleLength;
unsigned long cursor; //position of the cursor (0 = before first char)
bool entered; //after you pressed enter!
double draw_time; //for drawing the blinking cursor
MouseState auto_activate_mouse_state;
bool control_active;
mutable bool entering_done;
size_t sel0; //selection start
size_t sel1; //selection end
int mouseToCursor(int mouseX) const; //absolute mouse position to cursor position
public:
unsigned long l; //max length
Markup markup;
Markup titleMarkup;
ColorRGB cursorColor;
/*
types:
0: normal text line
1: password (displays * instead of text)
2: integer number
*/
int type;
std::string title;
std::string text;
InputLine();
void make(int x, int y, unsigned long l,
const Markup& markup = TS_W,
int type = 0, const std::string& title = "", const Markup& titleMarkup = TS_W, const ColorRGB& cursorColor = RGB_White);
virtual void drawWidget(IGUIDrawer& drawer) const;
virtual void handleWidget(const IGUIInput& input);
void setText(const std::string& i_text);
const std::string& getText() const;
void setTitle(const std::string& i_title);
const std::string& getTitle() const;
bool isControlActive() const { return control_active; } //i.e., the cursor is blinking and it's listening to keyboard input
bool enteringDone() const; //returns true if the user has been changing (or not) the text but now is done editing because the control is no longer active for the first time since then
void clear();
bool enter();
int getInteger() const; //returns any integer number that may be in the string you typed
void selectAll();
void deleteSelectedText();
void activate(bool i_active = true);
};
/*
MultiLineText
text divided over multiple lines, and functions
to let it create the lines in different ways, such as (including, but not limited to)
-use ascii 10 chars of the string for new lines
-break off between letters in limited rectangle
-break off between words in limited rectangle
the multiple line text is represented by an array of integers (line) containing the positions of the first letter of each new line
all functions that need to draw text this way HAVE to use this class from now on
*/
class MultiLineText
{
private:
std::vector<unsigned long> line; //if the value is -1, it's no new line, it means the end is reached
std::vector<Markup> lineMarkup; //only used of useFormatting is true: then it has to remember the start-markup of every line, which will be generated at the start with the function generateLineMarkup
public:
std::string text;
Markup markup; //the markup currently used
bool char10; //if true, ascii code 10 will act as newline and ascii code 13 will be ignored
bool useFormatting; //if true, the # codes of formatted text are used. See the printFormatted function for more about this!
void resetLines();
void splitWords(int width); //markup must be given to be able to calculate width ==> max amount of letters in one line
void splitLetters(int width);
void splitChar10(); //only make a new line at ascii code 10 characters
void generateLineMarkup(); //only used it useFormatting is true
void setText(const std::string& text);
void addText(const std::string& text);
unsigned long getNumLines() const;
std::string getLine(unsigned long i) const; //returns string filled with only that line
void draw(int x, int y, unsigned long startLine, unsigned long endLine) const;
void draw(int x, int y) const;
void cursorAtCharPos(unsigned long pos, unsigned long& cline, unsigned long& column) const;
int charAtCursorPos(unsigned long cline, unsigned long column) const;
MultiLineText();
};
//The String Stack
class StringStack
{
public:
int num; //max number of strings on your stringstack
int next; //physical address of next string that'll be written
int oldest; //physical address of oldest string that was written
std::vector<std::string> text;
StringStack();
StringStack(int num);
//void makeBuffer(int num);
//void deleteBuffer();
void make(int num);
void push(const std::string& text);
int increase(int pointer, int amount = 1);
int decrease(int pointer, int amount = 1);
int getNumText() const;
const std::string& getOldest(int offset = 0);
const std::string& getNewest(int offset = 0);
private:
int numFilled; //how much of the memory places are filled currently
};
//The console (only shows output text, use for example InputLine to input text into the console)
class Console : public Element
{
private:
int numLines; //how much actual lines does it take on screen
mutable StringStack stack; //mutable because it changes while drawing this widget
void calcNumLines(); //calculates number of lines from the current messages; to be recalculated when sizex changes or messages are pushed/popped
public:
int scroll; //how much lines away from the first line (= oldest) are you scrolled, note: line != message, message can be multiple lines big!
int num; //max number of messages
//for now, no special text markup yet. All text has the same color and format.
Markup markup;
int getNumLines() const;
int getNumMessages() const;
int getVisibleLines(const Markup& markup);
virtual void drawWidget(IGUIDrawer& drawer) const;
void push(const std::string& text);
Console();
void make(int x, int y, int sizex, int sizey, int num, const Markup& markup = TS_W);
};
//The textarea: can show you a single multilinetext and allows you to scroll (if you use optional or separate scrollbar or something else)
class TextArea : public Element
{
public:
int scroll; //how much lines away from the first line
MultiLineText text;
Markup markup;
int getNumLines() const;
int getVisibleLines(const Markup& markup) const;
virtual void drawWidget(IGUIDrawer& drawer) const;
virtual void handleWidget(const IGUIInput& input);
virtual void resizeWidget();
TextArea();
void make(int x, int y, int sizex, int sizey, const std::string& text, const Markup& markup = TS_W);
void setText(const std::string& text, const Markup& markup = TS_W);
///optional scrollbar
Scrollbar scrollbar;
bool scrollEnabled;
void addScrollbar();
void setScrollbarSize();
protected:
virtual Element* getAutoSubElement(unsigned long i);
};
class InputBox : public Element
{
private:
unsigned long cursor; //position of cursor in the text, column and line position will be calculated by the MultiLineText
int textWidth; //sizex - width of the scrollbar
int firstVisibleLine; //depends on the scrollbar position
MultiLineText multiText;
std::string text;
double draw_time; //for drawing the blinking cursor
void init();
MouseState auto_activate_mouse_state;
bool control_active;
public:
int maxLines;
InputBox();
void make(int x, int y, int sizex, int sizey, int maxLines, int border = 2,
const Markup& markup = TS_B,
//BackPanel panel = COLORPANEL(RGB_White),
const ColorRGB& cursorColor = RGB_Black);
void makeScrollbar(const GuiSet* set = &builtInGuiSet);
const std::string& getText() const { return text; }
virtual void drawWidget(IGUIDrawer& drawer) const;
virtual void handleWidget(const IGUIInput& input);
int border;
//get the positions (relative to the area's x and y) of the text area
int getLeftText() const;
int getRightText() const;
int getTopText() const;
int getBottomText() const;
int getTextAreaHeight() const;
int getTextAreaWidth() const;
int getLinesVisible() const; //how much lines of text are visible
Scrollbar bar;
BackPanel panel;
Markup markup;
ColorRGB cursorColor;
protected:
virtual Element* getAutoSubElement(unsigned long i);
};
class FormattedText : public Element
{
public:
Markup markup;
virtual void drawWidget(IGUIDrawer& drawer) const;
FormattedText();
void make(int x = 0, int y = 0, const std::string& text = "", const Markup& markup = TS_W);
void setText(const std::string& text);
const std::string& getText() const { return text; }
private:
std::string text;
};
} //namespace gui
} //namespace lpi
#endif