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.

323 lines
6.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "cbase.h"
  3. #include "vguiwnd.h"
  4. #include <vgui_controls/EditablePanel.h>
  5. #include "vgui/ISurface.h"
  6. #include "vgui/IVGui.h"
  7. #include "VGuiMatSurface/IMatSystemSurface.h"
  8. #include "FacePoser_VGui.h"
  9. // #include "material.h"
  10. #include "materialsystem/imaterialvar.h"
  11. #include "materialsystem/imaterial.h"
  12. #define REPAINT_TIMER_ID 1042 //random value, hopfully no collisions
  13. inline MaterialSystem_Config_t& MaterialSystemConfig()
  14. {
  15. extern MaterialSystem_Config_t g_materialSystemConfig;
  16. return g_materialSystemConfig;
  17. }
  18. inline IMaterialSystemHardwareConfig* MaterialSystemHardwareConfig()
  19. {
  20. extern IMaterialSystemHardwareConfig* g_pMaterialSystemHardwareConfig;
  21. return g_pMaterialSystemHardwareConfig;
  22. }
  23. class CBaseMainPanel : public vgui::EditablePanel
  24. {
  25. public:
  26. CBaseMainPanel(Panel *parent, const char *panelName) : vgui::EditablePanel( parent, panelName ) {};
  27. virtual void OnSizeChanged(int newWide, int newTall)
  28. {
  29. // call Panel and not EditablePanel OnSizeChanged.
  30. Panel::OnSizeChanged(newWide, newTall);
  31. }
  32. };
  33. int CVGuiPanelWnd::handleEvent( mxEvent *event )
  34. {
  35. if ( !HandeEventVGui( event ) )
  36. {
  37. return BaseClass::handleEvent( event );
  38. }
  39. return 1;
  40. }
  41. CVGuiWnd::CVGuiWnd(void)
  42. {
  43. m_pMainPanel = NULL;
  44. m_pParentWnd = NULL;
  45. m_hVGuiContext = vgui::DEFAULT_VGUI_CONTEXT;
  46. m_bIsDrawing = false;
  47. m_ClearColor.SetColor( 0,0,0,255 );
  48. m_bClearZBuffer = true;
  49. }
  50. CVGuiWnd::~CVGuiWnd(void)
  51. {
  52. if ( FaceposerVGui()->HasFocus( this ) )
  53. {
  54. FaceposerVGui()->SetFocus( NULL );
  55. }
  56. if ( m_hVGuiContext != vgui::DEFAULT_VGUI_CONTEXT )
  57. {
  58. vgui::ivgui()->DestroyContext( m_hVGuiContext );
  59. m_hVGuiContext = vgui::DEFAULT_VGUI_CONTEXT;
  60. }
  61. // kill the timer if any
  62. ::KillTimer( (HWND)m_pParentWnd->getHandle(), REPAINT_TIMER_ID );
  63. if ( m_pMainPanel )
  64. m_pMainPanel->MarkForDeletion();
  65. }
  66. void CVGuiWnd::SetParentWindow(mxWindow *pParent)
  67. {
  68. m_pParentWnd = pParent;
  69. /*
  70. m_pParentWnd->EnableWindow( true );
  71. m_pParentWnd->SetFocus();
  72. */
  73. HWND h = (HWND)m_pParentWnd->getHandle();
  74. EnableWindow( h, TRUE );
  75. SetFocus( h );
  76. }
  77. int CVGuiWnd::GetVGuiContext()
  78. {
  79. return m_hVGuiContext;
  80. }
  81. void CVGuiWnd::SetCursor(vgui::HCursor cursor)
  82. {
  83. if ( m_pMainPanel )
  84. {
  85. m_pMainPanel->SetCursor( cursor );
  86. }
  87. }
  88. void CVGuiWnd::SetCursor(const char *filename)
  89. {
  90. vgui::HCursor hCursor = vgui::surface()->CreateCursorFromFile( filename );
  91. m_pMainPanel->SetCursor( hCursor );
  92. }
  93. void CVGuiWnd::SetMainPanel( vgui::EditablePanel * pPanel )
  94. {
  95. SetRepaintInterval( 75 );
  96. Assert( m_pMainPanel == NULL );
  97. Assert( m_hVGuiContext == vgui::DEFAULT_VGUI_CONTEXT );
  98. m_pMainPanel = pPanel;
  99. m_pMainPanel->SetParent( vgui::surface()->GetEmbeddedPanel() );
  100. m_pMainPanel->SetVisible( true );
  101. m_pMainPanel->SetPaintBackgroundEnabled( false );
  102. m_pMainPanel->SetCursor( vgui::dc_arrow );
  103. m_hVGuiContext = vgui::ivgui()->CreateContext();
  104. vgui::ivgui()->AssociatePanelWithContext( m_hVGuiContext, m_pMainPanel->GetVPanel() );
  105. }
  106. vgui::EditablePanel *CVGuiWnd::CreateDefaultPanel()
  107. {
  108. return new CBaseMainPanel( NULL, "mainpanel" );
  109. }
  110. vgui::EditablePanel *CVGuiWnd::GetMainPanel()
  111. {
  112. return m_pMainPanel;
  113. }
  114. mxWindow *CVGuiWnd::GetParentWnd()
  115. {
  116. return m_pParentWnd;
  117. }
  118. void CVGuiWnd::SetRepaintInterval( int msecs )
  119. {
  120. m_pParentWnd->setTimer( msecs );
  121. }
  122. void CVGuiWnd::DrawVGuiPanel()
  123. {
  124. if ( !m_pMainPanel || !m_pParentWnd || m_bIsDrawing )
  125. return;
  126. m_bIsDrawing = true; // avoid recursion
  127. HWND hWnd = (HWND)m_pParentWnd->getHandle();
  128. int w,h;
  129. RECT rect;
  130. ::GetClientRect(hWnd, &rect);
  131. CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
  132. g_pMaterialSystem->SetView( hWnd );
  133. pRenderContext->Viewport( rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top );
  134. pRenderContext->ClearColor4ub( m_ClearColor.r(), m_ClearColor.g(), m_ClearColor.b(), m_ClearColor.a() );
  135. pRenderContext->ClearBuffers( true, m_bClearZBuffer );
  136. g_pMaterialSystem->BeginFrame( 0 );
  137. // draw from the main panel down
  138. m_pMainPanel->GetSize( w , h );
  139. if ( w != rect.right || h != rect.bottom )
  140. {
  141. m_pMainPanel->SetBounds( 2 + rect.left, 2 + rect.top, rect.right - rect.left - 4, rect.bottom - rect.top - 4 );
  142. m_pMainPanel->Repaint();
  143. }
  144. FaceposerVGui()->Simulate();
  145. vgui::surface()->PaintTraverseEx( m_pMainPanel->GetVPanel(), true );
  146. g_pMaterialSystem->EndFrame();
  147. g_pMaterialSystem->SwapBuffers();
  148. m_bIsDrawing = false;
  149. }
  150. CVGuiPanelWnd::CVGuiPanelWnd( mxWindow *parent, int x, int y, int w, int h )
  151. : BaseClass( parent, x, y, w, h )
  152. {
  153. }
  154. void CVGuiPanelWnd::redraw()
  155. {
  156. DrawVGuiPanel();
  157. }
  158. int CVGuiWnd::HandeEventVGui( mxEvent *event )
  159. {
  160. if ( !m_pParentWnd )
  161. return 0;
  162. HWND hWnd = (HWND)m_pParentWnd->getHandle();
  163. // switch( uMsg )
  164. // {
  165. // case WM_GETDLGCODE :
  166. // {
  167. // // forward all keyboard into to vgui panel
  168. // return DLGC_WANTALLKEYS|DLGC_WANTCHARS;
  169. // }
  170. // case WM_PAINT :
  171. // {
  172. // // draw the VGUI panel now
  173. // DrawVGuiPanel();
  174. // break;
  175. // }
  176. // case WM_TIMER :
  177. // {
  178. // if ( wParam == REPAINT_TIMER_ID )
  179. // {
  180. // m_pParentWnd->Invalidate();
  181. // }
  182. // break;
  183. // }
  184. // case WM_SETCURSOR:
  185. // return 1; // don't pass WM_SETCURSOR
  186. /*
  187. case WM_LBUTTONDOWN:
  188. case WM_RBUTTONDOWN:
  189. case WM_MBUTTONDOWN:
  190. case WM_MOUSEMOVE:
  191. {
  192. // switch vgui focus to this panel
  193. FaceposerVGui()->SetFocus( this );
  194. // request keyboard focus too on mouse down
  195. if ( uMsg != WM_MOUSEMOVE)
  196. {
  197. m_pParentWnd->Invalidate();
  198. m_pParentWnd->SetFocus();
  199. }
  200. break;
  201. }
  202. case WM_KILLFOCUS:
  203. {
  204. // restore normal arrow cursor when mouse leaves VGUI panel
  205. SetCursor( vgui::dc_arrow );
  206. break;
  207. }
  208. case WM_LBUTTONUP:
  209. case WM_RBUTTONUP:
  210. case WM_MBUTTONUP:
  211. case WM_LBUTTONDBLCLK:
  212. case WM_RBUTTONDBLCLK:
  213. case WM_MBUTTONDBLCLK:
  214. case WM_MOUSEWHEEL:
  215. case WM_KEYDOWN:
  216. case WM_SYSKEYDOWN:
  217. case WM_SYSCHAR:
  218. case WM_CHAR:
  219. case WM_KEYUP:
  220. case WM_SYSKEYUP:
  221. {
  222. // redraw window
  223. m_pParentWnd->Invalidate();
  224. break;
  225. }
  226. }
  227. */
  228. switch ( event->event )
  229. {
  230. case mxEvent::KeyUp:
  231. case mxEvent::KeyDown:
  232. case mxEvent::MouseUp:
  233. case mxEvent::MouseWheeled:
  234. case mxEvent::Char:
  235. case mxEvent::MouseMove:
  236. {
  237. InvalidateRect( hWnd, NULL, FALSE );
  238. }
  239. break;
  240. case mxEvent::Timer:
  241. {
  242. InvalidateRect( hWnd, NULL, FALSE );
  243. }
  244. break;
  245. case mxEvent::Focus:
  246. break;
  247. case mxEvent::MouseDown:
  248. {
  249. // switch vgui focus to this panel
  250. FaceposerVGui()->SetFocus( this );
  251. // request keyboard focus too on mouse down
  252. if ( event->event != mxEvent::MouseMove )
  253. {
  254. InvalidateRect( hWnd, NULL, FALSE );
  255. SetFocus( hWnd );
  256. }
  257. }
  258. break;
  259. }
  260. return 0;
  261. }