Skip to content

Commit

Permalink
Add the option to display ascii art in item description (#37598)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fris0uman authored Feb 13, 2020
1 parent 25a7bff commit b44cf7b
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 2 deletions.
27 changes: 27 additions & 0 deletions doc/JSON_INFO.md
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,33 @@ See also VEHICLE_JSON.md
"symbol": "[", // The item symbol as it appears on the map. Must be a Unicode string exactly 1 console cell width.
"looks_like": "rag", // hint to tilesets if this item has no tile, use the looks_like tile
"description": "Socks. Put 'em on your feet.", // Description of the item
"ascii_picture": [
" ,,,,,,,,,,,,,",
" .;;;;;;;;;;;;;;;;;;;,.",
" .;;;;;;;;;;;;;;;;;;;;;;;;,",
".;;;;;;;;;;;;;;;;;;;;;;;;;;;;.",
";;;;;@;;;;;;;;;;;;;;;;;;;;;;;;' .............",
";;;;@@;;;;;;;;;;;;;;;;;;;;;;;;'.................",
";;;;@@;;;;;;;;;;;;;;;;;;;;;;;;'...................`",
";;;;@;;;;;;;;;;;;;;;@;;;;;;;'.....................",
" `;;;;;;;;;;;;;;;;;;;@@;;;;;'..................;....", // Ascii art that will be displayed at the bottom of the item description. Array of string with each element being a line of the picture. Lines longer than 42 characters won't display properly.
" `;;;;;;;;;;;;;;;;@@;;;;'....................;;...",
" `;;;;;;;;;;;;;@;;;;'...;.................;;....",
" `;;;;;;;;;;;;' ...;;...............;.....",
" `;;;;;;' ...;;..................",
" ;; ..;...............",
" ` ............",
" ` ......",
" ` ..",
" ` '",
" ` '",
" ` '",
" ` `",
" ` `,",
" `",
" `",
" `."
],
"phase": "solid", // (Optional, default = "solid") What phase it is
"weight": "350 g", // Weight, weight in grams, mg and kg can be used - "50 mg", "5 g" or "5 kg". For stackable items (ammo, comestibles) this is the weight per charge.
"volume": "250 ml", // Volume, volume in ml and L can be used - "50 ml" or "2 L". For stackable items (ammo, comestibles) this is the volume of stack_size charges.
Expand Down
5 changes: 5 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3333,6 +3333,11 @@ void item::final_info( std::vector<iteminfo> &info, const iteminfo_query *parts,
}
}
}
if( get_option<bool>( "ENABLE_ASCII_ART_ITEM" ) ) {
for( const std::string &line : type->ascii_picture ) {
info.push_back( iteminfo( "DESCRIPTION", line ) );
}
}
}

std::string item::info( std::vector<iteminfo> &info, const iteminfo_query *parts, int batch ) const
Expand Down
13 changes: 13 additions & 0 deletions src/item_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "json.h"
#include "material.h"
#include "options.h"
#include "output.h"
#include "recipe_dictionary.h"
#include "requirements.h"
#include "string_formatter.h"
Expand Down Expand Up @@ -64,6 +65,8 @@ static void npc_implied_flags( itype &item_template );

extern const double MAX_RECOIL;

static const int ascii_art_width = 42;

bool item_is_blacklisted( const std::string &id )
{
return item_blacklist.count( id );
Expand Down Expand Up @@ -414,6 +417,14 @@ void Item_factory::finalize_post( itype &obj )
}
}
}

for( std::string &line : obj.ascii_picture ) {
if( utf8_width( remove_color_tags( line ) ) > ascii_art_width ) {
line = trim_by_length( line, ascii_art_width );
debugmsg( "ascii_picture in %s contains a line too long to be displayed (>%i char).", obj.id,
ascii_art_width );
}
}
}

void Item_factory::finalize()
Expand Down Expand Up @@ -2082,6 +2093,8 @@ void Item_factory::load_basic_info( const JsonObject &jo, itype &def, const std:
assign( jo, "magazine_well", def.magazine_well );
assign( jo, "explode_in_fire", def.explode_in_fire );
assign( jo, "insulation", def.insulation_factor );
assign( jo, "ascii_picture", def.ascii_picture );


if( jo.has_member( "thrown_damage" ) ) {
def.thrown_damage = load_damage_instance( jo.get_array( "thrown_damage" ) );
Expand Down
1 change: 1 addition & 0 deletions src/itype.h
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ struct itype {

std::string snippet_category;
translation description; // Flavor text
std::vector<std::string> ascii_picture;

// The container it comes in
cata::optional<itype_id> default_container;
Expand Down
6 changes: 6 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,12 @@ void options_manager::add_options_graphics()
true, COPT_CURSES_HIDE
);

add( "ENABLE_ASCII_ART_ITEM", "graphics",
translate_marker( "Enable ASCII art in item descriptions" ),
translate_marker( "When available item description will show a picture of the item in ascii art." ),
true, COPT_NO_HIDE
);

add_empty_line();

add( "USE_TILES", "graphics", translate_marker( "Use tiles" ),
Expand Down
9 changes: 7 additions & 2 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,12 @@ void print_colored_text( const catacurses::window &w, const point &p, nc_color &

void trim_and_print( const catacurses::window &w, const point &begin, int width,
nc_color base_color, const std::string &text )
{
std::string sText = trim_by_length( text, width );
print_colored_text( w, begin, base_color, base_color, sText );
}

std::string trim_by_length( const std::string &text, int width )
{
std::string sText;
if( utf8_width( remove_color_tags( text ) ) > width ) {
Expand Down Expand Up @@ -251,8 +257,7 @@ void trim_and_print( const catacurses::window &w, const point &begin, int width,
} else {
sText = text;
}

print_colored_text( w, begin, base_color, base_color, sText );
return sText;
}

int print_scrollable( const catacurses::window &w, int begin_line, const std::string &text,
Expand Down
1 change: 1 addition & 0 deletions src/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ inline int fold_and_print_from( const catacurses::window &w, const point &begin,
*/
void trim_and_print( const catacurses::window &w, const point &begin, int width,
nc_color base_color, const std::string &text );
std::string trim_by_length( const std::string &text, int width );
template<typename ...Args>
inline void trim_and_print( const catacurses::window &w, const point &begin,
const int width, const nc_color base_color, const char *const mes, Args &&... args )
Expand Down

0 comments on commit b44cf7b

Please sign in to comment.