Skip to content

Commit

Permalink
Add minimal status indicators window to hud. For day, harvesting and …
Browse files Browse the repository at this point in the history
…poison states.
  • Loading branch information
pjbroad committed Jan 3, 2014
1 parent 1235e17 commit 4d3dd18
Show file tree
Hide file tree
Showing 13 changed files with 321 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Makefile.bsd
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ COBJS=2d_objects.o 3d_objects.o \

CXXOBJS=achievements.o actor_init.o cal3d_wrapper.o command_queue.o \
context_menu.o elloggingwrapper.o engine/logging.o exceptions/extendedexception.o \
hud_timer.o icon_window.o io/cal3d_io_wrapper.o item_info.o item_lists.o named_colours.o \
hud_indicators.o hud_timer.o icon_window.o io/cal3d_io_wrapper.o item_info.o item_lists.o named_colours.o \
optimizer.o quest_log.o select.o sendvideoinfo.o trade_log.o user_menus.o \
xml/xmlhelper.o eye_candy_wrapper.o \
eye_candy/eye_candy.o eye_candy/math_cache.o eye_candy/effect_lamp.o \
Expand Down
2 changes: 1 addition & 1 deletion Makefile.linux
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ COBJS=2d_objects.o 3d_objects.o \

CXXOBJS=achievements.o actor_init.o cal3d_wrapper.o command_queue.o \
context_menu.o elloggingwrapper.o engine/logging.o exceptions/extendedexception.o \
hud_timer.o icon_window.o io/cal3d_io_wrapper.o item_info.o item_lists.o named_colours.o \
hud_indicators.o hud_timer.o icon_window.o io/cal3d_io_wrapper.o item_info.o item_lists.o named_colours.o \
optimizer.o quest_log.o select.o sendvideoinfo.o trade_log.o user_menus.o \
xml/xmlhelper.o eye_candy_wrapper.o \
eye_candy/eye_candy.o eye_candy/math_cache.o eye_candy/effect_lamp.o \
Expand Down
2 changes: 1 addition & 1 deletion Makefile.win
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ COBJS=2d_objects.o 3d_objects.o \

CXXOBJS=achievements.o actor_init.o cal3d_wrapper.o command_queue.o \
context_menu.o elloggingwrapper.o engine/logging.o exceptions/extendedexception.o \
hud_timer.o icon_window.o io/cal3d_io_wrapper.o item_info.o item_lists.o named_colours.o \
hud_indicators.o hud_timer.o icon_window.o io/cal3d_io_wrapper.o item_info.o item_lists.o named_colours.o \
optimizer.o quest_log.o select.o sendvideoinfo.o trade_log.o user_menus.o \
xml/xmlhelper.o eye_candy_wrapper.o \
eye_candy/eye_candy.o eye_candy/math_cache.o eye_candy/effect_lamp.o \
Expand Down
1 change: 1 addition & 0 deletions counters.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ static size_t *search_len = NULL;
static Uint32 misc_event_time = 0;

int harvesting = 0;
int now_harvesting(void) { return harvesting; }
Uint32 disconnect_time;
char harvest_name[32] = {0};
int killed_by_player = 0;
Expand Down
2 changes: 2 additions & 0 deletions counters.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C" {
#endif

extern int harvesting;
int now_harvesting(void);

extern char harvest_name[32];
extern int counters_win;
extern Uint32 disconnect_time;
Expand Down
16 changes: 16 additions & 0 deletions elconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "gamewin.h"
#include "gl_init.h"
#include "hud.h"
#include "hud_indicators.h"
#include "init.h"
#include "interface.h"
#include "items.h"
Expand Down Expand Up @@ -334,6 +335,20 @@ static void change_show_action_bar(int * var)
init_stats_display();
}

static void change_indicators_var(int * var)
{
*var= !*var;
if (*var)
{
if (indicators_win < 0)
init_hud_indicators();
else
show_window(indicators_win);
}
else
hide_window(indicators_win);
}

