00001 #ifndef BETAGUI
00002 #define BETAGUI
00003
00007
00008 #include "Ogre.h"
00009
00010 namespace BetaGUI
00011 {
00012
00013 class GUI;
00014
00015 class Widget;
00016 class Window;
00017
00018 class Button;
00019 class BoolButton;
00020 class MenuButton;
00021 class RealButton;
00022
00023 class StaticText;
00024 class TextInput;
00025
00026 class Callback;
00027
00028 class Effect;
00029 class AlphaEffect;
00030 class MoveEffect;
00031 class SizeEffect;
00032
00033
00034 enum WindowFeatureType
00035 {
00036 WFT_NONE = 0,
00037 WFT_MOVE ,
00038 WFT_RESIZE ,
00039 WFT_RESIZE_AND_MOVE
00040 };
00041
00042 enum WindowPlacementType
00043 {
00044 WPT_NONE = 0,
00045 WPT_VERTICAL ,
00046 WPT_HORIZONTAL
00047 };
00048
00049 enum WindowActivity
00050 {
00051 WA_NONE = 0,
00052 WA_MOVE ,
00053 WA_RESIZE ,
00054 WA_PRESS
00055 };
00056
00057 class GUI
00058 {
00059 public:
00060
00061 friend class Window;
00062
00063 friend class Button;
00064
00065 friend class MenuButton;
00066 friend class BoolButton;
00067 friend class RealButton;
00068
00069 friend class StaticText;
00070 friend class TextInput;
00071
00072 GUI(const Ogre::String &baseOverlay, const Ogre::String &font, int fontSize, Ogre::RenderWindow *win);
00073
00074 ~GUI();
00075
00076 void sortZOrder() ;
00077
00078 void setZRangeOrder(int nearZ, int FarZ);
00079
00080 bool injectMouse(int x, int y, bool LMB);
00081
00082 bool injectKey(const Ogre::String &key, int x, int y);
00083
00084 void injectBackspace(int x, int y);
00085
00086 Window* createWindow(
00087 const Ogre::Vector4 &Dimensions,
00088 const Ogre::String &Material,
00089 WindowFeatureType type,
00090 const Ogre::String &caption = "");
00091
00092 void destroyWindow(Window *window) {
00093 mNextWindowToDestroy=window;
00094 }
00095 Ogre::OverlayContainer* createOverlay(const Ogre::String &name,
00096 const Ogre::Vector2 &position,
00097 const Ogre::Vector2 &dimensions,
00098 const Ogre::String &material = "",
00099 const Ogre::String &caption = "");
00100
00101 Ogre::OverlayContainer* createMousePointer(Ogre::Vector2 dimensions,
00102 const Ogre::String &material);
00103
00104
00105
00106 Window *addMenuWindow(const Ogre::Vector2 dimensions);
00107
00108 void update(Ogre::Real timeElapsed);
00109 void addEffect (Effect* e);
00110 Ogre::Real getUpTime() const {return mUpTime;}
00111
00112 protected:
00113
00114 std::vector<Window*> mWindows;
00115 Window * mNextWindowToDestroy;
00116 Ogre::OverlayContainer* mMousePointerOverlay;
00117
00118 Ogre::String mFont;
00119 int mFontSize;
00120 int mWindowCount, mButtonCount, mTextCaptionCount, mTextCaptionCountArea, mButtonCountTextArea;
00121
00122
00123 Ogre::uint mDragWindowAction;
00124
00125 int mDragX, mDragY;
00126 int mDragWindowX, mDragWindowY;
00127
00128 Window * mActiveDraggedWindow;
00129 TextInput * mActiveTextInput;
00130 Button * mActiveButton;
00131
00132 Ogre::Vector2 mNextMenuWindowPos;
00133 Window * mMenuWindow;
00134 Ogre::RenderWindow * mRenderWindow;
00135
00136 std::list<Effect*> mActiveEffects;
00137
00138 Ogre::Real mUpTime;
00139 Ogre::String mName;
00140 int mFarZ;
00141 int mNearZ;
00142 };
00143
00144 class Widget
00145 {
00146 public:
00147
00148 friend class GUI;
00149
00150 Widget(const Ogre::Vector4 &Dimension,
00151 const Ogre::String &materialNameNormal,
00152 bool hasOverState = true,
00153 bool hasActiveState = true);
00154
00155 virtual ~Widget();
00156
00157 Ogre::Vector2 getPosition() const{return Ogre::Vector2(mPosX,mPosY);}
00158 Ogre::Vector2 getSize() const {return Ogre::Vector2(mWidth,mHeight); }
00159
00160 void setPosition(const Ogre::Vector2 &position);
00161 void setSize(const Ogre::Vector2 &size);
00162
00163 virtual void show(){setAlpha (1.0, true);};
00164 virtual void hide(){setAlpha (0.0, true);};
00165
00166 virtual bool isVisible() {return mMainOverlay->isVisible ();};
00167
00168 virtual void over(const bool a);
00169 virtual void activate(const bool a);
00170
00171 virtual void adjustSizeToContent();
00172
00173 virtual void setPosition(int X,int Y);
00174 virtual void setSize(int X,int Y);
00175 virtual void setAlpha(const Ogre::Real alpha, const bool isContainer);
00176 virtual void setMaterialBlendFactor(const Ogre::Real blendFactor, const bool isContainer);
00177
00178
00179 bool getUnderEffect() const { return mUnderEffect; }
00180
00181 void setUnderEffect(bool val);
00182
00183
00184 bool getOver() const { return mOver; }
00185
00186 void setOver(bool val) { mOver = val; }
00187
00188 bool getActive() const { return mActive; }
00189
00190 void setActive(bool val) { mActive = val; }
00191
00192
00193 void setMaterial(Ogre::MaterialPtr mat);
00194
00195
00196 protected:
00197
00198 inline bool isOut (const int x, const int y) const
00199 {
00200 return (x < mPosX) || (y < mPosY)
00201 || (x > (mPosX + mWidth)) ||
00202 (y > (mPosY + mHeight));
00203 }
00204
00205 inline bool isIn (const int x, const int y) const
00206 {
00207 return (x >= mPosX) && (y >= mPosY)
00208 && (x <= (mPosX + mWidth)) &&
00209 (y <= (mPosY + mHeight));
00210 }
00211
00212 void instantiateMaterial(Ogre::OverlayContainer * const oc, bool reset) const;
00213 void setOverlayAlpha(Ogre::OverlayContainer * const oc, const Ogre::Real alpha) const;
00214 void setOverlayMaterialBlendFactor(Ogre::OverlayContainer * const oc, const Ogre::Real blendFactor) const;
00215
00216 int mPosX, mPosY, mWidth, mHeight;
00217
00218 std::vector<Widget *> mChildren;
00219 Ogre::OverlayContainer* mMainOverlay;
00220 Ogre::OverlayContainer* mCaptionOverlay;
00221 Ogre::String mMaterialName;
00222
00223 bool mHasMaterial;
00224 bool mHasOverState;
00225 bool mHasActiveState;
00226 bool mUnderEffect;
00227 bool mOver;
00228 bool mActive;
00229 };
00230
00231 class Window : public Widget
00232 {
00233 public:
00234 friend class Button;
00235 friend class TextInput;
00236 friend class StaticText;
00237 friend class GUI;
00238
00239 Window(const Ogre::Vector4 &Dimensions, const Ogre::String &Material, WindowFeatureType type, const Ogre::String &caption, GUI *gui);
00240 virtual ~Window();
00241
00242 Button* createButton(const Ogre::Vector4 &Dimensions, const Ogre::String &Material,
00243 const Ogre::String &Text, Callback callback);
00244
00245 TextInput* createTextInput(const Ogre::Vector4 &Dimensions, const Ogre::String &Material,
00246 const Ogre::String & Value, int length);
00247
00248 StaticText* createStaticText(const Ogre::Vector4 &Dimensions, const Ogre::String &Text);
00249
00250 void setSize(int X,int Y);
00251
00252 MenuButton *addMenuButton(Window *target, const Ogre::String &label, WindowPlacementType wpt = WPT_NONE);
00253 BoolButton *addBoolButton(bool* value, const Ogre::String &label, WindowPlacementType wpt = WPT_NONE);
00254 RealButton *addRealButton(Ogre::Real* value, const Ogre::Vector4 &SliderMoves, const Ogre::String &label, WindowPlacementType wpt = WPT_NONE);
00255 StaticText* addStaticText(const Ogre::String &label, WindowPlacementType wpt = WPT_NONE);
00256
00257 Window *addMenuWindowTab(const Ogre::String &label,
00258 bool titleBarWin = true,
00259 WindowPlacementType wpt = WPT_VERTICAL);
00260
00261 MenuButton* createMenuButton(const Ogre::Vector4 &D, const Ogre::String &M, const Ogre::String &T, Callback C, Window *t);
00262 BoolButton* createBoolButton(const Ogre::Vector4 &D, const Ogre::String &M, const Ogre::String &T, Callback C, bool *boolValue);
00263
00264 RealButton *createRealButton(Ogre::Real* value,
00265 const Ogre::Vector4 &SliderMoves,
00266 const Ogre::String &label,
00267 const Ogre::Vector2 &pos);
00268
00269 GUI* getGUI() const {return mGUI;}
00270
00271 void setZOrder(int zOrder){mGuiOverlay->setZOrder (zOrder);}
00272 protected:
00273
00274
00275 bool check(int x, int y, bool LMB);
00276 bool checkKey(const Ogre::String &key, int x, int y);
00277
00278 TextInput* mActiveTextInput;
00279
00280 Button
00281 *mCloseButton,
00282 *mResizerButton,
00283 *mActiveButton,
00284 *mTitleBarButton;
00285
00286 GUI *mGUI;
00287
00288 std::vector<Button*> mButtons;
00289 std::vector<TextInput*> mTextInput;
00290
00291 Ogre::Overlay *mGuiOverlay;
00292
00293 void addWidget(const Widget * const w, WindowPlacementType wpt);
00294 Ogre::Vector2 mNextWidgetPos;
00295 WindowActivity mDragType;
00296
00297 };
00298
00299 class BetaGUIListener
00300 {
00301 public:
00302 virtual void onButtonPress(Button*, unsigned char) = 0;
00303 };
00304
00305 class Callback
00306 {
00307 public:
00308 friend class Window;
00309 friend class Button;
00310
00311 Callback() {
00312 mCallbackType=0;
00313 }
00314
00315 Callback( void(*functionPointer)(BetaGUI::Button*, bool) ) {
00316 mCallbackType=1;
00317 fp = functionPointer;
00318 }
00319
00320 Callback(BetaGUIListener *l){
00321 mCallbackType = 2;
00322 mListener = l;
00323 }
00324 protected:
00325 unsigned char mCallbackType;
00326 void(*fp)(BetaGUI::Button*, bool);
00327 BetaGUIListener *mListener;
00328 };
00329
00330 class Button : public Widget
00331 {
00332 public:
00333 friend class Window;
00334 friend class GUI;
00335
00336 Button(const Ogre::Vector4 &Dimensions, const Ogre::String &Material,
00337 const Ogre::String &Text, Callback callback, Window *parent);
00338 virtual ~Button() {}
00339
00340 protected:
00341 Callback mCallback;
00342 };
00343
00344 class MenuButton : public Button
00345 {
00346 public:
00347 MenuButton(const Ogre::Vector4 &Dimensions, const Ogre::String &Material,
00348 const Ogre::String &Text, Callback callback, Window *parent,
00349 Window *target) :
00350 Button(Dimensions, Material, Text, callback, parent),
00351 mTarget(target)
00352 {};
00353 virtual ~MenuButton(){};
00354 protected:
00355 void activate(const bool a) ;
00356
00357 Window *mTarget;
00358 };
00359
00360 class BoolButton : public Button
00361 {
00362 public:
00363 BoolButton(const Ogre::Vector4 &Dimensions, const Ogre::String &Material,
00364 const Ogre::String &Text, Callback callback, Window *parent, bool *boolValue):
00365 Button(Dimensions, Material, Text, callback, parent),
00366 mBoolValue(boolValue)
00367 {}
00368 virtual ~BoolButton(){};
00369
00370 protected:
00371 void activate(const bool a) ;
00372
00373 bool *mBoolValue;
00374 };
00375
00376 class TextInput : public Widget
00377 {
00378 public:
00379 friend class Window;
00380 friend class GUI;
00381
00382 TextInput(const Ogre::Vector4 &Dimensions, const Ogre::String &Material,
00383 const Ogre::String &Value, unsigned int length, Window *parent);
00384 virtual ~TextInput(){}
00385 const Ogre::String &getValue() const {return mValueText;}
00386 void setValue(const Ogre::String &v) {mCaptionOverlay->setCaption(mValueText = v);}
00387
00388 protected:
00389 unsigned int length;
00390 Ogre::String mValueText;
00391
00392 };
00393
00394 class StaticText : public Widget
00395 {
00396 public:
00397 friend class Window;
00398
00399 StaticText(const Ogre::Vector4 &Dimensions,
00400 const Ogre::String &Value,
00401 Window *parent);
00402 virtual ~StaticText(){}
00403
00404 const Ogre::String &getValue() const
00405 {return mValueText;}
00406
00407 void setValue(const Ogre::String &v)
00408 {
00409 mCaptionOverlay->setCaption(mValueText = v);
00410 mWidth = static_cast<int> (mValueText.size () * 7);
00411 mParent->adjustSizeToContent ();
00412 }
00413
00414 protected:
00415
00416 int length;
00417 Ogre::String mValueText;
00418 Window *mParent;
00419 };
00420
00421 class RealButton : public BetaGUIListener, public Widget
00422 {
00423 public:
00424 friend class Window;
00425
00426 RealButton(BetaGUI::Window *window,
00427 Ogre::Real* value,
00428 const Ogre::Vector4 &SliderMoves,
00429 const Ogre::String &label,
00430 const Ogre::Vector2 &pos);
00431
00432 virtual ~RealButton(){};
00433
00434 Ogre::Real getValue() const {return *mValue;}
00435
00436 void setValue(const Ogre::Real v);
00437
00438 void onButtonPress(BetaGUI::Button *ref, Ogre::uchar type);
00439 protected:
00440
00441 int length;
00442
00443 BetaGUI::TextInput *mtextValueInput;
00444 BetaGUI::Button *mForward, *mFastForward, *mRewind, *mFastRewind;
00445
00446 Ogre::Real *mValue;
00447 Ogre::String mValueText;
00448
00449 Ogre::Real mLowSlideUnit;
00450 Ogre::Real mFastSlideUnit;
00451 Ogre::Real mMinValue;
00452 Ogre::Real mMaxValue;
00453
00454 };
00455
00456 class Effect
00457 {
00458 public:
00459 enum EffectInterpolatorType
00460 {
00461 EFFECT_LINEAR = 0,
00462 EFFECT_LINEARX2,
00463 EFFECT_LINEARX4,
00464 EFFECT_SIN
00465 };
00466 public:
00467 Effect(Widget * const widget, const Ogre::Real duration,
00468 const Ogre::Real startTime, const Ogre::Real currTime = 0, const bool repeat = false,
00469 EffectInterpolatorType type = EFFECT_LINEARX4) :
00470 mWidget(widget),
00471 mStartTime(startTime),
00472 mDuration(duration),
00473 mRepeat(repeat),
00474 mCurrTime(currTime),
00475 mType(type)
00476 {
00477 assert (duration != 0);
00478 }
00479 virtual ~Effect(){};
00480
00481 virtual void updateValue(const Ogre::Real factor) = 0;
00482
00483 bool update(Ogre::Real timeElapsed);
00484 Ogre::Real getTimeFactor() ;
00485 bool getTimeOrIsFinished();
00486 Ogre::Real linearInterpolate(const Ogre::Real start,
00487 const Ogre::Real end,
00488 const Ogre::Real timeFactor) const;
00489 Ogre::Real interpolate(const Ogre::Real start,
00490 const Ogre::Real end,
00491 const Ogre::Real timeFactor) const;
00492
00493 protected:
00494 Widget * const mWidget;
00495 const Ogre::Real mStartTime;
00496 const Ogre::Real mDuration;
00497 const bool mRepeat;
00498
00499 Ogre::Real mCurrTime;
00500 const EffectInterpolatorType mType;
00501 };
00502
00503 class AlphaEffect : public Effect
00504 {
00505 public:
00506
00507 AlphaEffect(Widget * const widget,
00508 const Ogre::Real duration,
00509 const Ogre::Real startAlpha,
00510 const Ogre::Real endAlpha,
00511 const Ogre::Real startTime,
00512 const Ogre::Real currTime = 0,
00513 const bool repeat = false,
00514 EffectInterpolatorType type = EFFECT_LINEARX4) :
00515 Effect(widget, duration, startTime, currTime, repeat),
00516 mStartAlpha(startAlpha),
00517 mEndAlpha(endAlpha)
00518 {
00519 assert(mStartAlpha >= 0.0 && mStartAlpha <= 1.0);
00520 assert(mEndAlpha >= 0.0 && mEndAlpha <= 1.0);
00521 }
00522 virtual ~AlphaEffect(){};
00523
00524 void updateValue(const Ogre::Real factor);
00525 private:
00526 const Ogre::Real mStartAlpha;
00527 const Ogre::Real mEndAlpha;
00528 };
00529
00530 class MoveEffect : public Effect
00531 {
00532 public:
00533 MoveEffect(Widget * const widget,
00534 const Ogre::Real duration,
00535 const Ogre::Vector2 &startPosition,
00536 const Ogre::Vector2 &endPosition,
00537 const Ogre::Real startTime,
00538 const Ogre::Real currTime = 0,
00539 const bool repeat = false,
00540 EffectInterpolatorType type = EFFECT_LINEARX4) :
00541 Effect(widget, duration, startTime, currTime, repeat),
00542 mStartPosition(startPosition),
00543 mEndPosition(endPosition)
00544 {}
00545 virtual ~MoveEffect(){};
00546 void updateValue(const Ogre::Real factor);
00547
00548 private:
00549 const Ogre::Vector2 mStartPosition;
00550 const Ogre::Vector2 mEndPosition;
00551 };
00552
00553 class SizeEffect : public Effect
00554 {
00555 public:
00556 SizeEffect(Widget * const widget,
00557 const Ogre::Real duration,
00558 const Ogre::Vector2 &startSize,
00559 const Ogre::Vector2 &endSize,
00560 const Ogre::Real startTime,
00561 const Ogre::Real currTime = 0,
00562 const bool repeat = false,
00563 EffectInterpolatorType type = EFFECT_LINEARX4) :
00564 Effect(widget, duration, startTime, currTime, repeat),
00565 mStartSize(startSize),
00566 mEndSize(endSize)
00567 {}
00568 virtual ~SizeEffect(){};
00569 void updateValue(const Ogre::Real factor);
00570
00571 private:
00572 const Ogre::Vector2 mStartSize;
00573 const Ogre::Vector2 mEndSize;
00574 };
00575
00576 class ActivateEffect : public Effect
00577 {
00578 public:
00579 ActivateEffect(Widget * const widget,
00580 const Ogre::Real duration,
00581 const Ogre::Real startTime,
00582 const Ogre::Real currTime = 0,
00583 const bool repeat = false,
00584 EffectInterpolatorType type = EFFECT_LINEARX4):
00585 Effect(widget, duration, startTime, currTime, repeat)
00586 {};
00587 virtual ~ActivateEffect(){};
00588 void updateValue(const Ogre::Real factor);
00589
00590 };
00591
00592 class OverEffect : public Effect
00593 {
00594 public:
00595 OverEffect(Widget * const widget,
00596 const Ogre::Real duration,
00597 const Ogre::Real startTime,
00598 const Ogre::Real currTime = 0,
00599 const bool repeat = false,
00600 EffectInterpolatorType type = EFFECT_LINEARX4):
00601 Effect(widget, duration, startTime, currTime, repeat)
00602 {};
00603 virtual ~OverEffect(){};
00604 void updateValue(const Ogre::Real factor);
00605
00606 };
00607
00608
00609
00610 }
00611 #endif
00612