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() ) {
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
6 changes: 2 additions & 4 deletions src/inventory_ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "pimpl.h"
#include "units.h"
#include "item_category.h"
#include "ui.h"

class Character;
class item;
Expand All @@ -48,10 +49,7 @@ struct inventory_input;
using drop_location = std::pair<item_location, int>;
using drop_locations = std::list<drop_location>;

struct inventory_entry_drawn_info {
int text_x_start;
int text_x_end;
int y;
struct inventory_entry_drawn_info : entry_drawn_info {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not use entry_drawn_info directly? This inheritance does not add anything to the parent class.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

in future there may be something added

Copy link
Contributor

Choose a reason for hiding this comment

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

You can always create a new class in the future, but now it does nothing and only clutters up the code. And even if something is to be added in the future, I would recommend adding it to the parent class (any maybe guard it with a flag), so other UIs can also use the new functionality when they need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean there are a lot of variant UI things in game, else than inventory and uilist, there are pickup, inventory_item_menu, crafting, and whatever, each of them are having different kind of drawing, its just this two drawn info structs are currently not different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

besides, I am also considering to add some new things, such as button.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean there are a lot of variant UI things in game, else than inventory and uilist, there are pickup, inventory_item_menu, crafting, and whatever, each of them are having different kind of drawing

And I'm pretty sure it's a bad thing we'd like to get rid of and have a unified system to display menu stuff instead.

};

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
23 changes: 23 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,23 @@ struct mvwzstr {
int sym = 0;
};

struct 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 &&
p.x <= text_x_end &&
y == p.y ) {
return true;
}
return false;
}
};

struct uilist_entry_drawn_info : entry_drawn_info {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same for this inheritance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because the way of drawing for those two kinds of UI thing are not exactly the same, currently the drawn info records are not necessary to to be variant yet, but maybe in future one of them is to be changed.

};

/**
* uilist_entry: entry line for uilist
*/
Expand Down Expand Up @@ -96,6 +114,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 +358,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 +368,7 @@ class uilist // NOLINT(cata-xy)
int keypress = 0;

int selected = 0;

};

/**
Expand Down