Widget.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Copyright © 2011-2020 Frictional Games
  3. *
  4. * This file is part of Amnesia: A Machine For Pigs.
  5. *
  6. * Amnesia: A Machine For Pigs is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. * Amnesia: A Machine For Pigs is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Amnesia: A Machine For Pigs. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #ifndef HPL_WIDGET_H
  19. #define HPL_WIDGET_H
  20. #include <list>
  21. #include <vector>
  22. #include "gui/GuiTypes.h"
  23. #include "system/LowLevelSystem.h"
  24. namespace hpl {
  25. class cGui;
  26. class cGuiSet;
  27. class cGuiSkin;
  28. class cGuiSkinFont;
  29. class cGuiGfxElement;
  30. class cGuiClipRegion;
  31. class cGuiGlobalShortcut;
  32. class iFontData;
  33. //--------------------------------
  34. struct cWidgetCallback
  35. {
  36. cWidgetCallback(void *apObject, tGuiCallbackFunc apFunc)
  37. {
  38. mpObject = apObject;
  39. mpFunc = apFunc;
  40. }
  41. void *mpObject;
  42. tGuiCallbackFunc mpFunc;
  43. };
  44. typedef std::list<cWidgetCallback> tWidgetCallbackList;
  45. typedef tWidgetCallbackList::iterator tWidgetCallbackListIt;
  46. //--------------------------------
  47. class iWidget
  48. {
  49. friend class cGuiSet;
  50. public:
  51. iWidget(eWidgetType aType,cGuiSet *apSet, cGuiSkin *apSkin);
  52. virtual ~iWidget();
  53. /////////////////////////
  54. //General
  55. void Update(float afTimeStep);
  56. void Draw(float afTimeStep, cGuiClipRegion *apClipRegion);
  57. bool ProcessMessage(eGuiMessage aMessage, const cGuiMessageData& aData, bool abSkipVisCheck=false, bool abSkipEnabledCheck=false);
  58. void AddCallback(eGuiMessage aMessage,void *apObject,tGuiCallbackFunc apFunc);
  59. eWidgetType GetType(){ return mType;}
  60. void Init();
  61. virtual cGuiGlobalShortcut* AddShortcut(int alKeyModifiers, eKey aKey, eGuiMessage aMsg=eGuiMessage_ButtonPressed, bool abBypassVisibility=true, bool abBypassEnabled=true);
  62. /////////////////////////
  63. // ToolTip functions
  64. void SetToolTip(const tWString& asToolTip);
  65. const tWString& GetToolTip() { return msToolTip; }
  66. void SetToolTipEnabled(bool abX) { mbToolTipEnabled = abX; }
  67. bool IsToolTipEnabled() { return mbToolTipEnabled; }
  68. /////////////////////////
  69. // Public Helper functions
  70. virtual bool PointIsInside(const cVector2f& avPoint, bool abOnlyClipped);
  71. /////////////////////////
  72. //Hierarchy
  73. virtual void AttachChild(iWidget *apChild);
  74. virtual void RemoveChild(iWidget *apChild);
  75. tWidgetList& GetChildren() { return mlstChildren; }
  76. virtual void OnAttachChild(iWidget* apChild){}
  77. virtual void OnRemoveChild(iWidget* apChild){}
  78. /////////////////////////
  79. //Properties
  80. cGuiSet* GetSet(){ return mpSet; }
  81. iWidget *GetParent(){ return mpParent;}
  82. void SetEnabled(bool abX);
  83. bool IsEnabled();
  84. void SetVisible(bool abX);
  85. bool IsVisible();
  86. bool HasFocus();
  87. void SetName(const tString& asName){ msName = asName;}
  88. const tString& GetName(){ return msName;}
  89. void SetText(const tWString& asText);
  90. const tWString& GetText()const{ return msText; }
  91. iFontData *GetDefaultFontType(){ return mpDefaultFontType;}
  92. virtual void SetDefaultFontType(iFontData *apFont){ mpDefaultFontType = apFont;}
  93. const cColor& GetDefaultFontColor(){ return mDefaultFontColor;}
  94. virtual void SetDefaultFontColor(const cColor& aColor){ mDefaultFontColor = aColor;}
  95. const cVector2f& GetDefaultFontSize(){ return mvDefaultFontSize;}
  96. virtual void SetDefaultFontSize(const cVector2f& avSize){ mvDefaultFontSize = avSize;}
  97. void SetClipActive(bool abX){ mbClipsGraphics = abX;}
  98. bool GetClipActive(){ return mbClipsGraphics;}
  99. void SetPosition(const cVector3f &avPos);
  100. void SetGlobalPosition(const cVector3f &avPos);
  101. const cVector3f& GetLocalPosition();
  102. const cVector3f& GetGlobalPosition();
  103. void SetChildrenOffset(const cVector3f&);
  104. const cVector3f& GetChildrenOffset() { return mvChildrenOffset; }
  105. void SetAffectedByScroll(bool abX);
  106. void SetScrollAmount(const cVector3f& avX);
  107. const cVector3f& GetScrollAmount() { return mvScrollAmount; }
  108. void CenterGlobalPositionInSet();
  109. void SetSize(const cVector2f &avSize);
  110. cVector2f GetSize(){ return mvSize;}
  111. void SetColorMul(const cColor &aColor){ mColorMul = aColor;}
  112. const cColor& GetColorMul(){ return mColorMul;}
  113. bool ClipsGraphics();
  114. bool GetMouseIsOver(){ return mbMouseIsOver;}
  115. bool IsConnectedTo(iWidget *apWidget, bool abIsStartWidget=true);
  116. bool IsConnectedToChildren(){ return mbConnectedToChildren;}
  117. void SetConnectedToChildren(bool abX){ mbConnectedToChildren = abX;}
  118. cGuiGfxElement* GetPointerGfx();
  119. void SetGlobalKeyPressListener(bool abX) { mbGlobalKeyPressListener = abX; }
  120. bool IsGlobalKeyPressListener() { return mbGlobalKeyPressListener; }
  121. void SetUserData(void *apData) { mpUserData = apData;}
  122. void * GetUserData() { return mpUserData;}
  123. void SetUserValue(int alX) { mlUserValue = alX;}
  124. int GetUserValue() { return mlUserValue;}
  125. void SetCallbacksDisabled(bool abX) { mbCallbacksDisabled = abX; }
  126. bool GetCallbacksDisabled() { return mbCallbacksDisabled; }
  127. void SetFocusNavigation(eUIArrow aDir, iWidget* apWidget);
  128. iWidget* GetFocusNavigation(eUIArrow aDir);
  129. bool HasFocusNavigation();
  130. void SetGlobalUIInputListener(bool abX) { mbGlobalUIInputListener = abX; }
  131. bool IsGlobalUIInputListener() { return mbGlobalUIInputListener; }
  132. protected:
  133. /////////////////////////
  134. // Upper Widget functions
  135. virtual void OnLoadGraphics(){}
  136. virtual void OnChangeSize(){}
  137. virtual void OnChangePosition(){}
  138. virtual void OnChangeText(){}
  139. virtual void OnChildUpdate(iWidget* apChild){}
  140. virtual void OnInit(){}
  141. virtual void OnDraw(float afTimeStep, cGuiClipRegion *apClipRegion){}
  142. virtual void OnDrawAfterClip(float afTimeStep, cGuiClipRegion *apClipRegion){}
  143. virtual void OnUpdate(float afTimeStep){}
  144. virtual bool OnMessage(eGuiMessage aMessage, const cGuiMessageData& aData){return false;}
  145. virtual bool OnMouseMove(const cGuiMessageData& aData){return false;}
  146. virtual bool OnMouseDown(const cGuiMessageData& aData){return false;}
  147. virtual bool OnMouseUp(const cGuiMessageData& aData){return false;}
  148. virtual bool OnMouseDoubleClick(const cGuiMessageData& aData){return false;}
  149. virtual bool OnMouseEnter(const cGuiMessageData& aData){return false;}
  150. virtual bool OnMouseLeave(const cGuiMessageData& aData){return false;}
  151. virtual bool GetFocus(const cGuiMessageData& aData);
  152. virtual bool OnGotFocus(const cGuiMessageData& aData);
  153. virtual void OnChildGotFocus(iWidget* apChild, const cGuiMessageData& aData);
  154. virtual bool GetTabFocus(const cGuiMessageData& aData);
  155. virtual bool OnGotTabFocus(const cGuiMessageData& aData);
  156. virtual void OnChildGotTabFocus(iWidget* apChild, const cGuiMessageData& aData);
  157. virtual bool LoseFocus(const cGuiMessageData& aData);
  158. virtual bool OnLostFocus(const cGuiMessageData& aData){return false;}
  159. virtual void OnChildLostFocus(iWidget* apChild, const cGuiMessageData& aData);
  160. virtual bool OnKeyPress(const cGuiMessageData& aData){return false;}
  161. virtual bool OnGamepadButtonPress(const cGuiMessageData& aData){return false;}
  162. virtual bool OnUIArrowPress(const cGuiMessageData& aData){return false;}
  163. virtual bool OnUIArrowRelease(const cGuiMessageData& aData){return false;}
  164. virtual bool OnUIButtonPress(const cGuiMessageData& aData){return false;}
  165. virtual bool OnUIButtonRelease(const cGuiMessageData& aData){return false;}
  166. virtual bool OnUIButtonDoublePress(const cGuiMessageData& aData){return false;}
  167. virtual bool OnGetUINavFocus(const cGuiMessageData& aData){return false;}
  168. virtual bool OnLoseUINavFocus(const cGuiMessageData& aData){return false;}
  169. /////////////////////////
  170. // Private Helper functions
  171. bool CheckPointInsideClippingParent(const cVector2f& avPoint);
  172. cVector3f WorldToLocalPosition(const cVector3f &avPos);
  173. cVector2f GetPosRelativeToMouse(const cGuiMessageData& aData);
  174. //The order must be like this:
  175. //Borders: Right, Left, Up and Down
  176. //Corners: LEftUp, RightUp, RightDown and LEftDown.
  177. void DrawBordersAndCorners(cGuiGfxElement *apBackground,
  178. cGuiGfxElement **apBorderVec,cGuiGfxElement **apCornerVec,
  179. const cVector3f &avPosition, const cVector2f &avSize, bool abBordersOutside=false);
  180. void DrawSkinText(const tWString& asText,eGuiSkinFont aFont,const cVector3f& avPosition,
  181. eFontAlign aAlign = eFontAlign_Left);
  182. void DrawDefaultText( const tWString& asText,
  183. const cVector3f& avPosition,eFontAlign aAlign);
  184. void DrawDefaultText( const tWString& asText,
  185. const cVector3f& avPosition,eFontAlign aAlign, const cColor& aCol);
  186. void DrawDefaultTextHighlight( const tWString& asText,
  187. const cVector3f& avPosition, eFontAlign aAlign); //, int alFirstVisibleChar, int alHighlightStart, int alHighlightSize);
  188. void SetPositionUpdated();
  189. void LoadGraphics();
  190. /////////////////////////
  191. // Variables
  192. cGuiSet *mpSet;
  193. cGuiSkin *mpSkin;
  194. cGui *mpGui;
  195. tWString msText;
  196. cVector3f mvPosition;
  197. cVector3f mvGlobalPosition;
  198. cVector2f mvSize;
  199. cVector3f mvChildrenOffset;
  200. cVector3f mvScrollAmount;
  201. bool mbAffectedByScroll;
  202. tString msName;
  203. eWidgetType mType;
  204. int mlPositionCount;
  205. cGuiSkinFont *mpDefaultFont;
  206. iFontData *mpDefaultFontType;
  207. cColor mDefaultFontColor;
  208. cColor mDefaultFontHighlightColor;
  209. cVector2f mvDefaultFontSize;
  210. iWidget *mpParent;
  211. tWidgetList mlstChildren;
  212. bool mbEnabled;
  213. bool mbVisible;
  214. cColor mColorMul;
  215. bool mbMouseIsOver;
  216. bool mbClipsGraphics;
  217. cGuiGfxElement* mpPointerGfx;
  218. bool mbConnectedToChildren;
  219. bool mbToolTipEnabled;
  220. tWString msToolTip;
  221. std::vector<cGuiGlobalShortcut*> mvShortcuts;
  222. bool mbGlobalKeyPressListener;
  223. bool mbGlobalUIInputListener;
  224. bool mbTextChanged;
  225. bool mbCallbacksDisabled;
  226. private:
  227. void SetMouseIsOver(bool abX){ mbMouseIsOver = abX;}
  228. bool ProcessCallbacks(eGuiMessage aMessage, const cGuiMessageData& aData);
  229. int UIArrowToArrayPos(eUIArrow aDir);
  230. std::vector<tWidgetCallbackList> mvCallbackLists;
  231. bool mbPositionIsUpdated;
  232. void *mpUserData;
  233. int mlUserValue;
  234. std::vector<iWidget*> mvFocusNavWidgets;
  235. };
  236. };
  237. #endif // HPL_WIDGET_H