Allow removing labels by handle.

This commit is contained in:
Fabian Müller 2014-12-13 07:15:09 +01:00
parent 0459f8a574
commit 7525c2ed4c
2 changed files with 63 additions and 19 deletions

View file

@ -45,10 +45,11 @@ struct tooltip
surface foreground;
};
std::vector<tooltip> tips;
std::vector<tooltip>::const_iterator current_tooltip = tips.end();
std::map<int, tooltip> tips;
std::map<int, tooltip>::const_iterator current_tooltip = tips.end();
int tooltip_handle = 0;
int tooltip_id = 0;
surface current_background = NULL;
@ -131,12 +132,12 @@ void clear_tooltips()
void clear_tooltips(const SDL_Rect& rect)
{
for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ) {
if(sdl::rects_overlap(i->rect,rect)) {
for(std::map<int,tooltip>::iterator i = tips.begin(); i != tips.end(); ) {
if(sdl::rects_overlap(i->second.rect,rect)) {
if (i==current_tooltip) {
clear_tooltip();
}
i = tips.erase(i);
tips.erase(i++);
current_tooltip = tips.end();
} else {
++i;
@ -144,26 +145,64 @@ void clear_tooltips(const SDL_Rect& rect)
}
}
void add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action, bool use_markup, const surface& foreground)
bool update_tooltip(int id, const SDL_Rect& rect, const std::string& message,
const std::string& action, bool use_markup)
{
for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ++i) {
if(sdl::rects_overlap(i->rect,rect)) {
*i = tooltip(rect, message, action, use_markup, foreground);
return;
std::map<int, tooltip>::iterator it = tips.find(id);
if (it == tips.end() ) return false;
it->second.action = action;
it->second.markup = use_markup;
it->second.message = message;
it->second.rect = rect;
return true;
}
bool update_tooltip(int id, const SDL_Rect& rect, const std::string& message,
const std::string& action, bool use_markup, const surface& foreground)
{
std::map<int, tooltip>::iterator it = tips.find(id);
if (it == tips.end() ) return false;
it->second.action = action;
it->second.foreground = foreground;
it->second.markup = use_markup;
it->second.message = message;
it->second.rect = rect;
return true;
}
void remove_tooltip(int id)
{
tips.erase(id);
clear_tooltip();
}
int add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action, bool use_markup, const surface& foreground)
{
for(std::map<int, tooltip>::iterator it = tips.begin(); it != tips.end();) {
if(sdl::rects_overlap(it->second.rect,rect)) {
tips.erase(it++);
} else {
++it;
}
}
tips.push_back(tooltip(rect, message, action, use_markup, foreground));
int id = tooltip_id++;
tips.insert(std::make_pair(id, tooltip(rect, message, action, use_markup, foreground) ));
current_tooltip = tips.end();
return id;
}
void process(int mousex, int mousey)
{
for(std::vector<tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
if(mousex > i->rect.x && mousey > i->rect.y &&
mousex < i->rect.x + i->rect.w && mousey < i->rect.y + i->rect.h) {
for(std::map<int, tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
if(mousex > i->second.rect.x && mousey > i->second.rect.y &&
mousex < i->second.rect.x + i->second.rect.w && mousey < i->second.rect.y + i->second.rect.h) {
if(current_tooltip != i) {
show_tooltip(*i);
show_tooltip(i->second);
current_tooltip = i;
}
@ -177,10 +216,10 @@ void process(int mousex, int mousey)
bool click(int mousex, int mousey)
{
BOOST_FOREACH(tooltip tip, tips) {
if(!tip.action.empty() && sdl::point_in_rect(mousex, mousey, tip.rect)) {
for(std::map<int, tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
if(!i->second.action.empty() && sdl::point_in_rect(mousex, mousey, i->second.rect)) {
display* disp = resources::screen;
help::show_help(*disp, tip.action);
help::show_help(*disp, i->second.action);
return true;
}
}

View file

@ -31,7 +31,12 @@ struct manager
void clear_tooltips();
void clear_tooltips(const SDL_Rect& rect);
void add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action ="", bool use_markup = true, const surface& foreground = surface(NULL));
int add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action ="", bool use_markup = true, const surface& foreground = surface(NULL));
bool update_tooltip(int id, const SDL_Rect& rect, const std::string& message,
const std::string& action, bool use_markup, const surface& foreground);
bool update_tooltip(int id, const SDL_Rect& rect, const std::string& message,
const std::string& action, bool use_markup);
void remove_tooltip(int id);
void process(int mousex, int mousey);
// Check if we clicked on a tooltip having an action.