00001 //------------------------------------------------------------------------ 00002 // 00003 // Joe Kniss 00004 // 6-20-03 00005 // ________ ____ ___ 00006 // | \ / | / / 00007 // +---+ \/ |/ / 00008 // +--+| |\ /| < 00009 // | || | \ / | |\ \ 00010 // | | \/ | | \ \ 00011 // \_____| |__| \__\ 00012 // Copyright 2003 00013 // Joe Michael Kniss 00014 // <<< jmk@cs.utah.edu >>> 00015 // "All Your Base are Belong to Us" 00016 //------------------------------------------------------------------------- 00017 00018 /// PolygonWidget.h 00019 00020 #ifndef __POLYGON_WIDGET_DOT_H 00021 #define __POLYGON_WIDGET_DOT_H 00022 00023 #include "WidgetBase.h" 00024 #include "EdgeWidget.h" 00025 #include "NodeWidget.h" 00026 #include <iostream> 00027 00028 /// A "Composite Widget" for nodes and lines. 00029 /// The transform (_mat) is a temporary matrix which is non-identity only 00030 /// during rotation operations. 00031 class PolygonWidget : public WidgetItem 00032 { 00033 public: 00034 PolygonWidget(WidgetItem *parent) 00035 : WidgetItem(parent) 00036 {} 00037 virtual ~PolygonWidget() 00038 { 00039 for(unsigned int i=0; i<_nodes.size(); ++i) _nodes[i] = 0; 00040 for(unsigned int i=0; i<_edges.size(); ++i) _edges[i] = 0; 00041 } 00042 00043 /// object changed 00044 virtual void setChanged() 00045 { 00046 for(unsigned int i=0; i<_nodes.size(); ++i) 00047 _nodes[i]->setChanged(); 00048 } 00049 00050 ///@name Draw 00051 /// just forwards draw event to children 00052 ///@{ 00053 virtual void drawDef(const gutz::RenderEvent &r) 00054 { 00055 for(unsigned int i=0; i<_nodes.size(); ++i) 00056 _nodes[i]->draw(r); 00057 for(unsigned int i=0; i<_edges.size(); ++i) 00058 _edges[i]->draw(r); 00059 } 00060 ///@} 00061 00062 /// set/reset all points in polygon 00063 virtual void setPoints(const Point3Array &pts, 00064 const NodeWidget *const nodeProto, 00065 const EdgeWidget *const edgeProto) 00066 { 00067 _nodes.clear(); 00068 for(int i=0; i<pts.size(); ++i) 00069 { 00070 NodeWidget *n = nodeProto->cloneNode(); 00071 n->setPoint(pts[i]); 00072 n->setParent(this); 00073 _nodes.push_back(n); 00074 } 00075 _edges.clear(); 00076 for(int i=0; i<pts.size(); ++i) 00077 { 00078 EdgeWidget *e = edgeProto->cloneEdge(); 00079 e->setStartNode(_nodes[i]); 00080 e->setEndNode(_nodes[(i+1)%_nodes.size()]); 00081 e->setParent(this); 00082 _edges.push_back(e); 00083 } 00084 } 00085 00086 ///@name Transform Widget 00087 ///@{ 00088 virtual void scale(const gutz::vec3f scale) 00089 { 00090 for(unsigned int i=0; i<_nodes.size(); ++i) 00091 _nodes[i]->setPoint(_nodes[i]->getPoint() * scale); 00092 } 00093 00094 virtual void translate(const gutz::vec3f trans) 00095 { 00096 for(unsigned int i=0; i<_nodes.size(); ++i) 00097 _nodes[i]->setPoint(_nodes[i]->getPoint() + trans); 00098 } 00099 00100 virtual void applyXform(gutz::mat4f xf) 00101 { 00102 for(unsigned int i=0; i<_nodes.size(); ++i) 00103 _nodes[i]->setPoint(xf.tpoint(_nodes[i]->getPoint())); 00104 } 00105 ///@} 00106 00107 ///@name Node/Edge set/get 00108 /// carefull, the set may have no "meaning", may not be 00109 /// appropriate for subclass behaviors. 00110 /// The gets are very usefull though. 00111 ///@{ 00112 int getNodeSize() const {return _nodes.size();} 00113 NodeWidgetSP getNode(int idx) const {return _nodes[idx];} 00114 virtual void setNode(int idx, NodeWidgetSP n) {_nodes[idx] = n;} 00115 virtual void addNode(NodeWidgetSP n) {_nodes.push_back(n);} 00116 00117 int getEdgeSize() const {return _edges.size();} 00118 EdgeWidgetSP getEdge(int idx) const {return _edges[idx];} 00119 virtual void setEdge(int idx, EdgeWidgetSP e) {_edges[idx] = e;} 00120 virtual void addEdge(EdgeWidgetSP e) {_edges.push_back(e);} 00121 ///@} 00122 00123 00124 protected: 00125 00126 virtual void moveNode(int idx, const gutz::MouseMoveEvent &mme){} 00127 virtual void moveEdge(int idx, const gutz::MouseMoveEvent &mme){} 00128 00129 /// returns -1 if not found 00130 int getNodeIdx(const WidgetItem *ptr) const 00131 { 00132 for(unsigned int i=0; i<_nodes.size(); ++i) 00133 if(ptr == _nodes[i]) return i; 00134 return -1; 00135 } 00136 int getEdgeIdx(const WidgetItem *ptr) const 00137 { 00138 for(unsigned int i=0; i<_edges.size(); ++i) 00139 if(ptr == _edges[i]) return i; 00140 return -1; 00141 } 00142 00143 NodeWidgetVec _nodes; 00144 EdgeWidgetVec _edges; 00145 00146 }; 00147 00148 typedef gutz::SmartPtr<PolygonWidget> PolygonWidgetSP; 00149 00150 00151 #endif 00152 00153