26 #include <SDL2/SDL_rect.h> 29 #define DBG_FT LOG_STREAM(debug, log_font) 30 #define LOG_FT LOG_STREAM(info, log_font) 35 static const int text_width = 400;
39 tooltip(
const SDL_Rect& r,
const std::string&
msg,
const std::string& act =
"");
47 void update_label_pos();
51 : origin(r), message(msg), action(act),
label(msg)
54 DBG_FT <<
"created tooltip for " << origin <<
" at " << loc;
57 void tooltip::init_label()
59 const color_t bgcolor {0,0,0,192};
61 unsigned int border = 10;
63 label.set_font_size(font_size);
65 label.set_clip_rect(game_canvas);
66 label.set_width(text_width);
68 label.set_bg_color(bgcolor);
69 label.set_border_size(border);
71 label.create_texture();
76 void tooltip::update_label_pos()
81 loc = {0, 0, lsize.x, lsize.y};
84 if(origin.y > loc.h) {
85 loc.y = origin.y - loc.h;
87 loc.y = origin.y + origin.h;
94 }
else if(loc.x + loc.w > game_canvas.w) {
95 loc.x = game_canvas.w - loc.w;
98 label.set_position(loc.x, loc.y);
102 std::map<int, tooltip>
tips;
103 int active_tooltip = 0;
107 surface current_background =
nullptr;
120 if(!active_tooltip) {
123 DBG_FT <<
"clearing active tooltip " << active_tooltip;
124 tips.at(active_tooltip).label.undraw();
134 current_manager =
this;
142 current_manager =
nullptr;
147 if(!active_tooltip) {
152 tips.at(active_tooltip).label.update(SDL_GetTicks());
158 if(!active_tooltip) {
162 if(!tip.loc.overlaps(region)) {
169 rect manager::screen_location()
172 if(!active_tooltip) {
175 return tips.at(active_tooltip).loc;
181 LOG_FT <<
"clearing all tooltips";
188 for(
auto i =
tips.begin();
i !=
tips.end(); ) {
189 if(
i->second.origin.overlaps(r)) {
190 DBG_FT <<
"clearing tip " <<
i->first <<
" at " 191 <<
i->second.origin <<
" overlapping " << r;
193 if (
i->first == active_tooltip) {
194 i->second.label.undraw();
208 if (it ==
tips.end() )
return false;
210 if(tip.message == message && tip.origin == origin) {
213 if(tip.message != message) {
214 LOG_FT <<
"updating tooltip " <<
id <<
" message";
215 tip.message = message;
219 if(tip.origin != origin) {
220 DBG_FT <<
"updating tooltip " <<
id <<
" origin " << origin;
222 tip.update_label_pos();
230 DBG_FT <<
"removing tooltip " <<
id;
231 if(
id == active_tooltip) {
237 int add_tooltip(
const SDL_Rect& origin,
const std::string& message,
const std::string& action)
242 if(
tip.origin == origin &&
tip.message == message &&
tip.action == action) {
246 DBG_FT <<
"adding tooltip for " << origin;
251 int id = tooltip_id++;
252 tips.emplace(
id,
tooltip(origin, message, action));
259 if(!current_manager) {
260 throw game::error(
"trying to show tooltip with no tooltip manager");
267 if(active_tooltip ==
id) {
271 LOG_FT <<
"showing tip " <<
id <<
" for " << tip.origin;
274 tip.label.update(SDL_GetTicks());
280 point mouseloc{mousex, mousey};
282 if(
tip.origin.contains(mouseloc)) {
289 LOG_FT <<
"clearing tooltip because none hovered";
296 for(
auto& [
id,
tip] :
tips) { (void)
id;
297 if(!
tip.action.empty() &&
tip.origin.contains(mousex, mousey)) {
void show_help(const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
std::vector< game_tip > tips
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
rect game_canvas()
The game canvas area, in drawing coordinates.
std::string label
What to show in the filter's drop-down list.
The basic class for representing 8-bit RGB or RGBA colour values.
static std::unique_ptr< tooltip > tip
const color_t NORMAL_COLOR
std::string id
Text to match against addon_info.tags()
An abstract description of a rectangle with integer coordinates.
Base class for all the errors encountered by the engine.
Standard logging facilities (interface).
std::string::const_iterator iterator
std::string tooltip
Shown when hovering over an entry in the filter's drop-down list.
void raise_drawable(top_level_drawable *tld)
Raise a TLD to the top of the drawing stack.