tooltips.cpp

Go to the documentation of this file.
00001 /* $Id: tooltips.cpp 53555 2012-03-18 14:57:32Z mordante $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 #include "global.hpp"
00016 
00017 #include "tooltips.hpp"
00018 
00019 #include "font.hpp"
00020 #include "foreach.hpp"
00021 #include "game_display.hpp"
00022 #include "help.hpp"
00023 #include "marked-up_text.hpp"
00024 #include "resources.hpp"
00025 #include "video.hpp"
00026 
00027 namespace {
00028 
00029 CVideo* video_ = NULL;
00030 
00031 static const int font_size = font::SIZE_SMALL;
00032 static const int text_width = 400;
00033 
00034 struct tooltip
00035 {
00036     tooltip(const SDL_Rect& r, const std::string& msg, const std::string& act = "", bool use_markup = false)
00037     : rect(r), message(msg), action(act), markup(use_markup)
00038     {}
00039     SDL_Rect rect;
00040     std::string message;
00041     std::string action;
00042     bool markup;
00043 };
00044 
00045 std::vector<tooltip> tips;
00046 std::vector<tooltip>::const_iterator current_tooltip = tips.end();
00047 
00048 int tooltip_handle = 0;
00049 
00050 surface current_background = NULL;
00051 
00052 }
00053 
00054 static void clear_tooltip()
00055 {
00056     if(tooltip_handle != 0) {
00057         font::remove_floating_label(tooltip_handle);
00058         tooltip_handle = 0;
00059     }
00060 }
00061 
00062 static void show_tooltip(const tooltip& tip)
00063 {
00064     if(video_ == NULL) {
00065         return;
00066     }
00067 
00068     clear_tooltip();
00069 
00070     const SDL_Color bgcolor = {0,0,0,160};
00071     SDL_Rect area = screen_area();
00072 
00073     unsigned int border = 10;
00074 
00075     font::floating_label flabel(tip.message);
00076     flabel.use_markup(tip.markup);
00077     flabel.set_font_size(font_size);
00078     flabel.set_color(font::NORMAL_COLOR);
00079     flabel.set_clip_rect(area);
00080     flabel.set_width(text_width);
00081     flabel.set_alignment(font::LEFT_ALIGN);
00082     flabel.set_bg_color(bgcolor);
00083     flabel.set_border_size(border);
00084 
00085     tooltip_handle = font::add_floating_label(flabel);
00086 
00087     SDL_Rect rect = font::get_floating_label_rect(tooltip_handle);
00088 
00089     //see if there is enough room to fit it above the tip area
00090     if(tip.rect.y > rect.h) {
00091         rect.y = tip.rect.y - rect.h;
00092     } else {
00093         rect.y = tip.rect.y + tip.rect.h;
00094     }
00095 
00096     rect.x = tip.rect.x;
00097     if(rect.x < 0) {
00098         rect.x = 0;
00099     } else if(rect.x + rect.w > area.w) {
00100         rect.x = area.w - rect.w;
00101     }
00102 
00103     font::move_floating_label(tooltip_handle,rect.x,rect.y);
00104 }
00105 
00106 namespace tooltips {
00107 
00108 manager::manager(CVideo& video)
00109 {
00110     clear_tooltips();
00111     video_ = &video;
00112 }
00113 
00114 manager::~manager()
00115 {
00116     clear_tooltips();
00117     video_ = NULL;
00118 }
00119 
00120 void clear_tooltips()
00121 {
00122     clear_tooltip();
00123     tips.clear();
00124     current_tooltip = tips.end();
00125 }
00126 
00127 void clear_tooltips(const SDL_Rect& rect)
00128 {
00129     for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ) {
00130         if(rects_overlap(i->rect,rect)) {
00131             if (i==current_tooltip) {
00132                 clear_tooltip();
00133             }
00134             i = tips.erase(i);
00135             current_tooltip = tips.end();
00136         } else {
00137             ++i;
00138         }
00139     }
00140 }
00141 
00142 void add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action, bool use_markup)
00143 {
00144     for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ++i) {
00145         if(rects_overlap(i->rect,rect)) {
00146             *i = tooltip(rect, message, action);
00147             return;
00148         }
00149     }
00150 
00151     tips.push_back(tooltip(rect, message, action, use_markup));
00152     current_tooltip = tips.end();
00153 }
00154 
00155 void process(int mousex, int mousey)
00156 {
00157     for(std::vector<tooltip>::const_iterator i = tips.begin(); i != tips.end(); ++i) {
00158         if(mousex > i->rect.x && mousey > i->rect.y &&
00159            mousex < i->rect.x + i->rect.w && mousey < i->rect.y + i->rect.h) {
00160             if(current_tooltip != i) {
00161                 show_tooltip(*i);
00162                 current_tooltip = i;
00163             }
00164 
00165             return;
00166         }
00167     }
00168 
00169     clear_tooltip();
00170     current_tooltip = tips.end();
00171 }
00172 
00173 bool click(int mousex, int mousey)
00174 {
00175     foreach(tooltip tip, tips) {
00176         if(!tip.action.empty() && point_in_rect(mousex, mousey, tip.rect)) {
00177             display* disp = resources::screen;
00178             help::show_help(*disp, tip.action);
00179             return true;
00180         }
00181     }
00182     return false;
00183 }
00184 
00185 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:13 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs