Allow to associate a help page to each tooltip (open when clicking).
This commit is contained in:
parent
ef72ecf413
commit
992b6f0e3c
5 changed files with 55 additions and 28 deletions
|
@ -2231,10 +2231,11 @@ void display::refresh_report(reports::TYPE report_num, reports::report report)
|
|||
skip_element:
|
||||
if (!e->tooltip.empty()) {
|
||||
if (!used_ellipsis) {
|
||||
tooltips::add_tooltip(area, e->tooltip);
|
||||
tooltips::add_tooltip(area, e->tooltip, e->action);
|
||||
} else {
|
||||
// Collect all tooltips for the ellipsis.
|
||||
// TODO: need a better separator
|
||||
// TODO: assign an action
|
||||
ellipsis_tooltip << e->tooltip;
|
||||
if(e+1 != report.end())
|
||||
ellipsis_tooltip << "\n _________\n\n";
|
||||
|
|
|
@ -233,6 +233,9 @@ bool mouse_handler_base::right_click_show_menu(int /*x*/, int /*y*/, const bool
|
|||
|
||||
bool mouse_handler_base::left_click(int x, int y, const bool /*browse*/)
|
||||
{
|
||||
if(tooltips::click(x,y))
|
||||
return true;
|
||||
|
||||
// clicked on a hex on the minimap? then initiate minimap scrolling
|
||||
const map_location& loc = gui().minimap_location_on(x, y);
|
||||
minimap_scrolling_ = false;
|
||||
|
|
|
@ -45,7 +45,8 @@ namespace reports {
|
|||
explicit element(const std::string& text) :
|
||||
image(),
|
||||
text(text),
|
||||
tooltip()
|
||||
tooltip(),
|
||||
action()
|
||||
{}
|
||||
|
||||
// Invariant: either text or image should be empty
|
||||
|
@ -55,30 +56,34 @@ namespace reports {
|
|||
std::string text;
|
||||
|
||||
std::string tooltip;
|
||||
element(const std::string& text, const std::string& image, const std::string& tooltip) :
|
||||
image(image), text(text), tooltip(tooltip) {}
|
||||
std::string action;
|
||||
element(const std::string& text, const std::string& image,
|
||||
const std::string& tooltip, const std::string& action="") :
|
||||
image(image), text(text), tooltip(tooltip), action(action) {}
|
||||
|
||||
element(const std::string& text, const image::locator& image, const std::string& tooltip) :
|
||||
image(image), text(text), tooltip(tooltip) {}
|
||||
element(const std::string& text, const char* image, const std::string& tooltip) :
|
||||
image(image), text(text), tooltip(tooltip) {}
|
||||
element(const std::string& text, const image::locator& image,
|
||||
const std::string& tooltip, const std::string& action="") :
|
||||
image(image), text(text), tooltip(tooltip), action(action) {}
|
||||
element(const std::string& text, const char* image,
|
||||
const std::string& tooltip, const std::string& action="") :
|
||||
image(image), text(text), tooltip(tooltip), action(action) {}
|
||||
|
||||
bool operator==(const element& o) const {
|
||||
return o.text == text && o.image == image && o.tooltip == tooltip;
|
||||
return o.text == text && o.image == image && o.tooltip == tooltip && o.action == action;
|
||||
}
|
||||
bool operator!=(const element& o) const { return !(o == *this); }
|
||||
};
|
||||
struct report : public std::vector<element> {
|
||||
report() {}
|
||||
explicit report(const std::string& text) { this->push_back(element(text)); }
|
||||
report(const std::string& text, const std::string& image, const std::string& tooltip) {
|
||||
this->push_back(element(text, image, tooltip));
|
||||
report(const std::string& text, const std::string& image, const std::string& tooltip, const std::string& action="") {
|
||||
this->push_back(element(text, image, tooltip, action));
|
||||
}
|
||||
report(const std::string& text, const char* image, const std::string& tooltip) {
|
||||
this->push_back(element(text, image, tooltip));
|
||||
report(const std::string& text, const char* image, const std::string& tooltip, const std::string& action="") {
|
||||
this->push_back(element(text, image, tooltip, action));
|
||||
}
|
||||
report(const std::string& text, const image::locator& image, const std::string& tooltip) {
|
||||
this->push_back(element(text, image, tooltip));
|
||||
report(const std::string& text, const image::locator& image, const std::string& tooltip, const std::string& action="") {
|
||||
this->push_back(element(text, image, tooltip, action));
|
||||
}
|
||||
|
||||
// Convenience functions
|
||||
|
|
|
@ -11,16 +11,18 @@
|
|||
|
||||
See the COPYING file for more details.
|
||||
*/
|
||||
|
||||
#include "global.hpp"
|
||||
|
||||
#include "font.hpp"
|
||||
#include "marked-up_text.hpp"
|
||||
#include "tooltips.hpp"
|
||||
|
||||
#include "font.hpp"
|
||||
#include "foreach.hpp"
|
||||
#include "game_display.hpp"
|
||||
#include "help.hpp"
|
||||
#include "marked-up_text.hpp"
|
||||
#include "resources.hpp"
|
||||
#include "video.hpp"
|
||||
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
CVideo* video_ = NULL;
|
||||
|
@ -34,11 +36,12 @@ static const int text_width = 400;
|
|||
|
||||
struct tooltip
|
||||
{
|
||||
tooltip(const SDL_Rect& r, const std::string& msg)
|
||||
: rect(r), message(msg)
|
||||
tooltip(const SDL_Rect& r, const std::string& msg, const std::string& act = "")
|
||||
: rect(r), message(msg), action(act)
|
||||
{}
|
||||
SDL_Rect rect;
|
||||
std::string message;
|
||||
std::string action;
|
||||
};
|
||||
|
||||
std::vector<tooltip> tips;
|
||||
|
@ -141,16 +144,16 @@ void clear_tooltips(const SDL_Rect& rect)
|
|||
}
|
||||
}
|
||||
|
||||
void add_tooltip(const SDL_Rect& rect, const std::string& message)
|
||||
void add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action)
|
||||
{
|
||||
for(std::vector<tooltip>::iterator i = tips.begin(); i != tips.end(); ++i) {
|
||||
if(rects_overlap(i->rect,rect)) {
|
||||
*i = tooltip(rect, message);
|
||||
*i = tooltip(rect, message, action);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
tips.push_back(tooltip(rect,message));
|
||||
tips.push_back(tooltip(rect, message, action));
|
||||
current_tooltip = tips.end();
|
||||
}
|
||||
|
||||
|
@ -172,4 +175,16 @@ void process(int mousex, int mousey)
|
|||
current_tooltip = tips.end();
|
||||
}
|
||||
|
||||
bool click(int mousex, int mousey)
|
||||
{
|
||||
foreach(tooltip tip, tips) {
|
||||
if(!tip.action.empty() && point_in_rect(mousex, mousey, tip.rect)) {
|
||||
display* disp = resources::screen;
|
||||
help::show_help(*disp, tip.action);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,8 +19,6 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#include "font.hpp"
|
||||
|
||||
class CVideo;
|
||||
|
||||
namespace tooltips {
|
||||
|
@ -33,9 +31,14 @@ struct manager
|
|||
|
||||
void clear_tooltips();
|
||||
void clear_tooltips(const SDL_Rect& rect);
|
||||
void add_tooltip(const SDL_Rect& rect, const std::string& message);
|
||||
void add_tooltip(const SDL_Rect& rect, const std::string& message, const std::string& action ="");
|
||||
void process(int mousex, int mousey);
|
||||
|
||||
// Check if we clicked on a tooltip having an action.
|
||||
// If it is, then execute the action and return true
|
||||
// (only possible action are opening help page for the moment)
|
||||
bool click(int mousex, int mousey);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue