Skip to content

Commit

Permalink
vehicle part descriptions: handle overflow and scrolling
Browse files Browse the repository at this point in the history
change vpart_info::format_description to count the number of lines
printed and return that value.

change vehicle:print_vparts_desc to count the number of lines it is
printing versus the window size, and to add scroll messages if the
there are more lines of description than available space. Add an offset,
so the descriptions don't always start printing at the frame part.

adjust veh_interact::do_main_loop to add the keybinds for scrolling
through the descriptions.

incidentally, in veh_interact::move_cursor, correctly pass the size of
w_msg so descriptions get line wrapped to the width of the window and not
the height.
  • Loading branch information
mlangsdorf committed Jul 24, 2018
1 parent deb69b7 commit 96fff3d
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 deletions.
18 changes: 18 additions & 0 deletions data/raw/keybindings.json
Original file line number Diff line number Diff line change
Expand Up @@ -1897,6 +1897,24 @@
{ "input_method":"keyboard", "key":"TAB" }
]
},
{
"type" : "keybinding",
"id": "DESC_LIST_UP",
"category": "VEH_INTERACT",
"name": "Scroll up through vehicle part descriptions",
"bindings":[
{ "input_method":"keyboard", "key":"<" }
]
},
{
"type" : "keybinding",
"id": "DESC_LIST_DOWN",
"category": "VEH_INTERACT",
"name": "Scroll down through vehicle part descriptions",
"bindings":[
{ "input_method":"keyboard", "key":">" }
]
},
{
"type" : "keybinding",
"id": "TOGGLE_UNAVAILABLE_CONSTRUCTIONS",
Expand Down
16 changes: 14 additions & 2 deletions src/veh_interact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ veh_interact::veh_interact( vehicle &veh, int x, int y )
main_context.register_action("RELABEL");
main_context.register_action("FUEL_LIST_DOWN");
main_context.register_action("FUEL_LIST_UP");
main_context.register_action("DESC_LIST_DOWN");
main_context.register_action("DESC_LIST_UP");
main_context.register_action("CONFIRM");
main_context.register_action("HELP_KEYBINDINGS");
main_context.register_action("FILTER");
Expand Down Expand Up @@ -318,6 +320,10 @@ void veh_interact::do_main_loop()
} else if ( action == "FUEL_LIST_UP" ) {
move_fuel_cursor( -1 );
move_cursor( 0, 0 );
} else if ( action == "DESC_LIST_DOWN" ) {
move_cursor( 0, 0, 1 );
} else if ( action == "DESC_LIST_UP" ) {
move_cursor( 0, 0, -1 );
}
if ( sel_cmd != ' ' ) {
finish = true;
Expand Down Expand Up @@ -1566,14 +1572,20 @@ bool veh_interact::can_potentially_install(const vpart_info &vpart)
* Moves the cursor on the vehicle editing window.
* @param dx How far to move the cursor on the x-axis.
* @param dy How far to move the cursor on the y-axis.
* @param dstart_at How far to change the start position for vehicle part descriptions
*/
void veh_interact::move_cursor( int dx, int dy )
void veh_interact::move_cursor( int dx, int dy, int dstart_at )
{
const int hw = getmaxx( w_disp ) / 2;
const int hh = getmaxy( w_disp ) / 2;

ddx += dy;
ddy -= dx;
if( dx || dy ) {
start_at = 0;
} else {
start_at += dstart_at;
}

display_veh();
// Update the current active component index to the new position.
Expand All @@ -1598,7 +1610,7 @@ void veh_interact::move_cursor( int dx, int dy )
wrefresh( w_parts );

werase( w_msg );
veh->print_vparts_descs( w_msg, getmaxy( w_msg ), cpart );
veh->print_vparts_descs( w_msg, getmaxy( w_msg ), getmaxx( w_msg ), cpart, start_at );
wrefresh( w_msg );

can_mount.clear();
Expand Down
3 changes: 2 additions & 1 deletion src/veh_interact.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class veh_interact

int ddx = 0;
int ddy = 0;
int start_at = 0;
const vpart_info *sel_vpart_info = nullptr;
char sel_cmd = ' '; //Command currently being run by the player

Expand Down Expand Up @@ -92,7 +93,7 @@ class veh_interact
const std::map<skill_id, int> &skills, int moves ) const;

int part_at( int dx, int dy );
void move_cursor( int dx, int dy );
void move_cursor( int dx, int dy, int dstart_at = 0 );
task_reason cant_do( char mode );
bool can_potentially_install( const vpart_info &vpart );
/** Move index (parameter pos) according to input action:
Expand Down
8 changes: 6 additions & 2 deletions src/veh_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,10 @@ std::string vpart_info::name() const
return name_;
}

void vpart_info::format_description( std::ostringstream &msg, std::string format_color,
int width ) const
int vpart_info::format_description( std::ostringstream &msg, std::string format_color,
int width ) const
{
int lines = 0;
if( ! description.empty() ) {
msg << _( "<color_white>Description</color>\n" );
msg << "> " << format_color;
Expand All @@ -527,6 +528,7 @@ void vpart_info::format_description( std::ostringstream &msg, std::string format
msg << "\n " << wrap_descrip[i];
}
msg << "</color>\n";
lines += 1 + wrap_descrip.size();

// borrowed from item.cpp and adjusted
const quality_id quality_jack( "JACK" );
Expand All @@ -539,8 +541,10 @@ void vpart_info::format_description( std::ostringstream &msg, std::string format
( int )convert_weight( qual.second * TOOL_LIFT_FACTOR ), weight_units() );
}
msg << ".</color>\n";
lines += 1;
}
}
return lines;
}

requirement_data vpart_info::install_requirements() const
Expand Down
3 changes: 2 additions & 1 deletion src/veh_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ class vpart_info
bool legacy = true;

/** Format the description for display */
void format_description( std::ostringstream &msg, std::string format_color, int width ) const;
int format_description( std::ostringstream &msg, std::string format_color, int width ) const;


/** Installation requirements for this component */
requirement_data install_requirements() const;
Expand Down
26 changes: 20 additions & 6 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2889,12 +2889,12 @@ int vehicle::print_part_list( const catacurses::window &win, int y1, const int m
* Prints a list of descriptions for all parts to the screen inside of a boxed window
* highlighting a selected one.
* @param win The window to draw in.
* @param y1 The y-coordinate to start drawing at.
* @param max_y Draw no further than this y-coordinate.
* @param width The width of the window.
* @param &p The index of the part being examined.
* @param start_at Which vehicle part to start printing at.
*/
void vehicle::print_vparts_descs( const catacurses::window &win, int width, int &p ) const
void vehicle::print_vparts_descs( const catacurses::window &win, int max_y, int width, int &p, int start_at ) const
{
if( p < 0 || p >= ( int )parts.size() ) {
return;
Expand All @@ -2903,15 +2903,29 @@ void vehicle::print_vparts_descs( const catacurses::window &win, int width, int
std::vector<int> pl = this->parts_at_relative( parts[p].mount.x, parts[p].mount.y );
std::ostringstream msg;

for( size_t i = 0; i < pl.size(); i++ ) {
int lines = 0;
start_at = std::max( start_at, 0 );
if( start_at ) {
msg << "<color_yellow>" << "< " << _( "More parts here..." ) << "</color>\n";
lines += 1;
}
for( size_t i = start_at; i < pl.size(); i++ ) {
const vehicle_part &vp = parts[ pl [ i ] ];
std::ostringstream possible_msg;
std::string name_color = string_format( "<color_%1$s>",
string_from_color( vp.is_broken() ? c_dark_gray : c_light_green ) );
msg << name_color << vp.name() << "</color>\n";
possible_msg << name_color << vp.name() << "</color>\n";
std::string desc_color = string_format( "<color_%1$s>",
string_from_color( vp.is_broken() ? c_dark_gray : c_light_gray ) );
vp.info().format_description( msg, desc_color, width - 2 );
msg << "</color>\n";
int new_lines = 2 + vp.info().format_description( possible_msg, desc_color, width - 2 );
possible_msg << "</color>\n";
if( lines + new_lines <= max_y ) {
msg << possible_msg.str();
lines += new_lines;
} else {
msg << "<color_yellow>" << _( "More parts here..." ) << " >" << "</color>\n";
break;
}
}
werase( win );
fold_and_print( win, 0, 1, width, c_light_gray, msg.str() );
Expand Down
3 changes: 2 additions & 1 deletion src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ class vehicle
int hl = -1 ) const;

// Vehicle parts descriptions - descriptions for all the parts on a single tile
void print_vparts_descs( const catacurses::window &win, int width, int &p ) const;
void print_vparts_descs( const catacurses::window &win, int max_y, int width, int &p,
int start_at ) const;

/**
* Operate vehicle controls
Expand Down

0 comments on commit 96fff3d

Please sign in to comment.