Skip to content

Commit 26ca8b1

Browse files
committed
Path Manager: Name start and end
1 parent cb2f3c8 commit 26ca8b1

File tree

2 files changed

+95
-26
lines changed

2 files changed

+95
-26
lines changed

data/raw/keybindings.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3451,6 +3451,18 @@
34513451
"category": "PATH_MANAGER",
34523452
"name": "Move down"
34533453
},
3454+
{
3455+
"type": "keybinding",
3456+
"id": "RENAME_START",
3457+
"category": "PATH_MANAGER",
3458+
"name": "Rename start"
3459+
},
3460+
{
3461+
"type": "keybinding",
3462+
"id": "RENAME_END",
3463+
"category": "PATH_MANAGER",
3464+
"name": "Rename end"
3465+
},
34543466
{
34553467
"type": "keybinding",
34563468
"id": "ADD_ZONE",

src/path_manager.cpp

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "path_manager.h"
22

3+
#include <algorithm>
34
#include <cstddef>
45
#include <iterator>
56
#include <memory>
@@ -13,6 +14,7 @@
1314
#include "cata_path.h"
1415
#include "cata_utility.h"
1516
#include "catacharset.h"
17+
#include "color.h"
1618
#include "coordinates.h"
1719
#include "coords_fwd.h"
1820
#include "debug.h"
@@ -29,6 +31,7 @@
2931
#include "messages.h"
3032
#include "output.h"
3133
#include "path_info.h"
34+
#include "string_input_popup.h"
3235
#include "translations.h"
3336
#include "ui_manager.h"
3437

@@ -55,10 +58,16 @@ class path
5558
* Avatar must be at one of the ends of the path.
5659
*/
5760
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 );
5866
private:
67+
std::string set_name_popup( std::string old_name, const std::string &label );
5968
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;
6271
/// There are no 2 same tiles on the path
6372
// bool optimize_loops;
6473
/// 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
97106
void move_selected_down();
98107
private:
99108
bool recording_path();
100-
// todo int or size_t?
101109
/**
102110
* Set recording_path_index to p_index.
103111
* -1 means not recording.
@@ -163,21 +171,62 @@ void path::set_avatar_path()
163171
player_character.set_destination( route );
164172
}
165173

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+
166214
void path_manager_impl::start_recording()
167215
{
168-
paths.emplace_back();
216+
path &p = paths.emplace_back();
169217
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();
171220
}
172221

173222
void path_manager_impl::stop_recording()
174223
{
175-
const path &current_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 &current_path = paths[recording_path_index];
225+
if( current_path.recorded_path.size() <= 1 ) {
178226
paths.erase( paths.begin() + recording_path_index );
179227
popup( _( "Recorded path has no lenght. Path erased." ) );
180228
} else {
229+
current_path.set_name_end();
181230
popup( _( "Path saved." ) );
182231
}
183232

@@ -283,6 +332,10 @@ void path_manager_ui::draw_controls()
283332
enabled_active_button( "MOVE_PATH_DOWN", pimpl->selected_id != -1 &&
284333
pimpl->selected_id + 1 < static_cast<int>( pimpl->paths.size() ) );
285334

335+
enabled_active_button( "RENAME_START", pimpl->selected_id != -1 );
336+
ImGui::SameLine();
337+
enabled_active_button( "RENAME_END", pimpl->selected_id != -1 );
338+
286339
ImGui::BeginChild( "table" );
287340
if( ! ImGui::BeginTable( "PATH_MANAGER", 5, ImGuiTableFlags_Resizable ) ) {
288341
return;
@@ -307,15 +360,15 @@ void path_manager_ui::draw_controls()
307360
pimpl->selected_id = i;
308361
}
309362
ImGui::SameLine();
310-
draw_colored_text( "start", c_white );
363+
draw_colored_text( curr_path.name_start, c_white );
311364

312365
ImGui::TableNextColumn();
313366
std::string dist = direction_suffix( get_avatar().get_location(), curr_path.recorded_path.front() );
314367
dist = dist == "" ? _( "It's under your feet." ) : dist;
315368
draw_colored_text( dist, c_white );
316369

317370
ImGui::TableNextColumn();
318-
draw_colored_text( "end", c_white );
371+
draw_colored_text( curr_path.name_end, c_white );
319372

320373
ImGui::TableNextColumn();
321374
dist = direction_suffix( get_avatar().get_location(), curr_path.recorded_path.back() );
@@ -344,6 +397,8 @@ void path_manager_ui::run()
344397
ctxt.register_action( "DELETE_PATH" );
345398
ctxt.register_action( "MOVE_PATH_UP" );
346399
ctxt.register_action( "MOVE_PATH_DOWN" );
400+
ctxt.register_action( "RENAME_START" );
401+
ctxt.register_action( "RENAME_END" );
347402
std::string action;
348403

349404
ui_manager::redraw();
@@ -373,6 +428,10 @@ void path_manager_ui::run()
373428
pimpl->selected_id + 1 < static_cast<int>( pimpl->paths.size() )
374429
) {
375430
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();
376435
} else if( action == "QUIT" ) {
377436
break;
378437
}
@@ -424,29 +483,27 @@ void path_manager::serialize( std::ostream &fout )
424483

425484
void path_manager::deserialize( const JsonValue &jsin )
426485
{
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 );
438495
}
439496
}
440497

441498
void path_manager::serialize( JsonOut &jsout )
442499
{
443500
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 );
448505
}
449-
jsout.member( "recorded_paths", recorded_paths );
506+
jsout.end_array();
450507
}
451508

452509
void path_manager::show()

0 commit comments

Comments
 (0)