Skip to content

Commit

Permalink
vehicle part descriptions: properly handle the scroll limit
Browse files Browse the repository at this point in the history
limit scrolling such that you can't scroll if all the descriptions
fit it the window, and you can't scroll down past the point that
the remaining descriptions fit in the window, and you can't scroll
up past the first description.

Method hopefully documented adequately in the function, because I
may need to reuse it in the future.
  • Loading branch information
mlangsdorf committed Jul 26, 2018
1 parent 9202303 commit f1abdff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/veh_interact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ void veh_interact::move_cursor( int dx, int dy, int dstart_at )
ddx += dy;
ddy -= dx;
if( dx || dy ) {
start_at = 0;
start_limit = 0;
} else {
start_at += dstart_at;
}
Expand Down Expand Up @@ -1609,7 +1609,7 @@ void veh_interact::move_cursor( int dx, int dy, int dstart_at )
wrefresh( w_parts );

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

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

int ddx = 0;
int ddy = 0;
/* starting offset for vehicle parts description display and max offset for scrolling */
int start_at = 0;
int start_limit = 0;
const vpart_info *sel_vpart_info = nullptr;
char sel_cmd = ' '; //Command currently being run by the player

Expand Down
25 changes: 21 additions & 4 deletions src/vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2892,9 +2892,10 @@ int vehicle::print_part_list( const catacurses::window &win, int y1, const int m
* @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.
* @param start_limit the part index beyond which the display is full
*/
void vehicle::print_vparts_descs( const catacurses::window &win, int max_y, int width, int &p,
int &start_at ) const
int &start_at, int &start_limit ) const
{
if( p < 0 || p >= ( int )parts.size() ) {
return;
Expand All @@ -2904,9 +2905,23 @@ void vehicle::print_vparts_descs( const catacurses::window &win, int max_y, int
std::ostringstream msg;

int lines = 0;
/* never start before the start of the list */
/* guess the number of entries that can fit in the window, and don't scroll past that */
start_at = std::min( std::max( start_at, 0 ), max_y / 6 );
/*
* start_at and start_limit interaction is little tricky
* start_at and start_limit start at 0 when moving to a new frame
* if all the descriptions are displayed in the window, start_limit stays at 0 and
* start_at is capped at 0 - so no scrolling at all.
* if all the descriptions aren't displayed, start_limit jumps to the last displayed part
* and the next scrollthrough can start there - so scrolling down happens.
* when the scroll reaches the point where all the remaining descriptions are displayed in
* the window, start_limit is set to start_at again.
* on the next attempted scrolldown, start_limit is set to the nth item, and start_at is
* capped to the nth item, so no more scrolling down.
* start_at can always go down, but never below 0, so scrolling up is only possible after
* some scrolling down has occurred.
* important! the calling function needs to track p, start_at, and start_limit, and set
* start_limit to 0 if p changes.
*/
start_at = std::max( 0, std::min( start_at, start_limit ) );
if( start_at ) {
msg << "<color_yellow>" << "< " << _( "More parts here..." ) << "</color>\n";
lines += 1;
Expand All @@ -2924,8 +2939,10 @@ void vehicle::print_vparts_descs( const catacurses::window &win, int max_y, int
if( lines + new_lines <= max_y ) {
msg << possible_msg.str();
lines += new_lines;
start_limit = start_at;
} else {
msg << "<color_yellow>" << _( "More parts here..." ) << " >" << "</color>\n";
start_limit = i;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ class vehicle

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

/**
* Operate vehicle controls
Expand Down

0 comments on commit f1abdff

Please sign in to comment.