饭中淹的避难所~~~~~

偶尔来避难的地方~

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  94 随笔 :: 0 文章 :: 257 评论 :: 0 Trackbacks
  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     virtual void Release() = 0;
 57 
 58     virtual bool 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     virtual void AddFirst( IUIElement * pElement ) = 0;
 68     virtual void AddLast( IUIElement * pElement ) = 0;
 69     virtual void InsertAfter( IUIElement * pPrev, IUIElement * pElement ) = 0;
 70     virtual void InsertBefore( IUIElement * pNext, IUIElement * pElement ) = 0;
 71     virtual void Remove( IUIElement * pElement ) = 0;
 72 
 73     virtual void UpdateLayout() = 0;
 74     virtual void Render( IUIDrawContext * pDC ) = 0;
 75     virtual void 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     virtual void 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     virtual void Render( IUIDrawContext * pDC ) = 0;
104     virtual void Update( unsigned long ulMilliSeconds ) = 0;
105 
106     virtual void UpdateLayout() = 0;
107 
108     virtual unsigned long GetId() const = 0;
109     virtual const wchar_t * GetName() const = 0;
110     virtual long GetGlobalX() const = 0;
111     virtual long GetGlobalY() const = 0;
112     virtual long GetX() const = 0;
113     virtual long GetY() const = 0;
114     virtual long GetWidth() const = 0;
115     virtual long GetHeight() const = 0;
116     virtual const wchar_t * GetClassName() const = 0;
117 
118     virtual void SetPosition( long lX, long lY ) = 0;
119     virtual void SetSize( long lWidth, long lHeight ) = 0;
120 
121     virtual bool IsEnable() const = 0;
122     virtual void SetEnable( bool bEnable ) = 0;
123     virtual bool IsVisible() const = 0;
124     virtual void SetVisible( bool bVisible ) = 0;
125     virtual bool IsClipChildren() const = 0;
126     virtual void SetClipChildren( bool bClip ) = 0;
127     virtual bool IsTabStop() const = 0;
128     virtual void SetTabStop( bool bStop ) = 0;
129 
130     virtual int GetTabStopIndex() const = 0;
131 
132     virtual IUIElementEventListener * GetEventListener() const = 0;
133     virtual void SetEventListener( IUIElementEventListener * pEventListener ) = 0;
134     virtual IUIElementDataProvider * GetDataProvider() const = 0;
135     virtual void SetDataProvider( IUIElementDataProvider * pDataProvider ) = 0;
136 public:
137     virtual void OnEnableChanging( bool * pEnable ) = 0;
138     virtual void OnEnableChanged() = 0;
139     virtual void OnVisibleChanging( bool * pVisible ) = 0;
140     virtual void OnVisibleChanged() = 0;
141     virtual void OnClipChildrenChanging( bool * pClip ) = 0;
142     virtual void OnClipChildrenChanged() = 0;
143 
144     virtual void OnPositionChanging( long * pNewX, long * pNewY ) = 0;
145     virtual void OnPositionChanged() = 0;
146     virtual void OnSizeChanging( long * pNewWidth, long * pNewHeight ) = 0;
147     virtual void OnSizeChanged() = 0;
148 
149     virtual void OnInputEvent( int iEventId, void * pParam, int iParam ) = 0;
150 
151 public:
152     virtual void _SetNext( IUIElement * pNext ) = 0;
153     virtual void _SetPrev( IUIElement * pPrev ) = 0;
154     virtual void _SetParent( IUIElement * pParent ) = 0;
155 protected:
156     virtual bool _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     virtual void 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     virtual void * QueryData( IUIElement * pElement, int iDataType, void * pParam, int iParam, int * iGotDataLength ) = 0;
186     virtual void 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     virtual void Release() = 0;
237     virtual IUIElementContainer * GetContainer() const = 0;
238     virtual IUIElement * CreateUIElement( IUIDataSource * pDataSource ) = 0;
239     virtual bool RegisterUIElement( const wchar_t * pszClassName, PFN_CREATEUIELEMENT pfnCreateUIElement ) = 0;
240     virtual void 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     virtual void Render() = 0;
248     virtual void Update(unsigned long ulMilliSeconds) = 0;
249 
250     virtual void SetFocus( IUIElement * pElement ) = 0;
251     virtual IUIElement * GetFocus() const = 0;
252     virtual void SetCapture( IUIElement * pElement ) = 0;
253 
254     virtual void OnInputEvent( int iEventId, void * pParam, int iParam ) = 0;
255 
256     virtual void 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     virtual void Release() = 0;
271     virtual const wchar_t * GetPath() const = 0;
272 };
273 
274 //    UI字体接口,
275 //    提供接口返回文字的横向宽度的
276 //    以及字体的最大高度。
277 class IUIFont : public IUIResource
278 {
279 public:
280     virtual ~IUIFont() {}
281     virtual long GetCharIncX( wchar_t ch ) = 0;
282     virtual long GetHeight() const = 0;
283 };
284 
285 //    音效接口
286 //    因为在实际编码过程中尚未涉及到
287 //    所以这里的接口是临时的
288 //    并未经过仔细斟酌。
289 class IUISoundEffect : public IUIResource
290 {
291 public:
292     virtual ~IUISoundEffect() {}
293     virtual bool Play( bool bLoop = false ) = 0;
294     virtual void Stop() = 0;
295 };
296 
297 //    贴图接口
298 //    提供对图像引擎中的贴图的使用。
299 //    因为后来决定采用4点式贴图选取方式
300 //    从这里开始不兼容GDI了,
301 //    除非为GDI写一套贴图光栅化的东西。
302 class IUITexture : public IUIResource
303 {
304 public:
305     virtual ~IUITexture() {}
306     virtual long GetWidth() const = 0;
307     virtual long GetHeight() const = 0;
308     virtual void UpdateCoordinate( UIPointL ptTable[4] ) = 0;
309     virtual IUITexture * Clone() = 0;
310 };
311 
312 //    数据源接口
313 //    提供了对数据源的访问接口
314 //    让元素内部可以根据数据源进行各种属性的初始化
315 class IUIDataSource
316 {
317 public:
318     virtual ~IUIDataSource() {}
319     virtual IUIDataSource * GetFirstChild() const = 0;
320     virtual IUIDataSource * GetNext() const = 0;
321     virtual IUIDataSource * GetPrev() const = 0;
322     virtual const wchar_t * GetName() const = 0;
323     virtual const wchar_t * GetAttributeString( const wchar_t * pszAttributeName, const wchar_t * pszDefValue ) const = 0;
324     virtual long GetAttributeLong( const wchar_t * pszAttributeName, long lDefValue ) const = 0;
325     virtual unsigned long GetAttributeULong( const wchar_t * pszAttributeName, unsigned long ulDefValue ) const = 0;
326     virtual float GetAttributeFloat( const wchar_t * pszAttributeName, float fDefValue ) const = 0;
327     virtual double GetAttributeDouble( const wchar_t * pszAttributeName, double dDefValue ) const = 0;
328     virtual bool GetAttributeBool( const wchar_t * pszAttributeName, bool bDefValue ) const = 0;
329 
330     virtual IUIDataSource * CloneAlone() const = 0;
331 };
332 
333 //    UI上下文的接口,提供了上下文的共同方法
334 class IUIContext
335 {
336 public:
337     virtual ~IUIContext() {}
338     virtual bool InitWithRoot( IUIRoot * pRoot ) = 0;
339     virtual void Release() = 0;
340 };
341 
342 
343 //    繪圖上下文
344 //    提供了裁剪、绘制贴图、绘制和填充矩形,三角形、绘制线段、绘制文字的方法
345 class IUIDrawContext : public IUIContext
346 {
347 public:
348     virtual ~IUIDrawContext() {}
349 
350     virtual int PushClipper( int x, int y, int w, int h ) = 0;
351     virtual void PopClipper( int iClipperId ) = 0;
352 
353     virtual void DrawTexture( IUITexture * pTex, int x, int y ) = 0;
354     virtual void DrawTexture( IUITexture * pTex, int x, int y, int w, int h ) = 0;
355 
356     virtual void DrawTexture( IUITexture * pTex, int x, int y, unsigned long ulColor ) = 0;
357     virtual void DrawTexture( IUITexture * pTex, int x, int y, int w, int h, unsigned long ulColor ) = 0;
358 
359     virtual void DrawBox( int x, int y, int w, int h, unsigned long ulColor ) = 0;
360     virtual void DrawLine( int x0, int y0, int x1, int y1, unsigned long ulColor ) = 0;
361     virtual void FillBox( int x, int y, int w, int h, unsigned long ulColor ) = 0;
362     virtual void DrawTriangle( int x1, int y1, int x2, int y2, int x3, int y3, unsigned long ulColor ) = 0;
363     virtual void FillTriangle( int x1, int y1, int x2, int y2, int x3, int y3, unsigned long ulColor ) = 0;
364 
365     virtual void SetFont( IUIFont * pFont ) = 0;
366     virtual IUIFont * GetFont() const = 0;
367     virtual void DrawText( const wchar_t * pszText, size_t iLen, int x, int y, unsigned long ulColor ) = 0;
368     virtual void DrawMultiLineText( const wchar_t * pszText, size_t iLen, int x, int y, int iLineWidth, unsigned long ulColor ) = 0;
369     virtual void PrintText( int x, int y, unsigned long ulColor, const wchar_t * pszText,  ) = 0;
370 
371 };
372 //    声音上下文
373 //    因为尚未设计声音接口
374 //    此处为临时设计
375 class IUISoundContext : public IUIContext
376 {
377 public:
378     virtual ~IUISoundContext() {}
379     virtual void SetVolume( int iVolume ) = 0;
380 };
381 
382 //    输入上下文中的鼠标光标
383 //    因为编辑器的需求,
384 //    鼠标光标要表现调整空间大小,拖动控件等
385 //    输入上下文中加入了设置鼠标光标的方法
386 enum EUIINPUTMOUSECURSOR
387 {
388     EUIMC_POINTER,
389     EUIMC_HAND,
390     EUIMC_MOVE,
391     EUIMC_IBEAM,
392     EUIMC_UPDOWNRESIZER,
393     EUIMC_LEFTRIGHTRESIZER,
394     EUIMC_LEFTUPRIGHTDOWNRESIZER,
395     EUIMC_LEFTDOWNRIGHTUPRESIZER,
396     EUIMC_SYSTEMMAX,
397     EUIMC_USERCURSORINDEXBEGIN = 100,
398 };
399 
400 //    輸入上下文
401 //    提供CAPTURE的方法避免鼠标离开窗口后的事件中断
402 //    提供了设置鼠标光标的方法
403 //  提供了键盘测试和取鼠标位置的方法
404 //    还为UI的FOCUS事件处理,提供了IME的一些支持。
405 class IUIInputContext : public IUIContext
406 {
407 public:
408     virtual ~IUIInputContext() {}
409     virtual void BeginCapture() = 0;
410     virtual void EndCapture() = 0;
411 
412     virtual bool RegisterMouseCursor( int iIndex, IUITexture * pCursorTex ) = 0;
413     virtual void SetMouseCursor( int iIndex ) = 0;
414     virtual int GetMouseCursor() const = 0;
415 
416     virtual bool GetMousePosition( int * px, int * py ) const = 0;
417     virtual bool IsKeyDown( int key ) const = 0;
418     //    得到輸入焦點,打開CHAR和IME消息
419     virtual void OnInputElementGotFocus( IUIElement * pInputElement, bool bRequireIme = false ) = 0;
420     //    失去輸入焦點,關閉IME等。
421     virtual void OnInputElementLostFocus( IUIElement * pInputElement ) = 0;
422 };
423 
424 
425 //    以下为脚本接口
426 //    接口只进行了设计,
427 //    并未进行实际的测试和使用
428 //    不过一些需要的功能都在接口中得到了体现。
429 enum EUISCRIPTDATATYPE
430 {
431     EUSDT_INT,
432     EUSDT_UINT,
433     EUSDT_FLOAT,
434     EUSDT_DOUBLE,
435     EUSDT_STRING,
436     EUSDT_POINTER,
437     EUSDT_SCRIPTMETHOD,
438     EUSDT_SCRIPTINSTANCE,
439 };
440 
441 class IUIScriptInstance;
442 class IUIScriptMethod;
443 
444 class IUIScriptValue
445 {
446 public:
447     virtual ~IUIScriptValue() {}
448     virtual void Release() = 0;
449     virtual EUISCRIPTDATATYPE GetType() const = 0;
450 
451     virtual IUIScriptValue * Clone() = 0;
452     virtual long ToInt() const = 0;
453     virtual unsigned long ToUInt() const = 0;
454     virtual const wchar_t * ToString() const = 0;
455     virtual float ToFloat() const = 0;
456     virtual double ToDouble() const = 0;
457     virtual void * ToPointer() const = 0;
458     virtual IUIScriptInstance * ToScriptInstance() const = 0;
459     virtual IUIScriptMethod * ToScriptMethod() const = 0;
460 
461     virtual void SetValue( IUIScriptValue * pValue ) = 0;
462     virtual void SetPointer( void * pVal ) = 0;
463     virtual void SetInt( long lVal ) = 0;
464     virtual void SetUInt( unsigned long ulVal ) = 0;
465     virtual void SetString( const wchar_t * pszVal ) = 0;
466     virtual void SetFloat( float fVal ) = 0;
467     virtual void SetDouble( double dVal ) = 0;
468     virtual void SetScriptInstance( IUIScriptInstance * pInstance ) = 0;
469     virtual void SetScriptMethod( IUIScriptMethod * pMethod ) = 0;
470 };
471 
472 
473 class IUIScriptMethod
474 {
475 public:
476     virtual ~IUIScriptMethod() {}
477     virtual void Release() = 0;
478     virtual IUIScriptInstance * GetOwner() const = 0;
479     virtual IUIScriptValue * CallWithParams( IUIScriptValue ** pParams, int iParamCount ) = 0;
480     virtual IUIScriptValue * Call() = 0;
481 };
482 
483 class IUIScriptInstance
484 {
485 public:
486     virtual ~IUIScriptInstance() {}
487     virtual void Release() = 0;
488     virtual IUIScriptMethod * GetMethod( const wchar_t * pszMethodName ) = 0;
489     virtual IUIScriptValue * GetVariableValue( const wchar_t * pszVariableName ) = 0;
490     virtual void SetVariableValue( const wchar_t * pszVariableName, IUIScriptValue * pValue ) = 0;
491 };
492 
493 class IUIScriptContext : public IUIContext
494 {
495 public:
496     virtual ~IUIScriptContext() {}
497     virtual IUIScriptInstance * GetScriptInstance( const wchar_t * pszPath ) = 0;
498     virtual IUIScriptValue * ValueFromValue( IUIScriptValue * pValue ) = 0;
499     virtual IUIScriptValue * ValueFromPointer( void * pVal ) = 0;
500     virtual IUIScriptValue * ValueFromInt( long lVal ) = 0;
501     virtual IUIScriptValue * ValueFromUInt( unsigned long ulVal ) = 0;
502     virtual IUIScriptValue * ValueFromString( const wchar_t * pszVal ) = 0;
503     virtual IUIScriptValue * ValueFromFloat( float fVal ) = 0;
504     virtual IUIScriptValue * ValueFromDouble( double dVal ) = 0;
505     virtual IUIScriptValue * ValueFromScriptInstance( IUIScriptInstance * pInstance ) = 0;
506     virtual IUIScriptValue * ValueFromScriptMethod( IUIScriptMethod * pMethod ) = 0;
507 };
508 
509 //    UI资源管理器的接口
510 //    提供了获取资源的简单方法
511 class IUIResourceManager : public IUIContext
512 {
513 public:
514     virtual ~IUIResourceManager() {}
515     virtual IUIFont * GetFont( const wchar_t * pszPath ) = 0;
516     virtual IUITexture * GetTexture( const wchar_t * pszPath ) = 0;
517     virtual IUISoundEffect * GetSoundEffect( const wchar_t * pszPath ) = 0;
518 };
519 
520 
521 UICORE_NS_END
522 
523 
524 
525 
posted on 2011-04-30 14:43 饭中淹 阅读(2094) 评论(2)  编辑 收藏 引用 所属分类: 游戏客户端

评论

# re: 【游戏GUI】最近一个版本的UI接口 2011-04-30 23:08 so
首页里连着三篇GUI的文章。(哈哈)  回复  更多评论
  

# re: 【游戏GUI】最近一个版本的UI接口 2011-05-04 09:57 Condor
老大还在造轮子啊  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理