-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcuss.h
441 lines (330 loc) · 13.5 KB
/
cuss.h
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#ifndef _CUSS_H_
#define _CUSS_H_
#include <string>
#include <vector>
#include <istream>
#include <map>
#include "window.h"
#include "geometry.h"
namespace cuss {
enum element_type
{
ELE_NULL = 0, // Nothing
ELE_DRAWING, // Plain characters / text
ELE_TEXTBOX, // Scrollable text
ELE_LIST, // Scrollable list w/ selection
ELE_TEXTENTRY,// Type to enter text
ELE_NUMBER, // Number to select
ELE_MENU, // Drop-down menu
ELE_MAX
};
enum alignment
{
ALIGN_LEFT = 0,
ALIGN_RIGHT = 1,
ALIGN_CENTER = 2,
ALIGN_MAX
};
enum vertical_alignment
{
ALIGN_TOP = 0,
ALIGN_BOTTOM = 1,
ALIGN_VERT_MAX = 2
};
std::string element_type_name(element_type type);
struct element
{
std::string name;
int posx;
int posy;
int sizex;
int sizey;
bool selected;
bool selectable;
bool owns_data;
nc_color fg;
nc_color bg;
alignment align;
vertical_alignment v_align;
bool recently_selected;
element() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false;
fg = c_ltgray; bg = c_black; owns_data = true;
recently_selected = false; }
virtual ~element() { }
virtual element_type type() { return ELE_NULL; }
virtual void draw(Window *win) {}
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
virtual bool self_reference() { return false; }
virtual bool set_data(std::string data) { return false; }
virtual bool add_data(std::string data) { return false; }
virtual bool ref_data(std::string *data) { return false; }
virtual bool set_data(std::vector<std::string> data) { return false; }
virtual bool add_data(std::vector<std::string> data) { return false; }
virtual bool ref_data(std::vector<std::string> *data) { return false; }
virtual bool set_data(int data) { return false; }
virtual bool add_data(int data) { return false; }
virtual bool ref_data(int *data) { return false; }
virtual bool set_data(glyph gl, int posx, int posy) { return false; }
virtual bool handle_keypress(long ch) { return false; }
// This is used to set fg & bg, and hence is defined for element!
virtual bool set_data(nc_color FG, nc_color BG = c_null);
virtual bool set_alignment(alignment al) { align = al; return true; }
virtual void clear_data() {}
virtual std::string get_str() { std::string ret; return ret; }
virtual int get_int() { return 0; }
virtual std::vector<std::string> get_str_list()
{ std::vector<std::string> ret; return ret; }
};
struct ele_drawing : public element
{
std::map<Point, glyph, Pointcomp> drawing;
ele_drawing() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false;
fg = c_ltgray; bg = c_black; owns_data = true; }
virtual element_type type() { return ELE_DRAWING; };
virtual void draw(Window *win);
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
virtual bool set_data(glyph gl, int posx, int posy);
virtual bool set_data(nc_color FG, nc_color BG = c_null);
virtual bool handle_keypress(long ch) { return false; };
virtual void clear_data() { drawing.clear();};
/* Translate is breaking a rule here; it's a function that isn't inherited from
* element. I'm not sure I'm okay with this, but for now I think that translate
* is really drawing-specific; it looks for all instances of "from", and moves
* them to "to". While things like textbox could probably use this, I'm holding
* off for now.
*/
bool translate(long from, long to);
};
struct ele_textbox : public element
{
std::string *text;
int offset;
ele_textbox() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false; offset = 0;
fg = c_ltgray; bg = c_black; owns_data = false;
self_reference(); }
~ele_textbox() { if (owns_data) delete text; };
virtual element_type type() { return ELE_TEXTBOX; };
virtual void draw(Window *win);
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
/* We store this as a vector because the text needs to be split into seperate
* lines. It's more efficient to do this once, when the text is stored, than
* every time we print.
*/
virtual bool self_reference();
virtual bool set_data(std::string data);
virtual bool add_data(std::string data);
virtual bool ref_data(std::string *data);
virtual bool set_data(std::vector<std::string> data);
virtual bool add_data(std::vector<std::string> data);
// These adjust the offset
virtual bool set_data(int data);
virtual bool add_data(int data);
virtual bool handle_keypress(long ch) { return false; };
virtual void clear_data() { (*text) = ""; offset = 0;};
virtual std::string get_str() { return (*text); }
virtual std::vector<std::string> get_str_list();
};
struct ele_list : public element
{
std::vector<std::string> *list;
int offset;
int selection;
ele_list() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false; offset = 0;
selection = 0; fg = c_ltgray; bg = c_black; owns_data = false;
self_reference(); }
virtual element_type type() { return ELE_LIST; };
virtual void draw(Window *win);
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
virtual bool self_reference();
virtual bool set_data(std::string data);
virtual bool add_data(std::string data);
virtual bool set_data(std::vector<std::string> data);
virtual bool add_data(std::vector<std::string> data);
virtual bool ref_data(std::vector<std::string> *data);
// These are used to set the selection
virtual bool set_data(int data);
virtual bool add_data(int data);
// TODO: Implement search function
virtual bool handle_keypress(long ch) { return false; };
virtual void clear_data() { list->clear(); offset = 0; selection = 0;};
virtual int get_int();
virtual std::string get_str();
virtual std::vector<std::string> get_str_list() { return (*list); };
};
struct ele_textentry : public element
{
std::string *text;
ele_textentry() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false;
fg = c_ltgray; bg = c_black; owns_data = false;
self_reference(); }
virtual element_type type() { return ELE_TEXTENTRY; };
virtual void draw(Window *win);
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
virtual bool self_reference();
virtual bool set_data(std::string data);
virtual bool add_data(std::string data);
virtual bool ref_data(std::string *data);
virtual bool handle_keypress(long ch);
virtual void clear_data() { text->clear(); };
virtual std::string get_str() { return (*text); };
};
struct ele_number : public element
{
int *value;
ele_number() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false; value = 0;
fg = c_ltgray; bg = c_black; owns_data = false;
self_reference(); }
virtual element_type type() { return ELE_NUMBER; };
virtual void draw(Window *win);
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
virtual bool self_reference();
virtual bool set_data(int data);
virtual bool add_data(int data);
virtual bool ref_data(int *data);
virtual bool handle_keypress(long ch);
virtual void clear_data() { (*value) = 0; };
virtual int get_int() { return (*value); };
};
struct ele_menu : public element
{
std::string title;
std::vector<std::string> *list;
int selection;
bool open;
ele_menu() { name = ""; posx = 0; posy = 0; sizex = 0; sizey = 0;
selected = false; selectable = false; selection = -1;
open = false; fg = c_ltgray; bg = c_black; title = "";
owns_data = false; self_reference(); }
virtual element_type type() { return ELE_MENU; };
virtual void draw(Window *win);
virtual std::string save_data();
virtual void load_data(std::istream &datastream);
virtual bool self_reference();
// set_data sets the title--the only string unique identified w/o an index
virtual bool set_data(std::string data);
// add_data adds an option to the menu--the only place where adding makes sense
virtual bool add_data(std::string data);
virtual bool set_data(std::vector<std::string> data);
virtual bool add_data(std::vector<std::string> data);
virtual bool ref_data(std::vector<std::string> *data);
// Change the selection
virtual bool set_data(int data);
virtual bool add_data(int data);
virtual bool handle_keypress(long ch);
virtual void clear_data() { title.clear(); list->clear(); open = false;
selection = 0; };
virtual std::string get_str();
virtual int get_int();
virtual std::vector<std::string> get_str_list() { return (*list); };
};
enum action_id
{
ACT_NULL = 0, // Do nothing
ACT_SELECT_NEXT, // Select next
ACT_SELECT_LAST, // Select last
ACT_SELECT_NONE, // Select nothing
ACT_SELECT_STR, // Select something specific
ACT_SCROLL, // Scroll or adjust int field
ACT_SET_COLORS, // Set foreground color
ACT_TRANSLATE, // Change char A to char B
ACT_MAX
};
bool action_needs_element(action_id act);
std::string action_name(action_id act);
struct binding
{
action_id act;
std::string target;
int a, b;
binding(action_id ACT = ACT_NULL, std::string T = "", int A = 0, int B = 0):
act (ACT), target (T), a (A), b (B) { };
std::string save_data();
void load_data(std::istream &datastream);
};
class interface
{
public:
interface(std::string N = "", int X = 80, int Y = 24);
~interface();
void add_element(element_type type, std::string name, int posx, int posy,
int sizex, int sizey, bool selectable = true);
bool erase_element(std::string name);
void draw(Window *win);
void draw_prototype(Window *win); // For the editor
std::string save_data();
void load_data(std::istream &datastream);
bool save_to_file(std::string filename);
bool load_from_file(std::string filename, bool warn = true);
std::vector<std::string> element_names();
element* selected();
element* find_by_name(std::string name);
element* select_next(bool force = false);
element* select_last(bool force = false);
element* select(std::string name);
void select_none();
bool move_element_up (std::string name);
bool move_element_down(std::string name);
bool set_selectable(std::string name, bool setting);
// set_data replaces the element's data with whatever is passed
// add_data appends whatever is passed to the element's data
// These are all defined for each element type; if they're invalid, the type
// just returns false.
bool set_data(std::string name, std::string data);
bool add_data(std::string name, std::string data);
bool set_data(std::string name, std::vector<std::string> data);
bool add_data(std::string name, std::vector<std::string> data);
bool set_data(std::string name, int data);
bool add_data(std::string name, int data);
bool set_data(std::string name, glyph gl, int posx, int posy);
bool set_data(std::string name, nc_color fg, nc_color bg = c_null);
/* self_reference makes an element take control of its own data (meaning it will
* delete the data when it is deleted).
* ref_data makes an element reference some external set of data. Note that if
* a function is not appropriate, it will not change the reference and will
* return false (e.g. telling a number element to reference a vector of strings)
*/
bool self_reference(std::string name);
bool ref_data(std::string name, std::string *data);
bool ref_data(std::string name, std::vector<std::string> *data);
bool ref_data(std::string name, int *data);
bool clear_data(std::string name);
// These will return empty data if inappropriate to the element.
std::string get_str(std::string name);
int get_int(std::string name);
std::vector<std::string> get_str_list(std::string name);
std::vector<std::string> binding_list();
bool add_binding(long ch, action_id act, std::string target = "");
bool add_binding(long ch, action_id act, std::string target,
int a, int b = 0);
binding* bound_to(long ch);
bool has_bindings_for(action_id act);
bool has_bindings_for(std::string target);
bool rem_binding(long ch);
bool rem_all_bindings(action_id act = ACT_NULL);
bool rem_all_bindings(std::string target);
bool set_use_bindings(bool set = true);
bool handle_action (long ch); // Only does keybindings
bool handle_keypress(long ch); // May redirect to current object
// e.g. 0-9 will be used as input for number
std::string name;
int sizex, sizey;
private:
int active_element;
std::vector<element*> elements;
std::map<long, binding> bindings;
bool use_bindings;
};
}; // namespace cuss
#endif