Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
10 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef UITOPLEVELWINDOW_H
  6. #define UITOPLEVELWINDOW_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "utlstring.h"
  11. #include "utlmap.h"
  12. #include "utllinkedlist.h"
  13. #if !defined( SOURCE2_PANORAMA )
  14. #include "constants.h"
  15. #include "globals.h"
  16. #endif
  17. #include "reliabletimer.h"
  18. #if !defined( SOURCE2_PANORAMA )
  19. #include "framefunction.h"
  20. #endif
  21. #include "input/iuiinput.h"
  22. #include "input/mousecursors.h"
  23. #include "iuiengine.h"
  24. #include "color.h"
  25. #include "uievent.h"
  26. #include "iuiwindow.h"
  27. #include "iuirenderengine.h"
  28. #include "uipanel.h"
  29. namespace panorama
  30. {
  31. class CUIRenderEngine;
  32. class CPanel2D;
  33. class CUIWindowInput;
  34. class CUIEngine;
  35. class CMouseCursorTexture;
  36. class CImageResourceManager;
  37. class CFastScrollSoundManager;
  38. class IUI3DSurface;
  39. class CMouseCursorRender;
  40. //
  41. // Top level window class
  42. //
  43. class CTopLevelWindow : public panorama::IUIWindow
  44. {
  45. public:
  46. CTopLevelWindow( CUIEngine *pUIEngineParent );
  47. virtual ~CTopLevelWindow();
  48. // Delete the window object
  49. virtual void Delete() OVERRIDE{ delete this; }
  50. // Final step of initialization, post constructor, and post BInitializeSurface() on individidual window type
  51. virtual bool FinishInitialization();
  52. // Run any per window frame func logic
  53. virtual void RunPlatformFrame();
  54. // Set scaling factor that applies to all x/y values in the UI for the window, used so we can
  55. // author content at say 1080p but pass 0.6666666f for this to render in 720p on cards with poor
  56. // fill rates or TVs without 1080p support.
  57. virtual void SetUIScaleFactor( float flScaleFactor ) OVERRIDE;
  58. virtual float GetUIScaleFactor() OVERRIDE { return m_flScaleFactor; }
  59. // Window position/activation management
  60. const char * GetTargetMonitor() { return m_strTargetMonitor.String(); }
  61. virtual void GetWindowBounds( float &left, float &top, float &right, float &bottom ) = 0;
  62. virtual void GetClientDimensions( float &width, float &height ) OVERRIDE = 0;
  63. virtual bool BAllowInput( InputMessage_t &msg );
  64. // Access the rendering interface you use to draw onto this window
  65. virtual IUIRenderEngine * UIRenderEngine() OVERRIDE{ return (IUIRenderEngine*)m_pRenderEngine; }
  66. CUIRenderEngine *GetUIRenderEngine() { return m_pRenderEngine; }
  67. virtual bool BIsVisible() { return true; }
  68. virtual bool BIsOverlay() OVERRIDE { return UIEngine()->BIsOverlayTarget(m_eRenderTarget); }
  69. virtual bool BIsSteamWMOverlay() OVERRIDE { return m_eRenderTarget == IUIEngine::k_ERenderToOverlaySteamWM; }
  70. virtual bool BIsFullscreen() { return IUIEngine::BIsRenderingToFullScreen( m_eRenderTarget ); }
  71. virtual bool BIsFullscreenBorderlessWindow() { return m_eRenderTarget == IUIEngine::k_ERenderBorderlessFullScreenWindow; }
  72. bool SetFullscreen( bool bFullscreen );
  73. bool BEnforceWindowAspectRatio() { return m_bEnforceWindowAspectRatio; }
  74. virtual uint32 GetSurfaceWidth() OVERRIDE { return m_unSurfaceWidth; }
  75. virtual uint32 GetSurfaceHeight() OVERRIDE { return m_unSurfaceHeight; }
  76. virtual uint32 GetWindowWidth() OVERRIDE { return m_unWindowWidth; }
  77. virtual uint32 GetWindowHeight() OVERRIDE { return m_unWindowHeight; }
  78. void ConvertClientToSurfaceCoord( float *px, float *py );
  79. void GetFPSAverages( float &fpsPaint, float &fpsAnimation, float &fpsRender );
  80. virtual void GetSessionFPSAverages( float &fpsPaint, float &fpsAnimation, float &fpsRender ) OVERRIDE;
  81. virtual void GetNumPeriodsBelowMinFPS( int &nSlowPeriods ) OVERRIDE;
  82. // Clear color for the window, normally black, transparent for overlay
  83. virtual Color GetClearColor() { return Color( 0, 0, 0, 255 ); }
  84. // Panel management for the window
  85. int AddPanel( CUIPanel *pPanel, bool bVisible );
  86. void RemovePanel( int iPanelIndex, bool bVisible );
  87. int SetPanelVisible( int iPanelIndex, bool bVisible );
  88. virtual void AddClass( const char *pchName ) OVERRIDE;
  89. virtual void RemoveClass( const char *pchName ) OVERRIDE;
  90. // Layout/paint for window
  91. virtual void LayoutAndPaintIfNeeded();
  92. // Paint an empty frame so the animation/render threads will run but do nothing but LRU/clear data
  93. virtual void PaintEmptyFrameAndForceLaterRepaint();
  94. // Layout file auto-reload for windows children panels
  95. void ReloadLayoutFile( CPanoramaSymbol symPath );
  96. void OnReloadStyleFile( CPanoramaSymbol symPath );
  97. // Access input engine for window
  98. virtual IUIWindowInput *UIWindowInput() OVERRIDE { return (IUIWindowInput *)m_pInputEngine; }
  99. // custom mouse cursor support, returns true if we want our manually drawn one, false for OS specific ones
  100. bool BUseCustomMouseCursor() { return m_bUseCustomMouseCursor; }
  101. // used by the os specific case to update the cursor
  102. virtual void SetMouseCursor( EMouseCursors eCursor ) = 0;
  103. IImageSource *GetMouseCursorTexture( Vector2D *pptHotspot );
  104. virtual bool BCursorVisible() OVERRIDE;
  105. virtual bool BCursorFadingOut() OVERRIDE;
  106. virtual void WakeupMouseCursor() OVERRIDE;
  107. virtual void FadeOutCursorNow() OVERRIDE;
  108. // Access image manager for window
  109. CImageResourceManager* AccessImageManager() { return m_pImageResourceManager; }
  110. // Set a context ptr that is attached to the window, just lets other code (panels) that
  111. // has access to the window access some shared state across the window.
  112. virtual void SetContextPtr( void *pv ) OVERRIDE { m_pContextPtr = pv; }
  113. // Get the context ptr that is attached to the window
  114. virtual void * GetContextPtr() const OVERRIDE { return m_pContextPtr; }
  115. void ReloadChangedFile( const char *pchFile );
  116. // Access fast scroll sound manager for the window
  117. CFastScrollSoundManager * AccessFastScrollSoundMgr();
  118. void GetMouseWheelRepeats( bool bScrollUp, int lines, uint8 &unRepeats );
  119. virtual uint32 GetNumVisibleTopLevelPanels() const OVERRIDE { return (uint32)m_listVisiblePanels.Count(); }
  120. virtual const CUtlLinkedList< IUIPanel* > &GetTopLevelVisiblePanels() const OVERRIDE { return (CUtlLinkedList< IUIPanel* > &)m_listVisiblePanels; }
  121. // Get the last time the window layed out and painted
  122. float GetLastLayoutAndPaintTime() { return m_flLastLayoutAndPaintTime; }
  123. // Set max FPS for the window
  124. void SetMaxFPS( float flMaxFPS );
  125. // Set a min FPS for the window, this actually just prevents setting the max lower
  126. virtual void SetMinFPS( float flMinFPS ) OVERRIDE { m_flMinFPS = flMinFPS; }
  127. // access data about how the gamepad was used
  128. virtual bool BWasGamepadConnectedThisSession() OVERRIDE;
  129. virtual bool BWasGamepadUsedThisSession() OVERRIDE;
  130. virtual bool BWasSteamControllerConnectedThisSession() OVERRIDE;
  131. virtual bool BWasSteamControllerUsedThisSession() OVERRIDE;
  132. // metrics
  133. virtual void RecordDaisyWheelUsage( float flEntryTimeInSeconds, int nWordsEntered, bool bViaKeyboard, bool bViaGamepad ) OVERRIDE;
  134. virtual void GetDaisyWheelWPM( int &nWordsTyped, float &flMixedWPM, float &flKeyboardOnlyWPM, float &flGamepadOnlyWPM ) OVERRIDE;
  135. bool BFinishedInitialization() { return m_bFinishedInitialization; }
  136. virtual bool BIsWindowInLayoutPass() OVERRIDE { return m_bInLayoutTraverse; }
  137. virtual void SetInhibitInput( bool bInhibitInput ) OVERRIDE;
  138. virtual void SetPreventForceWindowOnTop( bool bPreventForceTopLevel ) OVERRIDE;
  139. // Access overlay window interface for this window, NULL on non Steam Overlay windows
  140. virtual IUIOverlayWindow *GetOverlayInterface() OVERRIDE { return NULL; }
  141. virtual void SetFocusBehavior( EWindowFocusBehavior eFocusBehavior ) OVERRIDE { m_eFocusBehavior = eFocusBehavior; }
  142. virtual EWindowFocusBehavior GetFocusBehavior() OVERRIDE{ return m_eFocusBehavior; }
  143. virtual void OnDeviceLost() OVERRIDE;
  144. virtual void OnDeviceRestored() OVERRIDE;
  145. virtual bool BDeviceLost() OVERRIDE;
  146. // Clears the GPU resources associated with the window before the next render frame
  147. virtual void ClearGPUResourcesBeforeNextFrame() OVERRIDE;
  148. #ifdef DBGFLAG_VALIDATE
  149. virtual void Validate( CValidator &validator, const tchar *pchName );
  150. bool PrepareForValidate();
  151. bool ResumeFromValidate();
  152. #endif
  153. protected:
  154. // Perform layout prior to paint
  155. void PerformLayout();
  156. bool BIsGuideButton( const InputMessage_t &msg );
  157. // Render engine instance for window
  158. CUIRenderEngine *m_pRenderEngine;
  159. // Do we need to clear gpu resources before repaint
  160. uint32 m_unFramesToClearGPUResourcesBeforeRepaint;
  161. // Image manager for the window
  162. CImageResourceManager *m_pImageResourceManager;
  163. bool m_bDeviceLost;
  164. bool m_bAlreadyForcedRepaintAllSinceLastPaint;
  165. CUtlString m_strTargetMonitor;
  166. uint32 m_unSurfaceWidth, m_unSurfaceHeight;
  167. uint32 m_unWindowWidth, m_unWindowHeight;
  168. IUIEngine::ERenderTarget m_eRenderTarget;
  169. bool m_bFixedSurfaceSize;
  170. bool m_bEnforceWindowAspectRatio;
  171. bool m_bUseCustomMouseCursor; // true if we draw our tenfoot cursors and not the win32 ones for this panel
  172. bool m_bCursorWasVisibleLastFrame;
  173. EMouseCursors m_eCursorCurrent; // current cursor to draw
  174. CMouseCursorTexture *m_pMouseCursor; // contains the image data for the cursors we can display
  175. // owner of details about the cursor state
  176. CMouseCursorRender *m_pCursorRender;
  177. // Input engine
  178. CUIWindowInput *m_pInputEngine;
  179. // the ui engine that owns us
  180. CUIEngine *m_pUIEngineParent;
  181. void *m_pContextPtr;
  182. // Scale factor for all drawing sizes
  183. float m_flScaleFactor;
  184. // Min fps value the max fps can be set to for this window
  185. float m_flMinFPS;
  186. // Track last layout and paint time
  187. double m_flLastLayoutAndPaintTime;
  188. // Pointer for the surface interface created for the window
  189. IUI3DSurface *m_pSurfaceInterface;
  190. // Panel lists
  191. CUtlLinkedList< CUIPanel * > m_listVisiblePanels;
  192. CUtlLinkedList< CUIPanel * > m_listInvisiblePanels;
  193. // Classes to apply to top level panels
  194. CUtlVector< CPanoramaSymbol > m_vecStyleClasses;
  195. // Panels to asynchronously add classes to
  196. CUtlVector< CPanelPtr< IUIPanel > > m_vecPanelsAddClasses;
  197. // Panels to asynchronously remove classes from
  198. CUtlVector< CPanelPtr< IUIPanel > > m_vecPanelsRemoveClasses;
  199. CFastScrollSoundManager *m_pFastScrollSoundManager;
  200. // Mouse wheel repeat tracking
  201. uint8 m_unMouseWheelUpRepeats;
  202. uint8 m_unMouseWheelDownRepeats;
  203. double m_flLastMouseWheelUp;
  204. double m_flLastMouseWheelDown;
  205. bool m_bInLayoutTraverse;
  206. bool m_bInPaintTraverse;
  207. bool m_bFinishedInitialization;
  208. // daisy wheel usage for this window
  209. enum EDaisyWheelInputType
  210. {
  211. eDaisyWheelInputType_KeyboardOnly,
  212. eDaisyWheelInputType_GamepadOnly,
  213. eDaisyWheelInputType_KeyboardAndGamePad,
  214. eDaisyWheelInputType_MAX
  215. };
  216. struct DaisyWheelUsage_t
  217. {
  218. int nWords;
  219. float flTime;
  220. };
  221. DaisyWheelUsage_t m_flDaisyWheelWPM[eDaisyWheelInputType_MAX];
  222. virtual void Shutdown();
  223. bool m_bInhibitInput;
  224. EWindowFocusBehavior m_eFocusBehavior;
  225. };
  226. } // namespace panorama
  227. #endif // UITOPLEVELWINDOW_H