Counter Strike : Global Offensive Source Code
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.

730 lines
22 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "hitarea.h"
  8. // To handle scaling
  9. #include "materialsystem/imaterialsystem.h"
  10. #include "animdata.h"
  11. #include "inputsystem/inputenums.h"
  12. #include "inputsystem/analogcode.h"
  13. #include "inputsystem/buttoncode.h"
  14. #include "gameuisystemmgr.h"
  15. #include "graphicgroup.h"
  16. #include "inputgameui.h"
  17. #include "graphicscriptinterface.h"
  18. #include "inputgameui.h"
  19. #include "gameuisystemmgr.h"
  20. #include "gameuiscript.h"
  21. #include "gameuisystem.h"
  22. // memdbgon must be the last include file in a .cpp file!!!
  23. #include "tier0/memdbgon.h"
  24. #define DEBUG_INPUT_EVENTS 0
  25. // Class factory for scripting.
  26. class CHitAreaClassFactory : IGameUIGraphicClassFactory
  27. {
  28. public:
  29. CHitAreaClassFactory()
  30. {
  31. Assert( g_pGameUISystemMgrImpl );
  32. g_pGameUISystemMgrImpl->RegisterGraphicClassFactory( "hitarea", this );
  33. }
  34. // Returns an instance of a graphic interface (keyvalues owned by caller)
  35. virtual CGameGraphic *CreateNewGraphicClass( KeyValues *kvRequest, CGameUIDefinition *pMenu )
  36. {
  37. Assert( pMenu );
  38. CHitArea *pNewGraphic = NULL;
  39. const char *pName = kvRequest->GetString( "name", NULL );
  40. if ( pName )
  41. {
  42. pNewGraphic = new CHitArea( pName );
  43. // Rects are normally 0,0, doing this so we can see script created rects.
  44. pNewGraphic->SetScale( 100, 100 );
  45. pMenu->AddGraphicToLayer( pNewGraphic, SUBLAYER_STATIC );
  46. // Now set the attributes.
  47. for ( KeyValues *arg = kvRequest->GetFirstSubKey(); arg != NULL; arg = arg->GetNextKey() )
  48. {
  49. pNewGraphic->HandleScriptCommand( arg );
  50. }
  51. }
  52. return pNewGraphic;
  53. }
  54. };
  55. static CHitAreaClassFactory g_CDynamicRectClassFactory;
  56. BEGIN_DMXELEMENT_UNPACK ( CHitArea )
  57. DMXELEMENT_UNPACK_FIELD_UTLSTRING( "name", "", m_pName )
  58. DMXELEMENT_UNPACK_FIELD( "center", "0 0", Vector2D, m_Geometry.m_Center )
  59. DMXELEMENT_UNPACK_FIELD( "scale", "0 0", Vector2D, m_Geometry.m_Scale )
  60. DMXELEMENT_UNPACK_FIELD( "rotation", "0", float, m_Geometry.m_Rotation )
  61. DMXELEMENT_UNPACK_FIELD( "maintainaspectratio", "0", bool, m_Geometry.m_bMaintainAspectRatio )
  62. DMXELEMENT_UNPACK_FIELD( "sublayertype", "0", int, m_Geometry.m_Sublayer )
  63. DMXELEMENT_UNPACK_FIELD( "visible", "1", bool, m_Geometry.m_bVisible )
  64. DMXELEMENT_UNPACK_FIELD( "initialstate", "-1", int, m_CurrentState )
  65. DMXELEMENT_UNPACK_FIELD( "dragenabled", "0", bool, m_bDragEnabled )
  66. DMXELEMENT_UNPACK_FIELD_UTLSTRING( "on_mouse_left_clicked_cmd", "", m_OnMouseLeftClickedScriptCommand )
  67. END_DMXELEMENT_UNPACK( CHitArea, s_HitAreaUnpack )
  68. //-----------------------------------------------------------------------------
  69. // Constructor
  70. //-----------------------------------------------------------------------------
  71. CHitArea::CHitArea( const char *pName )
  72. {
  73. m_bCanAcceptInput = true;
  74. m_bCanStartDragging = false;
  75. m_IsDragging = false;
  76. m_DragStartCursorPos[0] = 0;
  77. m_DragStartCursorPos[1] = 0;
  78. m_DragCurrentCursorPos[0] = 0;
  79. m_DragCurrentCursorPos[1] = 0;
  80. // DME default values.
  81. m_pName = pName;
  82. m_Geometry.m_Center.x = 0;
  83. m_Geometry.m_Center.y = 0;
  84. m_Geometry.m_Scale.x = 0;
  85. m_Geometry.m_Scale.y = 0;
  86. m_Geometry.m_Rotation = 0;
  87. m_Geometry.m_bMaintainAspectRatio = 0;
  88. m_Geometry.m_Sublayer = 0;
  89. m_Geometry.m_bVisible = true;
  90. m_CurrentState = -1;
  91. m_bDragEnabled = false;
  92. m_OnMouseLeftClickedScriptCommand = NULL;
  93. m_Geometry.m_RelativePositions.AddToTail( Vector2D( -.5, -.5 ) );
  94. m_Geometry.m_RelativePositions.AddToTail( Vector2D( .5, -.5 ) );
  95. m_Geometry.m_RelativePositions.AddToTail( Vector2D( .5, .5 ) );
  96. m_Geometry.m_RelativePositions.AddToTail( Vector2D( -.5, .5 ) );
  97. CTriangle triangle;
  98. triangle.m_PointIndex[0] = 0;
  99. triangle.m_PointIndex[1] = 1;
  100. triangle.m_PointIndex[2] = 2;
  101. m_Geometry.m_Triangles.AddToTail( triangle );
  102. triangle.m_PointIndex[0] = 0;
  103. triangle.m_PointIndex[1] = 2;
  104. triangle.m_PointIndex[2] = 3;
  105. m_Geometry.m_Triangles.AddToTail( triangle );
  106. }
  107. //-----------------------------------------------------------------------------
  108. //
  109. //-----------------------------------------------------------------------------
  110. CHitArea::~CHitArea()
  111. {
  112. // TODO: move to manager?/ as it should control allocations and deallocations.
  113. g_pInputGameUI->PanelDeleted( this );
  114. }
  115. //-----------------------------------------------------------------------------
  116. //
  117. //-----------------------------------------------------------------------------
  118. bool CHitArea::Unserialize( CDmxElement *pGraphic )
  119. {
  120. pGraphic->UnpackIntoStructure( this, s_HitAreaUnpack );
  121. // GEOMETRY
  122. CDmxAttribute *pRelativePositions = pGraphic->GetAttribute( "relativepositions" );
  123. if ( !pRelativePositions || pRelativePositions->GetType() != AT_VECTOR2_ARRAY )
  124. {
  125. return false;
  126. }
  127. const CUtlVector< Vector2D > &relpositions = pRelativePositions->GetArray< Vector2D >( );
  128. int nCount = relpositions.Count();
  129. m_Geometry.m_RelativePositions.RemoveAll();
  130. for ( int i = 0; i < nCount; ++i )
  131. {
  132. m_Geometry.m_RelativePositions.AddToTail( Vector2D( relpositions[i].x, relpositions[i].y ) );
  133. }
  134. CDmxAttribute *pTriangles = pGraphic->GetAttribute( "triangles" );
  135. if ( !pTriangles || pTriangles->GetType() != AT_ELEMENT_ARRAY )
  136. {
  137. return false;
  138. }
  139. const CUtlVector< CDmxElement * > &triangles = pTriangles->GetArray< CDmxElement * >( );
  140. nCount = triangles.Count();
  141. m_Geometry.m_Triangles.RemoveAll();
  142. for ( int i = 0; i < nCount; ++i )
  143. {
  144. CDmxAttribute *pPoints = triangles[i]->GetAttribute( "positionindexes" );
  145. const CUtlVector< int > &points = pPoints->GetArray< int >( );
  146. CTriangle triangle;
  147. triangle.m_PointIndex[0] = points[0];
  148. triangle.m_PointIndex[1] = points[1];
  149. triangle.m_PointIndex[2] = points[2];
  150. m_Geometry.m_Triangles.AddToTail( triangle );
  151. }
  152. // ANIMSTATES
  153. CDmxAttribute *pImageAnims = pGraphic->GetAttribute( "imageanims" );
  154. if ( !pImageAnims || pImageAnims->GetType() != AT_ELEMENT_ARRAY )
  155. {
  156. return false;
  157. }
  158. const CUtlVector< CDmxElement * > &imageanims = pImageAnims->GetArray< CDmxElement * >( );
  159. nCount = imageanims.Count();
  160. for ( int i = 0; i < nCount; ++i )
  161. {
  162. CAnimData *pAnimData = new CAnimData;
  163. if ( !pAnimData->Unserialize( imageanims[i] ) )
  164. {
  165. delete pAnimData;
  166. return false;
  167. }
  168. m_Anims.AddToTail( pAnimData );
  169. }
  170. SetState( "default" );
  171. return true;
  172. }
  173. //-----------------------------------------------------------------------------
  174. //
  175. //-----------------------------------------------------------------------------
  176. void CHitArea::UpdateGeometry()
  177. {
  178. if ( m_CurrentState == -1 )
  179. return;
  180. DmeTime_t flAnimTime = GetAnimationTimePassed();
  181. // Update center location
  182. m_Anims[ m_CurrentState ]->m_CenterPosAnim.GetValue( flAnimTime, &m_Geometry.m_Center );
  183. // Update scale
  184. m_Anims[ m_CurrentState ]->m_ScaleAnim.GetValue( flAnimTime, &m_Geometry.m_Scale );
  185. // Update rotation
  186. m_Anims[ m_CurrentState ]->m_RotationAnim.GetValue( flAnimTime, &m_Geometry.m_Rotation );
  187. }
  188. //-----------------------------------------------------------------------------
  189. //
  190. //-----------------------------------------------------------------------------
  191. void CHitArea::UpdateRenderData( color32 parentColor, CUtlVector< RenderGeometryList_t > &renderGeometryLists, int firstListIndex )
  192. {
  193. m_Geometry.CalculateExtents();
  194. bool bDrawHitAreas = false;
  195. if ( !m_Geometry.m_bVisible )
  196. return;
  197. if ( bDrawHitAreas )
  198. {
  199. // Time to invent some render data to draw this thing.
  200. int i = renderGeometryLists[firstListIndex].AddToTail();
  201. CRenderGeometry &renderGeometry = renderGeometryLists[firstListIndex][i];
  202. int nCount = m_Geometry.m_RelativePositions.Count();
  203. for ( int i = 0; i < nCount; ++i )
  204. {
  205. // Position
  206. Vector relativePosition( m_Geometry.m_RelativePositions[i].x, m_Geometry.m_RelativePositions[i].y, 0 );
  207. Vector screenpos;
  208. VectorTransform( relativePosition, m_Geometry.m_RenderToScreen, screenpos );
  209. renderGeometry.m_Positions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );;
  210. // Vertex Color
  211. color32 hitAreaColor;
  212. hitAreaColor.r = 255;
  213. hitAreaColor.g = 100;
  214. hitAreaColor.b = 100;
  215. hitAreaColor.a = 255;
  216. renderGeometry.m_VertexColors.AddToTail( hitAreaColor );
  217. }
  218. // TexCoords
  219. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 0) );
  220. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 0) );
  221. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 1 , 1) );
  222. renderGeometry.m_TextureCoords.AddToTail( Vector2D( 0 , 1) );
  223. // Triangles
  224. nCount = m_Geometry.m_Triangles.Count();
  225. for ( int i = 0; i < nCount; ++i )
  226. {
  227. renderGeometry.m_Triangles.AddToTail( m_Geometry.m_Triangles[i] );
  228. }
  229. // Anim Info
  230. renderGeometry.m_SheetSequenceNumber = 0;
  231. renderGeometry.m_AnimationRate = 1;
  232. renderGeometry.m_bAnimate = 0;
  233. renderGeometry.m_pImageAlias = NULL;
  234. }
  235. // Now transform our array of positions into local graphic coord system.
  236. int nCount = m_Geometry.m_RelativePositions.Count();
  237. m_ScreenPositions.RemoveAll();
  238. for ( int i = 0; i < nCount; ++i )
  239. {
  240. // Position
  241. Vector relativePosition( m_Geometry.m_RelativePositions[i].x, m_Geometry.m_RelativePositions[i].y, 0 );
  242. Vector screenpos;
  243. VectorTransform( relativePosition, m_Geometry.m_RenderToScreen, screenpos );
  244. m_ScreenPositions.AddToTail( Vector2D( screenpos.x, screenpos.y ) );
  245. }
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Determine if x,y is inside the graphic.
  249. //-----------------------------------------------------------------------------
  250. bool CHitArea::HitTest( int x, int y )
  251. {
  252. if ( !m_bCanAcceptInput ) // note if graphic is invisible, this is false
  253. return false;
  254. if ( m_ScreenPositions.Count() == 0 )
  255. return false;
  256. for ( int i = 0; i < m_Geometry.GetTriangleCount(); ++i )
  257. {
  258. if ( PointTriangleHitTest(
  259. m_ScreenPositions[ m_Geometry.m_Triangles[i].m_PointIndex[0] ],
  260. m_ScreenPositions[ m_Geometry.m_Triangles[i].m_PointIndex[1] ],
  261. m_ScreenPositions[ m_Geometry.m_Triangles[i].m_PointIndex[2] ],
  262. Vector2D( x, y ) ) )
  263. {
  264. //Msg( "%d, %d hit\n", x, y );
  265. return true;
  266. }
  267. }
  268. return false;
  269. }
  270. //-----------------------------------------------------------------------------
  271. // Event that occurs when cursor enters the geometry area
  272. //-----------------------------------------------------------------------------
  273. void CHitArea::OnCursorEnter()
  274. {
  275. #if ( DEBUG_INPUT_EVENTS )
  276. Msg( "CHitArea::OnCursorEnter\n" );
  277. #endif
  278. if ( m_pGroup )
  279. {
  280. m_pGroup->SetState( "AUTO_GAINMOUSEFOCUS" );
  281. }
  282. KeyValues *kvEvent = new KeyValues( "OnMouseFocusGained" );
  283. KeyValues::AutoDelete autodelete( kvEvent );
  284. // chain to main system if this graphic doesn't handle it.
  285. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  286. g_pGameUISystemMgrImpl->OnMouseFocusGained( this );
  287. }
  288. //-----------------------------------------------------------------------------
  289. // Event that occurs when cursor leaves the geometry area
  290. //-----------------------------------------------------------------------------
  291. void CHitArea::OnCursorExit()
  292. {
  293. #if ( DEBUG_INPUT_EVENTS )
  294. Msg( "CHitArea::OnCursorExit\n" );
  295. #endif
  296. if ( m_pGroup )
  297. {
  298. m_pGroup->SetState( "AUTO_LOSEMOUSEFOCUS" );
  299. }
  300. KeyValues *kvEvent = new KeyValues( "OnMouseFocusLost" );
  301. KeyValues::AutoDelete autodelete( kvEvent );
  302. // chain to main system if this graphic doesn't handle it.
  303. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  304. g_pGameUISystemMgrImpl->OnMouseFocusLost( this );
  305. }
  306. #define DRAG_THRESHOLD_SQUARED 16
  307. //-----------------------------------------------------------------------------
  308. // Event that occurs when cursor moved inside the geometry area
  309. //-----------------------------------------------------------------------------
  310. void CHitArea::OnCursorMove( const int &cursorX, const int &cursorY )
  311. {
  312. //Msg( "CHitArea::OnCursorMove\n" );
  313. if ( m_bCanStartDragging )
  314. {
  315. m_DragCurrentCursorPos[0] = cursorX;
  316. m_DragCurrentCursorPos[1] = cursorY;
  317. float dx = m_DragCurrentCursorPos[0] - m_DragStartCursorPos[0];
  318. float dy = m_DragCurrentCursorPos[1] - m_DragStartCursorPos[1];
  319. float distance = dx * dx + dy * dy;
  320. if ( distance > DRAG_THRESHOLD_SQUARED )
  321. {
  322. #if ( DEBUG_INPUT_EVENTS )
  323. Msg( "CHitArea::Starting dragging\n" );
  324. #endif
  325. OnDragStartCallScriptEvent( cursorX, cursorY );
  326. m_bCanStartDragging = false;
  327. m_IsDragging = true;
  328. }
  329. }
  330. else if ( m_IsDragging )
  331. {
  332. m_DragCurrentCursorPos[0] = cursorX;
  333. m_DragCurrentCursorPos[1] = cursorY;
  334. OnDragCallScriptEvent( cursorX, cursorY );
  335. }
  336. }
  337. //-----------------------------------------------------------------------------
  338. // Event that occurs when left mouse button is pressed
  339. //-----------------------------------------------------------------------------
  340. void CHitArea::OnMouseDown( const ButtonCode_t &code )
  341. {
  342. #if ( DEBUG_INPUT_EVENTS )
  343. Msg( "CHitArea::OnMouseDown\n" );
  344. #endif
  345. // Drag and drop supported for left mouse button only.
  346. // This hit area must be drag enabled to support dragging.
  347. if ( code == MOUSE_LEFT && m_bDragEnabled )
  348. {
  349. m_bCanStartDragging = true;
  350. g_pInputGameUI->GetCursorPos( m_DragStartCursorPos[0], m_DragStartCursorPos[1] );
  351. }
  352. if ( m_pGroup )
  353. {
  354. if ( code == MOUSE_LEFT )
  355. {
  356. m_pGroup->SetState( "AUTO_MOUSELEFTDOWN" );
  357. }
  358. else if ( code == MOUSE_RIGHT )
  359. {
  360. m_pGroup->SetState( "AUTO_MOUSERIGHTDOWN" );
  361. }
  362. else if ( code == MOUSE_MIDDLE )
  363. {
  364. m_pGroup->SetState( "AUTO_MOUSEMIDDLEDOWN" );
  365. }
  366. }
  367. // Check for scripting for this control first.
  368. KeyValues *kvEvent = new KeyValues( "OnMouseDown" );
  369. KeyValues::AutoDelete autodelete( kvEvent );
  370. kvEvent->SetInt( "code", code );
  371. // Always call generic click handler to allow host-overrides
  372. kvEvent->SetName( "OnMouseClicked" );
  373. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  374. // Call assigned handlers
  375. if ( code == MOUSE_LEFT && !m_OnMouseLeftClickedScriptCommand.IsEmpty() )
  376. {
  377. kvEvent->SetName( m_OnMouseLeftClickedScriptCommand );
  378. bool bExecuted = g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  379. if ( !bExecuted )
  380. {
  381. Warning( "Unable to find script function %s (assigned to OnMouseLeftClicked)\n", kvEvent->GetName() );
  382. }
  383. }
  384. }
  385. //-----------------------------------------------------------------------------
  386. // Event that occurs when mouse button is released
  387. // Script events should be tied to mouse release events only, not mouse down.
  388. // Scripts should only fire if this graphic has mousefocus.
  389. //-----------------------------------------------------------------------------
  390. void CHitArea::OnMouseUp( const ButtonCode_t &code, bool bFireScripts )
  391. {
  392. #if ( DEBUG_INPUT_EVENTS )
  393. Msg( "CHitArea::OnMouseUp\n" );
  394. #endif
  395. m_bCanStartDragging = false;
  396. if ( m_pGroup )
  397. {
  398. if ( code == MOUSE_LEFT )
  399. {
  400. m_pGroup->SetState( "AUTO_MOUSELEFTUP" );
  401. }
  402. else if ( code == MOUSE_RIGHT )
  403. {
  404. m_pGroup->SetState( "AUTO_MOUSERIGHTUP" );
  405. }
  406. else if ( code == MOUSE_MIDDLE )
  407. {
  408. m_pGroup->SetState( "AUTO_MOUSEMIDDLEUP" );
  409. }
  410. }
  411. if ( m_IsDragging )
  412. {
  413. #if ( DEBUG_INPUT_EVENTS )
  414. Msg( "CHitArea::Stopped dragging\n" );
  415. #endif
  416. OnDragStopCallScriptEvent( m_DragCurrentCursorPos[0], m_DragCurrentCursorPos[1] );
  417. }
  418. else if ( bFireScripts )
  419. {
  420. KeyValues *kvEvent = new KeyValues( "OnMouseUp" );
  421. KeyValues::AutoDelete autodelete( kvEvent );
  422. kvEvent->SetInt( "code", code );
  423. // Always call generic click handler to allow host-overrides
  424. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  425. }
  426. m_IsDragging = false;
  427. }
  428. //-----------------------------------------------------------------------------
  429. // Event that occurs when mouse button is double clicked
  430. //-----------------------------------------------------------------------------
  431. void CHitArea::OnMouseDoubleClick( const ButtonCode_t &code )
  432. {
  433. #if ( DEBUG_INPUT_EVENTS )
  434. Msg( "CHitArea::OnMouseDoubleClick\n" );
  435. #endif
  436. if ( m_pGroup )
  437. {
  438. m_pGroup->SetState( "AUTO_MOUSEDOUBLECLICK" );
  439. }
  440. }
  441. //-----------------------------------------------------------------------------
  442. // Event that occurs when a key is pressed
  443. //-----------------------------------------------------------------------------
  444. void CHitArea::OnKeyDown( const ButtonCode_t &code )
  445. {
  446. #if ( DEBUG_INPUT_EVENTS )
  447. Msg( "CHitArea::OnKeyDown\n" );
  448. #endif
  449. // Pad input gives you pressed and released messages only for buttons
  450. if ( IsJoystickCode( code ) )
  451. {
  452. KeyValues *kvEvent = new KeyValues( "OnButtonPressed" );
  453. KeyValues::AutoDelete autodelete( kvEvent );
  454. kvEvent->SetInt( "code", code );
  455. // chain to main system if this graphic doesn't handle it.
  456. g_pGameUISystemMgrImpl->OnKeyCodeTyped( code );
  457. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  458. }
  459. else
  460. {
  461. if ( m_pGroup )
  462. {
  463. m_pGroup->SetState( "AUTO_KEYDOWN" );
  464. }
  465. }
  466. }
  467. //-----------------------------------------------------------------------------
  468. // Event that occurs when a key is released
  469. //-----------------------------------------------------------------------------
  470. void CHitArea::OnKeyUp( const ButtonCode_t &code )
  471. {
  472. #if ( DEBUG_INPUT_EVENTS )
  473. Msg( "CHitArea::OnKeyUp\n" );
  474. #endif
  475. // Pad input gives you pressed and released messages only for buttons
  476. if ( IsJoystickCode( code ) )
  477. {
  478. KeyValues *kvEvent = new KeyValues( "OnButtonReleased" );
  479. KeyValues::AutoDelete autodelete( kvEvent );
  480. kvEvent->SetInt( "code", code );
  481. // chain to main system if this graphic doesn't handle it.
  482. g_pGameUISystemMgrImpl->OnKeyCodeTyped( code );
  483. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  484. }
  485. else
  486. {
  487. if ( m_pGroup )
  488. {
  489. m_pGroup->SetState( "AUTO_KEYUP" );
  490. }
  491. }
  492. }
  493. //-----------------------------------------------------------------------------
  494. // Event that occurs when a key code is typed
  495. //-----------------------------------------------------------------------------
  496. void CHitArea::OnKeyCodeTyped( const ButtonCode_t &code )
  497. {
  498. #if ( DEBUG_INPUT_EVENTS )
  499. Msg( "CHitArea::OnKeyCodeTyped\n" );
  500. #endif
  501. Assert( g_pInputGameUI->GetKeyFocus() == this );
  502. KeyValues *kvEvent = new KeyValues( "OnKeyTyped" );
  503. KeyValues::AutoDelete autodelete( kvEvent );
  504. kvEvent->SetInt( "code", code );
  505. // chain to main system if this graphic doesn't handle it.
  506. g_pGameUISystemMgrImpl->OnKeyCodeTyped( code );
  507. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  508. }
  509. //-----------------------------------------------------------------------------
  510. // Event that occurs when a key is typed
  511. //-----------------------------------------------------------------------------
  512. void CHitArea::OnKeyTyped( const wchar_t &unichar )
  513. {
  514. #if ( DEBUG_INPUT_EVENTS )
  515. Msg( "CHitArea::OnKeyTyped\n" );
  516. #endif
  517. Assert( g_pInputGameUI->GetKeyFocus() == this );
  518. // chain to main system if this graphic doesn't handle it.
  519. g_pGameUISystemMgrImpl->OnKeyTyped( unichar );
  520. }
  521. //-----------------------------------------------------------------------------
  522. // Event that occurs when key focus is lost
  523. //-----------------------------------------------------------------------------
  524. void CHitArea::OnLoseKeyFocus()
  525. {
  526. #if ( DEBUG_INPUT_EVENTS )
  527. Msg( "CHitArea::OnLoseKeyFocus\n" );
  528. #endif
  529. if ( m_pGroup )
  530. {
  531. m_pGroup->SetState( "AUTO_LOSEKEYFOCUS" );
  532. }
  533. KeyValues *kvEvent = new KeyValues( "OnKeyFocusLost" );
  534. KeyValues::AutoDelete autodelete( kvEvent );
  535. // chain to main system if this graphic doesn't handle it.
  536. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  537. }
  538. //-----------------------------------------------------------------------------
  539. // Event that occurs when key focus is gained
  540. //-----------------------------------------------------------------------------
  541. void CHitArea::OnGainKeyFocus()
  542. {
  543. #if ( DEBUG_INPUT_EVENTS )
  544. Msg( "CHitArea::OnGainKeyFocus\n" );
  545. #endif
  546. if ( m_pGroup )
  547. {
  548. m_pGroup->SetState( "AUTO_GAINKEYFOCUS" );
  549. }
  550. KeyValues *kvEvent = new KeyValues( "OnKeyFocusGained" );
  551. KeyValues::AutoDelete autodelete( kvEvent );
  552. // chain to main system if this graphic doesn't handle it.
  553. g_pGameUISystemMgrImpl->OnGameGraphicScriptEvent( this, kvEvent );
  554. }
  555. //-----------------------------------------------------------------------------
  556. //
  557. //-----------------------------------------------------------------------------
  558. void CHitArea::OnDragStartCallScriptEvent( const int &cursorX, const int &cursorY )
  559. {
  560. }
  561. //-----------------------------------------------------------------------------
  562. //
  563. //-----------------------------------------------------------------------------
  564. void CHitArea::OnDragCallScriptEvent( const int &cursorX, const int &cursorY )
  565. {
  566. }
  567. //-----------------------------------------------------------------------------
  568. //
  569. //-----------------------------------------------------------------------------
  570. void CHitArea::OnDragStopCallScriptEvent( const int &cursorX, const int &cursorY )
  571. {
  572. }
  573. //-----------------------------------------------------------------------------
  574. // Handle focus updating on visibility change.
  575. //-----------------------------------------------------------------------------
  576. void CHitArea::SetVisible( bool bVisible )
  577. {
  578. m_Geometry.m_bVisible = bVisible;
  579. m_bCanAcceptInput = bVisible;
  580. m_bCanStartDragging = false;
  581. if ( bVisible )
  582. {
  583. g_pGameUISystemMgrImpl->ForceFocusUpdate();
  584. }
  585. else
  586. {
  587. // Untested
  588. g_pInputGameUI->GraphicHidden( this );
  589. }
  590. }
  591. //-----------------------------------------------------------------------------
  592. // Handle commands from scripting
  593. //-----------------------------------------------------------------------------
  594. KeyValues * CHitArea::HandleScriptCommand( KeyValues *args )
  595. {
  596. char const *szCommand = args->GetName();
  597. if ( !Q_stricmp( "SetDragEnabled", szCommand ) )
  598. {
  599. m_bDragEnabled = args->GetBool( "dragenabled", 0 );
  600. return NULL;
  601. }
  602. else if ( !Q_stricmp( "SetMouseLeftClickedCommand", szCommand ) )
  603. {
  604. m_OnMouseLeftClickedScriptCommand = args->GetString( "command", "" );
  605. return NULL;
  606. }
  607. if ( !Q_stricmp( "RequestFocus", szCommand ) )
  608. {
  609. g_pGameUISystemMgrImpl->RequestKeyFocus( this, args );
  610. return NULL;
  611. }
  612. return CGameGraphic::HandleScriptCommand( args );
  613. }