1
1
#include " path_manager.h"
2
2
3
+ #include < algorithm>
3
4
#include < cstddef>
4
5
#include < iterator>
5
6
#include < memory>
13
14
#include " cata_path.h"
14
15
#include " cata_utility.h"
15
16
#include " catacharset.h"
17
+ #include " color.h"
16
18
#include " coordinates.h"
17
19
#include " coords_fwd.h"
18
20
#include " debug.h"
29
31
#include " messages.h"
30
32
#include " output.h"
31
33
#include " path_info.h"
34
+ #include " string_input_popup.h"
32
35
#include " translations.h"
33
36
#include " ui_manager.h"
34
37
@@ -55,10 +58,16 @@ class path
55
58
* Avatar must be at one of the ends of the path.
56
59
*/
57
60
void set_avatar_path ();
61
+ void set_name_start ();
62
+ void set_name_end ();
63
+
64
+ void serialize ( JsonOut &jsout );
65
+ void deserialize ( const JsonObject &jsin );
58
66
private:
67
+ std::string set_name_popup ( std::string old_name, const std::string &label );
59
68
std::vector<tripoint_abs_ms> recorded_path;
60
- // std::string name_start;
61
- // std::string name_end;
69
+ std::string name_start;
70
+ std::string name_end;
62
71
// / There are no 2 same tiles on the path
63
72
// bool optimize_loops;
64
73
// / A Character walking this path could never go from i-th tile to i+k-th tile, where k > 1
@@ -97,7 +106,6 @@ class path_manager_impl
97
106
void move_selected_down ();
98
107
private:
99
108
bool recording_path ();
100
- // todo int or size_t?
101
109
/* *
102
110
* Set recording_path_index to p_index.
103
111
* -1 means not recording.
@@ -163,21 +171,62 @@ void path::set_avatar_path()
163
171
player_character.set_destination ( route );
164
172
}
165
173
174
+ void path::set_name_start ()
175
+ {
176
+ name_start = set_name_popup ( name_start, _ ( " Name path start:" ) );
177
+ }
178
+
179
+ void path::set_name_end ()
180
+ {
181
+ name_end = set_name_popup ( name_end, _ ( " Name path end:" ) );
182
+ }
183
+
184
+ std::string path::set_name_popup ( std::string old_name, const std::string &label )
185
+ {
186
+ std::string new_name = old_name;
187
+ string_input_popup popup;
188
+ popup
189
+ .title ( label )
190
+ .width ( 85 )
191
+ .edit ( new_name );
192
+ if ( popup.confirmed () ) {
193
+ return new_name;
194
+ }
195
+ return old_name;
196
+ }
197
+
198
+ void path::serialize ( JsonOut &jsout )
199
+ {
200
+ jsout.start_object ();
201
+ jsout.member ( " recorded_path" , recorded_path );
202
+ jsout.member ( " name_start" , name_start );
203
+ jsout.member ( " name_end" , name_end );
204
+ jsout.end_object ();
205
+ }
206
+
207
+ void path::deserialize ( const JsonObject &jsin )
208
+ {
209
+ jsin.read ( " recorded_path" , recorded_path );
210
+ jsin.read ( " name_start" , name_start );
211
+ jsin.read ( " name_end" , name_end );
212
+ }
213
+
166
214
void path_manager_impl::start_recording ()
167
215
{
168
- paths.emplace_back ();
216
+ path &p = paths.emplace_back ();
169
217
set_recording_path ( paths.size () - 1 );
170
- paths.back ().record_step ( get_avatar ().get_location () );
218
+ p.record_step ( get_avatar ().get_location () );
219
+ p.set_name_start ();
171
220
}
172
221
173
222
void path_manager_impl::stop_recording ()
174
223
{
175
- const path ¤t_path = paths[recording_path_index];
176
- const std::vector<tripoint_abs_ms> &curr_path = current_path.recorded_path ;
177
- if ( curr_path.size () <= 1 ) {
224
+ path ¤t_path = paths[recording_path_index];
225
+ if ( current_path.recorded_path .size () <= 1 ) {
178
226
paths.erase ( paths.begin () + recording_path_index );
179
227
popup ( _ ( " Recorded path has no lenght. Path erased." ) );
180
228
} else {
229
+ current_path.set_name_end ();
181
230
popup ( _ ( " Path saved." ) );
182
231
}
183
232
@@ -283,6 +332,10 @@ void path_manager_ui::draw_controls()
283
332
enabled_active_button ( " MOVE_PATH_DOWN" , pimpl->selected_id != -1 &&
284
333
pimpl->selected_id + 1 < static_cast <int >( pimpl->paths .size () ) );
285
334
335
+ enabled_active_button ( " RENAME_START" , pimpl->selected_id != -1 );
336
+ ImGui::SameLine ();
337
+ enabled_active_button ( " RENAME_END" , pimpl->selected_id != -1 );
338
+
286
339
ImGui::BeginChild ( " table" );
287
340
if ( ! ImGui::BeginTable ( " PATH_MANAGER" , 5 , ImGuiTableFlags_Resizable ) ) {
288
341
return ;
@@ -307,15 +360,15 @@ void path_manager_ui::draw_controls()
307
360
pimpl->selected_id = i;
308
361
}
309
362
ImGui::SameLine ();
310
- draw_colored_text ( " start " , c_white );
363
+ draw_colored_text ( curr_path. name_start , c_white );
311
364
312
365
ImGui::TableNextColumn ();
313
366
std::string dist = direction_suffix ( get_avatar ().get_location (), curr_path.recorded_path .front () );
314
367
dist = dist == " " ? _ ( " It's under your feet." ) : dist;
315
368
draw_colored_text ( dist, c_white );
316
369
317
370
ImGui::TableNextColumn ();
318
- draw_colored_text ( " end " , c_white );
371
+ draw_colored_text ( curr_path. name_end , c_white );
319
372
320
373
ImGui::TableNextColumn ();
321
374
dist = direction_suffix ( get_avatar ().get_location (), curr_path.recorded_path .back () );
@@ -344,6 +397,8 @@ void path_manager_ui::run()
344
397
ctxt.register_action ( " DELETE_PATH" );
345
398
ctxt.register_action ( " MOVE_PATH_UP" );
346
399
ctxt.register_action ( " MOVE_PATH_DOWN" );
400
+ ctxt.register_action ( " RENAME_START" );
401
+ ctxt.register_action ( " RENAME_END" );
347
402
std::string action;
348
403
349
404
ui_manager::redraw ();
@@ -373,6 +428,10 @@ void path_manager_ui::run()
373
428
pimpl->selected_id + 1 < static_cast <int >( pimpl->paths .size () )
374
429
) {
375
430
pimpl->move_selected_down ();
431
+ } else if ( action == " RENAME_START" && pimpl->selected_id != -1 ) {
432
+ pimpl->paths [pimpl->selected_id ].set_name_start ();
433
+ } else if ( action == " RENAME_END" && pimpl->selected_id != -1 ) {
434
+ pimpl->paths [pimpl->selected_id ].set_name_end ();
376
435
} else if ( action == " QUIT" ) {
377
436
break ;
378
437
}
@@ -424,29 +483,27 @@ void path_manager::serialize( std::ostream &fout )
424
483
425
484
void path_manager::deserialize ( const JsonValue &jsin )
426
485
{
427
- try {
428
- JsonObject data = jsin.get_object ();
429
-
430
- data.read ( " recording_path_index" , pimpl->recording_path_index );
431
- // from `path` load only recorded_path
432
- std::vector<std::vector<tripoint_abs_ms>> recorded_paths;
433
- data.read ( " recorded_paths" , recorded_paths );
434
- for ( std::vector<tripoint_abs_ms> &p : recorded_paths ) {
435
- pimpl->paths .emplace_back ( std::move ( p ) );
436
- }
437
- } catch ( const JsonError &e ) {
486
+ JsonObject data = jsin.get_object ();
487
+ data.read ( " recording_path_index" , pimpl->recording_path_index );
488
+
489
+ JsonArray data_paths = data.get_array ( " paths" );
490
+ for ( int i = 0 ; data_paths.has_object ( i ); ++i ) {
491
+ JsonObject obj = data_paths.next_object ();
492
+ path p;
493
+ p.deserialize ( obj );
494
+ pimpl->paths .emplace_back ( p );
438
495
}
439
496
}
440
497
441
498
void path_manager::serialize ( JsonOut &jsout )
442
499
{
443
500
jsout.member ( " recording_path_index" , pimpl->recording_path_index );
444
- // from `path` save only recorded_path
445
- std::vector<std::vector<tripoint_abs_ms>> recorded_paths ;
446
- for ( const path &p : pimpl->paths ) {
447
- recorded_paths. emplace_back ( p. recorded_path );
501
+ jsout. member ( " paths " );
502
+ jsout. start_array () ;
503
+ for ( path &p : pimpl->paths ) {
504
+ p. serialize ( jsout );
448
505
}
449
- jsout.member ( " recorded_paths " , recorded_paths );
506
+ jsout.end_array ( );
450
507
}
451
508
452
509
void path_manager::show ()
0 commit comments