widgets.h

00001 #pragma once
00002 
00003 #include <sstream>
00004 #include "console.h"
00005 #include "renderer.h"
00006 #include "sprite.h"
00007 #include "list.h"
00008 #include "defines.h"
00009 
00010 #define WIDGET_NONE                             0
00011 #define WIDGET_ROLLOVER                 1
00012 #define WIDGET_ROLLOUT                  2
00013 #define WIDGET_CLICK                    3
00014 #define WIDGET_PRESS                    4
00015 #define WIDGET_DBLCLICK                 5
00016 #define WIDGET_FOCUS                    6
00017 #define WIDGET_FOCUSLOST                7
00018 #define WIDGET_DRAG                             8
00019 #define WIDGET_STOPDRAG                 9
00020 #define WIDGET_RETURN                   10
00021 
00022 #define WIDGET_DBLCLICK_DELAY   150
00023 
00024 #define WIDGET_BLINK_DELAY              500
00025 
00026 namespace zak {
00027 
00028 class ZAKENGINE_API WidgetMgr;
00029 
00038 class ZAKENGINE_API Widget : public Sprite { 
00039 
00040 public:
00045         virtual bool Initialize();
00046 
00050         virtual void OnFocus() { }
00051 
00055         virtual void OnFocusLost() {}
00056 
00060         virtual void OnClick() {}
00061 
00065         virtual void OnPress() {}
00066 
00070         virtual void OnDoubleClick() {}
00071 
00075         virtual void OnRollOver() {}
00076 
00080         virtual void OnRollOut() {}
00081 
00086         virtual void OnDrag() { }
00087 
00092         virtual void OnDropIn(Widget *widget) {}
00093 
00098         virtual void OnDropOut(Widget *widget) {}
00099 
00103         virtual void OnReturn() {}
00104 
00111         bool SetFont(ZAK_FONT_TYPES ft, int size=15);
00112 
00117         void SetFontColor(unsigned int color) { _color = color; }
00118 
00123         int GetFontHeight() { return _size; }
00124 
00129         ZAK_FONT_TYPES GetFont();
00130 
00135         bool Attach(Widget &widget);
00136 
00141         bool Detach(Widget &widget);
00142 
00146         void DetachAll();
00147 
00151         virtual void Draw();
00152 
00156         virtual void Update(float dt);
00157 
00162         bool GetFocused() { return _isFocused; } 
00163 
00168         void SetRelPosX(float x) { _relPosX = x; }
00169 
00174         void SetRelPosY(float y) { _relPosY = y; }
00175 
00181         void SetRelPos (float x, float y) { _relPosX = x; _relPosY = y; }
00182 
00187         float GetRelPosX() { return _relPosX; }
00188 
00193         float GetRelPosY() { return _relPosY; }
00194 
00199         void SetDragable(bool dragable) { _dragable = dragable; }
00200 
00205         bool GetDragable() { return _dragable; }
00206         
00211         void SetEnable(bool enable) { _enable = enable; }
00212 
00217         bool GetEnable() { return _enable; }
00218 
00223         void SetDoubleClickDelay(float dblClickDelay) { _dblClickDelay = dblClickDelay; }
00224 
00229         float GetDoubleClickDelay() { return _dblClickDelay; } 
00230 
00234         Widget();
00235 
00239         virtual ~Widget();
00240 
00241 protected:
00242         virtual void OnFocusInternal() { _isFocused = true; }
00243         virtual void OnFocusLostInternal() {_isFocused = false;  }
00244         virtual void OnClickInternal() {}
00245 
00246         void DrawString(string &text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT);
00247         void DrawString(wstring &text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT);
00248         void DrawString(char* text, float x, float y, float w, float h, unsigned int align=ZAK_TEXT_LEFT);
00249 
00250         Widget *DoEvents(int &event);
00251 
00252         Widget *GetParent() { return _parent; }
00253         WidgetMgr *GetMgr() { return _mgr; }
00254 
00255         void Release();
00256         void Reset();  
00257 
00258         ID3DXFont               *_pDefDXFont;
00259         Widget                  *_parent;
00260         WidgetMgr               *_mgr;
00261         bool                    _editing;
00262         unsigned int    _color;
00263         int                             _size;
00264 
00265 
00266 private:
00267         bool CheckRollOver() {
00268                 static float x1;
00269                 static float y1;
00270                 static float width;
00271                 static float height;
00272                 static float x2;
00273                 static float y2;
00274                 static float xv, yv;
00275 
00276                 width   = GetCollisionWidth();
00277                 height  = GetCollisionHeight();
00278                 x1              = GetPosX()-width*0.5f+GetPivotPosX();
00279                 y1              = GetPosY()-height*0.5f+GetPivotPosY();
00280 
00281                 g_renderer.GetViewPosition(xv, yv);
00282 
00283                 x2 = MouseAbsPosf(ZAK_INPUT_MOUSE_POS_X)-(int)(g_renderer.GetViewPortWidth()/2)+xv;
00284                 y2 = MouseAbsPosf(ZAK_INPUT_MOUSE_POS_Y)-(int)(g_renderer.GetViewPortHeight()/2)+yv;
00285 
00286                 if (x2 > x1 && x2 < x1+width && y2 > y1 && y2 < y1+height) {
00287                         return true;
00288                 }
00289 
00290                 return false;
00291         }       
00292 
00293         List<Widget*>   _widgets;
00294 
00295         float                   _relPosX;
00296         float                   _relPosY;
00297 
00298         bool                    _mouseOver;
00299         bool                    _dragging;
00300         
00301         bool                    _dragable;
00302         bool                    _enable;
00303 
00304         float                   _dblClickDelay;
00305         float                   _accumTime;
00306 
00307         bool                    _isFocused;
00308         bool                    _mouseClick;
00309 
00310         ZAK_FONT_TYPES  _pDefDXFontType;
00311 
00312         friend class WidgetMgr;
00313         friend class Widget;
00314 };
00315 
00320 class ZAKENGINE_API WdgEditBox : public Widget {
00321 public:
00326         string GetText() { return _string; } 
00327 
00332         void SetText(string str);
00333 
00337         virtual void Draw();
00338 
00343         virtual void Update(float dt);
00344 
00345         virtual void OnFocus() {
00346                 OnFocusInternal();
00347         }
00348         virtual void OnFocusLost() {
00349                 OnFocusLostInternal();
00350         }
00351 
00355         WdgEditBox();
00356 
00357 protected:
00358 
00359         void OnFocusInternal();
00360         void OnFocusLostInternal();
00361         void OnClickInternal();
00362 
00363 private:
00364         float           _timeAccum;
00365         bool            _blink;
00366 
00367         bool            _canEnter;
00368         bool            _blinked;
00369         bool            _inserting;
00370         bool            _textinput;
00371         int                     _lastElement;
00372         int                     _maxElement;
00373         int                     _cursor;
00374         int                     _return;
00375         int                     _blinking;
00376         string          _string;
00377         basic_string <char>::iterator _iter;
00378 
00379 };
00380 
00381 #define MAX_CONSOLE_LINES               20 
00382 #define CONSOLE_HEIGHT                  300
00383 
00384 class ZAKENGINE_API WdgConsoleEdit : public WdgEditBox {
00385 public:
00386 
00387         void OnReturn();
00388 
00389         WdgConsoleEdit(){}
00390         ~WdgConsoleEdit(){}
00391 private:
00392 };
00393 
00489 class ZAKENGINE_API WdgConsole : public Widget {
00490 
00491 public:
00496         bool Initialize();
00497 
00501         void Draw();
00502 
00507         void Update(float dt);
00508 
00513         void SetEditColor(DWORD color) { _edit.SetColor(color); }
00514 
00519         void SetEditFontColor(DWORD color) { _edit.SetFontColor(color); }
00520 
00526         void SetEditFont(ZAK_FONT_TYPES ft, int size=15);
00527 
00528         void OnFocus() { _edit.OnFocus(); }
00529         void OnFocusLost() { _edit.OnFocusLost(); }
00530 
00534         WdgConsole();
00535 
00539         ~WdgConsole();
00540 
00541 private:
00542         string                  _text;
00543         WdgConsoleEdit  _edit;
00544 };
00545 
00550 class ZAKENGINE_API WidgetMgr {
00551 
00552 public:
00553 
00557         WidgetMgr();
00558 
00562         virtual ~WidgetMgr();
00563 
00567         void Draw();
00568 
00573         void Update(float dt);
00574 
00580         bool SetFocus(Widget &widget);
00581 
00587         bool IsAttached(Widget &widget);
00588 
00594         bool Attach(Widget &widget);
00595 
00601         bool Detach(Widget &widget);
00602 
00606         void DetachAll();
00607 
00608         void ReleaseAll();
00609         void ResetAll();
00610 
00611 private:
00612         List<Widget*> _widgets;
00613 
00614         Widget *_focused;
00615         Widget *_dragging;
00616 };
00617 
00618 }

Generado el Tue May 29 14:46:15 2007 para Zak Engine v1.1.0 por  doxygen 1.5.1-p1