Skip to content

Commit cb2f3c8

Browse files
committed
Path Manager: Implement buttons and actions
1 parent dff6dc2 commit cb2f3c8

File tree

2 files changed

+148
-17
lines changed

2 files changed

+148
-17
lines changed

data/raw/keybindings.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,6 +3415,42 @@
34153415
"name": "Execute action keybinding",
34163416
"bindings": [ { "input_method": "keyboard_any", "key": "." }, { "input_method": "keyboard_code", "key": "KEYPAD_PERIOD" } ]
34173417
},
3418+
{
3419+
"type": "keybinding",
3420+
"id": "WALK_PATH",
3421+
"category": "PATH_MANAGER",
3422+
"name": "Walk path"
3423+
},
3424+
{
3425+
"type": "keybinding",
3426+
"id": "START_RECORDING",
3427+
"category": "PATH_MANAGER",
3428+
"name": "Start recording"
3429+
},
3430+
{
3431+
"type": "keybinding",
3432+
"id": "STOP_RECORDING",
3433+
"category": "PATH_MANAGER",
3434+
"name": "Stop recording"
3435+
},
3436+
{
3437+
"type": "keybinding",
3438+
"id": "DELETE_PATH",
3439+
"category": "PATH_MANAGER",
3440+
"name": "Delete"
3441+
},
3442+
{
3443+
"type": "keybinding",
3444+
"id": "MOVE_PATH_UP",
3445+
"category": "PATH_MANAGER",
3446+
"name": "Move up"
3447+
},
3448+
{
3449+
"type": "keybinding",
3450+
"id": "MOVE_PATH_DOWN",
3451+
"category": "PATH_MANAGER",
3452+
"name": "Move down"
3453+
},
34183454
{
34193455
"type": "keybinding",
34203456
"id": "ADD_ZONE",

src/path_manager.cpp

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ class path_manager_impl
8383
* Stop recording path.
8484
*/
8585
void stop_recording();
86+
/**
87+
* Delete path selected by `select_id`.
88+
*/
89+
void delete_selected();
90+
/**
91+
* Move path selected by `select_id` up.
92+
*/
93+
void move_selected_up();
94+
/**
95+
* Move path selected by `select_id` down.
96+
*/
97+
void move_selected_down();
8698
private:
8799
bool recording_path();
88100
// todo int or size_t?
@@ -91,6 +103,7 @@ class path_manager_impl
91103
* -1 means not recording.
92104
*/
93105
void set_recording_path( int p_index );
106+
void swap_selected( int index_a, int index_b );
94107
int recording_path_index = -1;
95108
int selected_id = -1;
96109
std::vector<path> paths;
@@ -102,6 +115,8 @@ class path_manager_ui : public cataimgui::window
102115
explicit path_manager_ui( path_manager_impl *pimpl_in );
103116
void run();
104117

118+
input_context ctxt = input_context( "PATH_MANAGER" );
119+
void enabled_active_button( const std::string action, bool enabled );
105120
protected:
106121
void draw_controls() override;
107122
cataimgui::bounds get_bounds() override;
@@ -160,10 +175,10 @@ void path_manager_impl::stop_recording()
160175
const path &current_path = paths[recording_path_index];
161176
const std::vector<tripoint_abs_ms> &curr_path = current_path.recorded_path;
162177
if( curr_path.size() <= 1 ) {
163-
add_msg( m_info, _( "Auto path: Recorded path has no lenght. Path erased." ) );
164178
paths.erase( paths.begin() + recording_path_index );
179+
popup( _( "Recorded path has no lenght. Path erased." ) );
165180
} else {
166-
add_msg( m_info, _( "Auto path: Path saved." ) );
181+
popup( _( "Path saved." ) );
167182
}
168183

169184
set_recording_path( -1 );
@@ -172,6 +187,42 @@ void path_manager_impl::stop_recording()
172187
// more flexibility, but it needs to be documented
173188
}
174189

190+
void path_manager_impl::delete_selected()
191+
{
192+
cata_assert( 0 <= selected_id && selected_id < static_cast<int>( paths.size() ) );
193+
if( selected_id == recording_path_index ) {
194+
set_recording_path( -1 );
195+
} else if( selected_id < recording_path_index ) {
196+
set_recording_path( recording_path_index - 1 );
197+
}
198+
paths.erase( paths.begin() + selected_id );
199+
selected_id = std::min( selected_id, static_cast<int>( paths.size() ) - 1 );
200+
}
201+
202+
void path_manager_impl::swap_selected( int index_a, int index_b )
203+
{
204+
cata_assert( 0 <= index_a && index_a < static_cast<int>( paths.size() ) );
205+
cata_assert( 0 <= index_b && index_b < static_cast<int>( paths.size() ) );
206+
if( index_a == recording_path_index ) {
207+
set_recording_path( index_b );
208+
} else if( index_b == recording_path_index ) {
209+
set_recording_path( index_a );
210+
}
211+
std::swap( paths[index_a], paths[index_b] );
212+
}
213+
214+
void path_manager_impl::move_selected_up()
215+
{
216+
swap_selected( selected_id, selected_id - 1 );
217+
--selected_id;
218+
}
219+
220+
void path_manager_impl::move_selected_down()
221+
{
222+
swap_selected( selected_id, selected_id + 1 );
223+
++selected_id;
224+
}
225+
175226
bool path_manager_impl::recording_path()
176227
{
177228
return recording_path_index != -1;
@@ -189,9 +240,7 @@ void path_manager_impl::auto_route_from_path()
189240
return;
190241
}
191242
}
192-
add_msg( m_info,
193-
_( "Auto path: Player doesn't stand at start or end of existing path. Recording new path." ) );
194-
start_recording();
243+
popup( _( "Player doesn't stand at start or end of existing path." ) );
195244
}
196245

