forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.h
433 lines (365 loc) · 12.8 KB
/
ui.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
#pragma once
#ifndef CATA_SRC_UI_H
#define CATA_SRC_UI_H
#include <initializer_list>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "color.h"
#include "cursesdef.h"
#include "memory_fast.h"
#include "pimpl.h"
#include "point.h"
#include "string_formatter.h"
class translation;
////////////////////////////////////////////////////////////////////////////////////
/**
* uilist constants
*/
const int UILIST_ERROR = -1024;
const int UILIST_WAIT_INPUT = -1025;
const int UILIST_UNBOUND = -1026;
const int UILIST_CANCEL = -1027;
const int UILIST_TIMEOUT = -1028;
const int UILIST_ADDITIONAL = -1029;
const int MENU_AUTOASSIGN = -1;
class input_context;
class string_input_popup;
class ui_adaptor;
struct input_event;
catacurses::window new_centered_win( int nlines, int ncols );
/**
* mvwzstr: line of text with horizontal offset and color
*/
struct mvwzstr {
int left = 0;
nc_color color = c_unset;
std::string txt;
int sym = 0;
};
/**
* uilist_entry: entry line for uilist
*/
struct uilist_entry {
// Return this int
int retval = -1;
// Darken, and forbid scrolling if hilight_disabled is false
bool enabled = true;
// If true, will never darken this option, even when disabled
bool force_color = false;
// Keycode from (int)getch(). Setting to MENU_AUTOASSIGN will automagically pick first free character: 1-9 a-z A-Z
int hotkey = MENU_AUTOASSIGN;
// Entry text
std::string txt;
// Optional, possibly longer, description
std::string desc;
// Second column text
std::string ctxt;
// Hotkey color
nc_color hotkey_color;
// Text color
nc_color text_color = c_red_red;
mvwzstr extratxt;
// Use 'hilite_color' when selected instead of uilist's 'hilight_color'
bool override_hilite_color = false;
nc_color hilite_color;
uilist_entry( std::string T ) : txt( T ) {}
// Verbose constuctors. Try to avoid using these in favor of more readable builder methods.
uilist_entry( std::string T, std::string D ) : txt( T ), desc( D ) {}
uilist_entry( std::string T, int K ) : hotkey( K ), txt( T ) {}
uilist_entry( int R, bool E, int K, std::string T ) : retval( R ), enabled( E ), hotkey( K ),
txt( T ) {}
uilist_entry( int R, bool E, int K, std::string T, std::string D ) : retval( R ), enabled( E ),
hotkey( K ), txt( T ), desc( D ) {}
uilist_entry( int R, bool E, int K, std::string T, std::string D, std::string C ) : retval( R ),
enabled( E ), hotkey( K ), txt( T ), desc( D ), ctxt( C ) {}
uilist_entry( int R, bool E, int K, std::string T, nc_color H, nc_color C ) : retval( R ),
enabled( E ), hotkey( K ), txt( T ), hotkey_color( H ), text_color( C ) {}
uilist_entry &with_retval( int R ) {
retval = R;
return *this;
}
uilist_entry &with_hotkey( int K ) {
hotkey = K;
return *this;
}
uilist_entry &with_enabled( bool E ) {
enabled = E;
return *this;
}
uilist_entry &with_descr( std::string s ) {
desc = std::move( s );
return *this;
}
uilist_entry &with_ctxt( std::string s ) {
ctxt = std::move( s );
return *this;
}
uilist_entry &with_hk_color( nc_color c ) {
hotkey_color = c;
return *this;
}
uilist_entry &with_txt_color( nc_color c ) {
text_color = c;
return *this;
}
};
/**
* Generic multi-function callback for highlighted items, key presses, and window control. Example:
*
* class monmenu_cb: public uilist_callback {
* public:
* bool key(int ch, int num, uilist * menu) {
* if ( ch == 'k' && num > 0 ) {
* std::vector<monster> * game_z=static_cast<std::vector<monster>*>(myptr);
* game_z[num]->dead = true;
* }
* }
* void refresh( uilist *menu ) {
* if( menu->selected >= 0 && static_cast<size_t>( menu->selected ) < game_z.size() ) {
* mvwprintz( menu->window, 0, 0, c_red, "( %s )",game_z[menu->selected]->name() );
* wnoutrefresh( menu->window );
* }
* }
* }
* uilist monmenu;
* for( size_t i = 0; i < z.size(); ++i ) {
* monmenu.addentry( z[i].name );
* }
* monmenu_cb * cb;
* cb->setptr( &g->z );
* monmenu.callback = cb
* monmenu.query();
*
*/
class uilist;
/**
* uilist::query() handles most input events first,
* and then passes the event to the callback if it can't handle it.
*
* The callback returninig a boolean false signifies that the callback can't "handle the
* event completely". This is unchanged before or after the PR.
* @{
*/
class uilist_callback
{
public:
/**
* After a new item is selected, call this once
*/
virtual void select( uilist * ) {}
virtual bool key( const input_context &, const input_event &/*key*/, int /*entnum*/,
uilist * ) {
return false;
}
virtual void refresh( uilist * ) {}
virtual ~uilist_callback() = default;
};
/*@}*/
/**
* uilist: scrolling vertical list menu
*/
class uilist // NOLINT(cata-xy)
{
public:
class size_scalar
{
public:
struct auto_assign {
};
size_scalar &operator=( auto_assign );
size_scalar &operator=( int val );
size_scalar &operator=( const std::function<int()> &fun );
friend class uilist;
private:
std::function<int()> fun;
};
class pos_scalar
{
public:
struct auto_assign {
};
pos_scalar &operator=( auto_assign );
pos_scalar &operator=( int val );
// the parameter to the function is the corresponding size vector element
// (width for x, height for y)
pos_scalar &operator=( const std::function<int( int )> &fun );
friend class uilist;
private:
std::function<int( int )> fun;
};
uilist();
uilist( const std::string &hotkeys_override );
// query() will be called at the end of these convenience constructors
uilist( const std::string &msg, const std::vector<uilist_entry> &opts );
uilist( const std::string &msg, const std::vector<std::string> &opts );
uilist( const std::string &msg, std::initializer_list<const char *const> opts );
~uilist();
// whether to report invalid color tag with debug message.
void color_error( bool report );
void init();
void setup();
// initialize the window or reposition it after screen size change.
void reposition( ui_adaptor &ui );
void show();
bool scrollby( int scrollby );
void query( bool loop = true, int timeout = -1 );
/**
* Repopulate filtered entries list (fentries) and set fselected accordingly.
*/
void filterlist();
/**
* Clear current filter string and repopulate filtered entries.
*/
void clear_filter();
/**
* Override current filter string and repopulate filtered entries.
* Note that uilist has inbuilt "query for filter string and apply it" functionality,
* but this method can be used for creating uilists with some filter pre-applied.
*/
void set_filter( const std::string &fstr );
/**
* Get current filter string.
*/
const std::string &get_filter() const {
return filter;
}
/**
* Get filtered list of entries.
* Elements are indices for 'entries'.
*/
const std::vector<int> &get_filtered() const {
return fentries;
}
/**
* Move selection to given entry.
* @param sel Entry to select (index for 'entries').
* @returns 'false' if entry does not exist / is not available with current filter.
*/
bool set_selected( int sel );
void addentry( const std::string &str );
void addentry( int r, bool e, int k, const std::string &str );
// K is templated so it matches a `char` literal and a `int` value.
// Using a fixed type (either `char` or `int`) will lead to ambiguity with the
// other overload when called with the wrong type.
template<typename K, typename ...Args>
void addentry( const int r, const bool e, K k, const char *const format, Args &&... args ) {
return addentry( r, e, k, string_format( format, std::forward<Args>( args )... ) );
}
void addentry_desc( const std::string &str, const std::string &desc );
void addentry_desc( int r, bool e, int k, const std::string &str, const std::string &desc );
void addentry_col( int r, bool e, int k, const std::string &str, const std::string &column,
const std::string &desc = "" );
void settext( const std::string &str );
void reset();
// Can be called before `uilist::query` to keep the uilist on UI stack after
// `uilist::query` returns. The returned `ui_adaptor` is cleared when the
// `uilist` is deconstructed.
//
// Example:
// shared_ptr_fast<ui_adaptor> ui = menu.create_or_get_ui_adaptor();
// menu.query()
// // before `ui` or `menu` is deconstructed, the menu will always be
// // displayed on screen.
shared_ptr_fast<ui_adaptor> create_or_get_ui_adaptor();
operator int() const;
private:
int scroll_amount_from_action( const std::string &action );
void apply_scrollbar();
// This function assumes it's being called from `query` and should
// not be made public.
void inputfilter();
public:
// Parameters
// TODO change to setters
std::string title;
std::string text;
// basically the same as desc, except it doesn't change based on selection
std::string footer_text;
std::vector<uilist_entry> entries;
std::string input_category;
std::vector<std::pair<std::string, translation>> additional_actions;
nc_color border_color;
nc_color text_color;
nc_color title_color;
nc_color hilight_color;
nc_color hotkey_color;
nc_color disabled_color;
uilist_callback *callback;
pos_scalar w_x_setup;
pos_scalar w_y_setup;
size_scalar w_width_setup;
size_scalar w_height_setup;
int textwidth;
size_scalar pad_left_setup;
size_scalar pad_right_setup;
// Maximum number of lines to be allocated for displaying descriptions.
// This only serves as a hint, not a hard limit, so the number of lines
// may still exceed this value when for example the description text is
// long enough.
int desc_lines_hint;
bool desc_enabled;
bool filtering;
bool filtering_igncase;
// return on selecting disabled entry, default false
bool allow_disabled = false;
// return UILIST_UNBOUND on keys unbound & unhandled by callback, default false
bool allow_anykey = false;
// return UILIST_CANCEL on "QUIT" action, default true
bool allow_cancel = true;
// return UILIST_ADDITIONAL if the input action is inside `additional_actions`
// and unhandled by callback, default false.
bool allow_additional = false;
bool hilight_disabled = false;
private:
std::string hotkeys;
report_color_error _color_error = report_color_error::yes;
public:
// Iternal states
// TODO make private
std::vector<std::string> textformatted;
catacurses::window window;
int w_x;
int w_y;
int w_width;
int w_height;
int pad_left;
int pad_right;
int vshift = 0;
int fselected = 0;
private:
std::vector<int> fentries;
std::map<int, int> keymap;
weak_ptr_fast<ui_adaptor> ui;
std::unique_ptr<string_input_popup> filter_popup;
std::string filter;
int max_entry_len;
int max_column_len;
int vmax = 0;
int desc_lines;
bool started = false;
public:
// Results
// TODO change to getters
std::string ret_act;
int ret;
int keypress;
int selected;
};
/**
* Callback for uilist that pairs menu entries with points
* When an entry is selected, view will be centered on the paired point
*/
class pointmenu_cb : public uilist_callback
{
private:
struct impl_t;
pimpl<impl_t> impl;
public:
pointmenu_cb( const std::vector< tripoint > &pts );
~pointmenu_cb() override;
void select( uilist *menu ) override;
};
#endif // CATA_SRC_UI_H