1. 程式人生 > >【遊戲GUI】最近一個版本的UI介面

【遊戲GUI】最近一個版本的UI介面

  1 #pragma once
  2   3 #include "Define.h"  4   5 UICORE_NS_BEGIN
  6   7 //    UI核心介面的版本號
  8 //    0.1.0.0  9 #define    UICORE_VERSION    0x00010000 10  11 //    UI點座標 12 template<typename tcoordinate> 13 struct TUIPoint
 14 {
 15     tcoordinate x;
 16     tcoordinate y;
 17 };
 18  19 //    整數型的點座標 20 typedef TUIPoint
<long> UIPointL;
 21 //    浮點型的點座標 22 typedef TUIPoint<float> UIPointF;
 23  24 //    前置介面宣告 25 class IUIFont;    //    字型介面 26 class IUISoundEffect;    //    音效介面 27 class IUITexture;        //    貼圖介面 28 class IUIDataSource;    //    資料來源 29 class IUISoundContext;    //    聲音上下文 30 class IUIDrawContext;    
//    繪圖上下文 31 class IUIInputContext;    //    輸入上下文 32 class IUIResourceManager;    //    資源管理器  33  34 class IUIRoot;                //    UI的根介面 35 class IUIElementEventListener;    //    UI事件監聽者介面 36 class IUIElementDataProvider;    //    UI資料提供者介面 37  38  39 class IUIElement;            //    UI元素介面 
 40  41  42
 //    UI元素的控制器 43 class IUIElementController
 44 {
 45 public:
 46 virtual~IUIElementController() {}
 47 virtual IUIElement * GetTarget() const=0;
 48 };
 49  50 //    UI元素的容器 51 class IUIElementContainer
 52 {
 53 public:
 54 virtual~IUIElementContainer() {}
 55  56 virtualvoid Release() =0;
 57  58 virtualbool IsEmpty() const=0;
 59  60 virtual IUIElement * GetFirstElement() const=0;
 61 virtual IUIElement * GetLastElement() const=0;
 62 virtual IUIElement * GetElementByPath( const wchar_t * pszPath ) const=0;
 63 virtual IUIElement * GetElementById( unsigned long ulId ) const=0;
 64 virtual IUIElement * GetElementByName( const wchar_t * pszName, unsigned int iLen ) const=0;
 65 virtual IUIElement * GetElementByPosition( long x, long y, IUIElement ** ppTopParentElement =0 ) const=0;
 66  67 virtualvoid AddFirst( IUIElement * pElement ) =0;
 68 virtualvoid AddLast( IUIElement * pElement ) =0;
 69 virtualvoid InsertAfter( IUIElement * pPrev, IUIElement * pElement ) =0;
 70 virtualvoid InsertBefore( IUIElement * pNext, IUIElement * pElement ) =0;
 71 virtualvoid Remove( IUIElement * pElement ) =0;
 72  73 virtualvoid UpdateLayout() =0;
 74 virtualvoid Render( IUIDrawContext * pDC ) =0;
 75 virtualvoid Update(unsigned long ulMilliSeconds) =0;
 76  77 };
 78  79 //    UI元素
 80 //    因為原本希望UI元素也可以存在多種實現
 81 //    後來發現這個沒必要。
 82 // 83 class IUIElement
 84 {
 85 public:
 86 virtual~IUIElement() {}
 87  88 virtualvoid Release() =0;
 89  90 virtual IUIElementContainer * GetContainer() const=0;
 91  92 virtual IUIElementController * GetController() const=0;
 93     template <class TController> 94     inline TController * GetController() const {
 95 return dynamic_cast<TController*>( GetController() );
 96     }
 97  98 virtual IUIElement * GetNext() const=0;
 99 virtual IUIElement * GetPrev() const=0;
100 virtual IUIElement * GetParent() const=0;
101 virtual IUIRoot * GetRoot() const=0;
102 103 virtualvoid Render( IUIDrawContext * pDC ) =0;
104 virtualvoid Update( unsigned long ulMilliSeconds ) =0;
105 106 virtualvoid UpdateLayout() =0;
107 108 virtual unsigned long GetId() const=0;
109 virtualconst wchar_t * GetName() const=0;
110 virtuallong GetGlobalX() const=0;
111 virtuallong GetGlobalY() const=0;
112 virtuallong GetX() const=0;
113 virtuallong GetY() const=0;
114 virtuallong GetWidth() const=0;
115 virtuallong GetHeight() const=0;
116 virtualconst wchar_t * GetClassName() const=0;
117 118 virtualvoid SetPosition( long lX, long lY ) =0;
119 virtualvoid SetSize( long lWidth, long lHeight ) =0;
120 121 virtualbool IsEnable() const=0;
122 virtualvoid SetEnable( bool bEnable ) =0;
123 virtualbool IsVisible() const=0;
124 virtualvoid SetVisible( bool bVisible ) =0;
125 virtualbool IsClipChildren() const=0;
126 virtualvoid SetClipChildren( bool bClip ) =0;
127 virtualbool IsTabStop() const=0;
128 virtualvoid SetTabStop( bool bStop ) =0;
129 130 virtualint GetTabStopIndex() const=0;
131 132 virtual IUIElementEventListener * GetEventListener() const=0;
133 virtualvoid SetEventListener( IUIElementEventListener * pEventListener ) =0;
134 virtual IUIElementDataProvider * GetDataProvider() const=0;
135 virtualvoid SetDataProvider( IUIElementDataProvider * pDataProvider ) =0;
136 public:
137 virtualvoid OnEnableChanging( bool* pEnable ) =0;
138 virtualvoid OnEnableChanged() =0;
139 virtualvoid OnVisibleChanging( bool* pVisible ) =0;
140 virtualvoid OnVisibleChanged() =0;
141 virtualvoid OnClipChildrenChanging( bool* pClip ) =0;
142 virtualvoid OnClipChildrenChanged() =0;
143 144 virtualvoid OnPositionChanging( long* pNewX, long* pNewY ) =0;
145 virtualvoid OnPositionChanged() =0;
146 virtualvoid OnSizeChanging( long* pNewWidth, long* pNewHeight ) =0;
147 virtualvoid OnSizeChanged() =0;
148 149 virtualvoid OnInputEvent( int iEventId, void* pParam, int iParam ) =0;
150 151 public:
152 virtualvoid _SetNext( IUIElement * pNext ) =0;
153 virtualvoid _SetPrev( IUIElement * pPrev ) =0;
154 virtualvoid _SetParent( IUIElement * pParent ) =0;
155 protected:
156 virtualbool _Init( IUIRoot * pRoot, IUIElement * pParent, IUIDataSource * pDataSource ) =0;
157 };
158 159 //    UI元素的普通事件160 enum EnumUIElementStandardEvent
161 {
162     EUISE_SHOW,
163     EUISE_HIDE,
164     EUISE_LOADED,
165     EUISE_UNLOAD,
166     EUISE_MOVING,
167     EUISE_MOVED,
168 };
169 170 //    事件監聽者171 class IUIElementEventListener
172 {
173 public:
174 virtual~IUIElementEventListener() {}
175 virtualvoid OnEvent( IUIElement * pElement, int iEventId, void* pParam, int iParam ) =0;
176 };
177 178 //    資料提供者
179 //    對於列表框等元素容器類的UI元素
180 //    使用這個介面可以方便應用程式向UI提供資料181 class IUIElementDataProvider
182 {
183 public:
184 virtual~IUIElementDataProvider() {}
185 virtualvoid* QueryData( IUIElement * pElement, int iDataType, void* pParam, int iParam, int* iGotDataLength ) =0;
186 virtualvoid FreeData( IUIElement * pElement, int iDataType, void* pData, int iDataLength ) =0;
187 };
188 189 //    輸入事件190 enum EnumUIElementStandardInputEvent
191 {
192     EUISIE_MOUSE_INPUTEVENT_START,
193     EUISIE_MOUSEENTER,
194     EUISIE_MOUSELEAVE,
195     EUISIE_MOUSEMOVE,
196     EUISIE_MOUSEWHEEL,
197     EUISIE_MOUSELBUTTONDOWN,
198     EUISIE_MOUSELBUTTONUP,
199     EUISIE_MOUSERBUTTONDOWN,
200     EUISIE_MOUSERBUTTONUP,
201     EUISIE_MOUSEMBUTTONDOWN,
202     EUISIE_MOUSEMBUTTONUP,
203     EUISIE_MOUSE_INPUTEVENT_USERDEFINE_START,
204     EUISIE_KEYBORAD_INPUTEVENT_START =1000,
205     EUISIE_KEYDOWN,
206     EUISIE_KEYUP,
207     EUISIE_CHAR,
208     EUISIE_IME_CHAR,
209     EUISIE_IME_RESULT,
210     EUISIE_KEYBORAD_INPUTEVENT_USERDEFINE_START,
211 212     EUISIE_GOTFOCUS,    //    取得焦點213     EUISIE_LOSTFOCUS,    //    失去焦點214     EUISIE_ACTIVATE,    //    啟用215     EUISIE_INACTIVATE,    //    非啟用216     EUISIE_GOTTOPPOSITION,    //    獲取到最高點(用於選單和COMBOBOX的LIST顯示)217     EUISIE_LOSTTOPPOSITION,    //    失去最高點218     EUISIE_POPUPCLOSE,
219 };
220 221 222 //    UI元素的建立函式
223 //    用於註冊到UIROOT中
224 //    以便於從資料來源中根據TAG名字直接建立UI元素。225 typedef IUIElement * (__stdcall * PFN_CREATEUIELEMENT)( IUIRoot * pRoot, IUIElement * pParent, IUIDataSource * pDataSource );
226 227 228 //    UI根介面
229 //    作為整個UI系統的管理中心
230 //    其中POPUP的方法,用來處理諸如combobox的彈出式列表框,彈出式選單等TOPMOST的UI元素。231 class IUIRoot
232 {
233 public:
234 virtual~IUIRoot() {}
235 236 virtualvoid Release() =0;
237 virtual IUIElementContainer * GetContainer() const=0;
238 virtual IUIElement * CreateUIElement( IUIDataSource * pDataSource ) =0;
239 virtualbool RegisterUIElement( const wchar_t * pszClassName, PFN_CREATEUIELEMENT pfnCreateUIElement ) =0;
240 virtualvoid UnregisterUIElement( const wchar_t * pszClassName ) =0;
241 virtual PFN_CREATEUIELEMENT GetUIElementCreator( const wchar_t * pszClassName ) =0;
242 virtual IUIDrawContext * GetDrawContext() const=0;
243 virtual IUIInputContext * GetInputContext() const=0;
244 virtual IUISoundContext * GetSoundContext() const=0;
245 virtual IUIResourceManager * GetResourceManager() const=0;
246 247 virtualvoid Render() =0;
248 virtualvoid Update(unsigned long ulMilliSeconds) =0;
249 250 virtualvoid SetFocus( IUIElement * pElement ) =0;
251 virtual IUIElement * GetFocus() const=0;
252 virtualvoid SetCapture( IUIElement * pElement ) =0;
253 254 virtualvoid OnInputEvent( int iEventId, void* pParam, int iParam ) =0;
255 256 virtualvoid PopupElement( IUIElement * pParent, IUIElement * pPopupElement, long x, long y ) =0;
257 258 };
259 260 261 262 263 264 //    UI資源介面
265 //    提供了資源的共有方法266 class IUIResource
267 {
268 public:
269 virtual~IUIResource() {}
270 virtualvoid Release() =0;
271 virtualconst wchar_t * GetPath() const=0;
272 };
273 274 //    UI字型介面,
275 //    提供介面返回文字的橫向寬度的
276 //    以及字型的最大高度。277 class IUIFont : public IUIResource
278 {
279 public:
280 virtual~IUIFont() {}
281 virtuallong GetCharIncX( wchar_t ch ) =0;
282 virtuallong GetHeight() const=0;
283 };
284 285 //    音效介面
286 //    因為在實際編碼過程中尚未涉及到
287 //    所以這裡的介面是臨時的
288 //    並未經過仔細斟酌。289 class IUISoundEffect : public IUIResource
290 {
291 public:
292 virtual~IUISoundEffect() {}
293 virtualbool Play( bool bLoop =false ) =0;
294 virtualvoid Stop() =0;
295 };
296 297 //    貼圖介面
298 //    提供對影象引擎中的貼圖的使用。
299 //    因為後來決定採用4點式貼圖選取方式
300 //    從這裡開始不相容GDI了,
301 //    除非為GDI寫一套貼圖光柵化的東西。302 class IUITexture : public IUIResource
303 {
304 public:
305 virtual~IUITexture() {}
306 virtuallong GetWidth() const=0;
307 virtuallong GetHeight() const=0;
308 virtualvoid UpdateCoordinate( UIPointL ptTable[4] ) =0;
309 virtual IUITexture * Clone() =0;