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 - rework #41605

Merged
merged 58 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from 56 commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
70e6421
edit
martinrhan Jun 13, 2020
c992213
edit
martinrhan Jun 13, 2020
cb1274c
Update inventory_ui.cpp
martinrhan Jun 13, 2020
6ccdd8a
Update inventory_ui.cpp
martinrhan Jun 13, 2020
b912bd4
Update inventory_ui.cpp
martinrhan Jun 13, 2020
8078efb
Update ui.cpp
martinrhan Jun 13, 2020
100e60b
Update inventory_ui.cpp
martinrhan Jun 13, 2020
3206b68
Update ui.cpp
martinrhan Jun 14, 2020
3faa3eb
edit
martinrhan Jun 14, 2020
3815ec1
Update src/ui.h
martinrhan Jun 19, 2020
04e8f80
edit
martinrhan Jun 19, 2020
7dfb153
Merge branch 'branch_mouse_uilist' into branch_mouse_pickup
martinrhan Jun 19, 2020
19a9fa5
Update pickup.h
martinrhan Jun 19, 2020
ac5e177
Update pickup.h
martinrhan Jun 19, 2020
e70471a
Update inventory_ui.h
martinrhan Jun 19, 2020
204e1c2
edit
martinrhan Jun 26, 2020
9315ac9
Merge branch 'master' into branch_mouse_pickup
martinrhan Jun 26, 2020
40cf710
Update src/inventory_ui.cpp
martinrhan Jun 27, 2020
046f864
Update src/pickup.cpp
martinrhan Jun 27, 2020
4e95d88
Update src/pickup.h
martinrhan Jun 27, 2020
a183e50
Update src/input.h
martinrhan Jun 27, 2020
b1b76d9
seperate checking is in window from get coordinate method
martinrhan Jun 27, 2020
4b0614c
added some const
martinrhan Jun 27, 2020
e77ac21
add missing brackets
martinrhan Jun 27, 2020
1f15635
fix comment
martinrhan Jun 27, 2020
42d762b
fix window_contains_point
martinrhan Jun 27, 2020
88b9259
Update sdltiles.h
martinrhan Jun 27, 2020
697a53e
fix drawn_rect max x issue
martinrhan Jun 27, 2020
dab79ec
Update sdltiles.cpp
martinrhan Jun 27, 2020
30ab7e9
Update sdltiles.cpp
martinrhan Jun 27, 2020
3b30934
Update input.cpp
martinrhan Jun 27, 2020
a23f04e
fix get window_includes_point
martinrhan Jun 27, 2020
11c9726
Update inventory_ui.cpp
martinrhan Jun 27, 2020
b358fec
Apply suggestions from code review
ZhilkinSerg Jul 6, 2020
7279394
Update src/inventory_ui.h
martinrhan Jul 8, 2020
b2a898e
Update sdltiles.h
martinrhan Jul 11, 2020
7e8ffb2
move some methods out of if defined(TILES)
martinrhan Jul 11, 2020
02b3c31
Update sdltiles.cpp
martinrhan Jul 11, 2020
04ff66a
Update sdltiles.h
martinrhan Jul 11, 2020
5dee434
Update sdltiles.cpp
martinrhan Jul 11, 2020
28da032
Update sdltiles.cpp
martinrhan Jul 11, 2020
9d36050
Apply suggestions from code review
ZhilkinSerg Jul 11, 2020
87e9249
Update sdltiles.h
ZhilkinSerg Jul 11, 2020
8128993
Update sdltiles.cpp
ZhilkinSerg Jul 11, 2020
b5cd32e
redo drawn rect record
martinrhan Jul 17, 2020
cb5075c
Merge branch 'master' into branch_mouse_pickup
ZhilkinSerg Jul 28, 2020
18c6940
Update ui.h
ZhilkinSerg Jul 28, 2020
d6e527d
Apply suggestions from code review
ZhilkinSerg Jul 28, 2020
4462ffd
Update input.cpp
ZhilkinSerg Jul 28, 2020
cfebf17
Update inventory_ui.cpp
ZhilkinSerg Jul 28, 2020
60ae6d1
Merge branch 'master' into branch_mouse_pickup
ZhilkinSerg Jul 28, 2020
7c79818
Apply suggestions from code review
ZhilkinSerg Jul 28, 2020
3bfb5a6
Update sdltiles.cpp
ZhilkinSerg Jul 28, 2020
a7723cf
Update pickup.cpp
ZhilkinSerg Jul 28, 2020
583f49f
Apply suggestions from code review
ZhilkinSerg Jul 28, 2020
14bbe4d
Update sdltiles.cpp
ZhilkinSerg Jul 28, 2020
f352e8f
Apply suggestions from code review
ZhilkinSerg Jul 28, 2020
33d75f8
Apply suggestions from code review
ZhilkinSerg Jul 28, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1551,36 +1551,24 @@ cata::optional<tripoint> input_context::get_coordinates( const catacurses::windo
}
#endif

std::pair<point, bool> input_context::get_coordinates_text( const catacurses::window
cata::optional<point> input_context::get_coordinates_text( const catacurses::window
&capture_win ) const
{
#if !defined( TILES )
( void ) capture_win;
return std::make_pair( point(), false );
return cata::nullopt;
#else
if( !coordinate_input_received ) {
return std::make_pair( point(), false );
return cata::nullopt;
}

const window_dimensions dim = get_window_dimensions( capture_win );

const int &fw = dim.scaled_font_size.x;
const int &fh = dim.scaled_font_size.y;
const point &win_min = dim.window_pos_pixel;
const point &win_size = dim.window_size_pixel;
const point win_max = win_min + win_size;

const half_open_rectangle<point> win_bounds( win_min, win_max );

const point screen_pos = coordinate - win_min;
const point selected( divide_round_down( screen_pos.x, fw ),
divide_round_down( screen_pos.y, fh ) );

if( !win_bounds.contains( coordinate ) ) {
return std::make_pair( selected, false );
}

return std::make_pair( selected, true );
return selected;
#endif
}
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
5 changes: 4 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,10 @@ class input_context
*/
input_event get_raw_input();

std::pair<point, bool> get_coordinates_text( const catacurses::window &capture_win ) const;
/**
* Get coordinate of text level from mouse input, difference between this and get_coordinates is that one is getting pixel level coordinate.
*/
cata::optional<point> get_coordinates_text( const catacurses::window &capture_win ) const;

/**
* Get the human-readable name for an action.
Expand Down
48 changes: 22 additions & 26 deletions src/inventory_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "vehicle_selector.h"
#include "visitable.h"
#include "vpart_position.h"
#include "sdltiles.h"

#if defined(__ANDROID__)
#include <SDL_keyboard.h>
Expand Down Expand Up @@ -919,12 +920,12 @@ static int num_parents( const item_location &loc )
return 2 + num_parents( loc.parent_item() );
}

void inventory_column::draw( const catacurses::window &win, const point &p )
void inventory_column::draw( const catacurses::window &win, const point &p,
std::vector<std::pair<inclusive_rectangle<point>, inventory_entry *>> &rect_entry_map )
{
if( !visible() ) {
return;
}

const auto available_cell_width = [ this ]( size_t index, size_t cell_index ) {
const size_t displayed_width = cells[cell_index].current_width;
const size_t real_width = get_entry_cell_width( index, cell_index );
Expand All @@ -933,6 +934,7 @@ void inventory_column::draw( const catacurses::window &win, const point &p )
};

// Do the actual drawing
rect_entry_map.clear();
for( size_t index = page_offset, line = 0; index < entries.size() &&
line < entries_per_page; ++index, ++line ) {
inventory_entry &entry = entries[index];
Expand All @@ -954,10 +956,11 @@ void inventory_column::draw( const catacurses::window &win, const point &p )

const bool selected = active && is_selected( entry );

entry.drawn_info.text_x_start = x1;
martinrhan marked this conversation as resolved.
Show resolved Hide resolved
const int hx_max = p.x + get_width() + contained_offset;
entry.drawn_info.text_x_end = hx_max;
entry.drawn_info.y = yy;
inclusive_rectangle<point> rect = inclusive_rectangle<point>( point( x1, yy ),
point( hx_max - 1, yy ) );
rect_entry_map.push_back( std::pair<inclusive_rectangle<point>, inventory_entry *>( rect,
&entry ) );

if( selected && visible_cells() > 1 ) {
for( int hx = x1; hx < hx_max; ++hx ) {
Expand Down Expand Up @@ -1418,20 +1421,11 @@ inventory_entry *inventory_selector::find_entry_by_invlet( int invlet ) const
return nullptr;
}

inventory_entry *inventory_selector::find_entry_by_coordinate( point coordinate ) const
inventory_entry *inventory_selector::find_entry_by_coordinate( const point coordinate ) const
{
std::vector<inventory_column *> columns = get_visible_columns();
const auto filter_to_selected = [&]( const inventory_entry & entry ) {
return entry.is_selectable();
};
for( inventory_column *column : columns ) {
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 ) {
return entry;
}
for( auto pair : rect_entry_map ) {
if( pair.first.contains( coordinate ) ) {
return pair.second;
}
}
return nullptr;
Expand Down Expand Up @@ -1765,15 +1759,15 @@ void inventory_selector::draw_columns( const catacurses::window &w )
}

if( !is_active_column( *elem ) ) {
elem->draw( w, point( x, y ) );
elem->draw( w, point( x, y ), rect_entry_map );
} else {
active_x = x;
}

x += elem->get_width() + gap;
}

get_active_column().draw( w, point( active_x, y ) );
get_active_column().draw( w, point( active_x, y ), rect_entry_map );
if( empty() ) {
center_print( w, getmaxy( w ) / 2, c_dark_gray, _( "Your inventory is empty." ) );
}
Expand Down Expand Up @@ -1887,12 +1881,14 @@ inventory_input inventory_selector::get_input()
res.action = ctxt.handle_input();
res.ch = ctxt.get_raw_input().get_first_input();

std::pair<point, bool> ct_pair = ctxt.get_coordinates_text( w_inv );
if( ct_pair.second ) {
point p = ct_pair.first;
res.entry = find_entry_by_coordinate( p );
if( res.entry != nullptr ) {
return res;
cata::optional<point> o_p = ctxt.get_coordinates_text( w_inv );
if( o_p ) {
point p = o_p.value();
if( window_contains_point_relative( w_inv, p ) ) {
res.entry = find_entry_by_coordinate( p );
if( res.entry != nullptr && res.entry->is_selectable() ) {
return res;
}
Copy link
Contributor

@Qrox Qrox Jul 23, 2020

Choose a reason for hiding this comment

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

I haven't tested, but judging from the backtrace the crash might happen when you click on a category heading, which doesn't contain any items and would fail the assertion in inventory_entry::any_item.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. This is it.
iTOOLS

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please test again? I think I've resolved the issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

Cannot reproduce a SIGABRT.

But I cannot select items in inventory in this inv. view mode if I changed the view mode (;) before selecting them. Clicking items on the left side does nothing:
unselectable items

I can select not worn items in another inv. view mode. But if I change view mode twice they are shown without the + sign.
no selection indicator

}
}

Expand Down
14 changes: 5 additions & 9 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 @@ -47,12 +48,6 @@ 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;
};

class inventory_entry
{
public:
Expand Down Expand Up @@ -133,8 +128,6 @@ class inventory_entry
nc_color get_invlet_color() const;
void update_cache();

inventory_entry_drawn_info drawn_info;

private:
const item_category *custom_category = nullptr;
bool enabled = true;
Expand Down Expand Up @@ -308,7 +301,8 @@ class inventory_column

inventory_entry *find_by_invlet( int invlet ) const;

void draw( const catacurses::window &win, const point & );
void draw( const catacurses::window &win, const point &p,
std::vector< std::pair<inclusive_rectangle<point>, inventory_entry *>> &rect_entry_map );

void add_entry( const inventory_entry &entry );
void move_entries_to( inventory_column &dest );
Expand Down Expand Up @@ -594,6 +588,8 @@ class inventory_selector
}
std::vector<inventory_column *> get_visible_columns() const;

std::vector< std::pair<inclusive_rectangle<point>, inventory_entry *>> rect_entry_map;

private:
// These functions are called from resizing/redraw callbacks of ui_adaptor
// and should not be made protected or public.
Expand Down
35 changes: 33 additions & 2 deletions src/pickup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "vehicle.h"
#include "vehicle_selector.h"
#include "vpart_position.h"
#include "sdltiles.h"

using ItemCount = std::pair<item, int>;
using PickupMap = std::map<std::string, ItemCount>;
Expand Down Expand Up @@ -624,6 +625,7 @@ void Pickup::pick_up( const tripoint &p, int min, from_where get_items_from )
ctxt.register_action( "ANY_INPUT" );
ctxt.register_action( "HELP_KEYBINDINGS" );
ctxt.register_action( "FILTER" );
ctxt.register_action( "SELECT" );
#if defined(__ANDROID__)
ctxt.allow_text_entry = true; // allow user to specify pickup amount
#endif
Expand Down Expand Up @@ -670,6 +672,7 @@ void Pickup::pick_up( const tripoint &p, int min, from_where get_items_from )
const std::string pickup_chars = ctxt.get_available_single_char_hotkeys( all_pickup_chars );

werase( w_pickup );
pickup_rect::list.clear();
for( int cur_it = start; cur_it < start + maxitems; cur_it++ ) {
if( cur_it < static_cast<int>( matches.size() ) ) {
int true_it = matches[cur_it];
Expand Down Expand Up @@ -740,8 +743,11 @@ void Pickup::pick_up( const tripoint &p, int min, from_where get_items_from )
item_name = string_format( "<color_light_red>!</color> %s", item_name );
}

trim_and_print( w_pickup, point( 6, 1 + ( cur_it % maxitems ) ), pickupW - 4, icolor,
item_name );
int y = 1 + ( cur_it % maxitems );
trim_and_print( w_pickup, point( 6, y ), pickupW - 4, icolor, item_name );
pickup_rect rect = pickup_rect( point( 6, y ), point( 6 + pickupW - 4 - 1, y ) );
rect.cur_it = cur_it;
pickup_rect::list.push_back( rect );
}
}

Expand Down Expand Up @@ -795,6 +801,19 @@ void Pickup::pick_up( const tripoint &p, int min, from_where get_items_from )
if( itemcount < 0 ) {
itemcount = 0;
}
} else if( action == "SELECT" ) {
cata::optional<point> p = ctxt.get_coordinates_text( w_pickup );
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
if( p ) {
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
if( window_contains_point_relative( w_pickup, p.value() ) ) {
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
pickup_rect *rect = pickup_rect::find_by_coordinate( p.value() );
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
if( rect != nullptr ) {
selected = rect->cur_it;
iScrollPos = 0;
idx = selected;
}
}
}

} else if( action == "SCROLL_UP" ) {
iScrollPos--;
} else if( action == "SCROLL_DOWN" ) {
Expand Down Expand Up @@ -1083,3 +1102,15 @@ int Pickup::cost_to_move_item( const Character &who, const item &it )
// Keep it sane - it's not a long activity
return std::min( 400, ret );
}

std::vector<Pickup::pickup_rect> Pickup::pickup_rect::list;

Pickup::pickup_rect *Pickup::pickup_rect::find_by_coordinate( const point p )
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
{
for( pickup_rect &rect : pickup_rect::list ) {
if( rect.contains( p ) ) {
return &rect;
}
}
return nullptr;
}
12 changes: 11 additions & 1 deletion src/pickup.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#define CATA_SRC_PICKUP_H

#include <vector>
#include "point.h"
#include "ui.h"

class item;
class item_location;
Expand Down Expand Up @@ -42,6 +44,14 @@ int cost_to_move_item( const Character &who, const item &it );
* @param m map they are on
*/
bool handle_spillable_contents( Character &c, item &it, map &m );
} // namespace Pickup

struct pickup_rect : inclusive_rectangle<point> {
pickup_rect() = default;
pickup_rect( const point &P_MIN, const point &P_MAX ) : inclusive_rectangle( P_MIN, P_MAX ) {}
int cur_it;
static std::vector<pickup_rect> list;
static pickup_rect *find_by_coordinate( const point p );
ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace Pickup
#endif // CATA_SRC_PICKUP_H
13 changes: 11 additions & 2 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#if defined(TILES)

#include "cursesdef.h" // IWYU pragma: associated
#include "sdltiles.h" // IWYU pragma: associated
#include "cuboid_rectangle.h"

#if defined(TILES)

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -4114,3 +4115,11 @@ HWND getWindowHandle()
#endif

#endif // TILES

bool window_contains_point_relative( const catacurses::window &win, const point &p )
{
const int x = catacurses::getmaxx( win );
const int y = catacurses::getmaxy( win );
const half_open_rectangle<point> win_bounds( point_zero, point( x, y ) );
return win_bounds.contains( p );
}
10 changes: 9 additions & 1 deletion src/sdltiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
#define CATA_SRC_SDLTILES_H

#include <array>
#include "point.h"

namespace catacurses
{
class window;
} // namespace catacurses

#if defined(TILES)

#include <string>
#include <memory>

#include "color_loader.h"
#include "point.h"
#include "sdl_wrappers.h"

class cata_tiles;
Expand Down Expand Up @@ -41,4 +47,6 @@ window_dimensions get_window_dimensions( const point &pos, const point &size );

#endif // TILES

ZhilkinSerg marked this conversation as resolved.
Show resolved Hide resolved
// Text level, valid only for a point relative to the window, not a point in overall space.
bool window_contains_point_relative( const catacurses::window &win, const point &p );
#endif // CATA_SRC_SDLTILES_H
Loading