fshake3d  0.0.1
FreeformDensity3DSurfaceEditor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines
ui_CrossSplitter.cpp
Go to the documentation of this file.
00001 #include "ui_CrossSplitter.hpp"
00002 
00003 CrossSplitter::CrossSplitter()
00004   : mSplit(0.5,0.5)
00005   , mBorder(10.0)
00006   , mDragBase(-1,-1)
00007   , mBaseSplit(0,0)
00008 {
00009   mViews[0] = 0;
00010   mViews[1] = 0;
00011   mViews[2] = 0;
00012   mViews[3] = 0;
00013 }
00014 
00015 void CrossSplitter::setChildView(int index, View* pView)
00016 {
00017   if (mViews[index])
00018   {
00019     mViews[index]->setParent(0);
00020   }
00021 
00022   mViews[index] = pView;
00023   if (pView)
00024   {
00025     pView->setParent(this);
00026   }
00027   layoutChildren();
00028 }
00029 
00030 void CrossSplitter::layoutChildren()
00031 {
00032   float w = width()  - mBorder;
00033   float h = height() - mBorder;
00034   if (w < 0.0f) w = 0.0f;
00035   if (mViews[0]) mViews[0]->setViewport(Vec4i(0,0,w*mSplit[0],h*mSplit[1]));
00036   if (mViews[1]) mViews[1]->setViewport(Vec4i(w*mSplit[0]+mBorder,0,w*(1-mSplit[0]),h*mSplit[1]));
00037   if (mViews[2]) mViews[2]->setViewport(Vec4i(0,h*mSplit[1]+mBorder,w*mSplit[0],h*(1-mSplit[1])));
00038   if (mViews[3]) mViews[3]->setViewport(Vec4i(w*mSplit[0]+mBorder,h*mSplit[1]+mBorder,w*(1-mSplit[0]),h*(1-mSplit[1])));
00039 }
00040 
00041 void CrossSplitter::reshape(int w, int h)
00042 {
00043   View::reshape(w,h);
00044   layoutChildren();
00045 }
00046 
00047 void CrossSplitter::display()
00048 {
00049   if (mViews[0]) mViews[0]->display();
00050   if (mViews[1]) mViews[1]->display();
00051   if (mViews[2]) mViews[2]->display();
00052   if (mViews[3]) mViews[3]->display();
00053 }
00054 
00055 /* 
00056  * @return 
00057  *   child index (>0) or 
00058  *   -1 xsplit control
00059  *   -2 ysplit control
00060  *   -3 both controls
00061  */
00062 
00063 int CrossSplitter::getChildIndexAt(int x, int y)
00064 {
00065   int x1 = ( width()-mBorder-1 )  * mSplit[0];
00066   int x2 = x1 + mBorder;
00067   int y1 = ( height()-mBorder-1 ) * mSplit[1];
00068   int y2 = y1 + mBorder;
00069 
00070   bool inLeft   = (x < x1);
00071   bool inRight  = (x > x2);
00072   bool inBottom = (y < y1);
00073   bool inTop    = (y > y2);
00074 /*
00075   bool inLeft   = (x < ( mSplit[0]*width()  - mBorder*0.5 ) );
00076   bool inRight  = (x > ( mSplit[0]*width()  + mBorder*0.5 ) );
00077   bool inBottom = (y < ( mSplit[1]*height() - mBorder*0.5 ) );
00078   bool inTop    = (y > ( mSplit[1]*height() + mBorder*0.5 ) );
00079 */
00080   if (inLeft)
00081   {
00082     if (inBottom)
00083     {
00084       return 0;
00085     }
00086     else if (inTop)
00087     {
00088       return 2;
00089     }
00090   }
00091   else if (inRight)
00092   {
00093     if (inBottom)
00094     {
00095       return 1;
00096     }
00097     else if (inTop)
00098     {
00099       return 3;
00100     }
00101   }        
00102 
00103   // not in childs
00104 
00105   int index = 0;
00106   
00107   if ( !(inLeft) && !(inRight) ) index  = 1;
00108   if ( !(inBottom) && !(inTop) ) index |= 2;
00109 
00110   return -index;
00111 }
00112 
00113 void CrossSplitter::onEvent(Event& e)
00114 {
00115   int index = getChildIndexAt(e.getMouseX(),e.getMouseY());
00116 
00117   if (index >= 0)
00118   {
00119     if (mViews[index])
00120     {
00121       mViews[index]->onEvent(e);
00122     }
00123   }
00124   else
00125   {   
00126     index *= -1;
00127     if (index & 1) {
00128       // mSplitX.onEvent(e); 
00129     }
00130     if (index & 2) { 
00131       // mSplitY.onEvent(e); 
00132     }
00133   }
00134 }
00135 
00136 void CrossSplitter::mouse(int button, int state, int x, int y)
00137 {
00138   int index = getChildIndexAt(x,y);
00139   if (index >= 0)
00140   {
00141     if (mViews[index])
00142      
00143      mViews[index]->mouse(button,state,x,y);
00144   }
00145   else
00146   {
00147     if (button == GLUT_LEFT_BUTTON)
00148     {
00149       if (state == GLUT_DOWN)
00150       {        
00151 
00152         int x1 = ( width()-mBorder-1 )  * mSplit[0];
00153         int x2 = x1 + mBorder;
00154         int y1 = ( height()-mBorder-1 ) * mSplit[1];
00155         int y2 = y1 + mBorder;
00156 
00157         bool inLeft   = (x < x1);
00158         bool inRight  = (x > x2);
00159         bool inBottom = (y < y1);
00160         bool inTop    = (y > y2);
00161 
00162         /*
00163         bool inLeft   = (x < mSplit[0]*width()  - mBorder*0.5 );
00164         bool inRight  = (x > mSplit[0]*width()  + mBorder*0.5 );
00165         bool inBottom = (y < mSplit[1]*height() - mBorder*0.5 );
00166         bool inTop    = (y > mSplit[1]*height() + mBorder*0.5 );
00167         */
00168 
00169         if ( (!inLeft) && (!inRight) )
00170         {
00171           mDragBase[0] = x;
00172           mBaseSplit[0] = mSplit[0];
00173         }
00174         if ( (!inBottom) && (!inTop) )
00175         {
00176           mDragBase[1] = y;
00177           mBaseSplit[1] = mSplit[1];
00178         }
00179       } 
00180       else
00181       {
00182         mDragBase[0] = -1;
00183         mDragBase[1] = -1;        
00184       }
00185     }
00186   }
00187 }
00188 
00189 void CrossSplitter::motion(int x, int y)
00190 {
00191   int index = getChildIndexAt(x,y);
00192   if (index >= 0)
00193   {
00194     if (mViews[index])
00195     {
00196       mViews[index]->motion(x,y);
00197     }
00198   }
00199 
00200   bool do_layout = false;
00201   if (mDragBase[0] >= 0)
00202   {
00203     int dx = x - mDragBase[0];
00204     float w = width() - mBorder;
00205     mSplit[0] = ( w*mBaseSplit[0]+dx ) / w;
00206     mSplit[0] = clamp(mSplit[0],0.0,1.0);
00207     do_layout = true;
00208   }
00209   if (mDragBase[1] >= 0)
00210   {
00211     int dy = y - mDragBase[1];
00212     float h = height() - mBorder;
00213     mSplit[1] = ( h*mBaseSplit[1]+dy ) / h;
00214     mSplit[1] = clamp(mSplit[1],0.0,1.0);
00215     do_layout = true;
00216   }
00217   if (do_layout)
00218   {
00219     layoutChildren();
00220   }
00221 }