fshake3d  0.0.1
FreeformDensity3DSurfaceEditor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
ui_Control.hpp
Go to the documentation of this file.
00001 class IButtonListener
00002 {
00003   virtual bool buttonDown(int button, int x, int y) = 0;
00004   virtual void buttonUp(int button, int x, int y) = 0;
00005 };
00006 
00007 class IMotionListener
00008 {
00009   virtual void motionUpdate(int x, int y) = 0;
00010 };
00011 
00012 class Control
00013 {
00014 public:
00015   void addButtonListener(IButtonListener* pButtonListener);
00016   void removeButtonListener(IButtonListener* pButtonListener);
00017   void addMotionListener(IMotionListener* pMotionListener);
00018   void removeMotionListener(IMotionListener* pMotionListener);
00019   void onEvent(Event& e)
00020   {
00021     switch(e.getType())
00022     {
00023     case EVENT_BUTTON_DOWN:
00024       if ( !mpIButtonListeners.empty() )
00025       {
00026         for ( list<IButtonListeners>::iterator i = mpIButtonListeners->begin(), end = mpIButtonListeners->end() ; i != end ; ++i )
00027         {
00028           if ( (*i)->buttonDown( e.getButton(), e.getMouseX(), e.getMouseY() ) )
00029           {
00030             e.consumed();
00031             return;
00032           }
00033         }
00034       }
00035       break;
00036     case EVENT_MOTION_UPDATE:
00037       if ( !mpIMotionListeners.empty() )
00038       {
00039 
00040       }
00041     }
00042   }
00043 private:
00044   list<IButtonListener*> mpIButtonListeners;
00045   list<IButtonListener*> mpIMotionListeners;
00046 };
00047 
00048 #include "ui_listeners.hpp"
00049 
00050 class IDragListener
00051 {
00052 public:
00053   virtual bool dragBegin(int button, int x, int y) = 0;
00054   virtual void dragUpdate(int button, int x, int y) = 0;
00055   virtual void dragEnd(int button, int x, int y) = 0;
00056   virtual void dragReset(int button, int x, int y) = 0;
00057 };
00058 
00059 class DragControl : public ButtonListener, public MotionListener
00060 {
00061 public:
00062   void addDragListener(DragListener* pListener)
00063   {
00064     mListeners.push_back(pListener);
00065   }
00066   void removeDragListener(DragListener* pListener)
00067   {
00068     mListeners.remove(pListener);
00069   }
00070 
00071   virtual void buttonDown(int button, int x, int y)
00072   {
00073     if (mpActiveListener)
00074     {
00075       finishDrag(x,y);
00076     }
00077     else
00078     {
00079       // try drag listeners      
00080       for (std::list<DragListener*>::iterator i = mListeners.begin, end = mListeners.end() ; i != end ; ++i)
00081       {
00082         DragListener* pListener = *i;
00083         if ( pListener->dragBegin( button , x, y ) )
00084         {
00085           startDrag(pListener, button);
00086         }
00087       }
00088     }
00089   }
00090   
00091   virtual void buttonUp(int button, int x, int y)
00092   {
00093     if (mpActiveListener)
00094     {
00095       stopDragListener(x,y);
00096     }
00097   }
00098 
00099   virtual void motionUpdate(int x, int y)
00100   {
00101     if (mpActiveListener)
00102     {
00103       mpActiveListener->dragUpdate( mActiveButton, x, y );
00104     }
00105   }
00106 
00107 private:
00108   void startDragListener(DragListener* pListener, int button)
00109   {
00110     mpActiveListener = pListener;
00111     mActiveButton = button;
00112     addMotionListener(this);
00113 
00114   }
00115   void stopDragListener(int x, int y)
00116   {
00117     mpActiveListener->dragEnd( mActiveButton, x, y );
00118     mpActiveListener = 0;
00119     releaseMotionListener(this);
00120   }
00121 private:
00122   list<DragListener*> mListeners;
00123   DragListener*       mpActiveListener;
00124   int                 mActiveButton;
00125 };
00126 
00127 class ScalarDragAdapter : public DragListener
00128 {
00129 };
00130 
00131 template<typename NodeT, typename FieldT>
00132 void fire(NodeT* pNode, FieldT* pField)
00133 {
00134 
00135 }
00136 
00137 class ScalarDragger : public NodeImplement<DragListener>
00138 {
00139 public:
00140   ScalarDragger()
00141     : mButton(BUTTON_NONE)
00142     , mAxis(AXIS_X)
00143     , mScalar(0)
00144   {
00145   }
00146   virtual bool dragBegin(int button, int x, int y)
00147   {
00148     if (button = mButton)
00149     {
00150       int data[2] = { x, y }
00151       mBase = data[mAxis];
00152       mScalarBase = mScalar;
00153       return true;
00154     }
00155   }
00156   virtual void dragUpdate(DragEvent& de)
00157   {
00158     int data[2] = { x, y }
00159     int delta = data[mAxis] - mBase;
00160     mScalar = mScalarBase + double(delta) * mScale;
00161     fire( this, mScalar );
00162   }
00163   virtual void dragEnd()
00164   {
00165   }
00166   static void def_class(Class& c)
00167   {
00168     c
00169       .name("ScalarDragger")
00170       .constructor<ScalarDragger>()
00171       .field("button", &ScalarDragger::mButton)
00172       .field("scalar", &ScalarDragger::mScalar)
00173       .field("scale",  &ScalarDragger::mScale)
00174     ;
00175   }
00176 private:
00177   int      mButton;
00178   double   mScalar;
00179   double   mScalarBase;
00180   int      mDragBase;
00181   double   mScale;
00182 };
00183 
00184 void init()
00185 {
00186   Class scalarDraggerClass;
00187   ScalarDragger::def_class(scalarDraggerClass);
00188   RegisterClass(scalarDraggerClass);
00189 }