touch: replace static arrays with linked list

This commit is contained in:
nillerusr 2021-10-13 18:18:36 +03:00
parent 63e458ef84
commit 977bac3205
6 changed files with 197 additions and 164 deletions

View File

@ -1206,7 +1206,8 @@ void CHLClient::Shutdown( void )
gHUD.Shutdown();
VGui_Shutdown();
gTouch.Shutdown();
ParticleMgr()->Term();
vgui::BuildGroup::ClearResFileCache();

View File

@ -19,7 +19,17 @@ extern ConVar cl_upspeed;
#define TOUCH_DEFAULT "0"
#endif
extern ConVar sensitivity;
ConVar touch_enable( "touch_enable", TOUCH_DEFAULT, FCVAR_ARCHIVE );
ConVar touch_forwardzone( "touch_forwardzone", "0.06", FCVAR_ARCHIVE, "forward touch zone" );
ConVar touch_sidezone( "touch_sidezone", "0.06", FCVAR_ARCHIVE, "side touch zone" );
ConVar touch_pitch( "touch_pitch", "90", FCVAR_ARCHIVE, "touch pitch sensitivity" );
ConVar touch_yaw( "touch_yaw", "120", FCVAR_ARCHIVE, "touch yaw sensitivity" );
ConVar touch_config_file( "touch_config_file", "touch.cfg", FCVAR_ARCHIVE, "current touch profile file" );
ConVar touch_grid_count( "touch_grid_count", "50", FCVAR_ARCHIVE, "touch grid count" );
ConVar touch_grid_enable( "touch_grid_enable", "1", FCVAR_ARCHIVE, "enable touch grid" );
ConVar touch_precise_amount( "touch_precise_amount", "0.5", FCVAR_ARCHIVE, "sensitivity multiplier for precise-look" );
#define boundmax( num, high ) ( (num) < (high) ? (num) : (high) )
#define boundmin( num, low ) ( (num) >= (low) ? (num) : (low) )
@ -27,13 +37,6 @@ ConVar touch_enable( "touch_enable", TOUCH_DEFAULT, FCVAR_ARCHIVE );
#define S
extern IVEngineClient *engine;
extern vgui::IInputInternal *g_pInputInternal;
static int g_LastDefaultButton = 0;
int screen_h, screen_w;
static CTouchButton g_Buttons[512];
static int g_LastButton = 0;
CTouchControls gTouch;
static VTouchPanel g_TouchPanel;
@ -56,10 +59,6 @@ CTouchPanel::CTouchPanel( vgui::VPANEL parent ) : BaseClass( NULL, "TouchPanel"
SetVisible( true );
}
CTouchPanel::~CTouchPanel( void )
{
}
bool CTouchPanel::ShouldDraw( void )
{
return touch_enable.GetBool() && !enginevgui->IsGameUIVisible();
@ -67,15 +66,44 @@ bool CTouchPanel::ShouldDraw( void )
void CTouchPanel::Paint()
{
gTouch.Frame();
if( ShouldDraw() )
gTouch.Frame();
}
CTouchControls::CTouchControls()
CON_COMMAND( touch_addbutton, "add native touch button" )
{
}
rgba_t color;
int argc = args.ArgC();
CTouchControls::~CTouchControls()
{
if( argc >= 12 )
{
color = rgba_t(Q_atoi(args[8]), Q_atoi(args[9]), Q_atoi(args[10]), Q_atoi(args[11]));
gTouch.IN_TouchAddButton( args[1], args[2], args[3],
Q_atof( args[4] ), Q_atof( args[5] ),
Q_atof( args[6] ), Q_atof( args[7] ) ,
color );
return;
}
if( argc >= 8 )
{
color = rgba_t(255,255,255);
gTouch.IN_TouchAddButton( args[1], args[2], args[3],
Q_atof( args[4] ), Q_atof( args[5] ),
Q_atof( args[6] ), Q_atof( args[7] ),
color );
return;
}
if( argc >= 4 )
{
color = rgba_t(255,255,255);
gTouch.IN_TouchAddButton( args[1], args[2], args[3], 0.4, 0.4, 0.6, 0.6 );
return;
}
Msg( "Usage: touch_addbutton <name> <texture> <command> [<x1> <y1> <x2> <y2> [ r g b a ] ]\n" );
}
void CTouchControls::Init()
@ -100,35 +128,32 @@ void CTouchControls::Init()
rgba_t color(255, 255, 255, 255);
IN_TouchAddButton( "use", "vgui/touch/use", "+use", touch_command, 0.880000, 0.213333, 1.000000, 0.426667, color );
IN_TouchAddButton( "jump", "vgui/touch/jump", "+jump", touch_command, 0.880000, 0.462222, 1.000000, 0.675556, color );
IN_TouchAddButton( "attack", "vgui/touch/shoot", "+attack", touch_command, 0.760000, 0.583333, 0.880000, 0.796667, color );
IN_TouchAddButton( "attack2", "vgui/touch/shoot_alt", "+attack2", touch_command, 0.760000, 0.320000, 0.880000, 0.533333, color );
IN_TouchAddButton( "duck", "vgui/touch/crouch", "+duck", touch_command, 0.880000, 0.746667, 1.000000, 0.960000, color );
IN_TouchAddButton( "tduck", "vgui/touch/tduck", ";+duck", touch_command, 0.560000, 0.817778, 0.620000, 0.924444, color );
IN_TouchAddButton( "look", "", "", touch_look, 0.5, 0, 1, 1, color );
IN_TouchAddButton( "move", "", "", touch_move, 0, 0, 0.5, 1, color );
IN_TouchAddButton( "zoom", "vgui/touch/zoom", "+zoom", touch_command, 0.680000, 0.00000, 0.760000, 0.142222, color );
IN_TouchAddButton( "speed", "vgui/touch/speed", "+speed", touch_command, 0.180000, 0.568889, 0.280000, 0.746667, color );
IN_TouchAddButton( "loadquick", "vgui/touch/load", "load quick", touch_command, 0.760000, 0.000000, 0.840000, 0.142222, color );
IN_TouchAddButton( "savequick", "vgui/touch/save", "save quick", touch_command, 0.840000, 0.000000, 0.920000, 0.142222, color );
IN_TouchAddButton( "reload", "vgui/touch/reload", "+reload", touch_command, 0.000000, 0.320000, 0.120000, 0.533333, color );
IN_TouchAddButton( "flashlight", "vgui/touch/flash_light_filled", "impulse 100", touch_command, 0.920000, 0.000000, 1.000000, 0.142222, color );
IN_TouchAddButton( "invnext", "vgui/touch/next_weap", "invnext", touch_command, 0.000000, 0.533333, 0.120000, 0.746667, color );
IN_TouchAddButton( "invprev", "vgui/touch/prev_weap", "invprev", touch_command, 0.000000, 0.071111, 0.120000, 0.284444, color );
IN_TouchAddButton( "menu", "vgui/touch/menu", "gameui_activate", touch_command, 0.000000, 0.00000, 0.080000, 0.142222, color );
IN_TouchAddButton( "use", "vgui/touch/use", "+use", 0.880000, 0.213333, 1.000000, 0.426667, color );
IN_TouchAddButton( "jump", "vgui/touch/jump", "+jump", 0.880000, 0.462222, 1.000000, 0.675556, color );
IN_TouchAddButton( "attack", "vgui/touch/shoot", "+attack", 0.760000, 0.583333, 0.880000, 0.796667, color );
IN_TouchAddButton( "attack2", "vgui/touch/shoot_alt", "+attack2", 0.760000, 0.320000, 0.880000, 0.533333, color );
IN_TouchAddButton( "duck", "vgui/touch/crouch", "+duck", 0.880000, 0.746667, 1.000000, 0.960000, color );
IN_TouchAddButton( "tduck", "vgui/touch/tduck", ";+duck", 0.560000, 0.817778, 0.620000, 0.924444, color );
IN_TouchAddButton( "_look", "", "", 0.5, 0, 1, 1, color );
IN_TouchAddButton( "_move", "", "", 0, 0, 0.5, 1, color );
IN_TouchAddButton( "zoom", "vgui/touch/zoom", "+zoom", 0.680000, 0.00000, 0.760000, 0.142222, color );
IN_TouchAddButton( "speed", "vgui/touch/speed", "+speed", 0.180000, 0.568889, 0.280000, 0.746667, color );
IN_TouchAddButton( "loadquick", "vgui/touch/load", "load quick", 0.760000, 0.000000, 0.840000, 0.142222, color );
IN_TouchAddButton( "savequick", "vgui/touch/save", "save quick", 0.840000, 0.000000, 0.920000, 0.142222, color );
IN_TouchAddButton( "reload", "vgui/touch/reload", "+reload", 0.000000, 0.320000, 0.120000, 0.533333, color );
IN_TouchAddButton( "flashlight", "vgui/touch/flash_light_filled", "impulse 100", 0.920000, 0.000000, 1.000000, 0.142222, color );
IN_TouchAddButton( "invnext", "vgui/touch/next_weap", "invnext", 0.000000, 0.533333, 0.120000, 0.746667, color );
IN_TouchAddButton( "invprev", "vgui/touch/prev_weap", "invprev", 0.000000, 0.071111, 0.120000, 0.284444, color );
//IN_TouchAddButton( "edit", "vgui/touch/settings", "touch_enableedit", 0.420000, 0.000000, 0.500000, 0.151486, color );
IN_TouchAddButton( "menu", "vgui/touch/menu", "gameui_activate", 0.000000, 0.00000, 0.080000, 0.142222, color );
}
#define GRID_COUNT 50
#define GRID_COUNT_X (GRID_COUNT)
#define GRID_COUNT_Y (GRID_COUNT * screen_h / screen_w)
#define GRID_X (1.0/GRID_COUNT_X)
#define GRID_Y (screen_w/screen_h/GRID_COUNT_X)
#define GRID_ROUND_X(x) ((float)round( x * GRID_COUNT_X ) / GRID_COUNT_X)
#define GRID_ROUND_Y(x) ((float)round( x * GRID_COUNT_Y ) / GRID_COUNT_Y)
void CTouchControls::Shutdown( )
{
btns.PurgeAndDeleteElements();
}
static void IN_TouchCheckCoords( float *x1, float *y1, float *x2, float *y2 )
void CTouchControls::IN_TouchCheckCoords( float *x1, float *y1, float *x2, float *y2 )
{
/// TODO: grid check here
if( *x2 - *x1 < GRID_X * 2 )
@ -144,17 +169,16 @@ static void IN_TouchCheckCoords( float *x1, float *y1, float *x2, float *y2 )
if( *x2 > 1 )
*x1 -= *x2 - 1, *x2 = 1;
*x1 = GRID_ROUND_X( *x1 );
*x2 = GRID_ROUND_X( *x2 );
*y1 = GRID_ROUND_Y( *y1 );
*y2 = GRID_ROUND_Y( *y2 );
if ( touch_grid_enable.GetBool() )
{
*x1 = GRID_ROUND_X( *x1 );
*x2 = GRID_ROUND_X( *x2 );
*y1 = GRID_ROUND_Y( *y1 );
*y2 = GRID_ROUND_Y( *y2 );
}
}
void CTouchControls::VidInit( ) { }
void CTouchControls::Shutdown( ) { }
void CTouchControls::Move( float frametime, CUserCmd *cmd )
void CTouchControls::Move( float /*frametime*/, CUserCmd *cmd )
{
cmd->sidemove -= cl_sidespeed.GetFloat() * side;
cmd->forwardmove += cl_forwardspeed.GetFloat() * forward;
@ -162,24 +186,24 @@ void CTouchControls::Move( float frametime, CUserCmd *cmd )
void CTouchControls::IN_Look()
{
if( !pitch && !yaw )
return;
if( !pitch && !yaw )
return;
QAngle ang;
engine->GetViewAngles( ang );
ang.x += pitch;
ang.y += yaw;
engine->SetViewAngles( ang );
pitch = yaw = 0;
QAngle ang;
engine->GetViewAngles( ang );
ang.x += pitch;
ang.y += yaw;
engine->SetViewAngles( ang );
pitch = yaw = 0;
}
void CTouchControls::Frame()
{
if (!initialized)
return;
if (!initialized)
return;
IN_Look();
Paint();
IN_Look();
Paint();
}
void CTouchControls::Paint( )
@ -187,44 +211,50 @@ void CTouchControls::Paint( )
if (!initialized)
return;
if ( !enginevgui->IsGameUIVisible() )
CUtlLinkedList<CTouchButton*>::iterator it;
for( it = btns.begin(); it != btns.end(); it++ )
{
for (int i = 0; i < g_LastButton; i++)
{
if( g_Buttons[i].type == touch_move || g_Buttons[i].type == touch_look )
continue;
g_pMatSystemSurface->DrawSetColor(255, 255, 255, 155);
g_pMatSystemSurface->DrawSetTexture( g_Buttons[i].textureID );
g_pMatSystemSurface->DrawTexturedRect( g_Buttons[i].x1*screen_w, g_Buttons[i].y1*screen_h, g_Buttons[i].x2*screen_w, g_Buttons[i].y2*screen_h );
}
CTouchButton *btn = *it;
if( btn->type == touch_move || btn->type == touch_look )
continue;
g_pMatSystemSurface->DrawSetColor(255, 255, 255, 155);
g_pMatSystemSurface->DrawSetTexture( btn->textureID );
g_pMatSystemSurface->DrawTexturedRect( btn->x1*screen_w, btn->y1*screen_h, btn->x2*screen_w, btn->y2*screen_h );
}
}
void CTouchControls::IN_TouchAddButton( const char *name, const char *texturefile, const char *command, ETouchButtonType type, float x1, float y1, float x2, float y2, rgba_t color )
void CTouchControls::IN_TouchAddButton( const char *name, const char *texturefile, const char *command, float x1, float y1, float x2, float y2, rgba_t color )
{
if( g_LastButton >= 64 )
return;
Q_strncpy( g_Buttons[g_LastButton].name, name, 32 );
Q_strncpy( g_Buttons[g_LastButton].texturefile, texturefile, 256 );
Q_strncpy( g_Buttons[g_LastButton].command, command, 256 );
CTouchButton *btn = new CTouchButton;
ETouchButtonType type = touch_command;
Q_strncpy( btn->name, name, sizeof(btn->name) );
Q_strncpy( btn->texturefile, texturefile, sizeof(btn->texturefile) );
Q_strncpy( btn->command, command, sizeof(btn->command) );
IN_TouchCheckCoords(&x1, &y1, &x2, &y2);
g_Buttons[g_LastButton].x1 = x1;
g_Buttons[g_LastButton].y1 = y1;
g_Buttons[g_LastButton].x2 = x2;
g_Buttons[g_LastButton].y2 = y1 + ( x2 - x1 ) * (((float)screen_w)/screen_h);
btn->x1 = x1;
btn->y1 = y1;
btn->x2 = x2;
btn->y2 = y1 + ( x2 - x1 ) * (((float)screen_w)/screen_h);
IN_TouchCheckCoords(&g_Buttons[g_LastButton].x1, &g_Buttons[g_LastButton].y1, &g_Buttons[g_LastButton].x2, &g_Buttons[g_LastButton].y2);
IN_TouchCheckCoords(&btn->x1, &btn->y1, &btn->x2, &btn->y2);
g_Buttons[g_LastButton].color = color;
g_Buttons[g_LastButton].type = type;
g_Buttons[g_LastButton].finger = -1;
g_Buttons[g_LastButton].textureID = g_pMatSystemSurface->CreateNewTextureID();
g_pMatSystemSurface->DrawSetTextureFile( g_Buttons[g_LastButton].textureID, g_Buttons[g_LastButton].texturefile, true, false);
if( Q_strcmp(name, "_look") == 0 )
type = touch_look;
else if( Q_strcmp(name, "_move") == 0 )
type = touch_move;
btn->color = color;
btn->type = type;
btn->finger = -1;
btn->textureID = g_pMatSystemSurface->CreateNewTextureID();
g_LastButton++;
g_pMatSystemSurface->DrawSetTextureFile( btn->textureID, btn->texturefile, true, false);
btns.AddToTail(btn);
}
void CTouchControls::ProcessEvent(touch_event_t *ev)
@ -245,21 +275,23 @@ void CTouchControls::FingerMotion(touch_event_t *ev)
float f, s;
for (int i = 0; i < g_LastButton; i++)
CUtlLinkedList<CTouchButton*>::iterator it;
for( it = btns.begin(); it != btns.end(); it++ )
{
if( g_Buttons[i].finger == ev->fingerid )
CTouchButton *btn = *it;
if( btn->finger == ev->fingerid )
{
if( g_Buttons[i].type == touch_move )
if( btn->type == touch_move )
{
f = ( move_start_y - y ) / touch_settings.sidezone;
s = ( move_start_x - x ) / touch_settings.sidezone;
f = ( move_start_y - y ) / touch_forwardzone.GetFloat();
s = ( move_start_x - x ) / touch_sidezone.GetFloat();
forward = bound( -1, f, 1 );
side = bound( -1, s, 1 );
}
else if( g_Buttons[i].type == touch_look )
else if( btn->type == touch_look )
{
yaw += touch_settings.yaw * ( dx - x ) * touch_settings.sensitivity;
pitch -= touch_settings.pitch * ( dy - y ) * touch_settings.sensitivity;
yaw += touch_yaw.GetFloat() * ( dx - x ) * sensitivity.GetFloat();
pitch -= touch_pitch.GetFloat() * ( dy - y ) * sensitivity.GetFloat();
dx = x;
dy = y;
}
@ -272,14 +304,17 @@ void CTouchControls::FingerPress(touch_event_t *ev)
float x = ev->x / (float)screen_w;
float y = ev->y / (float)screen_h;
CUtlLinkedList<CTouchButton*>::iterator it;
if( ev->type == IE_FingerDown )
{
for (int i = 0; i < g_LastButton; i++)
for( it = btns.begin(); it != btns.end(); it++ )
{
if( x > g_Buttons[i].x1 && x < g_Buttons[i].x2 && y > g_Buttons[i].y1 && y < g_Buttons[i].y2 )
CTouchButton *btn = *it;
if( x > btn->x1 && x < btn->x2 && y > btn->y1 && y < btn->y2 )
{
g_Buttons[i].finger = ev->fingerid;
if( g_Buttons[i].type == touch_move )
btn->finger = ev->fingerid;
if( btn->type == touch_move )
{
if( move_finger == -1 )
{
@ -288,9 +323,9 @@ void CTouchControls::FingerPress(touch_event_t *ev)
move_finger = ev->fingerid;
}
else
g_Buttons[i].finger = move_finger;
btn->finger = move_finger;
}
else if( g_Buttons[i].type == touch_look )
else if( btn->type == touch_look )
{
if( look_finger == -1 )
{
@ -299,39 +334,38 @@ void CTouchControls::FingerPress(touch_event_t *ev)
look_finger = ev->fingerid;
}
else
g_Buttons[i].finger = look_finger;
btn->finger = look_finger;
}
else
engine->ClientCmd( g_Buttons[i].command );
engine->ClientCmd( btn->command );
}
}
}
else if( ev->type == IE_FingerUp )
{
{
for (int i = 0; i < g_LastButton; i++)
{
if( g_Buttons[i].finger == ev->fingerid )
{
g_Buttons[i].finger = -1;
}
else if( ev->type == IE_FingerUp )
{
for( it = btns.begin(); it != btns.end(); it++ )
{
CTouchButton *btn = *it;
if( btn->finger == ev->fingerid )
{
btn->finger = -1;
if( g_Buttons[i].type == touch_move )
{
forward = side = 0;
move_finger = -1;
}
else if( g_Buttons[i].type == touch_look )
look_finger = -1;
else if( g_Buttons[i].command[0] == '+' )
{
char cmd[256];
if( btn->type == touch_move )
{
forward = side = 0;
move_finger = -1;
}
else if( btn->type == touch_look )
look_finger = -1;
else if( btn->command[0] == '+' )
{
char cmd[256];
snprintf( cmd, sizeof cmd, "%s", g_Buttons[i].command );
cmd[0] = '-';
engine->ClientCmd( cmd );
}
}
}
}
}
snprintf( cmd, sizeof cmd, "%s", btn->command );
cmd[0] = '-';
engine->ClientCmd( cmd );
}
}
}
}
}

