Author: mordante
Date: Sun Mar  9 18:38:36 2008
New Revision: 24447

URL: http://svn.gna.org/viewcvs/wesnoth?rev=24447&view=rev
Log:
Add text tag for the canvas

Modified:
    trunk/src/gui/widgets/canvas.cpp
    trunk/src/gui/widgets/canvas.hpp

Modified: trunk/src/gui/widgets/canvas.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/canvas.cpp?rev=24447&r1=24446&r2=24447&view=diff
==============================================================================
--- trunk/src/gui/widgets/canvas.cpp (original)
+++ trunk/src/gui/widgets/canvas.cpp Sun Mar  9 18:38:36 2008
@@ -18,12 +18,14 @@
 #include "gui/widgets/canvas.hpp"
 
 #include "config.hpp"
+#include "font.hpp"
 #include "image.hpp"
 #include "log.hpp"
 #include "serialization/parser.hpp"
 #include "variable.hpp"
 
 #include <algorithm>
+#include <cassert>
 
 #define DBG_GUI LOG_STREAM(debug, widget)
 #define LOG_GUI LOG_STREAM(info, widget)
@@ -134,8 +136,11 @@
                        shapes_.push_back(new trectangle(data));
                } else if(type == "image") {
                        shapes_.push_back(new timage(data));
+               } else if(type == "text") {
+                       shapes_.push_back(new ttext(data));
                } else {
-                       std::cerr << "Type of shape is unknown : " << type << 
'\n';
+                       ERR_GUI << "Type of shape is unknown : " << type << 
'\n';
+                       assert(false); // FIXME remove in production code.
                }
        }
 }
@@ -420,6 +425,7 @@
        }
 
 }
+
 void tcanvas::timage::draw(surface& canvas)
 {
        SDL_Rect src_clip = src_clip_;
@@ -427,4 +433,58 @@
        SDL_BlitSurface(image_, &src_clip, canvas, &dst_clip);
 }
 
+tcanvas::ttext::ttext(const vconfig& cfg) :
+       x_(lexical_cast_default<unsigned>(cfg["x"])),
+       y_(lexical_cast_default<unsigned>(cfg["y"])),
+       w_(lexical_cast_default<unsigned>(cfg["w"])),
+       h_(lexical_cast_default<unsigned>(cfg["h"])),
+       font_size_(lexical_cast_default<unsigned>(cfg["font_size"])),
+       colour_(decode_colour(cfg["colour"])),
+       text_(cfg["text"])
+{      
+/*WIKI
+ * [text]
+ *     x, y = (unsigned = 0), (unsigned = 0)    
+ *                                    The top left corner of the bounding
+ *                                    rectangle.
+ *     w = (unsigned = 0)             The width of the bounding rectangle.
+ *     h = (unsigned = 0)             The height of the bounding rectangle.
+ *     font_size = (unsigned = 0)     The size of the font.
+ *     colour = (widget.colour = "")  The colour of the text.
+ *     text = (t_string = "")         The text to print, for now always printed
+ *                                    centered in the area.
+ *     debug = (string = "")          Debug message to show upon creation
+ *                                    this message is not stored.
+ * [/rectangle]
+ */
+
+       const std::string& debug = (cfg["debug"]);
+       if(!debug.empty()) {
+               DBG_GUI << debug << '\n';
+       }
+
+}
+
+void tcanvas::ttext::draw(surface& canvas)
+{
+       SDL_Color col = { (colour_ >> 24), (colour_ >> 16), (colour_ >> 8), 
colour_ };
+       surface surf(font::get_rendered_text(text_, font_size_, col, 
TTF_STYLE_NORMAL));
+
+       if(surf->w > w_) {
+               WRN_GUI << "Text to wide, will be clipped.\n";
+       }
+       
+       if(surf->h > h_) {
+               WRN_GUI << "Text to high, will be clipped.\n";
+       }
+       
+       unsigned x_off = (surf->w >= w_) ? 0 : ((w_ - surf->w) / 2);
+       unsigned y_off = (surf->h >= h_) ? 0 : ((h_ - surf->h) / 2);
+       unsigned w_max = w_ - x_ - x_off;
+       unsigned h_max = h_ - y_ - y_off;
+
+       SDL_Rect dst = { x_ + x_off, y_ + y_off, w_max, h_max };
+       SDL_BlitSurface(surf, 0, canvas, &dst);
+}
+
 } // namespace gui2

Modified: trunk/src/gui/widgets/canvas.hpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/gui/widgets/canvas.hpp?rev=24447&r1=24446&r2=24447&view=diff
==============================================================================
--- trunk/src/gui/widgets/canvas.hpp (original)
+++ trunk/src/gui/widgets/canvas.hpp Sun Mar  9 18:38:36 2008
@@ -22,6 +22,7 @@
 #define __GUI_WIDGETS_CANVAS_HPP_INCLUDED__
 
 #include "sdl_utils.hpp"
+#include "tstring.hpp"
 
 #include <vector>
 
@@ -116,6 +117,22 @@
                surface image_;
        };
 
+       //! Definition of a text shape.
+       class ttext : public tshape
+       {
+       public:
+               ttext(const vconfig& cfg);
+               
+               //! Implement shape::draw().
+               void draw(surface& canvas);
+       private:
+               unsigned x_, y_;
+               unsigned w_, h_;
+               unsigned font_size_;
+               Uint32 colour_;
+               t_string text_;
+       };
+
        tcanvas();
        tcanvas(const config& cfg);
        ~tcanvas();


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to