void change_minimap_scale(float * var, float * value)
{
int shown = 0;
Expand Down Expand Up @@ -1887,6 +1902,7 @@ static void init_ELC_vars(void)
add_var(OPT_BOOL,"show_stats_in_hud","sstats",&show_stats_in_hud,change_var,0,"Stats In HUD","Toggle showing stats in the HUD",HUD);
add_var(OPT_BOOL,"show_statbars_in_hud","sstatbars",&show_statbars_in_hud,change_var,0,"StatBars In HUD","Toggle showing statbars in the HUD. Needs Stats in HUD",HUD);
add_var(OPT_BOOL,"show_action_bar","ssactionbar",&show_action_bar,change_show_action_bar,0,"Action Points Bar in HUD","Show the current action points level in a stats bar on the bottom HUD.",HUD);
add_var(OPT_BOOL,"show_indicators","showindicators",&show_indicators,change_indicators_var,1,"Show Status Indicators in HUD","Show status indicators for special day, harvesting and poision.",HUD);
add_var(OPT_BOOL,"logo_click_to_url","logoclick",&logo_click_to_url,change_var,0,"Logo Click To URL","Toggle clicking the LOGO opening a browser window",HUD);
add_var(OPT_STRING,"logo_link", "logolink", LOGO_URL_LINK, change_string, 128, "Logo Link", "URL when clicking the logo", HUD);
add_var(OPT_BOOL,"show_help_text","shelp",&show_help_text,change_var,1,"Help Text","Enable tooltips.",HUD);
Expand Down
12 changes: 10 additions & 2 deletions hud.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "gamewin.h"
#include "gl_init.h"
#include "global.h"
#include "hud_indicators.h"
#include "hud_timer.h"
#include "icon_window.h"
#include "init.h"
Expand Down Expand Up @@ -89,7 +90,7 @@ static int cm_music_enabled = 0;
static int cm_minimap_shown = 0;
static int cm_rangstats_shown = 0;
enum { CMH_STATS=0, CMH_STATBARS, CMH_KNOWBAR, CMH_TIMER, CMH_DIGCLOCK, CMH_ANACLOCK,
CMH_SECONDS, CMH_FPS, CMH_QUICKBM, CMH_SEP1, CMH_MINIMAP, CMH_RANGSTATS,
CMH_SECONDS, CMH_FPS, CMH_INDICATORS, CMH_QUICKBM, CMH_SEP1, CMH_MINIMAP, CMH_RANGSTATS,
CMH_SEP2, CMH_SOUND, CMH_MUSIC, CMH_SEP3, CMH_LOCATION };
enum { CMQB_RELOC=0, CMQB_DRAG, CMQB_RESET, CMQB_FLIP, CMQB_ENABLE };

Expand Down Expand Up @@ -131,7 +132,8 @@ void cleanup_hud(void)
destroy_window(misc_win);
destroy_window(stats_bar_win);
destroy_window(quickbar_win);
stats_bar_win = quickbar_win = misc_win = -1;
destroy_window(indicators_win);
stats_bar_win = quickbar_win = misc_win = indicators_win = -1;
}


Expand Down Expand Up @@ -188,6 +190,8 @@ void init_hud_interface (hud_interface type)
init_stats_display ();
init_quickbar ();
init_quickspell ();
if (show_indicators)
init_hud_indicators ();
ready_for_user_menus = 1;
if (enable_user_menus)
display_user_menus();
Expand All @@ -205,6 +209,7 @@ void show_hud_windows ()
if (misc_win >= 0) show_window (misc_win);
if (quickbar_win >= 0) show_window (quickbar_win);
if (quickspell_win >= 0) show_window (quickspell_win);
if (indicators_win >= 0) show_window (indicators_win);
}

void hide_hud_windows ()
Expand All @@ -214,6 +219,7 @@ void hide_hud_windows ()
if (misc_win >= 0) hide_window (misc_win);
if (quickbar_win >= 0) hide_window (quickbar_win);
if (quickspell_win >= 0) hide_window (quickspell_win);
if (indicators_win >= 0) hide_window (indicators_win);
}

