Skip to content

Commit

Permalink
rewrite V menu
Browse files Browse the repository at this point in the history
  • Loading branch information
mqrause committed Mar 11, 2024
1 parent 7d3234e commit de4d48d
Show file tree
Hide file tree
Showing 7 changed files with 1,670 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@
#include "stats_tracker.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "surroundings_menu.h"
#include "talker.h"
#include "text_snippets.h"
#include "tileray.h"
Expand Down Expand Up @@ -8338,18 +8339,27 @@ void game::list_items_monsters()
}

temp_exit_fullscreen();
game::vmenu_ret ret;
while( true ) {
ret = uistate.vmenu_show_items ? list_items( items ) : list_monsters( mons );
if( ret == game::vmenu_ret::CHANGE_TAB ) {
uistate.vmenu_show_items = !uistate.vmenu_show_items;
} else {
break;
std::optional<tripoint> path_start = u.pos();
std::optional<tripoint> path_end = std::nullopt;
if( get_option<bool>( "USE_IMGUI" ) ) {
surroundings_menu vmenu( u, m, path_end, 55 );
shared_ptr_fast<draw_callback_t> trail_cb = create_trail_callback( path_start, path_end, true );
add_draw_callback( trail_cb );
vmenu.execute();
} else {
game::vmenu_ret ret;
while( true ) {
ret = uistate.vmenu_show_items ? list_items( items ) : list_monsters( mons );
if( ret == game::vmenu_ret::CHANGE_TAB ) {
uistate.vmenu_show_items = !uistate.vmenu_show_items;
} else {
break;
}
}
}

if( ret == game::vmenu_ret::FIRE ) {
avatar_action::fire_wielded_weapon( u );
if( ret == game::vmenu_ret::FIRE ) {
avatar_action::fire_wielded_weapon( u );
}
}
reenter_fullscreen();
}
Expand Down
252 changes: 252 additions & 0 deletions src/map_entity_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
#include "map_entity_stack.h"

#include "creature.h"
#include "game.h"
#include "item.h"
#include "item_category.h"
#include "item_search.h"
#include "mapdata.h"

static const trait_id trait_INATTENTIVE( "INATTENTIVE" );

template<typename T>
const T *map_entity_stack<T>::get_selected_entity() const
{
if( !entities.empty() ) {
// todo: check index
return entities[selected_index].entity;
}
return nullptr;
}

template<typename T>
std::optional<tripoint_rel_ms> map_entity_stack<T>::get_selected_pos() const
{
if( !entities.empty() ) {
// todo: check index
return entities[selected_index].pos;
}
return std::nullopt;
}

template<typename T>
int map_entity_stack<T>::get_selected_count() const
{
if( !entities.empty() ) {
// todo: check index
return entities[selected_index].count;
}
return 0;
}

template<typename T>
void map_entity_stack<T>::select_next()
{
if( entities.empty() ) {
return;
}

selected_index++;

if( selected_index >= static_cast<int>( entities.size() ) ) {
selected_index = 0;
}
}

template<typename T>
void map_entity_stack<T>::select_prev()
{
if( entities.empty() ) {
return;
}

if( selected_index <= 0 ) {
selected_index = entities.size();
}

selected_index--;
}

template<typename T>
int map_entity_stack<T>::get_selected_index() const
{
return selected_index;
}

template<typename T>
map_entity_stack<T>::map_entity_stack() : totalcount( 0 )
{
entities.emplace_back();
}

template<typename T>
map_entity_stack<T>::map_entity_stack( const T *const entity, const tripoint_rel_ms &pos,
const int count ) : totalcount( count )
{
entities.emplace_back( pos, 1, entity );
}

template<typename T>
void map_entity_stack<T>::add_at_pos( const T *const entity, const tripoint_rel_ms &pos,
const int count )
{
if( entities.empty() || entities.back().pos != pos ) {
entities.emplace_back( pos, 1, entity );
} else {
entities.back().count++;
}

totalcount += count;
}

template<typename T>
const std::string map_entity_stack<T>::get_category() const

Check failure on line 103 in src/map_entity_stack.cpp

View workflow job for this annotation

GitHub Actions / build (src)

return type 'const std::string' (aka 'const basic_string<char>') is 'const'-qualified at the top level, which may reduce code readability without improving const correctness [readability-const-return-type,-warnings-as-errors]
{
return std::string();
}

template<>
const std::string map_entity_stack<item>::get_category() const

Check failure on line 109 in src/map_entity_stack.cpp

View workflow job for this annotation

GitHub Actions / build (src)

return type 'const std::string' (aka 'const basic_string<char>') is 'const'-qualified at the top level, which may reduce code readability without improving const correctness [readability-const-return-type,-warnings-as-errors]
{
const item *it = get_selected_entity();
if( it ) {
return it->get_category_of_contents().name_header();
}

return std::string();
}

template<>
const std::string map_entity_stack<Creature>::get_category() const

Check failure on line 120 in src/map_entity_stack.cpp

View workflow job for this annotation

GitHub Actions / build (src)

return type 'const std::string' (aka 'const basic_string<char>') is 'const'-qualified at the top level, which may reduce code readability without improving const correctness [readability-const-return-type,-warnings-as-errors]
{
const Creature *mon = get_selected_entity();
if( mon ) {
const Character &you = get_player_character();
if( you.has_trait( trait_INATTENTIVE ) ) {
return _( "Unknown" );
}
return Creature::get_attitude_ui_data( mon->attitude_to( you ) ).first.translated();
}

return std::string();
}

template<>
const std::string map_entity_stack<map_data_common_t>::get_category() const
{
const map_data_common_t *tf = get_selected_entity();
if( tf ) {
if( tf->is_terrain() ) {
return "TERRAIN";
} else {
return "FURNITURE";
}
}

return std::string();
}

template<typename T>
bool map_entity_stack<T>::compare( const map_entity_stack<T> &, bool ) const
{
return false;
}

template<>
bool map_entity_stack<item>::compare( const map_entity_stack<item> &rhs, bool use_category ) const
{
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) <
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos );

