gui/auxiliary/canvas.hpp

Go to the documentation of this file.
00001 /* $Id: canvas.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2007 - 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 /**
00017  * @file
00018  * This file contains the canvas object which is the part where the widgets
00019  * draw (tempory) images on.
00020  */
00021 
00022 #ifndef GUI_AUXILIARY_CANVAS_HPP_INCLUDED
00023 #define GUI_AUXILIARY_CANVAS_HPP_INCLUDED
00024 
00025 #include "formula_callable.hpp"
00026 #include "sdl_utils.hpp"
00027 
00028 class config;
00029 class variant;
00030 
00031 namespace gui2 {
00032 
00033 /**
00034  * A simple canvas which can be drawn upon.
00035  *
00036  * The class has a config which contains what to draw.
00037  *
00038  * NOTE we might add some caching in a later state, for now every draw cycle
00039  * does a full redraw.
00040  *
00041  * The copy constructor does a shallow copy of the shapes to draw.
00042  * a clone() will be implemented if really needed.
00043  */
00044 class tcanvas
00045 {
00046 public:
00047 
00048     /**
00049      * Abstract base class for all other shapes.
00050      *
00051      * The other shapes are declared and defined in canvas.cpp, since the
00052      * implementation details are not interesting for users of the canvas.
00053      */
00054     class tshape : public reference_counted_object
00055     {
00056     public:
00057         virtual ~tshape() {}
00058 
00059         /**
00060          * Draws the canvas.
00061          *
00062          * @param canvas          The resulting image will be blitted upon this
00063          *                        canvas.
00064          * @param variables       The canvas can have formulas in it's
00065          *                        definition, this parameter contains the values
00066          *                        for these formulas.
00067          */
00068         virtual void draw(surface& canvas
00069                 , const game_logic::map_formula_callable& variables) = 0;
00070     };
00071 
00072     typedef boost::intrusive_ptr<tshape> tshape_ptr;
00073     typedef boost::intrusive_ptr<const tshape> const_tshape_ptr;
00074 
00075     tcanvas();
00076 
00077     /**
00078      * Draws the canvas.
00079      *
00080      * @param force               If the canvas isn't dirty it isn't redrawn
00081      *                            unless force is set to true.
00082      */
00083     void draw(const bool force = false);
00084 
00085     /**
00086      * Blits the canvas unto another surface.
00087      *
00088      * It makes sure the image on the canvas is up to date. Also executes the
00089      * pre-blitting functions.
00090      *
00091      * @param surf                The surface to blit upon.
00092      * @param rect                The place to blit to.
00093      */
00094     void blit(surface& surf, SDL_Rect rect);
00095 
00096     /**
00097      * Sets the config.
00098      *
00099      * @param cfg                 The config object with the data to draw, see
00100      *                            http://www.wesnoth.org/wiki/GUICanvasWML for
00101      *                            more information.
00102      */
00103     void set_cfg(const config& cfg) { parse_cfg(cfg); }
00104 
00105     /***** ***** ***** setters / getters for members ***** ****** *****/
00106 
00107     void set_width(const unsigned width) { w_ = width; set_dirty(); }
00108     unsigned get_width() const { return w_; }
00109 
00110     void set_height(const unsigned height) { h_ = height; set_dirty(); }
00111     unsigned get_height() const { return h_; }
00112 
00113     surface& surf() { return canvas_; }
00114 
00115     void set_variable(const std::string& key, const variant& value)
00116     {
00117         variables_.add(key, value);
00118         set_dirty();
00119     }
00120 
00121 private:
00122     /** Vector with the shapes to draw. */
00123     std::vector<tshape_ptr> shapes_;
00124 
00125     /**
00126      * The depth of the blur to use in the pre committing.
00127      *
00128      * @note at the moment there's one pre commit function, namely the
00129      * blurring so use a variable here, might get more functions in the
00130      * future. When that happens need to evaluate whether variables are the
00131      * best thing to use.
00132      */
00133     unsigned blur_depth_;
00134 
00135     /** Width of the canvas. */
00136     unsigned w_;
00137 
00138     /** Height of the canvas. */
00139     unsigned h_;
00140 
00141     /** The surface we draw all items on. */
00142     surface canvas_;
00143 
00144     /** The variables of the canvas. */
00145     game_logic::map_formula_callable variables_;
00146 
00147     /** The dirty state of the canvas. */
00148     bool dirty_;
00149 
00150     void set_dirty(const bool dirty = true) { dirty_ = dirty; }
00151 
00152     /**
00153      * Parses a config object.
00154      *
00155      * The config object is parsed and serialized by this function after which
00156      * the config object is no longer required and thus not stored in the
00157      * object.
00158      *
00159      * @param cfg                 The config object with the data to draw, see
00160      *                            http://www.wesnoth.org/wiki/GUICanvasWML
00161      */
00162     void parse_cfg(const config& cfg);
00163 };
00164 
00165 } // namespace gui2
00166 
00167 #endif
00168 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Wed May 23 2012 01:02:40 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs