00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef FONT_HPP_INCLUDED
00016 #define FONT_HPP_INCLUDED
00017
00018 #include "exceptions.hpp"
00019 #include "SDL_ttf.h"
00020
00021 #include "sdl_utils.hpp"
00022
00023 class t_string;
00024
00025 namespace font {
00026
00027
00028 struct manager {
00029 manager();
00030 ~manager();
00031
00032
00033
00034
00035
00036
00037 void update_font_path() const;
00038
00039 struct error : public game::error {
00040 error() : game::error("Font initialization failed") {}
00041 };
00042 private:
00043
00044 void init() const;
00045
00046
00047 void deinit() const;
00048 };
00049
00050
00051 extern const SDL_Color NORMAL_COLOR, GRAY_COLOR, LOBBY_COLOR, GOOD_COLOR, BAD_COLOR,
00052 BLACK_COLOR, YELLOW_COLOR, BUTTON_COLOR, BIGMAP_COLOR,
00053 PETRIFIED_COLOR, TITLE_COLOR, DISABLED_COLOR, LABEL_COLOR;
00054
00055
00056 const int SIZE_NORMAL = 14;
00057 inline int relative_size(int size)
00058 {
00059 return (SIZE_NORMAL * size / 14);
00060 }
00061
00062
00063 const int
00064 SIZE_TINY = relative_size(10),
00065 SIZE_SMALL = relative_size(12),
00066
00067 SIZE_15 = relative_size(15),
00068 SIZE_PLUS = relative_size(16),
00069 SIZE_LARGE = relative_size(18),
00070 SIZE_XLARGE = relative_size(24)
00071 ;
00072
00073
00074 surface get_rendered_text(const std::string& text, int size, const SDL_Color& color, int style=0);
00075
00076 SDL_Rect draw_text_line(surface gui_surface, const SDL_Rect& area, int size,
00077 const SDL_Color& color, const std::string& text,
00078 int x, int y, bool use_tooltips, int style);
00079
00080
00081 int get_max_height(int size);
00082
00083
00084
00085
00086
00087 int line_width(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
00088
00089
00090
00091
00092
00093 SDL_Rect line_size(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
00094
00095
00096
00097
00098 std::string make_text_ellipsis(const std::string& text, int font_size, int max_width,
00099 int style = TTF_STYLE_NORMAL);
00100
00101
00102
00103
00104 struct floating_label_context
00105 {
00106 floating_label_context();
00107 ~floating_label_context();
00108 };
00109
00110 enum ALIGN { LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN };
00111
00112 enum LABEL_SCROLL_MODE { ANCHOR_LABEL_SCREEN, ANCHOR_LABEL_MAP };
00113
00114 class floating_label
00115 {
00116 public:
00117 floating_label(const std::string& text);
00118
00119 void set_font_size(int font_size) {font_size_ = font_size;}
00120
00121
00122 void set_position(double xpos, double ypos){
00123 xpos_ = xpos;
00124 ypos_ = ypos;
00125 }
00126
00127 void set_move(double xmove, double ymove){
00128 xmove_ = xmove;
00129 ymove_ = ymove;
00130 }
00131
00132 void set_lifetime(int lifetime) {
00133 lifetime_ = lifetime;
00134 alpha_change_ = -255 / lifetime_;
00135 }
00136 void set_color(const SDL_Color& color) {color_ = color;}
00137 void set_bg_color(const SDL_Color& bg_color) {
00138 bgcolor_ = bg_color;
00139 bgalpha_ = bg_color.unused;
00140 }
00141 void set_border_size(int border) {border_ = border;}
00142
00143 void set_width(int w) {width_ = w;}
00144 void set_height(int h) { height_ = h; }
00145 void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
00146 void set_alignment(ALIGN align) {align_ = align;}
00147 void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
00148 void use_markup(bool b) {use_markup_ = b;}
00149
00150 void move(double xmove, double ymove);
00151
00152 void draw(surface screen);
00153 void undraw(surface screen);
00154
00155 surface create_surface();
00156
00157 bool expired() const { return lifetime_ == 0; }
00158
00159 void show(const bool value) { visible_ = value; }
00160
00161 LABEL_SCROLL_MODE scroll() const { return scroll_; }
00162
00163 private:
00164
00165 int xpos(size_t width) const;
00166
00167 surface surf_, buf_;
00168 std::string text_;
00169 int font_size_;
00170 SDL_Color color_, bgcolor_;
00171 int bgalpha_;
00172 double xpos_, ypos_, xmove_, ymove_;
00173 int lifetime_;
00174 int width_, height_;
00175 SDL_Rect clip_rect_;
00176 int alpha_change_;
00177 bool visible_;
00178 font::ALIGN align_;
00179 int border_;
00180 LABEL_SCROLL_MODE scroll_;
00181 bool use_markup_;
00182 };
00183
00184
00185
00186
00187
00188 int add_floating_label(const floating_label& flabel);
00189
00190
00191
00192 void move_floating_label(int handle, double xmove, double ymove);
00193
00194
00195 void scroll_floating_labels(double xmove, double ymove);
00196
00197
00198 void remove_floating_label(int handle);
00199
00200
00201 void show_floating_label(int handle, bool show);
00202
00203 SDL_Rect get_floating_label_rect(int handle);
00204
00205 void draw_floating_labels(surface screen);
00206 void undraw_floating_labels(surface screen);
00207
00208 bool load_font_config();
00209
00210
00211 const t_string& get_font_families();
00212
00213 enum CACHE { CACHE_LOBBY, CACHE_GAME };
00214 void cache_mode(CACHE mode);
00215
00216 }
00217
00218 #endif