Advertisement
Guest User

Untitled

a guest
Jul 26th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.17 KB | None | 0 0
  1. #pragma once
  2. #include "VControl.h"
  3. #include "keylist.h"
  4.  
  5. class VHotkey : public VControl< int >
  6. {
  7. public:
  8. VHotkey( const char* szName, int x, int y, int w, int h, VPanel* parent, int* iSetting )
  9. : VControl( szName, x, y, w, h, parent, iSetting, TYPE_INT )
  10. {
  11. m_iTick = 0;
  12. m_bClicked = false;
  13. m_bKeyPressed = false;
  14.  
  15. SetValue( 0 );
  16. }
  17.  
  18. virtual void Render( int mousex, int mousey )
  19. {
  20. int x, y, w, h;
  21. GetPos( x, y );
  22. GetSize( w, h );
  23.  
  24. bool bHover = IsWithinRect( mousex, mousey );
  25.  
  26. Color color = bHover ? HoverColorText : ColorText;
  27.  
  28. if( m_bClicked )
  29. {
  30. g_pRender->DrawString( menuitemFont, ALIGN_LEFT, x, y, color, "%s: Press a key...", GetName() );
  31.  
  32. int key = GetPressedKey();
  33.  
  34. if( key > 0 )
  35. {
  36. m_bKeyPressed = true;
  37.  
  38. SetValue( key );
  39. }
  40. else if( key < 0 )
  41. {
  42. SetValue( 0 );
  43. m_bClicked = false;
  44. m_bKeyPressed = false;
  45.  
  46. return;
  47. }
  48. else
  49. {
  50. SetValue( 0 );
  51.  
  52. return;
  53. }
  54.  
  55. if( m_bKeyPressed )
  56. {
  57. m_bClicked = false;
  58. m_bKeyPressed = false;
  59. }
  60. }
  61. else
  62. {
  63. g_pRender->DrawString( menuitemFont, ALIGN_LEFT, x, y, color, "%s: %s", GetName(), GetKeyNameByVkCode( GetValue() ) );
  64. }
  65. }
  66.  
  67. virtual void OnClick( int mousex, int mousey )
  68. {
  69. if( IsWithinRect( mousex, mousey ) )
  70. {
  71. if( !m_bClicked )
  72. {
  73. m_bClicked = true;
  74. }
  75. else
  76. {
  77. SetValue( VK_LBUTTON );
  78.  
  79. m_bClicked = false;
  80. m_bKeyPressed = false;
  81. }
  82. }
  83. }
  84.  
  85. protected:
  86. int GetPressedKey()
  87. {
  88. if( GetAsyncKeyState( VK_ESCAPE ) & 1 )
  89. return -1;
  90.  
  91. for( int i = 0; i < 123; i++ )
  92. {
  93. if( GetAsyncKeyState( i ) & 1 )
  94. return i;
  95. }
  96.  
  97. return 0;
  98. }
  99.  
  100. const char* GetKeyNameByVkCode( int vkCode )
  101. {
  102. if( vkCode == 0 )
  103. {
  104. return "Not Set";
  105. }
  106.  
  107. if( vkCode < 0 || vkCode > ArraySize( hhb_vkey_list ) )
  108. {
  109. return "Not Set / Invalid";
  110. }
  111.  
  112. for( int i = 0; i < ArraySize( hhb_vkey_list ); i++ )
  113. {
  114. if( hhb_vkey_list[ i ].vkey == vkCode )
  115. {
  116. return hhb_vkey_list[ i ].description;
  117. }
  118. }
  119.  
  120. return "Not Set / Invalid";
  121. }
  122.  
  123.  
  124. protected:
  125. int m_iTick;
  126. bool m_bClicked;
  127. bool m_bKeyPressed;
  128. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement