Skip to content

Commit bb53835

Browse files
committed
Merge pull request #5: Nickname completion
Adds the ability to complete nicknames in the chatbox when the tab key is pressed - this feature is toggleable in the Interface section of the settings. This patch was submitted by @Audifire, and is also available on the bug mantis: http://bugs.mtasa.com/view.php?id=8734
2 parents 5f84736 + 5f0f79a commit bb53835

File tree

9 files changed

+118
-2
lines changed

9 files changed

+118
-2
lines changed

MTA10/core/CChat.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ void CChat::LoadCVars ( void )
124124
CVARS_GET ( "chat_line_life", (unsigned int &)m_ulChatLineLife );
125125
CVARS_GET ( "chat_line_fade_out", (unsigned int &)m_ulChatLineFadeOut );
126126
CVARS_GET ( "chat_font", (unsigned int &)Font ); SetChatFont ( (eChatFont)Font );
127+
CVARS_GET ( "chat_autocomplete", m_bAutocomplete );
127128

128129
// Modify default chat box to be like 'Transparent' preset
129130
SString strFlags;
@@ -596,9 +597,94 @@ bool CChat::CharacterKeyHandler ( CGUIKeyEventArgs KeyboardArgs )
596597
m_fSmoothScrollResetTime = GetSecondCount ();
597598
break;
598599
}
600+
case VK_TAB:
601+
{
602+
603+
if ( m_bAutocomplete && m_strInputText.size () > 0 )
604+
{
605+
bool bSuccess = false;
606+
607+
SString strCurrentInput = GetInputText ();
608+
size_t iFound;
609+
iFound = strCurrentInput.find_last_of ( " " );
610+
if ( iFound == std::string::npos )
611+
iFound = 0;
612+
else
613+
++iFound;
614+
615+
SString strPlayerNamePart = strCurrentInput.substr ( iFound );
616+
617+
CModManager* pModManager = CModManager::GetSingletonPtr ();
618+
if ( pModManager && pModManager->GetCurrentMod () )
619+
{
620+
//Create vector and get playernames from deathmatch module
621+
std::vector<SString> vPlayerNames;
622+
pModManager->GetCurrentMod ()->GetPlayerNames ( vPlayerNames );
623+
624+
for ( std::vector<SString>::iterator iter = vPlayerNames.begin ();
625+
iter != vPlayerNames.end ();
626+
++iter )
627+
{
628+
SString strPlayerName = *iter;
629+
630+
// Check if there is another player after our last result
631+
if ( m_strLastPlayerName.size () != 0 )
632+
{
633+
if ( strPlayerName.CompareI ( m_strLastPlayerName ) ) {
634+
m_strLastPlayerName.clear ();
635+
if ( *iter == vPlayerNames.back () )
636+
{
637+
CharacterKeyHandler ( KeyboardArgs );
638+
return true;
639+
}
640+
}
641+
continue;
642+
}
643+
644+
// Already a part?
645+
if ( m_strLastPlayerNamePart.size () != 0 )
646+
{
647+
strPlayerNamePart = m_strLastPlayerNamePart;
648+
}
649+
650+
// Check namepart
651+
if ( !strPlayerName.BeginsWith ( strPlayerNamePart ) )
652+
continue;
653+
else
654+
{
655+
//Check size if it's ok, then output
656+
SString strOutput = strCurrentInput.replace ( iFound, std::string::npos, strPlayerName );
657+
if ( MbUTF8ToUTF16 ( strOutput ).size () < CHAT_MAX_CHAT_LENGTH )
658+
{
659+
bSuccess = true;
660+
m_strLastPlayerNamePart = strPlayerNamePart;
661+
m_strLastPlayerName = strPlayerName;
662+
SetInputText ( strOutput );
663+
}
664+
665+
break;
666+
}
667+
}
668+
669+
// No success? Try again!
670+
if ( !bSuccess )
671+
m_strLastPlayerName.clear ();
672+
}
673+
674+
}
675+
break;
676+
}
599677

600678
default:
601679
{
680+
// Clear last namepart when pressing letter
681+
if (m_strLastPlayerNamePart.size() != 0)
682+
m_strLastPlayerNamePart.clear();
683+
684+
// Clear last name when pressing letter
685+
if (m_strLastPlayerName.size() != 0)
686+
m_strLastPlayerName.clear();
687+
602688
// If we haven't exceeded the maximum number of characters per chat message, append the char to the message and update the input control
603689
if ( MbUTF8ToUTF16(m_strInputText).size () < CHAT_MAX_CHAT_LENGTH )
604690
{

MTA10/core/CChat.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ class CChat
246246
float m_fSmoothScrollResetTime;
247247
float m_fSmoothRepeatTimer;
248248
CChatInputLine m_InputLine;
249+
SString m_strLastPlayerNamePart;
250+
SString m_strLastPlayerName;
249251

250252
CGUI* m_pManager;
251253
CGUIFont* m_pFont;
@@ -295,6 +297,8 @@ class CChat
295297
CVector2D m_RenderTargetChatSize;
296298
int m_iReportCount;
297299
CTickCount m_lastRenderTargetCreationFail;
300+
301+
bool m_bAutocomplete;
298302
};
299303

300304
#endif

MTA10/core/CClientVariables.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ void CClientVariables::LoadDefaults ( void )
285285
DEFAULT ( "chat_line_life", 12000 ); // chatbox line life time
286286
DEFAULT ( "chat_line_fade_out", 3000 ); // chatbox line fade out time
287287
DEFAULT ( "chat_use_cegui", false ); // chatbox uses cegui
288+
DEFAULT ( "chat_autocomplete", true ); // Enable autocomplete for nicknames in chatbox?
288289
DEFAULT ( "text_scale", 1.0f ); // text scale
289290
DEFAULT ( "invert_mouse", false ); // mouse inverting
290291
DEFAULT ( "fly_with_mouse", false ); // flying with mouse controls

MTA10/core/CSettings.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,11 @@ void CSettings::CreateGUI ( void )
11451145
m_pChatCssBackground->SetPosition ( CVector2D ( 10.0f, 350.0f ) );
11461146
m_pChatCssBackground->GetPosition ( vecTemp );
11471147
m_pChatCssBackground->AutoSize ( NULL, 20.0f );
1148+
1149+
m_pChatAutocomplete = reinterpret_cast < CGUICheckBox* > ( pManager->CreateCheckBox ( pTabInterface, _( "Nickname autocomplete with TAB" ) ) );
1150+
m_pChatAutocomplete->SetPosition ( CVector2D ( vecTemp.fX, vecTemp.fY + 20.0f ) );
1151+
m_pChatAutocomplete->GetPosition ( vecTemp );
1152+
m_pChatAutocomplete->AutoSize ( NULL, 20.0f );
11481153
}
11491154

11501155
/**
@@ -2971,6 +2976,7 @@ void CSettings::LoadData ( void )
29712976
CVARS_GET ( "chat_width", strVar ); m_pChatWidth->SetText ( strVar.c_str () );
29722977
CVARS_GET ( "chat_css_style_text", bVar ); m_pChatCssText->SetSelected ( bVar );
29732978
CVARS_GET ( "chat_css_style_background", bVar ); m_pChatCssBackground->SetSelected ( bVar );
2979+
CVARS_GET ( "chat_autocomplete", bVar ); m_pChatAutocomplete->SetSelected ( bVar );
29742980

29752981
{
29762982
int iVar;
@@ -3265,6 +3271,7 @@ void CSettings::SaveData ( void )
32653271
CVARS_SET ( "chat_width", m_pChatWidth->GetText () );
32663272
CVARS_SET ( "chat_css_style_text", m_pChatCssText->GetSelected () );
32673273
CVARS_SET ( "chat_css_style_background", m_pChatCssBackground->GetSelected () );
3274+
CVARS_SET ( "chat_autocomplete", m_pChatAutocomplete->GetSelected () );
32683275
CVARS_SET ( "chat_line_life", GetMilliseconds ( m_pChatLineLife ) );
32693276
CVARS_SET ( "chat_line_fade_out", GetMilliseconds ( m_pChatLineFadeout ) );
32703277

MTA10/core/CSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class CSettings
321321
CGUIEdit* m_pChatWidth;
322322

323323
CGUICheckBox* m_pChatCssBackground;
324+
CGUICheckBox* m_pChatAutocomplete;
324325
CGUICheckBox* m_pChatCssText;
325326
CGUIEdit* m_pChatLineLife;
326327
CGUIEdit* m_pChatLineFadeout;

MTA10/mods/deathmatch/CClient.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,18 @@ bool CClient::HandleException ( CExceptionInformation* pExceptionInformation )
297297
return true;
298298
#endif
299299
}
300+
301+
void CClient::GetPlayerNames ( std::vector<SString> &vPlayerNames )
302+
{
303+
if ( g_pClientGame ) {
304+
vPlayerNames.clear ();
305+
for ( std::vector<CClientPlayer*>::const_iterator iter = g_pClientGame->GetPlayerManager ()->IterBegin ();
306+
iter != g_pClientGame->GetPlayerManager ()->IterEnd ();
307+
++iter )
308+
{
309+
CClientPlayer* pClient = *iter;
310+
SString strPlayerName = pClient->GetNametagText ();
311+
vPlayerNames.push_back ( strPlayerName );
312+
}
313+
}
314+
};

MTA10/mods/deathmatch/CClient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CClient : public CClientBase
3737
//bool ProcessInput ( CInputMessage* pInputMessage );
3838

3939
bool HandleException ( CExceptionInformation* pExceptionInformation );
40+
void GetPlayerNames ( std::vector<SString> &vPlayerNames );
4041
};
4142

4243
#endif

MTA10/mods/shared_logic/CClientEntity.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class CClientEntity : public CClientEntityBase
187187
};
188188
bool CanUpdateSync ( unsigned char ucRemote );
189189

190-
inline const char* GetName ( void ) { return m_strName; }
190+
inline SString GetName ( void ) { return m_strName; }
191191
inline void SetName ( const char* szName ) { m_strName.AssignLeft ( szName, MAX_ELEMENT_NAME_LENGTH ); }
192192

193193
inline const char* GetTypeName ( void ) { return m_strTypeName; };

MTA10/sdk/core/CClientBase.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class CClientBase
3232
//virtual bool ProcessInput ( CInputMessage* pInputMessage ) = 0 *TODO*
3333

3434
virtual bool HandleException ( CExceptionInformation* pExceptionInformation ) = 0;
35+
virtual void GetPlayerNames ( std::vector<SString> &vPlayerNames ) = 0;
3536
};
3637

37-
#endif
38+
#endif

0 commit comments

Comments
 (0)