Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable mouse input for uilist #41272

Closed
wants to merge 13 commits into from
7 changes: 4 additions & 3 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1424,11 +1424,12 @@ inventory_entry *inventory_selector::find_entry_by_coordinate( point coordinate
return entry.is_selectable();
};
for( inventory_column *column : columns ) {
if( column->visible() == false ) {
martinrhan marked this conversation as resolved.
Show resolved Hide resolved
continue;
}
std::vector<inventory_entry *> entries = column->get_entries( filter_to_selected );
for( inventory_entry *entry : entries ) {
if( entry->drawn_info.text_x_start <= coordinate.x &&
coordinate.x <= entry->drawn_info.text_x_end &&
entry->drawn_info.y == coordinate.y ) {
if( entry->drawn_info.include_point( coordinate ) ) {
return entry;
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ struct inventory_entry_drawn_info {
int text_x_start;
int text_x_end;
int y;
bool include_point( point p )const {
if( text_x_start <= p.x &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you please just put this all on one line? General CDDA practice is to put multiple conditions on a single line if the total line length is less than 100 characters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hah? but I saw many cases of mutiple lined if conditions

p.x <= text_x_end &&
y == p.y ) {
return true;
}
return false;
}
};

class inventory_entry
Expand Down
29 changes: 28 additions & 1 deletion src/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,12 @@ void uilist::show()
// to be used.
const auto entry = utf8_wrapper( ei == selected ? remove_color_tags( entries[ ei ].txt ) :
entries[ ei ].txt );
trim_and_print( window, point( pad_left + 4, estart + si ),
int x = pad_left + 4;
int y = estart + si;
entries[ei].drawn_info.text_x_start = x;
entries[ei].drawn_info.text_x_end = x + max_entry_len;
entries[ei].drawn_info.y = y;
trim_and_print( window, point( x, y ),
martinrhan marked this conversation as resolved.
Show resolved Hide resolved
max_entry_len, co, "%s", entry.c_str() );

if( max_column_len && !entries[ ei ].ctxt.empty() ) {
Expand Down Expand Up @@ -790,6 +795,7 @@ void uilist::query( bool loop, int timeout )
if( allow_cancel ) {
ctxt.register_action( "QUIT" );
}
ctxt.register_action( "SELECT" );
ctxt.register_action( "CONFIRM" );
ctxt.register_action( "FILTER" );
ctxt.register_action( "ANY_INPUT" );
Expand Down Expand Up @@ -831,6 +837,16 @@ void uilist::query( bool loop, int timeout )
if( callback != nullptr ) {
callback->select( this );
}
} else if( !fentries.empty() && ret_act == "SELECT" ) {
std::pair<point, bool> p = ctxt.get_coordinates_text( window );
if( p.second ) {
uilist_entry *entry = find_entry_by_coordinate( p.first );
if( entry != nullptr ) {
if( entry->enabled ) {
ret = entry->retval;
}
}
}
} else if( !fentries.empty() && ret_act == "CONFIRM" ) {
if( entries[ selected ].enabled ) {
ret = entries[ selected ].retval; // valid
Expand Down Expand Up @@ -861,6 +877,17 @@ void uilist::query( bool loop, int timeout )
} while( loop && ret == UILIST_WAIT_INPUT );
}

uilist_entry *uilist::find_entry_by_coordinate( point p )
{
for( int i : fentries ) {
uilist_entry &entry = entries[i];
if( entry.drawn_info.include_point( p ) ) {
return &entry;
}
}
return nullptr;
}

///@}
/**
* cleanup
Expand Down
20 changes: 20 additions & 0 deletions src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "pimpl.h"
#include "point.h"
#include "string_formatter.h"
#include "input.h"

class translation;

Expand Down Expand Up @@ -49,6 +50,20 @@ struct mvwzstr {
int sym = 0;
};

struct uilist_entry_drawn_info {
int text_x_start;
int text_x_end;
int y;
bool include_point( point p )const {
martinrhan marked this conversation as resolved.
Show resolved Hide resolved
if( text_x_start <= p.x &&
p.x <= text_x_end &&
y == p.y ) {
return true;
}
return false;
}
};

/**
* uilist_entry: entry line for uilist
*/
Expand Down Expand Up @@ -96,6 +111,8 @@ struct uilist_entry {
uilist_entry( Enum e, Args && ... args ) :
uilist_entry( static_cast<int>( e ), std::forward<Args>( args )... )
{}

uilist_entry_drawn_info drawn_info;
};

/**
Expand Down Expand Up @@ -338,6 +355,8 @@ class uilist // NOLINT(cata-xy)

bool started = false;

uilist_entry *find_entry_by_coordinate( point p );

public:
// Results
// TODO change to getters
Expand All @@ -346,6 +365,7 @@ class uilist // NOLINT(cata-xy)
int keypress;

int selected;

};

/**
Expand Down