[go: up one dir, main page]

Menu

[r81]: / lpi_gui_ex.h  Maximize  Restore  History

Download this file

363 lines (283 with data), 11.4 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
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/>.
*/
/*
extra GUI elements such as menus, canvas, ...
NOTE: some of these elements are currently broken due to refactoring and will be fixed later
TODO: fix menu and other such elements
*/
#ifndef LPI_GUI_EX_H_INCLUDED
#define LPI_GUI_EX_H_INCLUDED
#include "lpi_gui.h"
#include "lpi_color.h"
#include "lpi_event.h"
#include "lpi_texture.h"
#include "lpi_text.h"
namespace lpi
{
namespace gui
{
class DropMenu : public Element
{
private:
std::vector<Button> menuButton;
std::vector<bool> separator;
/*
identity:
unique identifier (number) for each option, which can optionally be used to check options,
for example in an rpg game with a drop menu for items in inventory, option "drop item" can
always get number 5, option "use" can get number 7 for example, and then no matter at
what location in the menu the "use" option is, or no matter what alternative name (e.g.
"drink") the use option has, you can still check if it was pressed by using checkIdentity()
identities can only be set to useful values if you use the addOption function to add options
one by one, the setOptions function doesn't fill in useful identity values but only 0
negative identities are most certainly allowed
*/
std::vector<int> identity;
public:
//std::string text; //the menu texts, sorted, with | chars between them, and closed with a final NULL char
void setOptions(const std::string& text);
void clearOptions();
//text styles
Markup markup1;
Markup markup2;
BackPanel panel;
BackRule hrule; //for the separators
DropMenu();
void makeColored(int x, int y, const std::string& text,
const Markup& markup1 = TS_White, const Markup& markup2 = TS_White,
const ColorRGB& menuColor = RGB_Grey, BackRule hrule = DEFAULTHRULE); //make with simple color panel
void make(int x, int y, const std::string& text,
const Markup& markup1 = TS_White, const Markup& markup2 = TS_White,
BackPanel panel = DEFAULTPANEL, BackRule hrule = DEFAULTHRULE); //make with given panel
virtual void drawWidget() const;
virtual void handleWidget();
int check();
Button* getButton(int i);
Button* getButton(const std::string& name);
int getNumButtons() const { return menuButton.size(); }
std::string checkText();
/*
returns 0 if none, or a button where no identity was set, is pressed, or
the identity of the button if one with an identity was pressed
*/
int checkIdentity();
void addOption(const std::string& text, int id = 0); //id is an optional identity
bool autoDisable; //if true, the menu will totallyDisable itself if you click anywhere not on the menu
};
class Droplist : public Element
{
private:
std::vector<Button> textButton;
void init(const std::string& text, int numVisibleOptions);
int sizexc; //width when closed = width to be able to contain widest text in the list
int sizexo; //width when open = width of widest text in the list
int sizeyc; //height when closed
int sizeyo; //height when open
void open();
void close();
public:
//std::string text; //the menu texts, sorted, with | chars between them, and closed with a final NULL char
//text styles
Markup markup1; //Markup for a textbutton of the selection list
Markup markup2; //textbutton mouseOver
Markup markup3; //the selected one on top (instead of in the list)
BackPanel topPanel; //color of the top where the selected text is
BackPanel listPanel; //color of the selection list
int numVisibleOptions; //in the selectionlist
bool opened; //true if the selectionlist is visible
Droplist();
Scrollbar bar;
Button listButton; //the button to open and close the list
void scroll();
int selected;//the selected option of the list (it's index)
void make(int x, int y, const std::string& text, int numVisibleOptions = -1,
const Markup& markup1 = TS_Black, const Markup& markup2 = TS_Red, const Markup& markup3 = TS_Shadow,
BackPanel topPanel = COLORPANEL(RGB_Grey), BackPanel listPanel = COLORPANEL(RGB_White),
Texture* buttonTexture = &builtInTexture[28]);
void makeScrollbar(const GuiSet* set = &builtInGuiSet);
virtual void drawWidget() const;
int check();
std::string checkText();
virtual void handleWidget();
void addOption(const std::string& text);
};
/*
An invisible grid of numx * numy rectangles, it can return over which square the mouse is (top left one has coordinates 0, 0)
*/
class Matrix : public Element
{
//nothing required in here! it's a Element!
public:
void make(int x0, int y0, int x1, int y1, int numx, int numy);
unsigned long numx;
unsigned long numy;
unsigned long getTileX() const;
unsigned long getTileY() const;
int getScreenX(int tilex) const;
int getScreenY(int tiley) const;
};
/*
similar to Matrix, except here the size of the rectangles is constant, and not the number of them (if the gui element resizes)
*/
class Grid : public Element
{
//nothing required in here! it's a Element!
public:
void make(int x0, int y0, int x1, int y1, int tileSizeX, int tileSizeY);
void setNumTiles(int amount); //real amount can be larger, as the width of rows will stay the same, it'll add rows at the bottom or remove rows
Grid();
//returns x position on screen of center of given tile
int getTileCenterx(int index) const; //index = index of the tile
//returns y position on screen of center of given tile
int getTileCentery(int index) const; //index = index of the tile
bool showGrid;
ColorRGB gridColor;
virtual void drawWidget() const;
int tileSizeX;
int tileSizeY;
unsigned long getNumx() const;
unsigned long getNumy() const;
unsigned long getNumElements() const;
int getTileX() const;
int getTileY() const;
int getTile() const; //returns index of the tile
int getScreenX(int tilex) const;
int getScreenY(int tiley) const;
};
struct PainterStack
{
int x0;
int y0;
int x1;
int y1;
ColorRGB color;
Texture* texture;
std::string text;
Markup textMarkup;
int type; //0: point, 1: line, 2: texture (coord = top left), 3: centered texture, 4: text
};
class Painter : public Element
{
public:
ColorRGB color;
virtual void drawWidget() const;
Painter();
void make(int x, int y, int sizex = 200, int sizey = 150, const ColorRGB& color = RGB_Invisible);
void queuePoint(int x, int y, const ColorRGB& color = RGB_White);
void queueLine(int x0, int y0, int x1, int y1, const ColorRGB& color = RGB_White);
void queueRectangle(int x0, int y0, int x1, int y1, const ColorRGB& color = RGB_White);
void queueTexture(int x, int y, Texture* texture, const ColorRGB& colorMod = RGB_White);
void queueTextureCentered(int x, int y, Texture* texture, const ColorRGB& colorMod = RGB_White);
void queueText(int x, int y, const std::string& text, const Markup& markup);
private:
mutable std::vector<PainterStack> stack; //mutable because it's emptied everytime after drawing
};
//message boxes
class YesNoWindow : public Window
{
public:
Button yes;
Button no;
Text message;
YesNoWindow();
void make(int x, int y, int sizex, int sizey, const std::string& text);
};
class OkWindow : public Window
{
public:
Button ok;
Text message;
OkWindow();
void make(int x, int y, int sizex, int sizey, const std::string& text);
};
//a painting canvas that allows you to paint with the mouse --> TODO: make this more general usable as per-pixel plotting tool
class Canvas : public Element
{
private:
void init();
int oldMouseX;
int oldMouseY;
bool validOldMousePos;
public:
ColorRGB leftColor; //color of the brush for left mouse button
ColorRGB rightColor; //color of the brush for right mouse button
ColorRGB backColor; //the initial background texture of the canvas
double size; //size of the brush in pixels (it's the radius (not diameter) when the brush is round)
double hardness; //hardness of the brush from 0.0 to 1.0
double opacity; //opacity of the brush from 0.0 to 1.0
Texture canvas; //the OpenGL canvas texture (gets updated with canvasData all the time)
int border;
virtual void drawWidget() const;
virtual void handleWidget();
void clear(); //clear the whole texture to backColor, deleting it's previous contents
Canvas();
void make(int x, int y, int sizex, int sizey, const ColorRGB& backColor = RGB_White, int border = 0, const ColorRGB& leftColor = RGB_Black, const ColorRGB& rightColor = RGB_Red, double size = 1.0, double hardness = 1.0, double opacity = 1.0);
void setBrush(const ColorRGB& leftColor = RGB_Black, const ColorRGB& rightColor = RGB_Red, double size = 1.0, double hardness = 1.0, double opacity = 1.0);
};
template<typename T>
class Variable : public Element //can be anything the typename is: integer, float, ..., as long as it can be given to a stringstream
{
public:
T v;
std::string label;
Markup markup;
virtual void drawWidget() const
{
print(label, x0, y0, markup);
print(v, x0 + label.length() * markup.getWidth(), y0, markup);
}
Variable() { totallyDisable(); }
void make(int x, int y, T v, const std::string& label="", const Markup& markup = TS_W)
{
this->x0 = x;
this->y0 = y;
//for now, only the length of the label + 1 is used
this->setSizex((label.length() + 1) * markup.getWidth());
this->setSizey(markup.getHeight());
this->v = v;
this->label = label;
this->markup = markup;
this->visible = 1;
this->active = 1;
}
void setValue(T v) { this->v = v; }
T& getValue() { return this->v; }
const T& getValue() const { return this->v; }
};
class Rectangle : public Element
{
public:
ColorRGB color;
virtual void drawWidget() const;
Rectangle();
void make(int x, int y, int sizex=64, int sizey=64, const ColorRGB& color = RGB_Grey);
};
class Line : public Element
{
public:
ColorRGB color;
virtual void drawWidget() const;
Line();
int lx0;
int ly0;
int lx1;
int ly1;
void make(int x, int y, int sizex=64, int sizey=64, const ColorRGB& color = RGB_Grey);
void setEndpoints(int x0, int y0, int x1, int y1);
};
} //namespace gui
} //namespace lpi
#endif