Skip to content

Commit

Permalink
Custom sidebar widget: Compass
Browse files Browse the repository at this point in the history
  • Loading branch information
dseguin committed Jan 2, 2022
1 parent acacbb1 commit d444594
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 0 deletions.
87 changes: 87 additions & 0 deletions data/json/ui/compass.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
[
{
"id": "compass_N_text",
"type": "widget",
"label": "N",
"style": "text",
"var": "compass_N"
},
{
"id": "compass_S_text",
"type": "widget",
"label": "S",
"style": "text",
"var": "compass_S"
},
{
"id": "compass_E_text",
"type": "widget",
"label": "E",
"style": "text",
"var": "compass_E"
},
{
"id": "compass_W_text",
"type": "widget",
"label": "W",
"style": "text",
"var": "compass_W"
},
{
"id": "compass_NE_text",
"type": "widget",
"label": "NE",
"style": "text",
"var": "compass_NE"
},
{
"id": "compass_NW_text",
"type": "widget",
"label": "NW",
"style": "text",
"var": "compass_NW"
},
{
"id": "compass_SE_text",
"type": "widget",
"label": "SE",
"style": "text",
"var": "compass_SE"
},
{
"id": "compass_SW_text",
"type": "widget",
"label": "SW",
"style": "text",
"var": "compass_SW"
},
{
"//": "An empty space indicating threats nearby",
"id": "compass_local_text",
"type": "widget",
"label": " ",
"style": "text",
"var": "compass_local"
},
{
"id": "compass_top_layout",
"type": "widget",
"style": "layout",
"arrange": "columns",
"widgets": [ "compass_NW_text", "compass_N_text", "compass_NE_text" ]
},
{
"id": "compass_middle_layout",
"type": "widget",
"style": "layout",
"arrange": "columns",
"widgets": [ "compass_W_text", "compass_local_text", "compass_E_text" ]
},
{
"id": "compass_bottom_layout",
"type": "widget",
"style": "layout",
"arrange": "columns",
"widgets": [ "compass_SW_text", "compass_S_text", "compass_SE_text" ]
}
]
90 changes: 90 additions & 0 deletions src/panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2099,6 +2099,96 @@ static void draw_wind_padding( avatar &u, const catacurses::window &w )
render_wind( u, w, " %s: " );
}

enum class cardinal_directions {
NORTH = 0,
NORTHEAST = 1,
EAST = 2,
SOUTHEAST = 3,
SOUTH = 4,
SOUTHWEST = 5,
WEST = 6,
NORTHWEST = 7,
LOCAL = 8
};

static std::string get_compass_for_direction( const cardinal_directions dir, int max_width )
{
const int d = static_cast<int>( dir );
const monster_visible_info &mon_visible = get_avatar().get_mon_visible();
std::vector<std::pair<std::string, nc_color>> syms;
for( npc * n : mon_visible.unique_types[d] ) {
switch( n->get_attitude() ) {
case NPCATT_KILL:
syms.emplace_back( "@", c_red );
break;
case NPCATT_FOLLOW:
syms.emplace_back( "@", c_light_green );
break;
default:
syms.emplace_back( "@", c_pink );
break;
}
}
for( const std::pair<const mtype *, int> & m : mon_visible.unique_mons[d] ) {
syms.emplace_back( m.first->sym, m.first->color );
}

std::string ret;
for( int i = 0; i < static_cast<int>( syms.size() ); i++ ) {
if( i == max_width - 1 ) {
ret += colorize( "+", c_white );
break;
}
ret += colorize( syms[i].first, syms[i].second );
}
return ret;
}

