core/vgui/vgui_rubberband_tableau.h
Go to the documentation of this file.
00001 // This is core/vgui/vgui_rubberband_tableau.h
00002 #ifndef vgui_rubberband_tableau_h_
00003 #define vgui_rubberband_tableau_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \author  K.Y.McGaul
00010 // \date    31-MAR-2000
00011 // \brief   Tableau to rubberband circles, lines etc.
00012 //
00013 //  Contains classes: vgui_rubberband_tableau  vgui_rubberband_tableau_new
00014 //                    vgui_rubberband_client  vgui_rubberband_easy2D_client
00015 //
00016 // \verbatim
00017 //  Modifications
00018 //   31-MAR-2000 K.Y.McGaul - Initial version.
00019 //   31-MAR-2000 F.S. de M - Minor cosmetic changes.
00020 //   07-JUL-2000 Marko Bacic - Added support for linestrip
00021 //   19-JUL-2000 Marko Bacic - Now supports vgui_rubberband_client
00022 //   14-AUG-2000 FSM - Fixed so that it works with Windows
00023 //   17-SEP-2002 K.Y.McGaul - Added doxygen style comments.
00024 //   01-OCT-2002 K.Y.McGaul - Moved vgui_rubberbander to vgui_rubberband_tableau
00025 //   26-JUN-2003 Mark Johnson - Added clear_highlight method to client
00026 // \endverbatim
00027 
00028 
00029 #include <vgui/vgui_tableau.h>
00030 #include <vgui/vgui_easy2D_tableau.h>
00031 #include <vgui/vgui_event_condition.h>
00032 
00033 //-----------------------------------------------------------------------------
00034 //: Receives the parameters captured by vgui_rubberband_tableau.
00035 class vgui_rubberband_client
00036 {
00037  public:
00038   //: Destructor - delete this vgui_rubberband_client.
00039   virtual ~vgui_rubberband_client() {}
00040 
00041   //: Called by vgui_rubberband_tableau when the user has selected a point.
00042   virtual void add_point(float, float);
00043 
00044   //: Called by vgui_rubberband_tableau when the user has selected a finite line.
00045   virtual void add_line(float,float,float,float);
00046 
00047   //: Called by vgui_rubberband_tableau when user has selected an infinite line.
00048   virtual void add_infinite_line(float,float,float);
00049 
00050   //: Called by vgui_rubberband_tableau when the user has selected a circle.
00051   virtual void add_circle(float,float,float);
00052 
00053   //: Called by vgui_rubberband_tableau when the user has selected a linestrip.
00054   virtual void add_linestrip(int n,float const *,float const *);
00055 
00056   //: Called by vgui_rubberband_tableau when the user has selected a polygon.
00057   virtual void add_polygon(int n,float const*,float const*);
00058 
00059   //: Called by vgui_rubberband_tableau when user has selected a rectangular box
00060   virtual void add_box(float,float,float,float);
00061 
00062   //: Called by vgui_rubberband_tableau whenever mouse motion is captured
00063   virtual void clear_highlight();
00064 };
00065 
00066 //-----------------------------------------------------------------------------
00067 //: Rubberbanding onto a vgui_easy2D_tableau.
00068 //
00069 //  Special case of vgui_rubberband_client for cases where we just want to
00070 //  draw rubberbanded objects straight onto a vgui_easy2D_tableau.
00071 class vgui_rubberband_easy2D_client : public vgui_rubberband_client
00072 {
00073  public:
00074   //: Pointer to the vgui_easy2D_tableau object this client writes to.
00075   vgui_easy2D_tableau_sptr easy;
00076 
00077   //: Constructor - takes a pointer to a vgui_easy2D_tableau.
00078   vgui_rubberband_easy2D_client(vgui_easy2D_tableau_sptr const& e) : easy(e) {}
00079 
00080   //: Called by vgui_rubberband_tableau when the user has selected a point.
00081   //  Adds a point to the vgui_easy2D_tableau.
00082   void add_point(float x, float y);
00083 
00084   //: Called by vgui_rubberband_tableau when the user has selected a finite line.
00085   //  Adds a finite line to the vgui_easy2D_tableau.
00086   void add_line(float x0, float y0, float x1, float y1);
00087 
00088   //: Called by vgui_rubberband_tableau when user has selected an infinite line.
00089   //  Adds an infinite line to the vgui_easy2D_tableau.
00090   void add_infinite_line(float a, float b, float c);
00091 
00092   //: Called by vgui_rubberband_tableau when the user has selected a circle.
00093   //  Adds a circle to the vgui_easy2D_tableau.
00094   void add_circle(float x, float y, float r);
00095 
00096   //: Called by vgui_rubberband_tableau when the user has selected a linestrip.
00097   //  Adds a linestrip to the vgui_easy2D_tableau.
00098   void add_linestrip(int n, float const* x, float const* y);
00099 
00100   //: Called by vgui_rubberband_tableau when the user has selected a polygon.
00101   //  Adds a polygon to the vgui_easy2D_tableau.
00102   void add_polygon(int n, float const* x, float const* y);
00103 
00104   //: Called by vgui_rubberband_tableau when the user has selected a box.
00105   //  Since vgui_easy2D_tableau doesn't have a rectangular box object, this
00106   //  add an equivalent 4 point polygon instead.
00107   void add_box(float x0, float y0, float x1, float y1);
00108 
00109   //: Called by vgui_rubberband_tableau whenever mouse motion is captured.
00110   //  This turns off the highlighting of geometry objects to eliminate
00111   //  flickering highlights while drawing temporary objects.
00112   void clear_highlight();
00113 };
00114 
00115 //-----------------------------------------------------------------------------
00116 //: Tableau to rubberband circles, lines etc.
00117 //
00118 //  The values captured (eg. two points defining a line) are passed to the
00119 //  appropriate function (eg. add_line) in the client passed in to the
00120 //  constructor.  This client is derived from vgui_rubberband_client.
00121 //
00122 //  In more detail:
00123 //
00124 //  The user draws the object in the rendering area using the defined
00125 //  'gestures'.  For example, to draw a line, the first gesture is a mouse
00126 //  down event defining the start point.  A line is shown on the display
00127 //  between the start point and the mouse pointer until the second gesture
00128 //  is performed (releasing the mouse button) and this defines the end point.
00129 //
00130 //  These values are passed to add_line in the client.  These values could
00131 //  be used to draw the line (like vgui_rubberband_easy2D_client) or
00132 //  you can create your own client derived from vgui_rubberband_client to
00133 //  do something different.
00134 //
00135 //  See xcv to see a rubberbander in action (used to add geometric objects).
00136 //
00137 //  If you want to get values from a user and you want your code to wait
00138 //  until the values have been collected then rubberbander won't do this.
00139 //  You need to write something like ./oxl/xcv/xcv_picker_tableau that
00140 //  grabs the event loop.
00141 class vgui_rubberband_tableau : public vgui_tableau
00142 {
00143  public:
00144   //: Whether to use overlays or not.
00145   bool use_overlays;  // capes@robots - default is true
00146 
00147   //: Initialise vgui_rubberband_tableau.
00148   void init (vgui_rubberband_client* client);
00149 
00150   //: Constructor - don't use this, use vgui_rubberband_tableau_new.
00151   //  Takes the vgui_rubberband_client as a parameter.
00152   vgui_rubberband_tableau(vgui_rubberband_client* client) { init(client); }
00153 
00154 //vgui_rubberband_tableau(vgui_easy2D_tableau_sptr const& e) { init(new vgui_rubberband_tableau_easy2D_client(e)); }
00155 
00156   //: Return the type of this tableau ('vgui_rubberband_tableau').
00157   vcl_string type_name() const { return "vgui_rubberband_tableau"; }
00158 
00159   // These describe what the user has to do to use the rubberbanding gesture.
00160   vgui_event_condition gesture0;
00161   vgui_event_condition gesture1;
00162   vgui_event_condition gesture2;
00163 
00164   void rubberband_point();
00165   void rubberband_line();
00166   void rubberband_infinite_line();
00167   void rubberband_circle();
00168   void rubberband_polygon();
00169   void rubberband_linestrip(); // u97mb
00170   void rubberband_box();
00171   void rubberband_none();
00172 
00173   vgui_rubberband_client* get_client() { return client_; }
00174   void set_client(vgui_rubberband_client *client) { client_ = client; }
00175 
00176   void draw_point(float x0, float y0);
00177   void draw_line(float x0, float y0, float x1, float y1);
00178   void draw_infinite_line(float a, float b, float c); // ax + by + c = 0
00179   void draw_circle(float x0, float y0, float r);
00180   void draw_linestrip(float x,float y); // u97mb
00181   void draw_polygon(float x, float y);
00182   void draw_box(float x0,float y0, float x1,float y1);
00183 
00184  protected:
00185   ~vgui_rubberband_tableau() {}
00186   bool handle_point(vgui_event const&, float, float);
00187   bool handle_line(vgui_event const&, float, float);
00188   bool handle_linestrip(vgui_event const&,float , float ); // u97mb
00189   bool handle_infinite_line(vgui_event const&, float, float);
00190   bool handle_circle(vgui_event const&, float, float);
00191   bool handle_polygon(vgui_event const&, float, float);
00192   bool handle_box(vgui_event const&, float, float);
00193   bool handle(vgui_event const&);
00194 
00195  private:
00196   vgui_rubberband_client *client_;
00197   enum object_type {none_enum, point_enum, line_enum, infinite_line_enum,
00198                     circle_enum, polygon_enum, linestrip_enum,box_enum};
00199   bool active;
00200   static object_type obj_type;
00201   float lastx, lasty;   // position where mouse was last seen.
00202   vcl_vector<float>x_coords, y_coords;
00203 };
00204 
00205 //-----------------------------------------------------------------------------
00206 typedef vgui_tableau_sptr_t<vgui_rubberband_tableau> vgui_rubberband_tableau_sptr;
00207 
00208 //: Creates a smart-pointer to a vgui_rubberband_tableau tableau.
00209 struct vgui_rubberband_tableau_new : public vgui_rubberband_tableau_sptr
00210 {
00211   vgui_rubberband_tableau_new(vgui_rubberband_client* client)
00212     : vgui_rubberband_tableau_sptr(new vgui_rubberband_tableau(client)) {}
00213 };
00214 
00215 #endif // vgui_rubberband_tableau_h_