gui/widgets/helper.cpp

Go to the documentation of this file.
00001 /* $Id: helper.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Mark de Wever <koraq@xs4all.nl>
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 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/widgets/helper.hpp"
00019 
00020 #include "gui/auxiliary/log.hpp"
00021 #include "gui/widgets/settings.hpp"
00022 
00023 #include "formula_string_utils.hpp"
00024 
00025 #include "SDL_ttf.h"
00026 
00027 namespace gui2 {
00028 
00029 namespace {
00030     static bool initialized_ = false;
00031 }
00032 
00033 bool init() {
00034     if(initialized_) {
00035         return true;
00036     }
00037 
00038     load_settings();
00039 
00040     initialized_ = true;
00041 
00042     return initialized_;
00043 }
00044 
00045 SDL_Rect create_rect(const tpoint& origin, const tpoint& size)
00046 {
00047     return ::create_rect(origin.x, origin.y, size.x, size.y);
00048 }
00049 
00050 tpoint& tpoint::operator+=(const tpoint& point)
00051 {
00052     x += point.x;
00053     y += point.y;
00054     return *this;
00055 }
00056 
00057 tpoint& tpoint::operator-=(const tpoint& point)
00058 {
00059     x -= point.x;
00060     y -= point.y;
00061     return *this;
00062 }
00063 
00064 std::ostream &operator<<(std::ostream &stream, const tpoint& point)
00065 {
00066     stream << point.x << ',' << point.y;
00067     return stream;
00068 }
00069 
00070 unsigned decode_font_style(const std::string& style)
00071 {
00072     if(style == "bold") {
00073         return TTF_STYLE_BOLD;
00074     } else if(style == "italic") {
00075         return TTF_STYLE_ITALIC;
00076     } else if(style == "underline") {
00077         return TTF_STYLE_UNDERLINE;
00078     } else if(style.empty() || style == "normal") {
00079         return TTF_STYLE_NORMAL;
00080     }
00081 
00082     ERR_GUI_G << "Unknown style '" << style << "' using 'normal' instead.\n";
00083 
00084     return TTF_STYLE_NORMAL;
00085 }
00086 
00087 Uint32 decode_color(const std::string& color)
00088 {
00089     std::vector<std::string> fields = utils::split(color);
00090 
00091     // make sure we have four fields
00092     while(fields.size() < 4) fields.push_back("0");
00093 
00094     Uint32 result = 0;
00095     for(int i = 0; i < 4; ++i) {
00096         // shift the previous value before adding, since it's a nop on the
00097         // first run there's no need for an if.
00098         result = result << 8;
00099         result |= lexical_cast_default<int>(fields[i]);
00100     }
00101 
00102     return result;
00103 }
00104 
00105 PangoAlignment decode_text_alignment(const std::string& alignment)
00106 {
00107     if(alignment == "center") {
00108         return PANGO_ALIGN_CENTER;
00109     } else if(alignment == "right") {
00110         return PANGO_ALIGN_RIGHT;
00111     } else {
00112         if(!alignment.empty() && alignment != "left") {
00113             ERR_GUI_E << "Invalid text alignment '"
00114                     << alignment << "' falling back to 'left'.\n";
00115         }
00116         return PANGO_ALIGN_LEFT;
00117     }
00118 }
00119 
00120 std::string encode_text_alignment(const PangoAlignment alignment)
00121 {
00122     switch(alignment) {
00123         case PANGO_ALIGN_LEFT:   return "left";
00124         case PANGO_ALIGN_RIGHT:  return "right";
00125         case PANGO_ALIGN_CENTER: return "center";
00126     }
00127     assert(false);
00128     //FIXME: without this "control reaches end of non-void function" in release mode
00129     throw "Control should not reach this point.";
00130 }
00131 
00132 t_string missing_widget(const std::string& id)
00133 {
00134     utils::string_map symbols;
00135     symbols["id"] = id;
00136 
00137     return t_string(vgettext("Mandatory widget '$id' hasn't been defined.", symbols));
00138 }
00139 
00140 void get_screen_size_variables(game_logic::map_formula_callable& variable)
00141 {
00142     variable.add("screen_width", variant(settings::screen_width));
00143     variable.add("screen_height", variant(settings::screen_height));
00144     variable.add("gamemap_width", variant(settings::gamemap_width));
00145     variable.add("gamemap_height", variant(settings::gamemap_height));
00146 }
00147 
00148 game_logic::map_formula_callable get_screen_size_variables()
00149 {
00150     game_logic::map_formula_callable result;
00151     get_screen_size_variables(result);
00152 
00153     return result;
00154 }
00155 
00156 tpoint get_mouse_position()
00157 {
00158     int x, y;
00159     SDL_GetMouseState(&x, &y);
00160 
00161     return tpoint(x, y);
00162 }
00163 
00164 std::string debug_truncate(const std::string& text)
00165 {
00166     return text.substr(0, 15);
00167 }
00168 
00169 } // namespace gui2
00170 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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