if( !use_category ) {
return compare_dist;
}

const item_category &lhs_cat = get_selected_entity()->get_category_of_contents();
const item_category &rhs_cat = rhs.get_selected_entity()->get_category_of_contents();

if( lhs_cat == rhs_cat ) {
return compare_dist;
}

return lhs_cat < rhs_cat;
}

template<>
bool map_entity_stack<Creature>::compare( const map_entity_stack<Creature> &rhs,
bool use_category ) const
{
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) <
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos );

if( !use_category ) {
return compare_dist;
}

// maybe pass you as a parameter to avoid including game.h?
Character &you = get_player_character();
const Creature::Attitude att_lhs = get_selected_entity()->attitude_to( you );
const Creature::Attitude att_rhs = rhs.get_selected_entity()->attitude_to( you );

if( att_lhs == att_rhs ) {
return compare_dist;
}

return att_lhs < att_rhs;
}

template<>
bool map_entity_stack<map_data_common_t>::compare( const map_entity_stack<map_data_common_t> &rhs,
bool use_category ) const
{
bool compare_dist = rl_dist( tripoint_rel_ms(), entities[0].pos ) <
rl_dist( tripoint_rel_ms(), rhs.entities[0].pos );

if( !use_category ) {
return compare_dist;
}

const bool &lhs_cat = get_selected_entity()->is_terrain();
const bool &rhs_cat = rhs.get_selected_entity()->is_terrain();

if( lhs_cat == rhs_cat ) {
return compare_dist;
}

return rhs_cat;
}

//returns the first non priority items.
template<typename T>
int list_filter_high_priority( std::vector<map_entity_stack<T>> &, const std::string & )
{
}

template<typename T>
int list_filter_low_priority( std::vector<map_entity_stack<T>> &stack, const int start,
const std::string &priorities )
{
// todo: actually use it and specialization (only used for items)
// TODO:optimize if necessary
std::vector<map_entity_stack<T>> tempstack;
const auto filter_fn = item_filter_from_string( priorities );
for( auto it = stack.begin() + start; it != stack.end(); ) {
if( !priorities.empty() && it->example != nullptr && filter_fn( *it->example ) ) {
tempstack.push_back( *it );
it = stack.erase( it );
} else {
it++;
}
}

int id = stack.size();
for( map_item_stack &elem : tempstack ) {
stack.push_back( elem );
}
return id;
}

// explicit template instantiation
template class map_entity_stack<item>;
template class map_entity_stack<Creature>;
template class map_entity_stack<map_data_common_t>;
59 changes: 59 additions & 0 deletions src/map_entity_stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once
#ifndef CATA_SRC_MAP_ENTITY_STACK_H
#define CATA_SRC_MAP_ENTITY_STACK_H

#include "coordinates.h"
#include "point.h"

template<typename T>
class map_entity_stack
{
private:
class entity_group
{
public:
tripoint_rel_ms pos;
int count;
const T *entity;

//only expected to be used for things like lists and vectors
entity_group() : count( 0 ), entity( nullptr ) {};
entity_group( const tripoint_rel_ms &p, int arg_count, const T *entity ) :
pos( p ), count( arg_count ), entity( entity ) {};
};

int selected_index = 0;
public:
std::vector<entity_group> entities;
int totalcount;

const T *get_selected_entity() const;
std::optional<tripoint_rel_ms> get_selected_pos() const;
int get_selected_count() const;
int get_selected_index() const;

void select_next();
void select_prev();

//only expected to be used for things like lists and vectors
map_entity_stack();
map_entity_stack( const T *entity, const tripoint_rel_ms &pos, int count = 1 );

// This adds to an existing entity group if the last current
// entity group is the same position and otherwise creates and
// adds to a new entity group. Note that it does not search
// through all older entity groups for a match.
void add_at_pos( const T *entity, const tripoint_rel_ms &pos, int count = 1 );

bool compare( const map_entity_stack<T> &rhs, bool use_category = false ) const;
const std::string get_category() const;
};

template<typename T>
int list_filter_high_priority( std::vector<map_entity_stack<T>> &stack,
const std::string &priorities );
template<typename T>
int list_filter_low_priority( std::vector<map_entity_stack<T>> &stack, int start,
const std::string &priorities );

#endif // CATA_SRC_MAP_ENTITY_STACK_H
10 changes: 10 additions & 0 deletions src/mapdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,16 @@ void map_data_common_t::load( const JsonObject &jo, const std::string & )
}
}

bool map_data_common_t::is_terrain() const
{
return false;
}

bool ter_t::is_terrain() const
{
return true;
}

bool ter_t::is_null() const
{
return id == ter_str_id::NULL_ID();
Expand Down
3 changes: 3 additions & 0 deletions src/mapdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ struct map_data_common_t {
has_flag( ter_furn_flag::TFLAG_FLAMMABLE_HARD );
}

virtual bool is_terrain() const;

virtual void load( const JsonObject &jo, const std::string & );
virtual void check() const {};
};
Expand Down Expand Up @@ -627,6 +629,7 @@ struct ter_t : map_data_common_t {
static size_t count();

bool is_null() const;
bool is_terrain() const override;

void load( const JsonObject &jo, const std::string &src ) override;
void check() const override;
Expand Down
Loading

0 comments on commit de4d48d

Please sign in to comment.