Counter Strike : Global Offensive Source Code
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.

336 lines
11 KiB

  1. //===== Copyright � 1996-2009, Valve Corporation, All rights reserved. ========
  2. //
  3. // Purpose: Implementation of the CScriptEditorPanel class and associated helper
  4. // classes. The ScriptEditorPanel class represents a vgui panel which contains
  5. // a text editing panel which may be used to edit a script and text panel which
  6. // displays output from the script.
  7. //
  8. //=============================================================================
  9. #include "toolutils/scripteditorpanel.h"
  10. #include "vgui/iinput.h"
  11. #include "vgui/ischeme.h"
  12. #include "vgui/ivgui.h"
  13. #include "vgui/isurface.h"
  14. #include "vgui_controls/button.h"
  15. #include "vgui_controls/textentry.h"
  16. #include "vgui_controls/richtext.h"
  17. #include "tier1/utlbuffer.h"
  18. #include "vgui/ilocalize.h"
  19. // memdbgon must be the last include file in a .cpp file!!!
  20. #include "tier0/memdbgon.h"
  21. using namespace vgui;
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Constructor
  24. //-----------------------------------------------------------------------------
  25. CLineNumberPanel::CLineNumberPanel( vgui::Panel *pParent, vgui::TextEntry *pTextEntry, const char *pchName )
  26. : BaseClass( pParent, pchName )
  27. , m_pTextEntry( pTextEntry )
  28. , m_hFont( NULL )
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. // Purpose: Paint the background of the panel, including the line numbers
  33. //-----------------------------------------------------------------------------
  34. void CLineNumberPanel::PaintBackground()
  35. {
  36. BaseClass::PaintBackground();
  37. Panel *pParent = GetParent();
  38. if ( pParent )
  39. {
  40. if ( pParent->IsVisible() == false )
  41. return;
  42. }
  43. int width, height;
  44. GetSize( width, height );
  45. int fontHeight = surface()->GetFontTall( m_hFont );
  46. int yPos = 1;
  47. int line = m_pTextEntry->GetCurrentStartLine() + 1;
  48. surface()->DrawSetTextFont( m_hFont );
  49. surface()->DrawSetTextColor( m_Color );
  50. while ( (yPos + fontHeight) < height )
  51. {
  52. wchar_t sz[ 32 ];
  53. _snwprintf( sz, sizeof( sz ), L"%4i", line );
  54. surface()->DrawSetTextPos( 0, yPos );
  55. surface()->DrawPrintText( sz, wcslen( sz ) );
  56. yPos += fontHeight + 1;
  57. ++line;
  58. }
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: Apply the settings from the provided scheme, and save the font to
  62. // display line numbers.
  63. //-----------------------------------------------------------------------------
  64. void CLineNumberPanel::ApplySchemeSettings(IScheme *pScheme)
  65. {
  66. BaseClass::ApplySchemeSettings(pScheme);
  67. m_hFont = pScheme->GetFont( "ConsoleText", IsProportional() );
  68. m_Color = GetSchemeColor( "Console.TextColor", pScheme );
  69. }
  70. //-----------------------------------------------------------------------------
  71. // Purpose: Constructor, creates all of the controls owned by the panel.
  72. //-----------------------------------------------------------------------------
  73. CScriptEditorPanel::CScriptEditorPanel( vgui::Panel *pParent, const char *pName )
  74. : BaseClass( pParent, pName )
  75. {
  76. SetKeyBoardInputEnabled( true );
  77. SetMinimumSize(300,200);
  78. // Create the output panel
  79. m_pOutput = new RichText(this, "ConsoleHistory");
  80. m_pOutput->SetAllowKeyBindingChainToParent( false );
  81. SETUP_PANEL( m_pOutput );
  82. m_pOutput->SetVerticalScrollbar( true );
  83. m_pOutput->GotoTextEnd();
  84. // Create the submit button
  85. m_pSubmit = new Button(this, "ConsoleSubmit", "#Console_Submit");
  86. m_pSubmit->SetCommand("submit");
  87. m_pSubmit->SetVisible( true );
  88. // Create the script entry panel
  89. m_pScriptEntry = new TextEntry(this, "ScriptEntry" );
  90. m_pScriptEntry->AddActionSignalTarget(this);
  91. m_pScriptEntry->SetMultiline( true );
  92. m_pScriptEntry->SetVerticalScrollbar( true );
  93. m_pScriptEntry->SetCatchEnterKey( true );
  94. m_pScriptEntry->SetCatchTabKey( true );
  95. m_pScriptEntry->SendNewLine( true );
  96. m_pScriptEntry->SetTabPosition(1);
  97. // Create the line number display panel
  98. m_pLineNumberPanel = new CLineNumberPanel( this, m_pScriptEntry, "ScriptLineNumbers" );
  99. // Set the default text colors, these will be overridden by ApplySchemeSettings
  100. m_PrintColor = Color(216, 222, 211, 255);
  101. m_DPrintColor = Color(196, 181, 80, 255);
  102. // Add to global console list so the the console
  103. // output will be displayed in the output panel.
  104. g_pCVar->InstallConsoleDisplayFunc( this );
  105. }
  106. //-----------------------------------------------------------------------------
  107. // Purpose: Destructor, removes the output from the global console list and
  108. // destroys the controls.
  109. //-----------------------------------------------------------------------------
  110. CScriptEditorPanel::~CScriptEditorPanel()
  111. {
  112. // Remove from the global console list
  113. g_pCVar->RemoveConsoleDisplayFunc( this );
  114. // Destroy the controls
  115. delete m_pOutput;
  116. m_pOutput = NULL;
  117. delete m_pSubmit;
  118. m_pSubmit = NULL;
  119. delete m_pScriptEntry;
  120. m_pScriptEntry = NULL;
  121. delete m_pLineNumberPanel;
  122. m_pLineNumberPanel = NULL;
  123. }
  124. //-----------------------------------------------------------------------------
  125. // Purpose: Clears the output console
  126. //-----------------------------------------------------------------------------
  127. void CScriptEditorPanel::Clear()
  128. {
  129. m_pOutput->SetText("");
  130. m_pOutput->GotoTextEnd();
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose: color text print
  134. //-----------------------------------------------------------------------------
  135. void CScriptEditorPanel::ColorPrint( const Color& clr, const char *msg )
  136. {
  137. m_pOutput->InsertColorChange( clr );
  138. m_pOutput->InsertString( msg );
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: normal text print
  142. //-----------------------------------------------------------------------------
  143. void CScriptEditorPanel::Print(const char *msg)
  144. {
  145. ColorPrint( m_PrintColor, msg );
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose: debug text print
  149. //-----------------------------------------------------------------------------
  150. void CScriptEditorPanel::DPrint( const char *msg )
  151. {
  152. ColorPrint( m_DPrintColor, msg );
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Returns the console text
  156. //-----------------------------------------------------------------------------
  157. void CScriptEditorPanel::GetConsoleText( char *pchText, size_t bufSize ) const
  158. {
  159. wchar_t *temp = new wchar_t[ bufSize ];
  160. m_pScriptEntry->GetText( temp, bufSize * sizeof( wchar_t ) );
  161. g_pVGuiLocalize->ConvertUnicodeToANSI( temp, pchText, bufSize );
  162. delete[] temp;
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose: Called whenever the user types text
  166. //-----------------------------------------------------------------------------
  167. void CScriptEditorPanel::OnTextChanged(Panel *panel)
  168. {
  169. if (panel != m_pScriptEntry)
  170. return;
  171. RequestFocus();
  172. m_pScriptEntry->RequestFocus();
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose: generic vgui command handler
  176. //-----------------------------------------------------------------------------
  177. void CScriptEditorPanel::OnCommand(const char *command)
  178. {
  179. if ( !Q_stricmp( command, "Submit" ) )
  180. {
  181. // always go the end of the buffer when the user has typed something
  182. m_pOutput->GotoTextEnd();
  183. // Get the text from the script entry and store in a single string.
  184. int textLength = m_pScriptEntry->GetTextLength() + 2;
  185. CUtlBuffer scriptBuffer( 0, textLength, CUtlBuffer::TEXT_BUFFER );
  186. m_pScriptEntry->GetText( (char*)scriptBuffer.Base(), scriptBuffer.Size() );
  187. // Run the script
  188. RunScript( scriptBuffer );
  189. }
  190. else
  191. {
  192. BaseClass::OnCommand(command);
  193. }
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Purpose: Run the specified script
  197. //-----------------------------------------------------------------------------
  198. void CScriptEditorPanel::RunScript( const CUtlBuffer& scriptBuffer )
  199. {
  200. // No script execution exists at this level, derived classes should
  201. // override this function to execute scripts.
  202. }
  203. //-----------------------------------------------------------------------------
  204. // Purpose: Determine of the script entry panel has the current focus
  205. //-----------------------------------------------------------------------------
  206. bool CScriptEditorPanel::TextEntryHasFocus() const
  207. {
  208. return ( input()->GetFocus() == m_pScriptEntry->GetVPanel() );
  209. }
  210. //-----------------------------------------------------------------------------
  211. // Purpose: Request that the script entry panel receive the input focus
  212. //-----------------------------------------------------------------------------
  213. void CScriptEditorPanel::TextEntryRequestFocus()
  214. {
  215. m_pScriptEntry->RequestFocus();
  216. }
  217. //-----------------------------------------------------------------------------
  218. // Purpose: Lays out the controls within the panel
  219. //-----------------------------------------------------------------------------
  220. void CScriptEditorPanel::PerformLayout()
  221. {
  222. BaseClass::PerformLayout();
  223. // setup tab ordering
  224. GetFocusNavGroup().SetDefaultButton(m_pSubmit);
  225. IScheme *pScheme = scheme()->GetIScheme( GetScheme() );
  226. m_pScriptEntry->SetBorder(pScheme->GetBorder("DepressedButtonBorder"));
  227. m_pOutput->SetBorder(pScheme->GetBorder("DepressedButtonBorder"));
  228. // layout controls
  229. int wide, tall;
  230. GetSize(wide, tall);
  231. const int lineNumberWidth = 24;
  232. const int inset = 8;
  233. const int topHeight = 4;
  234. const int entryInset = 4;
  235. const int submitWide = 64;
  236. const int submitHeight = 20;
  237. const int submitInset = 7; // x inset to pull the submit button away from the frame grab
  238. const int editHeight = ( tall - ( submitHeight + topHeight + (inset * 4 ) ) ) / 2;
  239. m_pOutput->SetPos(inset, inset + topHeight);
  240. m_pOutput->SetSize(wide - (inset * 2), editHeight );
  241. m_pOutput->InvalidateLayout();
  242. int nSubmitXPos = wide - ( inset + submitWide + submitInset );
  243. m_pSubmit->SetPos( nSubmitXPos, tall - (entryInset * 2 + submitHeight ));
  244. m_pSubmit->SetSize( submitWide, submitHeight );
  245. int nScriptEntryYPos = topHeight + ( inset * 2 ) + editHeight;
  246. m_pScriptEntry->SetPos( ( inset * 2 ) + lineNumberWidth, nScriptEntryYPos );
  247. m_pScriptEntry->SetSize( wide - ( (inset * 3) + lineNumberWidth ), editHeight );
  248. m_pLineNumberPanel->SetPos( inset, nScriptEntryYPos );
  249. m_pLineNumberPanel->SetSize( lineNumberWidth, editHeight );
  250. }
  251. //-----------------------------------------------------------------------------
  252. // Purpose: Apply the color, font, and size settings from the specified scheme
  253. //-----------------------------------------------------------------------------
  254. void CScriptEditorPanel::ApplySchemeSettings( IScheme *pScheme )
  255. {
  256. BaseClass::ApplySchemeSettings( pScheme );
  257. m_PrintColor = GetSchemeColor( "Console.TextColor", pScheme );
  258. m_DPrintColor = GetSchemeColor( "Console.DevTextColor", pScheme );
  259. m_pOutput->SetFont( pScheme->GetFont( "ConsoleText", IsProportional() ) );
  260. m_pScriptEntry->SetFont( pScheme->GetFont( "ConsoleText", IsProportional() ) );
  261. InvalidateLayout();
  262. }