Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "VControl.h"
- #include "keylist.h"
- class VHotkey : public VControl< int >
- {
- public:
- VHotkey( const char* szName, int x, int y, int w, int h, VPanel* parent, int* iSetting )
- : VControl( szName, x, y, w, h, parent, iSetting, TYPE_INT )
- {
- m_iTick = 0;
- m_bClicked = false;
- m_bKeyPressed = false;
- SetValue( 0 );
- }
- virtual void Render( int mousex, int mousey )
- {
- int x, y, w, h;
- GetPos( x, y );
- GetSize( w, h );
- bool bHover = IsWithinRect( mousex, mousey );
- Color color = bHover ? HoverColorText : ColorText;
- if( m_bClicked )
- {
- g_pRender->DrawString( menuitemFont, ALIGN_LEFT, x, y, color, "%s: Press a key...", GetName() );
- int key = GetPressedKey();
- if( key > 0 )
- {
- m_bKeyPressed = true;
- SetValue( key );
- }
- else if( key < 0 )
- {
- SetValue( 0 );
- m_bClicked = false;
- m_bKeyPressed = false;
- return;
- }
- else
- {
- SetValue( 0 );
- return;
- }
- if( m_bKeyPressed )
- {
- m_bClicked = false;
- m_bKeyPressed = false;
- }
- }
- else
- {
- g_pRender->DrawString( menuitemFont, ALIGN_LEFT, x, y, color, "%s: %s", GetName(), GetKeyNameByVkCode( GetValue() ) );
- }
- }
- virtual void OnClick( int mousex, int mousey )
- {
- if( IsWithinRect( mousex, mousey ) )
- {
- if( !m_bClicked )
- {
- m_bClicked = true;
- }
- else
- {
- SetValue( VK_LBUTTON );
- m_bClicked = false;
- m_bKeyPressed = false;
- }
- }
- }
- protected:
- int GetPressedKey()
- {
- if( GetAsyncKeyState( VK_ESCAPE ) & 1 )
- return -1;
- for( int i = 0; i < 123; i++ )
- {
- if( GetAsyncKeyState( i ) & 1 )
- return i;
- }
- return 0;
- }
- const char* GetKeyNameByVkCode( int vkCode )
- {
- if( vkCode == 0 )
- {
- return "Not Set";
- }
- if( vkCode < 0 || vkCode > ArraySize( hhb_vkey_list ) )
- {
- return "Not Set / Invalid";
- }
- for( int i = 0; i < ArraySize( hhb_vkey_list ); i++ )
- {
- if( hhb_vkey_list[ i ].vkey == vkCode )
- {
- return hhb_vkey_list[ i ].description;
- }
- }
- return "Not Set / Invalid";
- }
- protected:
- int m_iTick;
- bool m_bClicked;
- bool m_bKeyPressed;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement