00001
00002 #ifndef OglGui_Tabs_h
00003 #define OglGui_Tabs_h
00004
00005
00006
00007
00008
00009
00010 #ifndef OglGui_vector_included
00011 #include <vector>
00012 #define OglGui_vector_included
00013 #endif
00014
00015 #ifndef Button_h
00016 #include "OglGui/Button.h"
00017 #endif
00018
00019 #ifndef TabsListener_h
00020 #include "OglGui/TabsListener.h"
00021 #endif
00022
00023 #include "OglGui/Transition.h"
00024
00025 namespace OglGui
00026 {
00027
00028 class Tabs : public Window, public ButtonListener, public WindowListener
00029 {
00030 public:
00031
00032 Tabs(int x, int y, int wndW, int wndH, int labelW = 120, int labelH = 22):
00033 Window(x, y, wndW, wndH)
00034 {
00035 Init(labelW, labelH);
00036 }
00037
00038 Tabs(Window* parent, int wndW, int wndH, int labelW = 120, int labelH = 22):
00039 Window(parent, wndW, wndH)
00040 {
00041 Init(labelW, labelH);
00042 }
00043
00044 Tabs(Window* parent, int x, int y, int wndW, int wndH, int labelW = 120,
00045 int labelH = 22) :
00046 Window(parent, x, y, wndW, wndH)
00047 {
00048 Init(labelW, labelH);
00049 }
00050
00051 void SetTabsListener(TabsListener* listener, void* listenerData = 0)
00052 {
00053 mTabsListener = listener;
00054 mTabsListenerData = listenerData;
00055 }
00056
00057 Window* TabBar() { return mTitleWnd; }
00058 void TabsDissolve(bool mode) { mDissolve = mode; }
00059 void TabsAnimated(int mode) { mTabsAnimated = abs(mode); }
00060 void TabsAnimDivider(int d) { mTabAnimDiv = abs(d%40); }
00061
00062 void TabsTransition(int m1, int m2)
00063 {
00064 mTabTransit1 = m1;
00065 mTabTransit2 = m2;
00066 }
00067
00068 void TabsTransitionGridXY(int x, int y)
00069 {
00070 mGridX=x;
00071 mGridY=y;
00072 }
00073
00074 void ScaleTabBar(bool fixedHeight)
00075 {
00076 RepositionViewports();
00077 if (fixedHeight)
00078 mTitleWnd->ConnectTo(this,L2L|R2R|T2T|B2T);
00079 else
00080 mTitleWnd->ScaleTo(this);
00081 mTitleWnd->SetAllowScaling(false);
00082 mTitleWnd->RepositionViewports();
00083 mTitleWnd->ScaleChildren();
00084 mTitleWnd->SetAllowChildScaling(false);
00085 }
00086
00087 Window*
00088 CreateTab(std::string title, int labelW = -1, int tabControlData = -1)
00089 {
00090 if (labelW == -1)
00091 labelW = mLabelWidth;
00092 int curBut = mCanvasList.size();
00093 Button* but = new Button(mTitleWnd, labelW, mLabelHeight+4, title);
00094 but->SetRoundness(200, 200, 2, 2);
00095 but->SetButtonListener(this, (void*)curBut);
00096 int canvasHeight = mOglWnd->height - mLabelHeight - 4;
00097 Window* canvas = new Window(this, 0, 0, mOglWnd->width, canvasHeight);
00098 canvas->ConnectTo(this, ALLSIDES | TPARENT);
00099 canvas->SetExternWindowReference(but);
00100 mCanvasList.push_back(canvas);
00101 mTabControlData.push_back(tabControlData);
00102 return canvas;
00103 }
00104
00105
00106 void SetActiveTab(int idx, bool idxIsTabNr = true)
00107 {
00108 if (!idxIsTabNr)
00109 {
00110 for (unsigned int i=0 ; i<mTabControlData.size() ; i++)
00111 if (mTabControlData[i] == idx)
00112 idx = i;
00113 }
00114 if (idx < (int) mCanvasList.size())
00115 {
00116 for (int i=0 ; i < (int) mCanvasList.size() ; i++)
00117 {
00118 Button* btn =
00119 (Button *) mCanvasList[i]->GetExternWindowReference();
00120 btn->SetBorderFillOpacity((i == idx) ? 0.8f : 0.3f);
00121 if (i == idx)
00122 {
00123 mActiveTabId = idx;
00124 HandleTabChangeAnimation();
00125 mCanvasList[i]->SetVisible(true);
00126 oglFocusWnd = mCanvasList[i]->GetOGLWND();
00127 oglSys.SetUpdateScene(oglFocusWnd, 1);
00128 }
00129 else
00130 mCanvasList[i]->SetVisible(false);
00131 }
00132 RepositionViewports();
00133 if (mTabsListener)
00134 {
00135 if (mTabControlData[idx] >= 0)
00136 mTabsListener->TabSelectionEvent(this, idx,
00137 mTabsListenerData,
00138 mTabControlData[idx]);
00139 else
00140 mTabsListener->TabSelectionEvent(this, idx,
00141 mTabsListenerData);
00142 }
00143 }
00144 }
00145
00146 int
00147 GetActiveTab()
00148 {
00149 return mActiveTabId;
00150 }
00151
00152 int
00153 GetActiveTabControlData()
00154 {
00155 return mTabControlData[GetActiveTab()];
00156 }
00157
00158 void
00159 ActivateNextTab()
00160 {
00161 int sz = mCanvasList.size();
00162
00163 if (sz > 0)
00164 SetActiveTab((mActiveTabId+1) % sz);
00165 }
00166
00167
00168
00169 virtual void
00170 ButtonSelectionEvent(Button *src, void* userData)
00171 {
00172 SetActiveTab((int)(long long) userData);
00173 }
00174
00175 virtual void DisplayFunc()
00176 {
00177 HandleTabAnimation();
00178 Window::DisplayFunc();
00179 }
00180
00181 virtual void
00182 WindowKeyboardEvent(Window *src, int c, int state, void *userData)
00183 {
00184 if (c >= '0' && c <= '9')
00185 TabsAnimated(c-'0');
00186 if (c == 'd')
00187 TabsDissolve(!mDissolve);
00188 if (c == oglCTRL('t'))
00189 TabsTransition(0,0);
00190 if (c == 't')
00191 TabsTransition((mTabTransit1+1)%32,mTabTransit2);
00192 if (c == 'T')
00193 TabsTransition(mTabTransit1,(mTabTransit2+1)%32);
00194 if (c == 's')
00195 mShowAnimInfo = !mShowAnimInfo;
00196 if (c == oglCTRL('x'))
00197 TabsTransitionGridXY(1+mGridX%7,mGridY);
00198 if (c == oglCTRL('y'))
00199 TabsTransitionGridXY(mGridX,1+mGridY%7);
00200 }
00201
00202 virtual void
00203 WindowDisplayEvent(Window *src, void *userData)
00204 {
00205 if (mShowAnimInfo)
00206 {
00207 oglSys.PosColPrintf(src->GetOGLWND(),src->W()-160,4,oglRED,
00208 "A:%d D:%d t:%d T:%d X:%d Y:%d",
00209 mTabsAnimated, mDissolve ? 1 : 0,
00210 mTabTransit1, mTabTransit2, mGridX, mGridY);
00211 }
00212 }
00213
00214 protected:
00215
00216 void StartTabTransition()
00217 {
00218 if (!(mDissolve || mTabTransit1 || mTabTransit2))
00219 return;
00220 if (!mTransitionWnd)
00221 mTransitionWnd = new Transition(this);
00222 mTransitionWnd->GridXY(mGridX, mGridY);
00223
00224 mTransitionWnd->FillColors(0x80d0c0b0,oglTrWHITE,oglTrWHITE, 0x80d0c0b0);
00225 mTransitionWnd->TransitionStart(mDissolve, mTabTransit1, mTabTransit2);
00226 }
00227
00228 void StartTabAnimation()
00229 {
00230 if (!mTabsAnimated)
00231 return;
00232 Window* wnd = mCanvasList[mActiveTabId];
00233 int x,y,w,h;
00234 wnd->GetDimensions(x,y,w,h);
00235 if (mTabsAnimated & 5)
00236 x = (mTabsAnimated & 1) ? -w : w;
00237 if (mTabsAnimated & 10)
00238 y = (mTabsAnimated & 2) ? h : -h;
00239 wnd->SetDimensions(x,y,w,h);
00240 SetAlwaysDraw(mTabAnimating = true);
00241
00242 }
00243
00244 void HandleTabChangeAnimation()
00245 {
00246 StartTabAnimation();
00247 StartTabTransition();
00248 }
00249
00250 void HandleTabAnimation()
00251 {
00252 if (!mTabAnimating)
00253 return;
00254 Window* wnd = mCanvasList[mActiveTabId];
00255 int x,y,w,h;
00256 wnd->GetDimensions(x,y,w,h);
00257 int dX = (mTabsAnimated & 1) ? 1 : -1;
00258 int dY = (mTabsAnimated & 2) ? -1 : 1;
00259 if (x < -1 || x > 1 || y < -1 || y > 1)
00260 {
00261 int addX = (x==-1||x==1) ? 0 :
00262 ((mTabsAnimated & 5) ? -x / mTabAnimDiv + dX : 0);
00263 int addY = (y==-1||y==1) ? 0 :
00264 ((mTabsAnimated & 10) ? -y / mTabAnimDiv + dY : 0);
00265 addX = (addX > 40) ? 40 : addX;
00266 addX = (addX < -40) ? -40 : addX;
00267 addY = (addY > 40) ? 40 : addY;
00268 addY = (addY < -40) ? -40 : addY;
00269 wnd->SetDimensions(x+addX,y+addY,w,h);
00270 }
00271 else
00272 {
00273 SetAlwaysDraw(mTabAnimating = false);
00274 wnd->SetDimensions(0,0,w,h);
00275 wnd->UpdateScene();
00276 }
00277 }
00278
00279 private:
00280
00281 void
00282 Init(int labelW, int labelH)
00283 {
00284 mTabsListener = 0;
00285 mTabsListenerData = 0;
00286 mLabelWidth = labelW;
00287 mLabelHeight = labelH;
00288 mTitleWnd = new Window(this, mOglWnd->width, mLabelHeight + 1);
00289 RepositionViewports();
00290 mTitleWnd->ConnectTo(this, OglGui::TOTOP | OglGui::TOLEFTRIGHT);
00291 mTitleWnd->SetWindowListener(this);
00292 mActiveTabId = 0;
00293 mTabsAnimated = 0;
00294 mTabAnimDiv = 4;
00295 mTabAnimating = false;
00296 mDissolve = false;
00297 mTransitionWnd = 0;
00298 mTabTransit1 = mTabTransit2 = 0;
00299 mShowAnimInfo = false;
00300 mGridX = 3;
00301 mGridY = 3;
00302 }
00303
00304 int mLabelWidth;
00305 int mLabelHeight;
00306 TabsListener* mTabsListener;
00307 void* mTabsListenerData;
00308 std::vector<int> mTabControlData;
00309
00310 Window* mTitleWnd;
00311 std::vector<Window*> mCanvasList;
00312
00313 int mActiveTabId;
00314
00315 bool mShowAnimInfo;
00316
00317 int mTabsAnimated;
00318 int mTabAnimDiv;
00319 bool mTabAnimating;
00320
00321 bool mDissolve;
00322
00323 Transition* mTransitionWnd;
00324 int mTabTransit1;
00325 int mTabTransit2;
00326 int mGridX;
00327 int mGridY;
00328 };
00329
00330 }
00331
00332 #endif