// draw everything related to the hud
Expand Down Expand Up @@ -1135,6 +1141,7 @@ void init_misc_display(hud_interface type)
cm_bool_line(cm_hud_id, CMH_ANACLOCK, &view_analog_clock, "view_analog_clock");
cm_bool_line(cm_hud_id, CMH_SECONDS, &show_game_seconds, "show_game_seconds");
cm_bool_line(cm_hud_id, CMH_FPS, &show_fps, "show_fps");
cm_bool_line(cm_hud_id, CMH_INDICATORS, &show_indicators, "show_indicators");
cm_bool_line(cm_hud_id, CMH_MINIMAP, &cm_minimap_shown, NULL);
cm_bool_line(cm_hud_id, CMH_RANGSTATS, &cm_rangstats_shown, NULL);
cm_bool_line(cm_hud_id, CMH_QUICKBM, &cm_quickbar_enabled, NULL);
Expand All @@ -1151,6 +1158,7 @@ void init_misc_display(hud_interface type)
cm_grey_line(cm_hud_id, CMH_STATS, (type == HUD_INTERFACE_NEW_CHAR));
cm_grey_line(cm_hud_id, CMH_STATBARS, (type == HUD_INTERFACE_NEW_CHAR));
cm_grey_line(cm_hud_id, CMH_FPS, (type == HUD_INTERFACE_NEW_CHAR));
cm_grey_line(cm_hud_id, CMH_INDICATORS, (type == HUD_INTERFACE_NEW_CHAR));
cm_grey_line(cm_hud_id, CMH_MINIMAP, (type == HUD_INTERFACE_NEW_CHAR));
cm_grey_line(cm_hud_id, CMH_RANGSTATS, (type == HUD_INTERFACE_NEW_CHAR));
cm_grey_line(cm_hud_id, CMH_QUICKBM, (type == HUD_INTERFACE_NEW_CHAR));
Expand Down
242 changes: 242 additions & 0 deletions hud_indicators.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
// Status Indicators
//
// Simple HUD window to show the status of multiple client states
// Harvesting, Special Day and Poison status are initial provided.
//
// TODO change list
// Possibly use a coloured light or preferably an icon for
// the indicators rather than a simple character.
// Make movable and save state, allow reset too. Use context menu?
//
// Author bluap/pjbroad Jan 2014
//

#include <vector>
#include <string>
#include <cstring>

#include "asc.h"
#include "chat.h"
#include "counters.h"
#include "font.h"
#include "elwindows.h"
#include "gamewin.h"
#include "gl_init.h"
#include "hud.h"
#include "sound.h"
#include "spells.h"
#include "text.h"
#include "translate.h"


static void init_indicators(void);

extern "C"
{
int indicators_win = -1;
int show_indicators = 1;
void init_hud_indicators(void) { init_indicators(); }
}


// Constants used by the window
//
class Vars
{
public:
static const float zoom(void) { return 1.0; }
static const int space(void) { return 10; }
static const int border(void) { return 2; }
static const float font_x(void) { return DEFAULT_FONT_X_LEN; }
static const float font_y(void) { return DEFAULT_FONT_Y_LEN; }
static const int y_len(void) { return static_cast<int>(border() + zoom() * font_y() + 0.5); }
};


// A class to hold the state for an individual indicator
//
class Indicator
{
public:
Indicator(const char *the_strings, int (*ctrl)(void), int no, const char *the_action);
void do_draw(void);
void do_action(void) const;
bool is_over(int mx);
void show_tooltip(void) const;
private:
std::string disp;
std::string on_tooltip;
std::string off_tooltip;
std::string action;
int (*cntr_func)(void);
int x_pos;
int y_pos;
bool mouse_over;
};


// Construct the indicator deriving the strings from the "||" separated string passed
//
Indicator::Indicator(const char *the_strings, int (*ctrl)(void), int no, const char *the_action)
: disp("*"), on_tooltip("Unset"), off_tooltip("Unset"), cntr_func(ctrl), y_pos(Vars::border()), mouse_over(false)
{
x_pos = static_cast<int>(Vars::border() + no * (Vars::zoom() * Vars::font_x() + Vars::space()) + 0.5);

if (the_action)
action = the_action;

if (the_strings)
{
std::string line_text(the_strings);
std::string::size_type from_index = 0;
std::string::size_type to_index = 0;
std::string delim = "||";
std::string::size_type len = 0;
std::vector<std::string> fields;
while ((to_index = line_text.find(delim, from_index)) != std::string::npos)
{
if ((len = to_index-from_index) > 0)
fields.push_back(line_text.substr(from_index, len));
from_index = to_index + delim.size();
}
if ((len = line_text.size()-from_index) > 0)
fields.push_back(line_text.substr(from_index, len));
if (fields.size() == 3)
{
disp = fields[0].substr(0, 1);
on_tooltip = fields[1];
off_tooltip = fields[2];
}
}
}


// Simply draw the single character, highlighted if the status is true
//
void Indicator::do_draw(void)
{
if (mouse_over)
glColor3f(1.0f,1.0f,1.0f);
else if (cntr_func && cntr_func())
glColor3f(0.99f,0.87f,0.65f);
else
glColor3f(0.40f,0.30f,0.20f);
draw_string_zoomed(x_pos, y_pos, (const unsigned char*)disp.c_str(), 1, Vars::zoom());
mouse_over = false;
}


// If an action string is defined, execute it as a command line
//
void Indicator::do_action(void) const
{
if (action.empty())
{
do_alert1_sound();
return;
}
size_t command_len = action.size() + 1;
do_click_sound();
char temp[command_len];
safe_strncpy(temp, action.c_str(), command_len);
parse_input(temp, strlen(temp));
}


// Show the tooltip that explains the status
//
void Indicator::show_tooltip(void) const
{
const char *tooltip = ((cntr_func && cntr_func()) ?on_tooltip.c_str() : off_tooltip.c_str());
show_help(tooltip, -static_cast<int>(Vars::border() + SMALL_FONT_X_LEN * (1 + strlen(tooltip)) + 0.5), y_pos );
}


// If mouse is over, set for this frame and return true
//
bool Indicator::is_over(int mx)
{
mouse_over = ((mx > x_pos && (mx < x_pos + Vars::zoom() * Vars::font_x())));
return mouse_over;
}


// The indicators
static std::vector<Indicator> indicators;


// Window display callback, display the indicators
//
static int display_indicators_handler(window_info *win)
{
std::vector<Indicator>::iterator i;
for (i=indicators.begin(); i<indicators.end(); ++i)
i->do_draw();
return 1;
}


// Window mouse over callback, show tooltip if over an indicator
//
static int mouseover_indicators_handler(window_info *win, int mx, int my)
{
std::vector<Indicator>::iterator i;
for (i=indicators.begin(); i<indicators.end(); ++i)
if (i->is_over(mx))
i->show_tooltip();
return 0;
}


// Window mouse click callback, execute action if over an indicator
//
static int click_indicators_handler(window_info *win, int mx, int my, Uint32 flags)
{
// TODO move window code, finish/abandon....
/*if (flags&ELW_CTRL)
{
if (win->flags&ELW_TITLE_BAR)
win->flags &= ~(ELW_TITLE_BAR|ELW_DRAGGABLE);
else
win->flags |= ELW_TITLE_BAR|ELW_DRAGGABLE;
return 1;
}*/
if (flags&ELW_LEFT_MOUSE)
{
std::vector<Indicator>::iterator i;
for (i=indicators.begin(); i<indicators.end(); ++i)
if (i->is_over(mx))
i->do_action();
}
return 1;
}


// Initialise the indicators, create or re-init the window
//
static void init_indicators(void)
{
if (indicators.empty())
{
indicators.reserve(3);
indicators.push_back(Indicator(day_indicator_str, today_is_special_day, indicators.size(), "#day"));
indicators.push_back(Indicator(harvest_indicator_str, now_harvesting, indicators.size(), 0));
indicators.push_back(Indicator(poison_indicator_str, we_are_poisoned, indicators.size(), 0));
}

int x_len = static_cast<int>(Vars::font_x() * indicators.size() * Vars::zoom() +
2 * Vars::border() + Vars::space() * (indicators.size()-1) + 0.5);
int y_len = static_cast<int>(Vars::border() + Vars::zoom() * Vars::font_y() + 0.5);
int x_pos = window_width - HUD_MARGIN_X - x_len;
int y_pos = window_height - y_len;

if(indicators_win < 0)
{
indicators_win = create_window("Indicators", -1, 0, x_pos, y_pos, x_len, y_len, ELW_SHOW);
set_window_handler(indicators_win, ELW_HANDLER_DISPLAY, (int (*)())&display_indicators_handler);
set_window_handler(indicators_win, ELW_HANDLER_MOUSEOVER, (int (*)())&mouseover_indicators_handler);
set_window_handler(indicators_win, ELW_HANDLER_CLICK, (int (*)())&click_indicators_handler);
}
else
init_window(indicators_win, -1, 0, x_pos, y_pos, x_len, y_len);
}
Loading

0 comments on commit 4d3dd18

Please sign in to comment.