std::pair<std::string, nc_color> display::compass_N_text_color()
{
return { get_compass_for_direction( cardinal_directions::NORTH, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_S_text_color()
{
return { get_compass_for_direction( cardinal_directions::SOUTH, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_E_text_color()
{
return { get_compass_for_direction( cardinal_directions::EAST, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_W_text_color()
{
return { get_compass_for_direction( cardinal_directions::WEST, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_NE_text_color()
{
return { get_compass_for_direction( cardinal_directions::NORTHEAST, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_NW_text_color()
{
return { get_compass_for_direction( cardinal_directions::NORTHWEST, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_SE_text_color()
{
return { get_compass_for_direction( cardinal_directions::SOUTHEAST, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_SW_text_color()
{
return { get_compass_for_direction( cardinal_directions::SOUTHWEST, 4 ), c_white };
}

std::pair<std::string, nc_color> display::compass_local_text_color()
{
return { get_compass_for_direction( cardinal_directions::LOCAL, 4 ), c_white };
}

static void draw_health_classic( avatar &u, const catacurses::window &w )
{
vehicle *veh = g->remoteveh();
Expand Down
11 changes: 11 additions & 0 deletions src/panels.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ std::pair<std::string, nc_color> per_text_color( const Character &p );
std::pair<std::string, nc_color> safe_mode_text_color( const bool classic_mode );
std::pair<std::string, nc_color> wind_text_color( const Character &u );

// Get visible threats by cardinal direction
std::pair<std::string, nc_color> compass_N_text_color();
std::pair<std::string, nc_color> compass_S_text_color();
std::pair<std::string, nc_color> compass_E_text_color();
std::pair<std::string, nc_color> compass_W_text_color();
std::pair<std::string, nc_color> compass_NE_text_color();
std::pair<std::string, nc_color> compass_NW_text_color();
std::pair<std::string, nc_color> compass_SE_text_color();
std::pair<std::string, nc_color> compass_SW_text_color();
std::pair<std::string, nc_color> compass_local_text_color();

// Define color for displaying the body temperature
nc_color bodytemp_color( const Character &u, const bodypart_id &bp );
// Returns color which this limb would have in healing menus
Expand Down
58 changes: 58 additions & 0 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ std::string enum_to_string<widget_var>( widget_var data )
return "mana";
case widget_var::morale_level:
return "morale_level";
// Compass
case widget_var::compass_N:
return "compass_N";
case widget_var::compass_S:
return "compass_S";
case widget_var::compass_E:
return "compass_E";
case widget_var::compass_W:
return "compass_W";
case widget_var::compass_NE:
return "compass_NE";
case widget_var::compass_NW:
return "compass_NW";
case widget_var::compass_SE:
return "compass_SE";
case widget_var::compass_SW:
return "compass_SW";
case widget_var::compass_local:
return "compass_local";
// Base stats
case widget_var::stat_str:
return "stat_str";
Expand Down Expand Up @@ -334,6 +353,15 @@ bool widget::uses_text_function()
switch( _var ) {
case widget_var::activity_text:
case widget_var::body_temp_text:
case widget_var::compass_N:
case widget_var::compass_S:
case widget_var::compass_E:
case widget_var::compass_W:
case widget_var::compass_NE:
case widget_var::compass_NW:
case widget_var::compass_SE:
case widget_var::compass_SW:
case widget_var::compass_local:
case widget_var::date_text:
case widget_var::env_temp_text:
case widget_var::fatigue_text:
Expand Down Expand Up @@ -437,6 +465,36 @@ std::string widget::color_text_function_string( const avatar &ava )
case widget_var::wind_text:
desc = display::wind_text_color( ava );
break;
// Compass color is specific to individual threats.
// Skip colorization.
case widget_var::compass_N:
desc = display::compass_N_text_color();
return desc.first;
case widget_var::compass_S:
desc = display::compass_S_text_color();
return desc.first;
case widget_var::compass_E:
desc = display::compass_E_text_color();
return desc.first;
case widget_var::compass_W:
desc = display::compass_W_text_color();
return desc.first;
case widget_var::compass_NE:
desc = display::compass_NE_text_color();
return desc.first;
case widget_var::compass_NW:
desc = display::compass_NW_text_color();
return desc.first;
case widget_var::compass_SE:
desc = display::compass_SE_text_color();
return desc.first;
case widget_var::compass_SW:
desc = display::compass_SW_text_color();
return desc.first;
case widget_var::compass_local:
desc = display::compass_local_text_color();
return desc.first;
// ------------------------------------------------
default:
debugmsg( "Unexpected widget_var %s - no text_color function defined",
io::enum_to_string<widget_var>( _var ) );
Expand Down
10 changes: 10 additions & 0 deletions src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ enum class widget_var : int {
mood, // TODO
cardio_fit, // Cardio fitness, integer near BMR
cardio_acc, // Cardio accumulator, integer
// Compass - Visible threats by cardinal direction
compass_N, // North
compass_S, // South
compass_E, // East
compass_W, // West
compass_NE, // North-East
compass_NW, // North-West
compass_SE, // South-East
compass_SW, // South-West
compass_local, // Local/here
// Text vars
activity_text, // Activity level text, color string
body_temp_text, // Felt body temperature, color string
Expand Down

0 comments on commit d444594

Please sign in to comment.