forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_note.cpp
382 lines (306 loc) · 12.5 KB
/
auto_note.cpp
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
#include "auto_note.h"
#include <iostream>
#include "game.h"
#include "json.h"
#include "string_formatter.h"
#include "color.h"
#include "cursesdef.h"
#include "input.h"
#include "options.h"
#include "cata_utility.h"
#include "output.h"
#include "debug.h"
#include "filesystem.h"
#include "translations.h"
#include "avatar.h"
#include "map_extras.h"
#include "generic_factory.h"
namespace auto_notes
{
std::string auto_note_settings::build_save_path() const
{
return g->get_player_base_save_path() + ".ano.json";
}
void auto_note_settings::clear()
{
autoNoteEnabled.clear();
}
bool auto_note_settings::save()
{
if( !file_exist( g->get_player_base_save_path() + ".sav" ) ) {
return true;
}
return write_to_file( build_save_path(), [&]( std::ostream & fstr ) {
JsonOut jout{ fstr, true };
jout.start_object();
jout.member( "enabled" );
jout.start_array();
for( const string_id<map_extra> &entry : autoNoteEnabled ) {
jout.write( entry.str() );
}
jout.end_array();
jout.member( "discovered" );
jout.start_array();
for( const string_id<map_extra> &entry : discovered ) {
jout.write( entry.str() );
}
jout.end_array();
jout.end_object();
}, _( "auto notes configuration" ) );
}
void auto_note_settings::load()
{
clear();
const auto parseJson = [&]( JsonIn & jin ) {
jin.start_object();
while( !jin.end_object() ) {
const std::string name = jin.get_member_name();
if( name == "enabled" ) {
jin.start_array();
while( !jin.end_array() ) {
const std::string entry = jin.get_string();
autoNoteEnabled.insert( string_id<map_extra> {entry} );
}
} else if( name == "discovered" ) {
jin.start_array();
while( !jin.end_array() ) {
const std::string entry = jin.get_string();
discovered.insert( string_id<map_extra> {entry} );
}
} else {
jin.skip_value();
}
}
};
if( !read_from_file_optional_json( build_save_path(), parseJson ) ) {
default_initialize();
save();
}
}
void auto_note_settings::default_initialize()
{
clear();
for( auto &extra : MapExtras::mapExtraFactory().get_all() ) {
if( extra.autonote ) {
autoNoteEnabled.insert( extra.id );
}
}
}
void auto_note_settings::set_discovered( const string_id<map_extra> &mapExtId )
{
discovered.insert( mapExtId );
}
bool auto_note_settings::was_discovered( const string_id<map_extra> &mapExtId ) const
{
return discovered.count( mapExtId ) != 0;
}
void auto_note_settings::show_gui()
{
auto_note_manager_gui gui{ };
gui.show();
if( gui.was_changed() ) {
save();
}
}
bool auto_note_settings::has_auto_note_enabled( const string_id<map_extra> &mapExtId ) const
{
return autoNoteEnabled.count( mapExtId ) != 0;
}
void auto_note_settings::set_auto_note_status( const string_id<map_extra> &mapExtId,
const bool enabled )
{
if( enabled ) {
autoNoteEnabled.insert( mapExtId );
} else if( has_auto_note_enabled( mapExtId ) ) {
autoNoteEnabled.erase( mapExtId );
}
}
auto_note_manager_gui::auto_note_manager_gui()
{
const auto_note_settings &settings = get_auto_notes_settings();
for( auto &extra : MapExtras::mapExtraFactory().get_all() ) {
// Ignore all extras that have autonote disabled in the JSON.
// This filters out lots of extras users shouldnt see (like "normal")
if( !extra.autonote ) {
continue;
}
bool isAutoNoteEnabled = settings.has_auto_note_enabled( extra.id );
mapExtraCache.emplace( std::make_pair( extra.id, std::make_pair( extra,
isAutoNoteEnabled ) ) );
if( settings.was_discovered( extra.id ) ) {
displayCache.push_back( extra.id );
}
}
}
bool auto_note_manager_gui::was_changed() const
{
return wasChanged;
}
void auto_note_manager_gui::show()
{
const int iHeaderHeight = 3;
const int iContentHeight = FULL_SCREEN_HEIGHT - 2 - iHeaderHeight;
const int iOffsetX = TERMX > FULL_SCREEN_WIDTH ? ( TERMX - FULL_SCREEN_WIDTH ) / 2 : 0;
const int iOffsetY = TERMY > FULL_SCREEN_HEIGHT ? ( TERMY - FULL_SCREEN_HEIGHT ) / 2 : 0;
catacurses::window w_help = catacurses::newwin( FULL_SCREEN_HEIGHT / 2 + 2,
FULL_SCREEN_WIDTH * 3 / 4,
point( iOffsetX + 19 / 2, 7 + iOffsetY + FULL_SCREEN_HEIGHT / 2 / 2 ) );
catacurses::window w_border = catacurses::newwin( FULL_SCREEN_HEIGHT, FULL_SCREEN_WIDTH,
point( iOffsetX, iOffsetY ) );
catacurses::window w_header = catacurses::newwin( iHeaderHeight, FULL_SCREEN_WIDTH - 2,
point( 1 + iOffsetX, 1 + iOffsetY ) );
catacurses::window w = catacurses::newwin( iContentHeight, FULL_SCREEN_WIDTH - 2,
point( 1 + iOffsetX, iHeaderHeight + 1 + iOffsetY ) );
// ===========================================================================
// Perform initial draw. This includes things like the window border that do
// not need to be refreshed more than once.
// == Draw border
draw_border( w_border, BORDER_COLOR, _( " AUTO NOTES MANAGER " ) );
mvwputch( w_border, point( 0, 2 ), c_light_gray, LINE_XXXO );
mvwputch( w_border, point( 79, 2 ), c_light_gray, LINE_XOXX );
mvwputch( w_border, point( 61, FULL_SCREEN_HEIGHT - 1 ), c_light_gray, LINE_XXOX );
wrefresh( w_border );
// == Draw header
int tmpx = 0;
tmpx += shortcut_print( w_header, point( tmpx, 0 ), c_white, c_light_green, _( "<E>nable" ) ) + 2;
tmpx += shortcut_print( w_header, point( tmpx, 0 ), c_white, c_light_green, _( "<D>isable" ) ) + 2;
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
tmpx += shortcut_print( w_header, point( tmpx, 0 ), c_white, c_light_green,
_( "<Enter> - Toggle" ) ) + 2;
// Draw horizontal line and corner pieces of the table
for( int x = 0; x < 78; x++ ) {
if( x == 60 ) {
mvwputch( w_header, point( x, 1 ), c_light_gray, LINE_OXXX );
mvwputch( w_header, point( x, 2 ), c_light_gray, LINE_XOXO );
} else {
mvwputch( w_header, point( x, 1 ), c_light_gray, LINE_OXOX );
}
}
mvwprintz( w_header, point( 1, 2 ), c_white, _( "Map Extra" ) );
mvwprintz( w_header, point( 62, 2 ), c_white, _( "Enabled" ) );
wrefresh( w_header );
// ===========================================================================
// If the display cache contains no entries, the player might not have discovered any of
// the map extras. In this case, we switch to a special state that alerts the user of this
// in order to avoid confusion a completely empty GUI might normally create.
const bool emptyMode = displayCache.empty();
int currentLine = 0;
int startPosition = 0;
int endPosition = 0;
input_context ctx{ "AUTO_NOTES" };
ctx.register_action( "QUIT" );
ctx.register_action( "SWITCH_AUTO_NOTE_OPTION" );
if( !emptyMode ) {
ctx.register_cardinal();
ctx.register_action( "CONFIRM" );
ctx.register_action( "QUIT" );
ctx.register_action( "ENABLE_MAPEXTRA_NOTE" );
ctx.register_action( "DISABLE_MAPEXTRA_NOTE" );
}
while( true ) {
mvwprintz( w_header, point( 39, 0 ), c_white, _( "Auto notes enabled:" ) );
int currentX = 60;
currentX += shortcut_print( w_header, point( currentX, 0 ),
get_option<bool>( "AUTO_NOTES" ) ? c_light_green : c_light_red, c_white,
get_option<bool>( "AUTO_NOTES" ) ? _( "True" ) : _( "False" ) );
currentX += shortcut_print( w_header, point( currentX, 0 ), c_white, c_light_green, " " );
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
currentX += shortcut_print( w_header, point( currentX, 0 ), c_white, c_light_green,
_( "<S>witch " ) );
// Clear table
for( int y = 0; y < iContentHeight; y++ ) {
for( int x = 0; x < 79; x++ ) {
// The middle beam needs special treatment
if( x == 60 ) {
mvwputch( w, point( x, y ), c_light_gray, LINE_XOXO );
} else {
mvwputch( w, point( x, y ), c_black, ' ' );
}
}
}
const int cacheSize = static_cast<int>( displayCache.size() );
draw_scrollbar( w_border, currentLine, iContentHeight, cacheSize, point( 0, 4 ) );
if( emptyMode ) {
// NOLINTNEXTLINE(cata-use-named-point-constants)
mvwprintz( w, point( 1, 0 ), c_light_gray,
_( "Discover more special encounters to populate this list" ) );
} else {
calcStartPos( startPosition, currentLine, iContentHeight, displayCache.size() );
endPosition = startPosition + ( iContentHeight > cacheSize ? cacheSize : iContentHeight );
for( int i = startPosition; i < endPosition; ++i ) {
const string_id<map_extra> &displayCacheEntry = displayCache[i];
const auto &cacheEntry = mapExtraCache[displayCacheEntry];
const auto lineColor = ( i == currentLine ) ? hilite( c_white ) : c_white;
const auto statusColor = cacheEntry.second ? c_green : c_red;
const auto statusString = cacheEntry.second ? _( "yes" ) : _( "no" );
mvwprintz( w, point( 1, i - startPosition ), lineColor, "" );
if( i == currentLine ) {
wprintz( w, c_yellow, ">> " );
} else {
wprintz( w, c_yellow, " " );
}
wprintz( w, lineColor, "%s", cacheEntry.first.name );
// Since yes is longer than no, we need to clear the space for the status string before
// displaying the current text. Otherwise artifacts might occur.
mvwprintz( w, point( 64, i - startPosition ), statusColor, " " );
mvwprintz( w, point( 64, i - startPosition ), statusColor, "%s", statusString );
}
}
wrefresh( w_header );
wrefresh( w_border );
wrefresh( w );
const std::string currentAction = ctx.handle_input();
// Actions that also work with no items to display
if( currentAction == "SWITCH_AUTO_NOTE_OPTION" ) {
get_options().get_option( "AUTO_NOTES" ).setNext();
if( get_option<bool>( "AUTO_NOTES" ) && !get_option<bool>( "AUTO_NOTES_MAP_EXTRAS" ) ) {
get_options().get_option( "AUTO_NOTES_MAP_EXTRAS" ).setNext();
}
get_options().save();
} else if( currentAction == "QUIT" ) {
break;
}
if( emptyMode ) {
continue;
}
const string_id<map_extra> ¤tItem = displayCache[currentLine];
std::pair<const map_extra, bool> &entry = mapExtraCache[currentItem];
if( currentAction == "UP" ) {
if( currentLine > 0 ) {
--currentLine;
} else {
currentLine = cacheSize - 1;
}
} else if( currentAction == "DOWN" ) {
if( currentLine == cacheSize - 1 ) {
currentLine = 0;
} else {
++currentLine;
}
} else if( currentAction == "ENABLE_MAPEXTRA_NOTE" ) {
entry.second = true;
wasChanged = true;
} else if( currentAction == "DISABLE_MAPEXTRA_NOTE" ) {
entry.second = false;
wasChanged = true;
} else if( currentAction == "CONFIRM" ) {
entry.second = !entry.second;
wasChanged = true;
}
}
if( !was_changed() ) {
return;
}
if( query_yn( _( "Save changes?" ) ) ) {
auto_notes::auto_note_settings &settings = get_auto_notes_settings();
for( const auto &entry : mapExtraCache ) {
settings.set_auto_note_status( entry.second.first.id, entry.second.second );
}
}
}
} // namespace auto_notes
auto_notes::auto_note_settings &get_auto_notes_settings()
{
static auto_notes::auto_note_settings staticSettings;
return staticSettings;
}