View File

@ -7,17 +7,13 @@
extern ConVar touch_enable;
#define STEAMCONTROLLER_A -3
#define MOUSE_EVENT_PRESS 0x02
#define MOUSE_EVENT_RELEASE 0x04
enum ActivationType_t
{
ACTIVATE_ONPRESSEDANDRELEASED, // normal button behaviour
ACTIVATE_ONPRESSED, // menu buttons, toggle buttons
ACTIVATE_ONRELEASED, // menu items
};
#define GRID_COUNT touch_grid_count.GetInt()
#define GRID_COUNT_X (GRID_COUNT)
#define GRID_COUNT_Y (GRID_COUNT * screen_h / screen_w)
#define GRID_X (1.0/GRID_COUNT_X)
#define GRID_Y (screen_w/screen_h/GRID_COUNT_X)
#define GRID_ROUND_X(x) ((float)round( x * GRID_COUNT_X ) / GRID_COUNT_X)
#define GRID_ROUND_Y(x) ((float)round( x * GRID_COUNT_Y ) / GRID_COUNT_Y)
#define CMD_SIZE 64
@ -95,22 +91,13 @@ public:
int textureID;
};
struct touch_settings_s
{
float pitch = 90;
float yaw = 120;
float sensitivity = 2;
float forwardzone = 0.8;
float sidezone = 0.12;
};
class CTouchPanel : public vgui::Panel
{
DECLARE_CLASS_SIMPLE( CTouchPanel, vgui::Panel );
public:
CTouchPanel( vgui::VPANEL parent );
virtual ~CTouchPanel( void );
virtual ~CTouchPanel( void ) {};
virtual void Paint();
virtual bool ShouldDraw( void );
@ -153,17 +140,16 @@ public:
class CTouchControls
{
public:
CTouchControls();
~CTouchControls();
void VidInit( );
void Shutdown( );
void Init( );
void Shutdown( );
void Paint( );
void Frame( );
void IN_TouchAddButton( const char *name, const char *texturefile, const char *command, ETouchButtonType type, float x1, float y1, float x2, float y2, rgba_t color );
void IN_TouchAddButton( const char *name, const char *texturefile, const char *command, float x1, float y1, float x2, float y2, rgba_t color = rgba_t(255, 255, 255, 255) );
void IN_TouchCheckCoords( float *x1, float *y1, float *x2, float *y2 );
void Move( float frametime, CUserCmd *cmd );
void IN_Look( );
@ -175,7 +161,7 @@ public:
private:
bool initialized;
ETouchState state;
CUtlLinkedList<CTouchButton> btns;
CUtlLinkedList<CTouchButton*> btns;
int look_finger;
int move_finger;
@ -205,7 +191,7 @@ private:
vgui::HFont textfont;
int mouse_events;
struct touch_settings_s touch_settings;
int screen_h, screen_w;
};
extern CTouchControls gTouch;

View File

@ -1562,3 +1562,10 @@ ISteamController* CInputSystem::SteamControllerInterface()
return m_SteamAPIContext.SteamController();
}
}
void CInputSystem::StartTextInput()
{
#ifdef USE_SDL
SDL_StartTextInput();
#endif
}

View File

@ -374,6 +374,8 @@ public:
m_bSkipControllerInitialization = bSkip;
}
void StartTextInput();
#if defined( USE_SDL )
void PollInputState_Platform();

View File

@ -28,6 +28,7 @@
#include <vgui_controls/MenuItem.h>
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#include <inputsystem/iinputsystem.h>
enum
{
@ -1482,7 +1483,9 @@ void TextEntry::OnMousePressed(MouseCode code)
BaseClass::OnMousePressed( code );
return;
}
g_pInputSystem->StartTextInput();
// move the cursor to where the mouse was pressed
int x, y;
input()->GetCursorPos(x, y);