vgfx-util.h

Go to the documentation of this file.
00001 /*
00002  * vgfx-util.h
00003  *
00004  * Copyright 2007 (C)  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  * Helper methods for dealing with VGFX objects.
00032  * At the moment, these are most useful for copy/paste/drag/drop operations.
00033  */
00034 
00035 #ifndef WAVEPACKET_VGFX_UTIL_H__
00036 #define WAVEPACKET_VGFX_UTIL_H__
00037 
00038 // includes --------------------------------------------------------------------
00039 #include "vgfx.h"
00040 
00041 #include "graph/dag.h"
00042 
00043 
00044 /// \ingroup vgfx
00045 /*@{*/
00046 
00047 
00048 namespace vgfx {
00049 
00050 
00051 /// NodeChecker -- base class that can be used for walk validation
00052 ///     see the bottom of this file for some useful validators
00053 class NodeChecker {
00054 public:
00055         virtual ~NodeChecker(void) throw();
00056         virtual bool checkNode(IN visit_result_t& vr) { return false; }
00057         bool operator()(IN visit_result_t& vr) { return this->checkNode(vr); }
00058 };
00059 
00060 
00061 
00062 /// getDragDropRootNodeID() - returns the id of the root node used for all
00063 ///     drag/drop/copy/paste operations
00064 const char * getDragDropRootNodeID(void) throw();
00065 
00066 /// getDragRequest() - given root object in map, and selection of tag_paths,
00067 ///     return a drag/drop request (all selected objects attached to the
00068 ///     known drag/drop root node id)
00069 void getDragRequest(IN ObjectMap * map,
00070                                 IN Primitive * root,
00071                                 IN const SetString& selection,
00072                                 IN float x_cm,
00073                                 IN float y_cm,
00074                                 OUT std::string& request);
00075 
00076 /// getDropRequest() - given a drag_request that has been dropped at (x_cm, y_cm)
00077 ///     on a given root in a given map, return a request that adds all of the
00078 ///     dropped objects to the correct position in the root.
00079 bool getDropRequest(IN ObjectMap * map,
00080                                 IN Primitive * root,
00081                                 IN const SetString& selection,
00082                                 IN std::istream& drag_request,
00083                                 IN float x_cm,
00084                                 IN float y_cm,
00085                                 IN NodeChecker& okayToReceiveDrop,
00086                                 OUT std::string& request,
00087                                 OUT SetString& out_selection);
00088 
00089 
00090 /// getSelectionRect() - given a set of selected objects, return bounding rect
00091 void getSelectionRect(IN ObjectMap * map,
00092                                 IN Primitive * root,
00093                                 IN const SetString& selection,  // tag_paths
00094                                 OUT rect_t& r_cm);
00095 
00096 
00097 /// addDependenciesToGraph(map, id, dag) - given a top-level object (id) in a
00098 ///     map, run through and find all dependencies of the object.
00099 void addDependenciesToGraph(IN ObjectMap * map,
00100                                 IN const char * id,
00101                                 IO graph::DAG * dag);
00102 
00103 /// hit_result_t -- returned from hit detection
00104 struct hit_result_t {
00105         hit_result_t(void) throw() { this->clear(); }
00106         void clear(void) throw() {
00107                         p = NULL;
00108                         tag_path = "";
00109                         T.setIdentity();
00110                 }
00111 
00112         // data fields
00113         Primitive      *p;
00114         std::string     tag_path;
00115         xform_2d_t      T;              // object local --> global coordinates
00116         float           x;              // object-local x-value
00117         float           y;              // object-local y-value
00118 };
00119 
00120 bool getObjectAt(IN Primitive * root,
00121                                 IN float x_cm,
00122                                 IN float y_cm,
00123                                 IN NodeChecker& checker,
00124                                 OUT hit_result_t& hr);
00125 
00126 
00127 
00128 struct meta_check_t {
00129         meta_check_t(void) throw() { this->clear(); }
00130         void clear(void) throw() {
00131                         exists = false;
00132                         value.clear();
00133                 }
00134 
00135         operator bool(void) const throw() { return exists; }
00136         const char * c_str(void) const throw() { return value.c_str(); }
00137 
00138         // data fields
00139         std::string             value;
00140         bool                    exists;
00141 };
00142 
00143 meta_check_t getMeta(IN Primitive * p, IN const char * name);
00144 std::string getOptionalMeta(IN Primitive * p,
00145                                 IN const char * name,
00146                                 IN const char * default_value);
00147 
00148 ////////////////////////////////////////////////////////////////////////////////
00149 //
00150 //      validators (derived from NodeChecker)
00151 //
00152 ////////////////////////////////////////////////////////////////////////////////
00153 
00154 class MetaKeyValueChecker : public NodeChecker {
00155 public:
00156         ~MetaKeyValueChecker(void) throw();
00157         MetaKeyValueChecker(IN const char * key, IN const char * value);
00158         bool checkNode(IN visit_result_t& vr);
00159 private:
00160         std::string             m_key;
00161         std::string             m_value;
00162 };
00163 
00164 
00165 };      // vgfx namespace
00166 
00167 #endif  // WAVEPACKET_VGFX_UTIL_H__
00168