197246
void path_manager_impl::set_recording_path( int p_index )
@@ -208,9 +257,34 @@ cataimgui::bounds path_manager_ui::get_bounds()
208257
return { -1.f, -1.f, float( str_width_to_pixels( TERMX ) ), float( str_height_to_pixels( TERMY ) ) };
209258
}
210259

260+
void path_manager_ui::enabled_active_button( const std::string action, bool enabled )
261+
{
262+
ImGui::BeginDisabled( !enabled );
263+
action_button( action, ctxt.get_button_text( action ) );
264+
ImGui::EndDisabled();
265+
}
266+
211267
void path_manager_ui::draw_controls()
212268
{
213-
if( ! ImGui::BeginTable( "PATH_MANAGER", 5, ImGuiTableFlags_Resizable )) {
269+
// general buttons
270+
enabled_active_button( "WALK_PATH", true );
271+
ImGui::SameLine();
272+
enabled_active_button( "START_RECORDING", !pimpl->recording_path() );
273+
ImGui::SameLine();
274+
enabled_active_button( "STOP_RECORDING", pimpl->recording_path() );
275+
276+
// buttons related to selected path
277+
draw_colored_text( _( "Selected path:" ), c_white );
278+
ImGui::SameLine();
279+
enabled_active_button( "DELETE_PATH", pimpl->selected_id != -1 );
280+
ImGui::SameLine();
281+
enabled_active_button( "MOVE_PATH_UP", pimpl->selected_id > 0 );
282+
ImGui::SameLine();
283+
enabled_active_button( "MOVE_PATH_DOWN", pimpl->selected_id != -1 &&
284+
pimpl->selected_id + 1 < static_cast<int>( pimpl->paths.size() ) );
285+
286+
ImGui::BeginChild( "table" );
287+
if( ! ImGui::BeginTable( "PATH_MANAGER", 5, ImGuiTableFlags_Resizable ) ) {
214288
return;
215289
}
216290
// TODO invlet
@@ -253,24 +327,53 @@ void path_manager_ui::draw_controls()
253327
}
254328
}
255329
ImGui::EndTable();
330+
ImGui::EndChild();
256331
}
257332

258333
void path_manager_ui::run()
259334
{
260-
input_context ctxt( "HELP_KEYBINDINGS" );
261335
ctxt.register_action( "QUIT" );
262336
ctxt.register_action( "SELECT" );
263337
ctxt.register_action( "MOUSE_MOVE" );
264338
ctxt.register_action( "ANY_INPUT" );
265339
ctxt.register_action( "HELP_KEYBINDINGS" );
340+
341+
ctxt.register_action( "WALK_PATH" );
342+
ctxt.register_action( "START_RECORDING" );
343+
ctxt.register_action( "STOP_RECORDING" );
344+
ctxt.register_action( "DELETE_PATH" );
345+
ctxt.register_action( "MOVE_PATH_UP" );
346+
ctxt.register_action( "MOVE_PATH_DOWN" );
266347
std::string action;
267348

268349
ui_manager::redraw();
269350

270351
while( is_open ) {
271352
ui_manager::redraw();
272-
action = ctxt.handle_input( 17 );
273-
if( action == "QUIT" ) {
353+
if( has_button_action() ) {
354+
action = get_button_action();
355+
} else {
356+
action = ctxt.handle_input( 17 );
357+
}
358+
359+
if( action == "WALK_PATH" ) {
360+
pimpl->auto_route_from_path();
361+
break;
362+
} else if( action == "START_RECORDING" && !pimpl->recording_path() ) {
363+
pimpl->start_recording();
364+
break;
365+
} else if( action == "STOP_RECORDING" && pimpl->recording_path() ) {
366+
pimpl->stop_recording();
367+
break;
368+
} else if( action == "DELETE_PATH" && pimpl->selected_id != -1 ) {
369+
pimpl->delete_selected();
370+
} else if( action == "MOVE_PATH_UP" && pimpl->selected_id > 0 ) {
371+
pimpl->move_selected_up();
372+
} else if( action == "MOVE_PATH_DOWN" && pimpl->selected_id != -1 &&
373+
pimpl->selected_id + 1 < static_cast<int>( pimpl->paths.size() )
374+
) {
375+
pimpl->move_selected_down();
376+
} else if( action == "QUIT" ) {
274377
break;
275378
}
276379
}
@@ -349,14 +452,6 @@ void path_manager::serialize( JsonOut &jsout )
349452
void path_manager::show()
350453
{
351454
path_manager_ui( pimpl.get() ).run();
352-
// todo move to GUI
353-
if( pimpl->recording_path() ) {
354-
pimpl->stop_recording();
355-
return;
356-
}
357-
358-
pimpl->auto_route_from_path();
359-
360455
// todo activity title and progress
361456
// player_character.assign_activity( workout_activity_actor( player_character.pos() ) );
362457
}

0 commit comments

Comments
 (0)