Skip to content

Commit

Permalink
Enable mouse input - rework (#41605)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinrhan committed Aug 3, 2020
1 parent cda7028 commit aefa22a
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 62 deletions.
20 changes: 4 additions & 16 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1560,36 +1560,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
}

Expand Down
5 changes: 4 additions & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,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;
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;
}
}
}

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 @@ -628,6 +629,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 @@ -674,6 +676,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 @@ -744,8 +747,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 @@ -799,6 +805,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> pos = ctxt.get_coordinates_text( w_pickup );
if( pos ) {
if( window_contains_point_relative( w_pickup, pos.value() ) ) {
pickup_rect *rect = pickup_rect::find_by_coordinate( pos.value() );
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 @@ -1089,3 +1108,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 )
{
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 );
};

} // 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

// 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

0 comments on commit aefa22a

Please sign in to comment.