My Project
GUIRoot.h
1 #pragma once
2 #include "GUIBase.h"
3 #include "GUIContainer.h"
4 #include "GUIScript.h"
5 #include "GUIToolTip.h"
6 #include "SpriteRenderer.h"
7 #include "util/mutex.h"
8 #include <vector>
9 #include <string>
10 #include <map>
11 namespace ParaEngine
12 {
13  using namespace std;
15 #ifndef KEYBUFFER_SIZE
16 #define KEYBUFFER_SIZE 15
17 #endif
18  class CGUIIMEEditBox;
19  class CGDIGraphics;
20  class CTouchGesturePinch;
21  struct AssetEntity;
22  typedef std::vector<MSG> GUIMsgEventList_type;
23  struct TouchEvent;
24  struct MouseEvent;
25  struct AccelerometerEvent;
26  class TouchEventSession;
27 
48  class CGUIRoot : public CGUIContainer
49  {
50  public:
52  // implementation of IAttributeFields
53 
55  virtual int GetAttributeClassID(){ return ATTRIBUTE_CLASSID_CGUIRoot; }
57  virtual const char* GetAttributeClassName(){ static const char name[] = "CGUIRoot"; return name; }
59  virtual const char* GetAttributeClassDescription(){ static const char desc[] = ""; return desc; }
61  virtual int InstallFields(CAttributeClass* pClass, bool bOverride);
62 
63  ATTRIBUTE_METHOD1(CGUIRoot, GetUIScale_s, Vector2*) { float x, y; cls->GetUIScale(&x, &y); p1->x = x; p1->y = y; return S_OK; }
64  ATTRIBUTE_METHOD1(CGUIRoot, SetUIScale_s, Vector2) { cls->SetUIScale(p1.x, p1.y); return S_OK; }
65 
66  ATTRIBUTE_METHOD1(CGUIRoot, GetBackBufferSize_s, Vector2*) { cls->GetBackBufferSize(&(p1->x), &(p1->y)); return S_OK; }
67 
68  ATTRIBUTE_METHOD1(CGUIRoot, GetMousePosition_s, Vector2*) { int x, y; cls->GetMousePosition(&x, &y); p1->x = (float)x; p1->y = (float)y; return S_OK; }
69  ATTRIBUTE_METHOD1(CGUIRoot, SetMousePosition_s, Vector2) { cls->SetMousePosition((int)(p1.x), (int)(p1.y)); return S_OK; }
70 
71  ATTRIBUTE_METHOD1(CGUIRoot, GetHasIMEFocus_s, bool*) { *p1 = cls->HasIMEFocus(); return S_OK; }
72  ATTRIBUTE_METHOD1(CGUIRoot, SetHasIMEFocus_s, bool) { cls->SetHasIMEFocus(p1); return S_OK; }
73 
74  ATTRIBUTE_METHOD1(CGUIRoot, GetEnableIME_s, bool*) { *p1 = cls->GetEnableIME(); return S_OK; }
75  ATTRIBUTE_METHOD1(CGUIRoot, SetEnableIME_s, bool) { cls->SetEnableIME(p1); return S_OK; }
76 
77  ATTRIBUTE_METHOD1(CGUIRoot, GetUseSystemCursor_s, bool*) { *p1 = cls->GetUseSystemCursor(); return S_OK; }
78  ATTRIBUTE_METHOD1(CGUIRoot, SetUseSystemCursor_s, bool) { cls->SetUseSystemCursor(p1); return S_OK; }
79 
80  ATTRIBUTE_METHOD1(CGUIRoot, GetFingerSizePixels_s, int*) { *p1 = cls->GetFingerSizePixels(); return S_OK; }
81  ATTRIBUTE_METHOD1(CGUIRoot, SetFingerSizePixels_s, int) { cls->SetFingerSizePixels(p1); return S_OK; }
82 
83  ATTRIBUTE_METHOD1(CGUIRoot, GetFingerStepSizePixels_s, int*) { *p1 = cls->GetFingerStepSizePixels(); return S_OK; }
84  ATTRIBUTE_METHOD1(CGUIRoot, SetFingerStepSizePixels_s, int) { cls->SetFingerStepSizePixels(p1); return S_OK; }
85 
86  ATTRIBUTE_METHOD1(CGUIRoot, SendKeyDownEvent_s, int) { cls->SendKeyDownEvent(p1); return S_OK; }
87  ATTRIBUTE_METHOD1(CGUIRoot, SendKeyUpEvent_s, int) { cls->SendKeyUpEvent(p1); return S_OK; }
88 
89  ATTRIBUTE_METHOD1(CGUIRoot, IsMouseButtonSwapped_s, bool*) { *p1 = cls->IsMouseButtonSwapped(); return S_OK; }
90  ATTRIBUTE_METHOD1(CGUIRoot, SetMouseButtonSwapped_s, bool) { cls->SetMouseButtonSwapped(p1); return S_OK; }
91 
92  ATTRIBUTE_METHOD1(CGUIRoot, SendInputMethodEvent_s, const char*) { cls->SendInputMethodEvent(p1); return S_OK; }
93 
94  ATTRIBUTE_METHOD1(CGUIRoot, SetMinimumScreenSize_s, Vector2) { cls->SetMinimumScreenSize((int)(p1.x), (int)(p1.y)); return S_OK; }
95 
96  ATTRIBUTE_METHOD1(CGUIRoot, IsMouseCaptured_s, bool*) { *p1 = cls->IsMouseCaptured(); return S_OK; }
97  ATTRIBUTE_METHOD1(CGUIRoot, SetCaptureMouse_s, bool) { cls->SetCaptureMouse(p1); return S_OK; }
98 
99  ATTRIBUTE_METHOD1(CGUIRoot, IsNonClient_s, bool*) { *p1 = cls->IsNonClient(); return S_OK; }
100  ATTRIBUTE_METHOD1(CGUIRoot, SetIsNonClient_s, bool) { cls->SetIsNonClient(p1); return S_OK; }
101  public:
102  ParaEngine::GUIState& GetGUIState() { return m_stateGUI; }
103  CDirectMouse* GetMouse() { return m_pMouse; }
104  CDirectKeyboard* GetKeyboard() { return m_pKeyboard; }
106  void SetCaptureMouse(bool bCapture);
107  bool IsMouseCaptured();
108 
109  bool PushEvent(const MSG& msg);
110  bool handleTouchEvent(const TouchEvent& touch);
111  bool handleNonClientTest(const MouseEvent& mouseEvent);
112  bool handleAccelerometerEvent(const AccelerometerEvent& accelerator);
113  bool handleGesturePinch(CTouchGesturePinch& pinch_gesture);
114  void TranslateMousePointInTouchEvent(TouchEvent &touch);
115 
122  void SetUIScale(float fScalingX, float fScalingY, bool bEnsureMinimumScreenSize = true, bool bNotifySizeChange = true);
123 
128  void GetUIScale(float* pfScalingX = NULL, float* pfScalingY = NULL);
129 
131  void GetBackBufferSize(float* pWidth, float* pHeight);
132 
140  void SetMinimumScreenSize(int nWidth, int nHeight, bool bAutoUIScaling = true);
141 
142 
146  bool UpdateViewport(int nLeft, int nTop, int nWidth, int nHeight, bool bForceUpdate = false);
147 
153  void PushTopLevelControl(CGUIContainer* pTopLevelControl);
158  void RemoveTopLevelControl(CGUIContainer* pTopLevelControl);
159 
161  void ClearAllTopLevelControls();
162 
166  CGUIContainer* GetTopLevelControl(){ return m_TopLevelCtrls[0]; };
167 
168  CGUIBase* GetIMEFocus() const;
169  void SetIMEFocus(CGUIBase* val);
170 
177  static void DeleteGUIObject(CGUIBase * pObj);
178 
179  static void PostDeleteGUIObject(CGUIBase * pObj);
185  void AttachGUIElement(CGUIBase* pParent, CGUIBase* pChild);
186 
192  void DetachGUIElement(CGUIBase* pChild);
193 
199  int DestroyGUIElement(CGUIBase* pObj);
200 
207  int DestroyGUIElement(const char* sID);
208 
214  CGUIBase* GetUIObject(const char * strObjectName);
215  CGUIBase* GetUIObject(const std::string& strObjectName);
216 
222  CGUIBase* GetUIObject(int nID);
223 
230  CGUIBase* GetUIObject(int x, int y);
231 
240  CGUIBase* GetDefaultObject(const char *strType);
241 
247  CGUIBase* GetUIKeyFocus();
248 
250  void SetUIKeyFocus(CGUIBase* control);
251 
257  CGUIBase* GetUIMouseFocus();
258 
260  void SetUIMouseFocus(CGUIBase* control);
261 
263  void UseDefaultMouseCursor(bool bUseDefaultMouseCursor);
264 
275  int HandleUserInput();
276 
281  void SendKeyDownEvent(int nVirtualkey);
282  void SendKeyUpEvent(int nVirtualkey);
283  void SendInputMethodEvent(const char* pStr);
284 
286  bool IsMouseButtonSwapped();
287  void SetMouseButtonSwapped(bool bSwapped);
288 
289  bool DispatchKeyboardMsg(bool bKeyHandled);
290 
291  void DispatchTouchMouseEvent(bool &bMouseHandled);
292 
293  //virtual bool MsgProc(CGUIEvent *event=NULL);
294  virtual bool MsgProc(MSG *event);
298  virtual void Reset();
299 
303  virtual void DestroyChildren();
304 
308  void Clear();
312  void Initialize();
316  void AdvanceGUI(float fElapsedTime);
317 
318 
319  void AddScript();
320 
321  HRESULT OneTimeGUIInit();
322  LRESULT MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, bool &bNoFurtherProcess);
323 
324  virtual HRESULT Render(GUIState* pGUIState, float fElapsedTime);
325  virtual bool OnClick(int MouseState, int X, int Y);
326 
327  virtual HRESULT InitDeviceObjects(); // device independent
328  HRESULT RestoreDeviceObjects(UINT nBkbufWidth, UINT nBkbufHeight); // device dependent
329  virtual HRESULT InvalidateDeviceObjects();
330  virtual HRESULT DeleteDeviceObjects();
331  virtual int Release();
332  virtual string ToScript(int option = 0);
333 
334  //to set whether the mouse cursor is in the client area of the program
335  void SetMouseInClient(bool bMouseInClient);
336  bool GetMouseInClient();
338  bool CheckLoadCursor();
339 
341  void ActivateRoot();
343  void InactivateRoot();
346  bool IsActive();
347 
348  bool IsKeyboardProcessed();
349  bool IsMouseProcessed();
350 
354  bool IsNonClient() const;
355  void SetIsNonClient(bool val);
356 
362  void GetMousePosition(int* nX, int* nY) const;
363 
364  void SetMousePosition(int nX, int nY);
365 
367  int GetFingerSizePixels() const;
368  void SetFingerSizePixels(int val);
370  int GetFingerStepSizePixels() const;
371  void SetFingerStepSizePixels(int val);
372 
373  Matrix4* Get3DViewProjMatrix(){ return &m_matViewProj; };
374  void Set3DViewProjMatrix(const Matrix4& mat){ m_matViewProj = mat; };
375 #ifdef USE_DIRECTX_RENDERER
376  DEVMODE *GetDisplayMode(){return &m_displaymode;}
377 #endif
378 
379  void AddToNameMap(const string& name, CGUIBase* pObj);
381  void AddToIDMap(int nID, CGUIBase* pObj);
382 
383  std::map <string, CGUIBase*> m_namemap;
384  std::map <int, CGUIBase*> m_idmap;
385 
386  //some static function and variables
387  static CGUIRoot* GetInstance();
388  static CGUIRoot* CreateInstance();
389  static void DeleteInstance(CGUIRoot* pThis = NULL);
390 
392  void SetRenderImageCursor(bool bEnable);
393 
395  void SetUseSystemCursor(bool bUseSystem);
396 
398  bool GetUseSystemCursor();
399 
400  virtual const IType* GetType()const{ return m_type; }
401 
403  bool HasIMEFocus();
404  void SetHasIMEFocus(bool bHasFocus);
405 
407  bool GetEnableIME();
408  void SetEnableIME(bool bHasIME);
409 
413  void EnableClipCursor(bool bEnable);
414 
416  bool IsCursorClipped();
417 
418  CGUIBase* GetActiveWindow() const;
419  void SetActiveWindow(CGUIBase* val);
420  public:
421  virtual ~CGUIRoot(void);
422 
423  //
424  // interface of PaintDevice
425  //
426  virtual CPaintEngine * paintEngine() const;
427  virtual int metric(PaintDeviceMetric metric) const;
428  virtual float GetUIScalingX() const;
429  virtual float GetUIScalingY() const;
430  virtual float GetViewportLeft() const;
431  virtual float GetViewportTop() const;
432 
433  /* get painter with default device. mostly used for text size calculation, etc. */
434  CPainter* GetPainter();
435  private:
441  void Update3DObject(float fElapsedTime);
442 
444  void UpdateCursorPosition();
445 
446  void TranslateMousePos(int &inout_x, int &inout_y);
447 
448  void TranslateTouchEvent(const TouchEvent &touch);
449 
452  bool AutoLocateTouchClick(int ui_mouse_x, int ui_mouse_y, int &mouse_x, int &mouse_y);
453 
454  public:
455  static DWORD WheelScrollLines;
456  static DWORD KeyDelay;
457  static DWORD KeyRepeat;
458  static DWORD MouseDelay;
459  static DWORD MouseRepeat;
460  static DWORD MouseHover;
461  static int MouseThreshold1;
462  static int MouseThreshold2;
463  static DWORD MouseAcceleration;
464  static float MouseSpeed;
465 
469  //shall delete
470  //list <MouseEvent> m_MouseEvents;
471 
472  GUIMsgEventList_type m_events;
473 
474  CDirectKeyboard* m_pKeyboard;
475 
476  CDirectMouse* m_pMouse;
477 
478  // GUI_DRAG_AND_DROP m_pDragObj;
479 
480  CGUIBase* m_IMEFocus;
481 
482 
483  CGUIScript m_scripts;
484 
485  bool m_bDesign;
487  bool m_bActive;
488  CGUIToolTip* m_tooltip;
489 
491  vector <CGUIContainer*> m_TopLevelCtrls;
492 
493  protected:
494  static const IType* m_type;
495  CGUIRoot(void);
496 
497  mutable CPaintEngine *engine;
498  bool m_bIsCursorClipped;
499  RECT m_rcOldClipRect;
500  bool m_bMouseProcessed;
501  bool m_bKeyboardProcessed;
502  bool m_bMouseCaptured;
505 #ifdef USE_DIRECTX_RENDERER
506  DEVMODE m_displaymode;
507 #endif
508  Matrix4 m_matViewProj;
509  bool m_bMouseInClient;
510  bool m_bWindowChanging;//when window moved or resize, it is set true;
511  bool m_bRenderImageCursor;
512  bool m_bUseSystemCursor;
513  // delete queue id
514  vector<int> m_deleteQueue;
515 
516  float m_fUIScalingX;
517  float m_fUIScalingY;
518 
519 
520  float m_fViewportLeft;
521  float m_fViewportTop;
522  float m_fViewportWidth;
523  float m_fViewportHeight;
524 
529 
532 
541  int m_nFingerStepSizePixels;
543  int m_nLastTouchX, m_nLastTouchY;
545  std::map<int, int> m_touch_id_to_ui_obj;
546 
549  CPainter* m_pPainter;
550  GUIState m_stateGUI;
553 
554  friend CGUIBase;
555  };
556 }
IType is for type information and validating It contains type information of an object.
Definition: Type.h:75
Touch gesture recognizer.
Definition: TouchGesturePinch.h:8
an attribute class is a collection of attribute fields.
Definition: AttributeClass.h:10
bool m_bHasIMEFocus
whether there is a visible IME virtual CGUIIMEEditBox that has key focus.
Definition: GUIRoot.h:534
Definition: EventBinding.h:22
float m_fMinScreenHeight
the minimum screen height.
Definition: GUIRoot.h:531
ParaEngine::mutex m_mutex
mostly for the ime focus.
Definition: GUIRoot.h:548
different physics engine has different winding order.
Definition: EventBinding.h:32
Definition: ManagedDef.h:18
std::map< int, int > m_touch_id_to_ui_obj
mapping from touch id to ui object.
Definition: GUIRoot.h:545
float m_fMinScreenWidth
the minimum screen width.
Definition: GUIRoot.h:528
simple mouse event struct
Definition: EventClasses.h:27
Definition: GUIDirectInput.h:106
GUIMsgEventList_type m_events
GUI root&#39;s mouse events.
Definition: GUIRoot.h:472
virtual int GetAttributeClassID()
attribute class ID should be identical, unless one knows how overriding rules work.
Definition: GUIRoot.h:55
base object for all 2D GUI objects (1) 2D GUI object are not tested against view frustum, instead it is controlled by visibility tag automatically or through user input.
Definition: GUIBase.h:54
IAttributeFields * m_pLastMouseDownObject
please note this can be the 3d scene object.
Definition: GUIRoot.h:504
Standard 2-dimensional vector.
Definition: ParaVector2.h:16
vector< CGUIContainer * > m_TopLevelCtrls
the top level controls in a array, m_TopCtrl[0] always refers to the top level, m_TopCtrl[1] refers t...
Definition: GUIRoot.h:491
Definition: GUIDirectInput.h:112
touch event
Definition: EventClasses.h:118
m_objResource->m_objArtwork->DrawingRects[0]: client rectangle; m_objResource->m_objArtwork->DrawingR...
Definition: GUIContainer.h:19
base class for a surface to paint to.
Definition: Painter.h:38
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: ParaMatrix4.h:23
It&#39;s used as parameter to Render method of each GUI object.
Definition: GUIState.h:16
virtual const char * GetAttributeClassDescription()
a static string, describing the attribute class object
Definition: GUIRoot.h:59
bool m_bIsNonClient
whether the mouse is in non-client area, we will allow the user to drag the window if this is true...
Definition: GUIRoot.h:538
m_objResource->m_objArtwork->DrawingRects[0]: text rectangle; m_objResource->m_objArtwork->DrawingRec...
Definition: GUIToolTip.h:50
A common interface for all classes implementing IAttributeFields By implementing this class&#39;s virtual...
Definition: IAttributeFields.h:59
The CPaintEngine class provides an abstract definition of how CPainter draws to a given device on a g...
Definition: PaintEngine.h:34
the total 3d vector of force that is currently on the device, including gravity.
Definition: EventClasses.h:162
int m_nFingerSizePixels
touch finger size in pixels.
Definition: GUIRoot.h:540
CGUIContainer * GetTopLevelControl()
get the top level control at level 0
Definition: GUIRoot.h:166
CGUIBase * m_pActiveWindow
last active GUI object object who has onactivate event handler in script.
Definition: GUIRoot.h:552
int m_nLastTouchX
last touch position in mobile version.
Definition: GUIRoot.h:543
bool m_bActive
if the gui is active
Definition: GUIRoot.h:487
cross platform mutex
Definition: mutex.h:95
virtual const char * GetAttributeClassName()
a static string, describing the attribute class object&#39;s name
Definition: GUIRoot.h:57
When rendering scene, root scene and the root GUI are rendered in sequence.
Definition: GUIRoot.h:48