Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * Implements the in-game console with scripting support. 20 : */ 21 : 22 : #ifndef INCLUDED_CCONSOLE 23 : #define INCLUDED_CCONSOLE 24 : 25 : #include "lib/code_annotation.h" 26 : #include "lib/file/vfs/vfs_path.h" 27 : #include "ps/Input.h" 28 : 29 : #include <cstddef> 30 : #include <deque> 31 : #include <memory> 32 : #include <mutex> 33 : #include <string> 34 : 35 : class CCanvas2D; 36 : class CTextRenderer; 37 : 38 : /** 39 : * In-game console. 40 : * 41 : * Thread-safety: 42 : * - Expected to be constructed/destructed in the main thread. 43 : * - InsertMessage may be called from any thread while the object is alive. 44 : */ 45 : class CConsole 46 : { 47 : NONCOPYABLE(CConsole); 48 : 49 : public: 50 : CConsole(); 51 : ~CConsole(); 52 : 53 : void Init(); 54 : 55 : void UpdateScreenSize(int w, int h); 56 : 57 : void SetVisible(bool visible); 58 : 59 : /** 60 : * @param deltaRealTime Elapsed real time since the last frame. 61 : */ 62 : void Update(const float deltaRealTime); 63 : 64 : void Render(CCanvas2D& canvas); 65 : 66 : void InsertMessage(const std::string& message); 67 : 68 : void SetBuffer(const wchar_t* szMessage); 69 : 70 : void FlushBuffer(); 71 : 72 0 : bool IsActive() const { return m_Visible; } 73 : 74 : private: 75 : // Lock for all state modified by InsertMessage 76 : std::mutex m_Mutex; 77 : 78 : float m_FontHeight; 79 : float m_FontWidth; 80 : float m_FontOffset; // distance to move up before drawing 81 : size_t m_CharsPerPage; 82 : 83 : float m_X; 84 : float m_Y; 85 : float m_Height; 86 : float m_Width; 87 : 88 : // "position" in show/hide animation, how visible the console is (0..1). 89 : // allows implementing other animations than sliding, e.g. fading in/out. 90 : float m_VisibleFrac; 91 : 92 : std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex 93 : std::deque<std::wstring> m_BufHistory; 94 : 95 : int m_MsgHistPos; 96 : 97 : std::unique_ptr<wchar_t[]> m_Buffer; 98 : int m_BufferPos; 99 : int m_BufferLength; 100 : 101 : VfsPath m_HistoryFile; 102 : int m_MaxHistoryLines; 103 : bool m_HistoryIgnoreDuplicates; 104 : std::string m_consoleFont; 105 : 106 : bool m_Visible; // console is to be drawn 107 : bool m_Toggle; // show/hide animation is currently active 108 : double m_PrevTime; // the previous time the cursor draw state changed (used for blinking cursor) 109 : bool m_CursorVisState; // if the cursor should be drawn or not 110 : bool m_QuitHotkeyWasShown; // show console.toggle hotkey values at first time 111 : double m_CursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0 112 : 113 : void InsertChar(const int szChar, const wchar_t cooked); 114 : 115 : void DrawWindow(CCanvas2D& canvas); 116 : void DrawHistory(CTextRenderer& textRenderer); 117 : void DrawBuffer(CTextRenderer& textRenderer); 118 : void DrawCursor(CTextRenderer& textRenderer); 119 : 120 : // Is end of Buffer? 121 : bool IsEOB() const; 122 : // Is beginning of Buffer? 123 : bool IsBOB() const; 124 : bool IsFull() const; 125 : bool IsEmpty() const; 126 : 127 : void ProcessBuffer(const wchar_t* szLine); 128 : 129 : void LoadHistory(); 130 : void SaveHistory(); 131 : 132 : struct InputHandler 133 : { 134 : CConsole& console; 135 : Input::Reaction operator()(const SDL_Event& ev); 136 : }; 137 : Input::Handler<InputHandler> m_InputHandler; 138 : }; 139 : 140 : extern CConsole* g_Console; 141 : 142 : #endif // INCLUDED_CCONSOLE