Play 'round
|
00001 #ifndef __WIDGET_H_ 00002 #define __WIDGET_H_ 00003 00004 #include <iostream> 00005 #include <vector> 00006 #include <map> 00007 #include <string> 00008 00009 #include "stk/Mutex.h" 00010 00011 // Forward declarations 00012 class Network; 00013 class Widget; 00014 class Track; 00015 class SoundSource; 00016 class RoundPad; 00017 class SpiralTrack; 00018 class LineTrack; 00019 class String; 00020 00021 #include "Shape.h" 00022 #include "include/Common.h" 00023 #include "osc/OscOutboundPacketStream.h" 00024 #include "stk/Plucked.h" 00025 00026 typedef std::map<std::string, Widget*> WidgetMap; 00027 typedef std::pair<std::string, Widget*> WidgetData; 00028 00029 typedef std::map<std::string, SoundSource*> SoundSourceMap; 00030 typedef std::pair<std::string, SoundSource*> SoundSourceData; 00031 00032 #define LEFT_SIDE -1 00033 #define RIGHT_SIDE 1 00034 typedef int Side; 00035 typedef std::map<std::string, Side> StringStateMap; 00036 typedef std::pair<std::string, Side> StringStateData; 00037 00041 class Widget : public Shape 00042 { 00043 public: 00044 Widget(); 00045 virtual ~Widget(); 00046 00047 static WidgetMap* getAll(); 00048 static Widget* getSelected(); 00052 static void setSelected(Widget*); 00053 00054 // Mouse interaction 00055 virtual bool onMouseDown(int button, float x, float y); 00056 virtual bool onMouseUp(int button, float x, float y); 00057 virtual bool onMouseMove(float x, float y); 00058 virtual bool onMouseOver(float x, float y); 00059 00060 virtual void draw() = 0; 00061 virtual Widget *hitTest(float x, float y) { return NULL; } 00062 00063 virtual void setParent(Widget *parent); 00064 virtual void setNetwork(Network *network); 00065 00066 virtual void addChild(Widget *child); 00067 virtual Widget* removeChild(Widget *child); 00068 00069 virtual void setUuid(const char*); 00070 virtual const char* getUuid() const; 00071 00072 virtual void toOutboundPacketStream(osc::OutboundPacketStream&) const; 00073 00074 virtual std::vector<Widget *>* getChildren() { return &m_children; } 00075 virtual Widget* getParent() { return m_parent; } 00076 virtual std::string toString() { return ""; } 00077 00078 virtual Widget* getEngine(); 00079 00080 protected: 00084 virtual bool handleDraw(float x, float y) { return false; } 00088 virtual bool handleDrawEnd(float x, float y) { return false; } 00092 virtual bool handleSelect(float x, float y, bool = false) { return false; } 00096 virtual bool handleHover(float x, float y) { return false; } 00097 00098 virtual void drawChildren(); 00099 00100 std::vector<Widget *> m_children; 00101 Widget *m_parent, *m_engine; 00102 Widget *m_mouseDownOn; 00103 bool m_fLeftButtonDown, m_fRightButtonDown; 00104 Point2D m_mouseDownPos, m_drawStartPos; 00105 std::string m_uuid; 00106 00107 static Network *s_network; 00108 }; 00109 00113 class Joint : public Widget 00114 { 00115 public: 00116 Joint(Point2D center, float radius); 00117 ~Joint(); 00118 00119 void addTrack(Track *track); 00120 Point2D getCenter() { return m_center; } 00121 void setCenter(Point2D center); 00122 void setColor(Color color); 00123 std::vector<Track *> *getTracks() { return &m_tracks; } 00124 00125 virtual void draw(); 00126 virtual Widget *hitTest(float x, float y); 00127 virtual std::string toString(); 00128 RoundPad *getParentRoundPad() { return m_parentRoundPad; } 00129 void setParentRoundPad(RoundPad *pad) { m_parentRoundPad = pad; } 00130 00131 protected: 00132 virtual bool handleDraw(float x, float y); 00133 virtual bool handleDrawEnd(float x, float y); 00134 virtual bool handleSelect(float x, float y, bool = false); 00135 00136 std::vector<Track *> m_tracks; 00137 Point2D m_center; 00138 float m_radius; 00139 Spiral *m_circle; 00140 00141 SpiralTrack* m_newSpiralTrack; 00142 LineTrack* m_newLineTrack; 00143 Line m_dragLine; 00144 bool m_drawing; 00145 00146 RoundPad *m_parentRoundPad; 00147 }; 00148 00152 class Track : public Widget 00153 { 00154 public: 00155 Track(); 00156 virtual ~Track(); 00157 00158 virtual void draw() = 0; 00159 00160 virtual Point2D getEndPoint1() = 0; 00161 virtual Point2D getEndPoint2() = 0; 00162 Joint *getJoint1() { return m_joint1; } 00163 Joint *getJoint2() { return m_joint2; } 00167 virtual Point2D getNextPos(bool fReverse, float distance, Point2D pos) = 0; 00168 void addPlucker(); 00169 00170 void setEnabled(bool fEnabled) { m_fEnabled = fEnabled; } 00171 void setActive(bool fActive) { m_fActive = fActive; } 00172 void setDirected(bool fDirected) { m_fDirected = fDirected; } 00173 void setImmediate(bool fImmediate) { m_fImmediate = fImmediate; } 00174 void setJoints(Joint *j1, Joint *j2) { m_joint1 = j1; m_joint2 = j2; } 00175 bool isEnabled() { return m_fEnabled; } 00176 bool isActive() { return m_fActive; } 00177 bool isDirected() { return m_fDirected;} 00178 bool isImmediate() { return m_fImmediate; } 00179 00180 protected: 00181 virtual void drawChildren(); 00182 virtual void detachJoint(Joint *joint); 00183 void setupJoints(Joint *joint1, Joint *joint2); 00184 00185 Joint *m_joint1, *m_joint2; // Start joint and end joint 00186 bool m_fEnabled, m_fActive, m_fDirected, m_fImmediate; 00187 }; 00188 00192 class SpiralTrack : public Track 00193 { 00194 public: 00195 SpiralTrack(Point2D center, float startAngle, float startRadius, 00196 float endAngle, float endRadius, Joint *joint1, Joint *joint2); 00197 ~SpiralTrack(); 00198 00199 void initialize(Point2D center, float startAngle, float startRadius, 00200 float endAngle, float endRadius, Joint *joint1, Joint *joint2); 00201 00202 virtual void draw(); 00203 virtual Widget *hitTest(float x, float y); 00204 00205 virtual void toOutboundPacketStream(osc::OutboundPacketStream&) const; 00206 00207 virtual Point2D getEndPoint1(); 00208 virtual Point2D getEndPoint2(); 00209 virtual Point2D getNextPos(bool fReverse, float distance, Point2D pos); 00210 00211 Point2D getCenter(); 00212 void setCenter(const Point2D& center); 00213 void setEndAngle(float angle); 00214 void setEndRadius(float radius); 00215 00216 float getStartAngle(); 00217 float getEndAngle(); 00218 Spiral* getSpiral() { return m_spiral; } 00219 00220 virtual bool handleHover(float x, float y); 00221 00222 virtual std::string toString(); 00223 00224 protected: 00225 Point2D m_center; 00226 float m_startRadius, m_endRadius; 00227 float m_startAngle, m_endAngle; 00228 Spiral *m_spiral; 00229 }; 00230 00234 class LineTrack : public Track 00235 { 00236 public: 00237 LineTrack(Point2D p1, Point2D p2, Joint *joint1, Joint *joint2); 00238 ~LineTrack(); 00239 00240 void initialize(Point2D p1, Point2D p2, Joint *joint1, Joint *joint2); 00241 00242 virtual void draw(); 00243 virtual Widget *hitTest(float x, float y); 00244 00245 virtual void toOutboundPacketStream(osc::OutboundPacketStream&) const; 00246 00247 virtual Point2D getEndPoint1() { return m_p1; } 00248 virtual Point2D getEndPoint2() { return m_p2; } 00249 virtual Point2D getNextPos(bool fReverse, float distance, Point2D pos); 00250 00251 virtual bool handleHover(float x, float y); 00252 00253 Line* getLine() { return m_line; } 00254 00255 protected: 00256 Line *m_line; 00257 Point2D m_p1, m_p2; 00258 }; 00259 00263 class Plucker : public Widget 00264 { 00265 public: 00266 Plucker(Track *track, Joint *startJoint, Joint *endJoint); 00267 ~Plucker(); 00268 00272 std::vector<Plucker *> split(); 00273 bool isAtEnd(); 00274 00278 void tick(); 00279 virtual void draw(); 00280 00281 Point2D getPos() { return m_pos; } 00282 00283 Side getSideOfString(String*); 00284 Side updateSideOfString(String*); 00285 00286 private: 00287 Track *m_track; 00288 Joint *m_startJoint, *m_endJoint; 00289 Point2D m_pos; 00290 Spiral *m_circle; 00291 00292 StringStateMap m_stringStates; 00293 }; 00294 00298 class SoundSource : public Widget 00299 { 00300 public: 00301 static void initializeGlobals(); 00302 static void lockGlobals(); 00303 static void unlockGlobals(); 00304 static void swapGlobals(); 00305 static SoundSourceMap* getAllForAudio(); 00306 static SoundSourceMap* getAllForEngine(); 00310 virtual SAMPLE tick() { return 0.0f; } 00311 00312 private: 00313 static SoundSourceMap *s_forAudio, *s_forEngine; 00314 static stk::Mutex s_swapMutex; 00315 }; 00316 00320 class String : public SoundSource 00321 { 00322 public: 00323 String(Point2D p1, Point2D p2, float radius); 00324 ~String(); 00325 00326 void initialize(Point2D p1, Point2D p2, float radius); 00327 bool pluck(Plucker *plucker); 00328 00329 // Overrides Widget 00330 virtual Widget *hitTest(float x, float y); 00331 virtual void draw(); 00332 virtual bool handleHover(float x, float y); 00333 00334 virtual void toOutboundPacketStream(osc::OutboundPacketStream&) const; 00335 00336 void setPadRadius(float); 00337 00338 // Overrides SoundSource 00339 virtual SAMPLE tick(); 00340 00341 Line* getLine(); 00342 00343 Side getMouseSide(float, float); 00344 Side updateMouseSide(float, float); 00345 00346 protected: 00347 Line *m_line; // Shape on GUI 00348 Point2D m_p1, m_p2; // For determining the pitch 00349 Spiral *m_p1Dot, *m_p2Dot; 00350 Plucker *m_lastPlucker; 00351 Side m_mouseSide; 00352 stk::Plucked m_plucked; // stk string 00353 float m_freq; // Frequency to be plucked 00354 }; 00355 00359 class RoundPad : public Widget 00360 { 00361 public: 00362 RoundPad(Point2D center, float radius); 00363 ~RoundPad(); 00364 00365 virtual Point2D getCenter() { return m_center; } 00366 virtual float getRadius() { return m_radius; } 00367 virtual void setRadius(float radius); 00368 00369 virtual void draw(); 00370 virtual Widget *hitTest(float x, float y); 00371 00372 virtual void toOutboundPacketStream(osc::OutboundPacketStream&) const; 00373 00374 virtual Text* getCommentText() const { return m_commentText; } 00375 virtual void setCommentText(const std::string comment); 00376 virtual void appendCommentText(const std::string comment); 00377 00378 protected: 00379 Point2D m_center; 00380 float m_radius; 00381 Track* m_newTrack; 00382 Line m_hoverLine, m_dragLine; 00383 Text* m_commentText; 00384 00385 virtual bool handleDraw(float x, float y); 00386 virtual bool handleDrawEnd(float x, float y); 00387 virtual bool handleHover(float x, float y); 00388 00389 SpiralTrack* m_newSpiralTrack; 00390 String* m_newString; 00391 std::vector<Spiral *> m_circles; 00392 std::vector<Track *> m_tracks; 00393 }; 00394 00395 #endif