p_rect.cpp

Go to the documentation of this file.
00001 /*
00002  * p_rect.cpp
00003  *
00004  * Copyright (C) 2007,2009  Thomas A. Vaughan
00005  * All rights reserved.
00006  *
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *     * Redistributions of source code must retain the above copyright
00011  *       notice, this list of conditions and the following disclaimer.
00012  *     * Redistributions in binary form must reproduce the above copyright
00013  *       notice, this list of conditions and the following disclaimer in the
00014  *       documentation and/or other materials provided with the distribution.
00015  *     * Neither the name of the <organization> nor the
00016  *       names of its contributors may be used to endorse or promote products
00017  *       derived from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THOMAS A. VAUGHAN ''AS IS'' AND ANY
00020  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00021  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THOMAS A. VAUGHAN BE LIABLE FOR ANY
00023  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00024  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00026  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00028  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  *
00031  * Implementation of a rect primitive.
00032  * (p_ stands for Primitive)
00033  */
00034 
00035 // includes --------------------------------------------------------------------
00036 #include "vgfx.h"             // all primitives should include this header first
00037 #include "drawer.h"
00038 
00039 #include "common/wave_ex.h"
00040 #include "perf/perf.h"
00041 #include "util/parsing.h"
00042 
00043 
00044 
00045 namespace vgfx {
00046 
00047 ////////////////////////////////////////////////////////////////////////////////
00048 //
00049 //      static helper methods
00050 //
00051 ////////////////////////////////////////////////////////////////////////////////
00052 
00053 class Rectangle : public Primitive {
00054 public:
00055         ~Rectangle(void) throw() { }
00056 
00057         // public class methods ------------------------------------------------
00058         void initialize(IN const init_rect_t& init);
00059 
00060         // vgfx::Primitive class interface methods -----------------------------
00061         virtual const char * getType(void) const throw() { return "rect"; }
00062         virtual void persist(OUT std::ostream& stream) const;
00063         virtual bool doesContainerExist(IN const VecString& path) const;
00064         virtual bool canCreateContainer(IN const VecString& path) const;
00065         virtual void removeContainer(IN const VecString& path);
00066         virtual void getContainerDictionary(IN const VecString& path,
00067                                 OUT dictionary_t& data) const;
00068         virtual void setContainerDictionary(IN const VecString& path,
00069                                 IN const dictionary_t& data);
00070         virtual void recalcBoundingRect(IN const char * tag_path,
00071                                 IN Drawer * drawer,
00072                                 IN const xform_2d_t& T) { }
00073         virtual bool getBoundingRect(OUT rect_t& r) const throw();
00074         virtual void draw(IN Drawer * drawer,
00075                                 IN const rect_t& r_cm,
00076                                 IN const xform_2d_t& T);
00077 
00078 private:
00079         rect_t          m_rect;         // our actual coordinates
00080         bool            m_fill;
00081 };
00082 
00083 
00084 
00085 ////////////////////////////////////////////////////////////////////////////////
00086 //
00087 //      Rectangle -- public class methods
00088 //
00089 ////////////////////////////////////////////////////////////////////////////////
00090 
00091 void
00092 Rectangle::initialize
00093 (
00094 IN const init_rect_t& init
00095 )
00096 {
00097         this->setID(init.id.c_str());
00098         m_rect = init.rect;
00099         m_fill = init.fill;
00100 }
00101 
00102 
00103 
00104 bool
00105 Rectangle::getBoundingRect
00106 (
00107 OUT rect_t& r
00108 )
00109 const throw()
00110 {
00111         r = m_rect;
00112         return true;
00113 }
00114 
00115 
00116 
00117 #define WRITE_R_FIELD(q) { if (m_rect.q) { stream << " " << #q << " " << m_rect.q; } }
00118 
00119 void
00120 Rectangle::persist
00121 (
00122 OUT std::ostream& stream
00123 )
00124 const
00125 {
00126         stream << "rect id " << this->getID();
00127         WRITE_R_FIELD(left)
00128         WRITE_R_FIELD(top)
00129         WRITE_R_FIELD(right)
00130         WRITE_R_FIELD(bottom)
00131 
00132         if (m_fill) {
00133                 stream << " fill true";
00134         }
00135 }
00136 
00137 
00138 
00139 bool
00140 Rectangle::doesContainerExist
00141 (
00142 IN const VecString& path
00143 )
00144 const
00145 {
00146         if (path.size() > 0) {
00147                 return false;
00148         }
00149         return true;
00150 }
00151 
00152 
00153 bool
00154 Rectangle::canCreateContainer
00155 (
00156 IN const VecString& path
00157 )
00158 const
00159 {
00160         return false;
00161 }
00162 
00163 
00164 void
00165 Rectangle::removeContainer
00166 (
00167 IN const VecString& path
00168 )
00169 {
00170         WAVE_EX(wex);
00171         wex << "rectangles have no removable containers";
00172 }
00173 
00174 
00175 
00176 #define ADD_R_VALUE(q) { data[ #q ] = getStringValue( m_rect.q ); }
00177 
00178 void
00179 Rectangle::getContainerDictionary
00180 (
00181 IN const VecString& path,
00182 OUT dictionary_t& data
00183 )
00184 const
00185 {
00186         if (path.size() > 0) {
00187                 WAVE_EX(wex);
00188                 wex << "Rectangles do not support subcontainers";
00189         }
00190 
00191         data.clear();
00192         ADD_R_VALUE(left)
00193         ADD_R_VALUE(top)
00194         ADD_R_VALUE(right)
00195         ADD_R_VALUE(bottom)
00196 }
00197 
00198 
00199 
00200 #define CHECK_R_VALUE(q) { if (!strcmp(name, #q )) { m_rect.q = atof(value); continue; } }
00201 
00202 void
00203 Rectangle::setContainerDictionary
00204 (
00205 IN const VecString& path,
00206 IN const dictionary_t& data
00207 )
00208 {
00209         if (path.size() > 0) {
00210                 WAVE_EX(wex);
00211                 wex << "Rectangles do not support subcontainers";
00212         }
00213 
00214         for (dictionary_t::const_iterator i = data.begin(); i != data.end();
00215              ++i) {
00216                 const char * name = i->first.c_str();
00217                 const char * value = i->second.c_str();
00218 
00219                 CHECK_R_VALUE(left)
00220                 CHECK_R_VALUE(top)
00221                 CHECK_R_VALUE(right)
00222                 CHECK_R_VALUE(bottom)
00223         
00224                 {
00225                         WAVE_EX(wex);
00226                         wex << "Invalid property for rect curve: '";
00227                         wex << name << "'";
00228                 }
00229         }
00230 }
00231 
00232 
00233 
00234 void
00235 Rectangle::draw
00236 (
00237 IN Drawer * drawer,
00238 IN const rect_t& r_cm,
00239 IN const xform_2d_t& T
00240 )
00241 {
00242         ASSERT(drawer, "null");
00243 
00244         drawer->setTransform(T);
00245         if (m_fill)
00246                 drawer->fillRect(m_rect);
00247         else
00248                 drawer->drawRect(m_rect);
00249 }
00250 
00251 
00252 
00253 ////////////////////////////////////////////////////////////////////////////////
00254 //
00255 //      public API
00256 //
00257 ////////////////////////////////////////////////////////////////////////////////
00258 
00259 #define SET_R_FIELD(x)   init.rect.x = atof(getOptionalValue(data, #x , "0.0"));
00260 
00261 void
00262 init_rect_from_data
00263 (
00264 IN const dictionary_t& data,
00265 OUT init_rect_t& init
00266 )
00267 {
00268         init.id = getRequiredValue(data, "id");
00269 
00270         SET_R_FIELD(left)
00271         SET_R_FIELD(top)
00272         SET_R_FIELD(right)
00273         SET_R_FIELD(bottom)
00274 
00275         init.fill = !strcmp("true", getOptionalValue(data, "fill", "false"));
00276 }
00277 
00278 
00279 
00280 smart_ptr<Primitive>
00281 create_rect
00282 (
00283 IN const init_rect_t& init
00284 )
00285 {
00286         smart_ptr<Rectangle> local = new Rectangle;
00287         ASSERT(local, "out of memory");
00288 
00289         local->initialize(init);
00290 
00291         return local;
00292 }
00293 
00294 
00295 };      // vgfx namespace
00296