Use named parameter idiom for floating labels
This commit is contained in:
parent
0d982d4b52
commit
c35d05ce2d
10 changed files with 190 additions and 125 deletions
|
@ -914,9 +914,13 @@ void display::update_display()
|
|||
drawn_hexes_ = 0;
|
||||
invalidated_hexes_ = 0;
|
||||
|
||||
fps_handle_ = font::add_floating_label(stream.str(),12,
|
||||
benchmark ? font::BAD_COLOUR : font::NORMAL_COLOUR,
|
||||
10,100,0,0,-1,screen_area(),font::LEFT_ALIGN);
|
||||
font::floating_label flabel(stream.str());
|
||||
flabel.set_font_size(12);
|
||||
flabel.set_colour(benchmark ? font::BAD_COLOUR : font::NORMAL_COLOUR);
|
||||
flabel.set_position(10, 100);
|
||||
flabel.set_alignement(font::LEFT_ALIGN);
|
||||
|
||||
fps_handle_ = font::add_floating_label(flabel);
|
||||
}
|
||||
} else if(fps_handle_ != 0) {
|
||||
font::remove_floating_label(fps_handle_);
|
||||
|
@ -1164,7 +1168,13 @@ void display::set_diagnostic(const std::string& msg)
|
|||
}
|
||||
|
||||
if(msg != "") {
|
||||
diagnostic_label_ = font::add_floating_label(msg,font::SIZE_PLUS,font::YELLOW_COLOUR,300.0,50.0,0.0,0.0,-1,map_outside_area());
|
||||
font::floating_label flabel(msg);
|
||||
flabel.set_font_size(font::SIZE_PLUS);
|
||||
flabel.set_colour(font::YELLOW_COLOUR);
|
||||
flabel.set_position(300, 50);
|
||||
flabel.set_clip_rect(map_outside_area());
|
||||
|
||||
diagnostic_label_ = font::add_floating_label(flabel);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1292,14 +1302,14 @@ void display::enable_menu(const std::string& item, bool enable)
|
|||
|
||||
void display::announce(const std::string& message, const SDL_Color& colour)
|
||||
{
|
||||
font::add_floating_label(message,
|
||||
font::SIZE_XLARGE,
|
||||
colour,
|
||||
map_outside_area().w/2,
|
||||
map_outside_area().h/3,
|
||||
0.0,0.0,100,
|
||||
map_outside_area(),
|
||||
font::CENTER_ALIGN);
|
||||
font::floating_label flabel(message);
|
||||
flabel.set_font_size(font::SIZE_XLARGE);
|
||||
flabel.set_colour(colour);
|
||||
flabel.set_position(map_outside_area().w/2, map_outside_area().h/3);
|
||||
flabel.set_lifetime(100);
|
||||
flabel.set_clip_rect(map_outside_area());
|
||||
|
||||
font::add_floating_label(flabel);
|
||||
}
|
||||
|
||||
void display::draw_border(const map_location& loc, const int xpos, const int ypos)
|
||||
|
|
|
@ -65,8 +65,13 @@ namespace gui{
|
|||
if (label_ != 0)
|
||||
font::remove_floating_label(label_);
|
||||
|
||||
label_ = font::add_floating_label(label_string_,font::SIZE_NORMAL,
|
||||
font::YELLOW_COLOUR,area.x+border_size,ypos,0,0,-1, area,font::LEFT_ALIGN);
|
||||
font::floating_label flabel(label_string_);
|
||||
flabel.set_colour(font::YELLOW_COLOUR);
|
||||
flabel.set_position(area.x + border_size, ypos);
|
||||
flabel.set_alignement(font::LEFT_ALIGN);
|
||||
flabel.set_clip_rect(area);
|
||||
|
||||
label_ = font::add_floating_label(flabel);
|
||||
|
||||
if (label_ == 0)
|
||||
return;
|
||||
|
|
84
src/font.cpp
84
src/font.cpp
|
@ -911,60 +911,27 @@ std::string make_text_ellipsis(const std::string &text, int font_size,
|
|||
|
||||
}
|
||||
|
||||
//floating labels
|
||||
namespace {
|
||||
|
||||
class floating_label
|
||||
{
|
||||
public:
|
||||
floating_label(const std::string& text, int font_size, const SDL_Color& colour, const SDL_Color& bgcolour,
|
||||
double xpos, double ypos, double xmove, double ymove, int lifetime, const SDL_Rect& clip_rect,
|
||||
font::ALIGN align, int border_size, bool scroll_with_map, bool use_markup)
|
||||
: surf_(NULL), buf_(NULL), text_(text), font_size_(font_size), colour_(colour),
|
||||
bgcolour_(bgcolour), bgalpha_(bgcolour.unused), xpos_(xpos), ypos_(ypos),
|
||||
xmove_(xmove), ymove_(ymove), lifetime_(lifetime), clip_rect_(clip_rect),
|
||||
alpha_change_(-255 / lifetime), visible_(true), align_(align),
|
||||
border_(border_size), scroll_(scroll_with_map), use_markup_(use_markup)
|
||||
{}
|
||||
|
||||
void move(double xmove, double ymove);
|
||||
|
||||
void draw(surface screen);
|
||||
void undraw(surface screen);
|
||||
|
||||
surface create_surface();
|
||||
|
||||
bool expired() const { return lifetime_ == 0; }
|
||||
|
||||
|
||||
void show(const bool value) { visible_ = value; }
|
||||
|
||||
bool scroll() const { return scroll_; }
|
||||
|
||||
private:
|
||||
|
||||
int xpos(size_t width) const;
|
||||
|
||||
surface surf_, buf_;
|
||||
std::string text_;
|
||||
int font_size_;
|
||||
SDL_Color colour_, bgcolour_;
|
||||
int bgalpha_;
|
||||
double xpos_, ypos_, xmove_, ymove_;
|
||||
int lifetime_;
|
||||
SDL_Rect clip_rect_;
|
||||
int alpha_change_;
|
||||
bool visible_;
|
||||
font::ALIGN align_;
|
||||
int border_;
|
||||
bool scroll_, use_markup_;
|
||||
};
|
||||
|
||||
typedef std::map<int,floating_label> label_map;
|
||||
typedef std::map<int, font::floating_label> label_map;
|
||||
label_map labels;
|
||||
int label_id = 1;
|
||||
|
||||
std::stack<std::set<int> > label_contexts;
|
||||
}
|
||||
|
||||
|
||||
namespace font {
|
||||
|
||||
floating_label::floating_label(const std::string& text)
|
||||
: surf_(NULL), buf_(NULL), text_(text),
|
||||
font_size_(SIZE_NORMAL),
|
||||
colour_(NORMAL_COLOUR), bgcolour_(), bgalpha_(0),
|
||||
xpos_(0), ypos_(0),
|
||||
xmove_(0), ymove_(0), lifetime_(-1), clip_rect_(screen_area()),
|
||||
alpha_change_(0), visible_(true), align_(CENTER_ALIGN),
|
||||
border_(0), scroll_(ANCHOR_LABEL_SCREEN), use_markup_(true)
|
||||
{}
|
||||
|
||||
void floating_label::move(double xmove, double ymove)
|
||||
{
|
||||
|
@ -1108,31 +1075,14 @@ void floating_label::undraw(surface screen)
|
|||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace font {
|
||||
int add_floating_label(const std::string& text, int font_size, const SDL_Color& colour,
|
||||
double xpos, double ypos, double xmove, double ymove, int lifetime, const SDL_Rect& clip_rect, ALIGN align,
|
||||
const SDL_Color *bg_colour, int border_size, LABEL_SCROLL_MODE scroll_mode,
|
||||
bool use_markup)
|
||||
int add_floating_label(const floating_label& flabel)
|
||||
{
|
||||
if(label_contexts.empty()) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(lifetime <= 0) {
|
||||
lifetime = -1;
|
||||
}
|
||||
|
||||
SDL_Color bg = {0,0,0,0};
|
||||
if(bg_colour != NULL) {
|
||||
bg = *bg_colour;
|
||||
}
|
||||
|
||||
++label_id;
|
||||
labels.insert(std::pair<int, floating_label>(label_id, floating_label(
|
||||
text, font_size, colour, bg, xpos, ypos, xmove, ymove, lifetime, clip_rect,
|
||||
align, border_size, scroll_mode == ANCHOR_LABEL_MAP, use_markup)));
|
||||
labels.insert(std::pair<int, floating_label>(label_id, flabel));
|
||||
label_contexts.top().insert(label_id);
|
||||
return label_id;
|
||||
}
|
||||
|
|
80
src/font.hpp
80
src/font.hpp
|
@ -129,22 +129,74 @@ enum ALIGN { LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN };
|
|||
|
||||
enum LABEL_SCROLL_MODE { ANCHOR_LABEL_SCREEN, ANCHOR_LABEL_MAP };
|
||||
|
||||
class floating_label
|
||||
{
|
||||
public:
|
||||
floating_label(const std::string& text);
|
||||
|
||||
void set_font_size(int font_size) {font_size_ = font_size;}
|
||||
|
||||
// set the location on the screen to display the text.
|
||||
void set_position(double xpos, double ypos){
|
||||
xpos_ = xpos;
|
||||
ypos_ = ypos;
|
||||
}
|
||||
// set the amount to move the text each frame
|
||||
void set_move(double xmove, double ymove){
|
||||
xmove_ = xmove;
|
||||
ymove_ = ymove;
|
||||
}
|
||||
// set the number of frames to display the text for, or -1 to display until removed
|
||||
void set_lifetime(int lifetime) {lifetime_ = lifetime;}
|
||||
void set_colour(const SDL_Color& colour) {colour_ = colour;}
|
||||
void set_bg_colour(const SDL_Color& bg_colour) {
|
||||
bgcolour_ = bg_colour;
|
||||
bgalpha_ = bg_colour.unused;
|
||||
}
|
||||
void set_border_size(int border) {border_ = border;}
|
||||
void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
|
||||
void set_alignement(ALIGN align) {align_ = align;}
|
||||
void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
|
||||
void use_markup(bool b) {use_markup_ = b;}
|
||||
|
||||
void move(double xmove, double ymove);
|
||||
|
||||
void draw(surface screen);
|
||||
void undraw(surface screen);
|
||||
|
||||
surface create_surface();
|
||||
|
||||
bool expired() const { return lifetime_ == 0; }
|
||||
|
||||
void show(const bool value) { visible_ = value; }
|
||||
|
||||
bool scroll() const { return scroll_; }
|
||||
|
||||
private:
|
||||
|
||||
int xpos(size_t width) const;
|
||||
|
||||
surface surf_, buf_;
|
||||
std::string text_;
|
||||
int font_size_;
|
||||
SDL_Color colour_, bgcolour_;
|
||||
int bgalpha_;
|
||||
double xpos_, ypos_, xmove_, ymove_;
|
||||
int lifetime_;
|
||||
SDL_Rect clip_rect_;
|
||||
int alpha_change_;
|
||||
bool visible_;
|
||||
font::ALIGN align_;
|
||||
int border_;
|
||||
bool scroll_, use_markup_;
|
||||
};
|
||||
|
||||
|
||||
/// add a label floating on the screen above everything else.
|
||||
/// 'text': the text to display
|
||||
/// 'font_size': the size to display the text in
|
||||
/// 'colour': the colour of the text
|
||||
/// 'xpos,ypos': the location on the screen to display the text.
|
||||
/// 'xmove,ymove': the amount to move the text each frame
|
||||
/// 'lifetime': the number of frames to display the text for, or -1 to display until removed
|
||||
/// 'clip_rect': the rectangle to clip the label to.
|
||||
///
|
||||
/// @returns a handle to the label which can be used with other label functions
|
||||
int add_floating_label(const std::string& text, int font_size, const SDL_Color& colour,
|
||||
double xpos, double ypos, double xmove, double ymove, int lifetime,
|
||||
const SDL_Rect& clip_rect, ALIGN alignment=CENTER_ALIGN,
|
||||
const SDL_Color* bg_colour=NULL, int border_size=0,
|
||||
LABEL_SCROLL_MODE scroll_mode = ANCHOR_LABEL_SCREEN,
|
||||
bool use_markup = true);
|
||||
|
||||
int add_floating_label(const floating_label& flabel);
|
||||
|
||||
|
||||
/// moves the floating label given by 'handle' by (xmove,ymove)
|
||||
void move_floating_label(int handle, double xmove, double ymove);
|
||||
|
|
|
@ -843,8 +843,17 @@ void game_display::float_label(const map_location& loc, const std::string& text,
|
|||
|
||||
const SDL_Color color = {red,green,blue,255};
|
||||
int lifetime = static_cast<int>(60/turbo_speed());
|
||||
font::add_floating_label(text,font::SIZE_XLARGE,color,get_location_x(loc)+zoom_/2,get_location_y(loc),
|
||||
0,-2*turbo_speed(),lifetime,screen_area(),font::CENTER_ALIGN,NULL,0,font::ANCHOR_LABEL_MAP);
|
||||
/* font::floating_label flabel(text,font::SIZE_XLARGE,color,get_location_x(loc)+zoom_/2,get_location_y(loc),
|
||||
0,-2*turbo_speed(),lifetime,screen_area(),font::CENTER_ALIGN,NULL,0,font::ANCHOR_LABEL_MAP);*/
|
||||
font::floating_label flabel(text);
|
||||
flabel.set_font_size(font::SIZE_XLARGE);
|
||||
flabel.set_colour(color);
|
||||
flabel.set_position(get_location_x(loc)+zoom_/2, get_location_y(loc));
|
||||
flabel.set_move(0, -2 * turbo_speed());
|
||||
flabel.set_lifetime(lifetime);
|
||||
flabel.set_scroll_mode(font::ANCHOR_LABEL_MAP);
|
||||
|
||||
font::add_floating_label(flabel);
|
||||
}
|
||||
|
||||
struct is_energy_colour {
|
||||
|
@ -1374,16 +1383,31 @@ void game_display::add_chat_message(const time_t& time, const std::string& speak
|
|||
message_complete << preferences::get_chat_timestamp(time) << str.str();
|
||||
|
||||
const SDL_Rect rect = map_outside_area();
|
||||
int speaker_handle = font::add_floating_label(message_complete.str(),
|
||||
font::SIZE_SMALL, speaker_colour, rect.x + chat_message_x,
|
||||
rect.y + ypos, 0, 0, -1, rect, font::LEFT_ALIGN, &chat_message_bg,
|
||||
chat_message_border, font::ANCHOR_LABEL_SCREEN, false);
|
||||
|
||||
int message_handle = font::add_floating_label(message_str.str(),
|
||||
font::SIZE_SMALL, message_colour,
|
||||
rect.x + chat_message_x + font::get_floating_label_rect(speaker_handle).w,
|
||||
rect.y + ypos, 0, 0, -1, rect, font::LEFT_ALIGN, &chat_message_bg,
|
||||
chat_message_border, font::ANCHOR_LABEL_SCREEN, false);
|
||||
font::floating_label spk_flabel(message_complete.str());
|
||||
spk_flabel.set_font_size(font::SIZE_SMALL);
|
||||
spk_flabel.set_colour(speaker_colour);
|
||||
spk_flabel.set_position(rect.x + chat_message_x, rect.y + ypos);
|
||||
spk_flabel.set_clip_rect(rect);
|
||||
spk_flabel.set_alignement(font::LEFT_ALIGN);
|
||||
spk_flabel.set_bg_colour(chat_message_bg);
|
||||
spk_flabel.set_border_size(chat_message_border);
|
||||
spk_flabel.use_markup(false);
|
||||
|
||||
int speaker_handle = font::add_floating_label(spk_flabel);
|
||||
|
||||
font::floating_label msg_flabel(message_str.str());
|
||||
msg_flabel.set_font_size(font::SIZE_SMALL);
|
||||
msg_flabel.set_colour(message_colour);
|
||||
msg_flabel.set_position(rect.x + chat_message_x + font::get_floating_label_rect(speaker_handle).w,
|
||||
rect.y + ypos);
|
||||
msg_flabel.set_clip_rect(rect);
|
||||
msg_flabel.set_alignement(font::LEFT_ALIGN);
|
||||
msg_flabel.set_bg_colour(chat_message_bg);
|
||||
msg_flabel.set_border_size(chat_message_border);
|
||||
msg_flabel.use_markup(false);
|
||||
|
||||
int message_handle = font::add_floating_label(msg_flabel);
|
||||
|
||||
// Send system notification if appropriate.
|
||||
send_notification(speaker, message);
|
||||
|
|
|
@ -1988,8 +1988,15 @@ WML_HANDLER_FUNCTION(print, /*event_info*/, cfg)
|
|||
const std::string& msg = text;
|
||||
if(msg != "") {
|
||||
const SDL_Rect rect = resources::screen->map_outside_area();
|
||||
floating_label = font::add_floating_label(msg,size,colour,
|
||||
rect.w/2,rect.h/2,0.0,0.0,lifetime,rect,font::CENTER_ALIGN);
|
||||
|
||||
font::floating_label flabel(msg);
|
||||
flabel.set_font_size(size);
|
||||
flabel.set_colour(colour);
|
||||
flabel.set_position(rect.w/2,rect.h/2);
|
||||
flabel.set_lifetime(lifetime);
|
||||
flabel.set_clip_rect(rect);
|
||||
|
||||
floating_label = font::add_floating_label(flabel);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -442,18 +442,14 @@ void terrain_label::draw()
|
|||
// the scenario.
|
||||
bool use_markup = colour_ == font::LABEL_COLOUR;
|
||||
|
||||
handle_ = font::add_floating_label(text_,
|
||||
font::SIZE_NORMAL,
|
||||
colour_,
|
||||
xloc, yloc,
|
||||
0,0,
|
||||
-1,
|
||||
parent_->disp().map_outside_area(),
|
||||
font::CENTER_ALIGN,
|
||||
NULL, 0,
|
||||
font::ANCHOR_LABEL_MAP,
|
||||
use_markup
|
||||
);
|
||||
font::floating_label flabel(text_);
|
||||
flabel.set_colour(colour_);
|
||||
flabel.set_position(xloc, yloc);
|
||||
flabel.set_clip_rect(parent_->disp().map_outside_area());
|
||||
flabel.set_scroll_mode(font::ANCHOR_LABEL_MAP);
|
||||
flabel.use_markup(use_markup);
|
||||
|
||||
handle_ = font::add_floating_label(flabel);
|
||||
|
||||
calculate_shroud();
|
||||
}
|
||||
|
|
|
@ -232,8 +232,14 @@ void playmp_controller::play_human_turn(){
|
|||
SDL_Color colour = {255,255,255,255};
|
||||
|
||||
SDL_Rect rect = gui_->map_area();
|
||||
font::add_floating_label(msg,size, colour,
|
||||
rect.w/2,rect.h/2,0.0,0.0,lifetime,rect,font::CENTER_ALIGN);
|
||||
font::floating_label flabel(msg);
|
||||
flabel.set_font_size(size);
|
||||
flabel.set_colour(colour);
|
||||
flabel.set_position(rect.w/2, rect.h/2);
|
||||
flabel.set_lifetime(lifetime);
|
||||
flabel.set_clip_rect(rect);
|
||||
|
||||
font::add_floating_label(flabel);
|
||||
}
|
||||
|
||||
while(!undo_stack_.empty())
|
||||
|
|
|
@ -77,8 +77,16 @@ static void show_tooltip(const tooltip& tip)
|
|||
#endif
|
||||
|
||||
const std::string wrapped_message = font::word_wrap_text(tip.message, font_size, text_width);
|
||||
tooltip_handle = font::add_floating_label(wrapped_message,font_size,tip.color,
|
||||
0,0,0,0,-1,area,font::LEFT_ALIGN,&bgcolour,border);
|
||||
|
||||
font::floating_label flabel(wrapped_message);
|
||||
flabel.set_font_size(font_size);
|
||||
flabel.set_colour(tip.color);
|
||||
flabel.set_clip_rect(area);
|
||||
flabel.set_alignement(font::LEFT_ALIGN);
|
||||
flabel.set_bg_colour(bgcolour);
|
||||
flabel.set_border_size(border);
|
||||
|
||||
tooltip_handle = font::add_floating_label(flabel);
|
||||
|
||||
SDL_Rect rect = font::get_floating_label_rect(tooltip_handle);
|
||||
|
||||
|
|
|
@ -479,7 +479,14 @@ int CVideo::set_help_string(const std::string& str)
|
|||
const int border = 5;
|
||||
#endif
|
||||
|
||||
help_string_ = font::add_floating_label(str,size,font::NORMAL_COLOUR,getx()/2,gety(),0.0,0.0,-1,screen_area(),font::CENTER_ALIGN,&colour,border);
|
||||
font::floating_label flabel(str);
|
||||
flabel.set_font_size(size);
|
||||
flabel.set_position(getx()/2, gety());
|
||||
flabel.set_bg_colour(colour);
|
||||
flabel.set_border_size(border);
|
||||
|
||||
help_string_ = font::add_floating_label(flabel);
|
||||
|
||||
const SDL_Rect& rect = font::get_floating_label_rect(help_string_);
|
||||
font::move_floating_label(help_string_,0.0,-double(rect.h));
|
||||
return help_string_;
|
||||
|
|
Loading…
Add table
Reference in a new issue