From 0142dabe87b20f884e9957e80d968aff1568701f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20M=C3=BCller?= Date: Sat, 13 Dec 2014 07:26:14 +0100 Subject: [PATCH] Support for tooltips bound to a map label. --- src/map_label.cpp | 95 ++++++++++++++++++++++++++++++++++------------- src/map_label.hpp | 35 +++++++++++------ 2 files changed, 93 insertions(+), 37 deletions(-) diff --git a/src/map_label.cpp b/src/map_label.cpp index 5b460fdbd9c..6193260448b 100644 --- a/src/map_label.cpp +++ b/src/map_label.cpp @@ -19,6 +19,7 @@ #include "game_data.hpp" #include "map_label.hpp" #include "resources.hpp" +#include "tooltips.hpp" #include "formula_string_utils.hpp" #include @@ -147,7 +148,8 @@ const terrain_label* map_labels::set_label(const map_location& loc, const SDL_Color color, const bool visible_in_fog, const bool visible_in_shroud, - const bool immutable) + const bool immutable, + const t_string& tooltip ) { terrain_label* res = NULL; @@ -173,7 +175,7 @@ const terrain_label* map_labels::set_label(const map_location& loc, } else { - current_label->second->update_info(text, team_name, color, visible_in_fog, visible_in_shroud, immutable); + current_label->second->update_info(text, tooltip, team_name, color, visible_in_fog, visible_in_shroud, immutable); res = current_label->second; } } @@ -190,7 +192,8 @@ const terrain_label* map_labels::set_label(const map_location& loc, color, visible_in_fog, visible_in_shroud, - immutable); + immutable, + tooltip); add_label(loc, res); // Hide the old label. @@ -285,22 +288,24 @@ void map_labels::recalculate_shroud() /// creating new label terrain_label::terrain_label(const t_string& text, - const std::string& team_name, - const map_location& loc, - const map_labels& parent, - const SDL_Color color, - const bool visible_in_fog, - const bool visible_in_shroud, - const bool immutable) : - handle_(0), - text_(text), - team_name_(team_name), - visible_in_fog_(visible_in_fog), - visible_in_shroud_(visible_in_shroud), - immutable_(immutable), - color_(color), - parent_(&parent), - loc_(loc) + const std::string& team_name, + const map_location& loc, + const map_labels& parent, + const SDL_Color color, + const bool visible_in_fog, + const bool visible_in_shroud, + const bool immutable, + const t_string& tooltip ) : + handle_(0), + text_(text), + tooltip_(tooltip), + team_name_(team_name), + visible_in_fog_(visible_in_fog), + visible_in_shroud_(visible_in_shroud), + immutable_(immutable), + color_(color), + parent_(&parent), + loc_(loc) { draw(); } @@ -309,6 +314,7 @@ terrain_label::terrain_label(const t_string& text, terrain_label::terrain_label(const map_labels &parent, const config &cfg) : handle_(0), text_(), + tooltip_(), team_name_(), visible_in_fog_(true), visible_in_shroud_(false), @@ -335,6 +341,7 @@ void terrain_label::read(const config &cfg) std::string tmp_color = cfg["color"]; text_ = cfg["text"]; + tooltip_ = cfg["tooltip"]; team_name_ = cfg["team_name"].str(); visible_in_fog_ = cfg["visible_in_fog"].to_bool(true); visible_in_shroud_ = cfg["visible_in_shroud"].to_bool(); @@ -357,6 +364,7 @@ void terrain_label::write(config& cfg) const { loc_.write(cfg); cfg["text"] = text(); + cfg["tooltip"] = tooltip(); cfg["team_name"] = (this->team_name()); cfg["color"] = cfg_color(); cfg["visible_in_fog"] = visible_in_fog_; @@ -369,6 +377,11 @@ const t_string& terrain_label::text() const return text_; } +const t_string& terrain_label::tooltip() const +{ + return tooltip_; +} + const std::string& terrain_label::team_name() const { return team_name_; @@ -420,16 +433,19 @@ void terrain_label::set_text(const t_string& text) } void terrain_label::update_info(const t_string& text, + const t_string& tooltip, const std::string& team_name, const SDL_Color color) { color_ = color; text_ = text; + tooltip_ = tooltip; team_name_ = team_name; draw(); } void terrain_label::update_info(const t_string& text, + const t_string& tooltip, const std::string& team_name, const SDL_Color color, const bool visible_in_fog, @@ -439,7 +455,7 @@ void terrain_label::update_info(const t_string& text, visible_in_fog_ = visible_in_fog; visible_in_shroud_ = visible_in_shroud; immutable_ = immutable; - update_info(text, team_name, color); + update_info(text, tooltip, team_name, color); } void terrain_label::recalculate() @@ -447,17 +463,42 @@ void terrain_label::recalculate() draw(); } -void terrain_label::calculate_shroud() const +void terrain_label::calculate_shroud() { - if (handle_) - { + if (handle_) { font::show_floating_label(handle_, !hidden()); } + + if (tooltip_.empty() || hidden()) { + tooltips::remove_tooltip(tooltip_handle_); + tooltip_handle_ = 0; + return; + } + + //tooltips::update_tooltip(tooltip_handle, get_rect(), tooltip_.str(), "", true); + + if (tooltip_handle_) + tooltips::update_tooltip(tooltip_handle_,get_rect(), tooltip_.str(), "", true); + else + tooltip_handle_ = tooltips::add_tooltip(get_rect(), tooltip_.str()); +} + +SDL_Rect terrain_label::get_rect() const +{ + SDL_Rect rect; + int hex_size = parent_->disp().hex_size(); + + rect.x = parent_->disp().get_location_x(loc_) + hex_size / 4; + rect.y = parent_->disp().get_location_y(loc_); + rect.h = parent_->disp().hex_size(); + rect.w = parent_->disp().hex_size() - hex_size/2; + + return rect; } void terrain_label::draw() { - if (text_.empty()) + if (text_.empty() && tooltip_.empty()) return; clear(); @@ -487,7 +528,6 @@ void terrain_label::draw() handle_ = font::add_floating_label(flabel); calculate_shroud(); - } /** @@ -541,4 +581,9 @@ void terrain_label::clear() font::remove_floating_label(handle_); handle_ = 0; } + if (tooltip_handle_) + { + tooltips::remove_tooltip(tooltip_handle_); + tooltip_handle_ = 0; + } } diff --git a/src/map_label.hpp b/src/map_label.hpp index d5491c229d2..bb7cfe2d881 100644 --- a/src/map_label.hpp +++ b/src/map_label.hpp @@ -51,19 +51,20 @@ public: const SDL_Color color = font::NORMAL_COLOR, const bool visible_in_fog = true, const bool visible_in_shroud = false, - const bool immutable = false); + const bool immutable = false, + const t_string& tooltip = "" ); bool enabled() const { return enabled_; } void enable(bool is_enabled); - void add_label(const map_location &, terrain_label *); void clear(const std::string&, bool force); void recalculate_labels(); + void recalculate_shroud(); + bool visible_global_label(const map_location&) const; - void recalculate_shroud(); const display& disp() const; @@ -74,6 +75,9 @@ public: void clear_all(); private: + + void add_label(const map_location &, terrain_label *); + void clear_map(label_map &, bool); /// For our private use, a wrapper for get_label() that can return a pointer /// to a non-const terrain_label. @@ -94,14 +98,15 @@ private: class terrain_label { public: - terrain_label(const t_string&, - const std::string&, - const map_location&, - const map_labels&, - const SDL_Color color = font::NORMAL_COLOR, - const bool visible_in_fog = true, - const bool visible_in_shroud = false, - const bool immutable = false); + terrain_label(const t_string& text, + const std::string& team_name, + const map_location& loc, + const map_labels& parent, + const SDL_Color color = font::NORMAL_COLOR, + const bool visible_in_fog = true, + const bool visible_in_shroud = false, + const bool immutable = false, + const t_string& tooltip = "" ); terrain_label(const map_labels &, const config &); @@ -111,6 +116,7 @@ public: void read(const config &cfg); const t_string& text() const; + const t_string& tooltip() const; const std::string& team_name() const; bool visible_in_fog() const; bool visible_in_shroud() const; @@ -121,10 +127,12 @@ public: void set_text(const t_string&); void update_info(const t_string&, + const t_string&, const std::string&, const SDL_Color); void update_info(const t_string& text, + const t_string& tooltip, const std::string& team_name, const SDL_Color color, const bool visible_in_fog, @@ -132,7 +140,7 @@ public: const bool immutable); void recalculate(); - void calculate_shroud() const; + void calculate_shroud(); private: terrain_label(const terrain_label&); @@ -144,8 +152,10 @@ private: std::string cfg_color() const; int handle_; + int tooltip_handle_; t_string text_; + t_string tooltip_; std::string team_name_; bool visible_in_fog_; bool visible_in_shroud_; @@ -156,6 +166,7 @@ private: const map_labels* parent_; map_location loc_; + SDL_Rect get_rect() const; }; #endif