Team Fortress 2 Source Code as on 22/4/2020
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.

170 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================
  7. #include <stdio.h>
  8. #include "MOTDPanel.h"
  9. #include <VGUI_Controls.h>
  10. #include <VGUI_ISystem.h>
  11. #include <VGUI_ISurface.h>
  12. #include <VGUI_IVGui.h>
  13. #include <VGUI_KeyValues.h>
  14. #include <VGUI_Label.h>
  15. #include <VGUI_TextEntry.h>
  16. #include <VGUI_Button.h>
  17. #include <VGUI_ToggleButton.h>
  18. #include <VGUI_RadioButton.h>
  19. #include <VGUI_ListPanel.h>
  20. #include <VGUI_ComboBox.h>
  21. #include <VGUI_PHandle.h>
  22. #include <VGUI_PropertySheet.h>
  23. using namespace vgui;
  24. //-----------------------------------------------------------------------------
  25. // Purpose: Constructor
  26. //-----------------------------------------------------------------------------
  27. CMOTDPanel::CMOTDPanel(vgui::Panel *parent, const char *name) : PropertyPage(parent, name)
  28. {
  29. m_pRcon=NULL;
  30. m_pMOTDPanel = new TextEntry(this, "ServerMOTDText");
  31. m_pMOTDPanel->SetMultiline(true);
  32. m_pMOTDPanel->SetEnabled(true);
  33. m_pMOTDPanel->SetEditable(true);
  34. m_pMOTDPanel->SetVerticalScrollbar(true);
  35. m_pMOTDPanel->SetRichEdit(false);
  36. m_pMOTDPanel->SetCatchEnterKey(true);
  37. m_pMOTDPanel->setMaximumCharCount(1024);
  38. m_pSendMOTDButton = new Button(this, "SendMOTD", "&Send");
  39. m_pSendMOTDButton->SetCommand(new KeyValues("SendMOTD"));
  40. }
  41. //-----------------------------------------------------------------------------
  42. // Purpose: Destructor
  43. //-----------------------------------------------------------------------------
  44. CMOTDPanel::~CMOTDPanel()
  45. {
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: Activates the page
  49. //-----------------------------------------------------------------------------
  50. void CMOTDPanel::OnPageShow()
  51. {
  52. m_pMOTDPanel->RequestFocus();
  53. }
  54. //-----------------------------------------------------------------------------
  55. // Purpose: Hides the page
  56. //-----------------------------------------------------------------------------
  57. void CMOTDPanel::OnPageHide()
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: Relayouts the data
  62. //-----------------------------------------------------------------------------
  63. void CMOTDPanel::PerformLayout()
  64. {
  65. BaseClass::PerformLayout();
  66. // setup the layout of the panels
  67. m_pMOTDPanel->SetBounds(5,5,GetWide()-10,GetTall()-35);
  68. m_pSendMOTDButton->SetBounds(GetWide()-70,GetTall()-25,60,20);
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose: inserts a new string into the main chat panel
  72. //-----------------------------------------------------------------------------
  73. void CMOTDPanel::DoInsertString(const char *str)
  74. {
  75. m_pMOTDPanel->SetText("");
  76. if(strlen(str)>1024)
  77. {
  78. char *fix = const_cast<char *>(str);
  79. fix[1024]='\0';
  80. }
  81. m_pMOTDPanel->DoInsertString(str);
  82. }
  83. //-----------------------------------------------------------------------------
  84. // Purpose: passes the rcon class to use
  85. //-----------------------------------------------------------------------------
  86. void CMOTDPanel::SetRcon(CRcon *rcon)
  87. {
  88. m_pRcon=rcon;
  89. }
  90. //-----------------------------------------------------------------------------
  91. // Purpose: run when the send button is pressed, send a rcon "say" to the server
  92. //-----------------------------------------------------------------------------
  93. void CMOTDPanel::OnSendMOTD()
  94. {
  95. if(m_pRcon)
  96. {
  97. char chat_text[2048];
  98. _snprintf(chat_text,512,"motd_write ");
  99. m_pMOTDPanel->GetText(0,chat_text+11,2048-11);
  100. if(strlen("motd_write ")!=strlen(chat_text)) // check there is something in the text panel
  101. {
  102. unsigned int i=0;
  103. while(i<strlen(chat_text) && i<2048)
  104. {
  105. if(chat_text[i]=='\n')
  106. {
  107. // shift everything up one
  108. for(unsigned int k=strlen(chat_text)+1;k>i;k--)
  109. {
  110. chat_text[k+1]=chat_text[k];
  111. }
  112. // replace the newline with the string "\n"
  113. chat_text[i]='\\';
  114. chat_text[i+1]='n';
  115. i++; // skip this insert
  116. }
  117. i++;
  118. }
  119. m_pRcon->SendRcon(chat_text);
  120. }
  121. }
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose: Called when the game dir combo box is changed
  125. //-----------------------------------------------------------------------------
  126. void CMOTDPanel::OnTextChanged(Panel *panel, const char *text)
  127. {
  128. // BUG - TextEntry NEVER lets the enter key through... This doesn't work
  129. if( text[strlen(text)-1]=='\n') // the enter key was just pressed :)
  130. {
  131. OnSendMOTD();
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose: Message map
  136. //-----------------------------------------------------------------------------
  137. MessageMapItem_t CMOTDPanel::m_MessageMap[] =
  138. {
  139. MAP_MESSAGE( CMOTDPanel, "SendMOTD", OnSendMOTD ),
  140. MAP_MESSAGE( CMOTDPanel, "PageShow", OnPageShow ),
  141. // MAP_MESSAGE_PTR_CONSTCHARPTR( CMOTDPanel, "TextChanged", OnTextChanged, "panel", "text" ),
  142. };
  143. IMPLEMENT_PANELMAP( CMOTDPanel, Frame );