Line data Source code
1 : /* Copyright (C) 2022 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/file/vfs/vfs_path.h"
26 : #include "lib/input.h"
27 :
28 : #include <deque>
29 : #include <memory>
30 : #include <mutex>
31 : #include <string>
32 :
33 : class CCanvas2D;
34 : class CTextRenderer;
35 :
36 : /**
37 : * In-game console.
38 : *
39 : * Thread-safety:
40 : * - Expected to be constructed/destructed in the main thread.
41 : * - InsertMessage may be called from any thread while the object is alive.
42 : */
43 0 : class CConsole
44 : {
45 : NONCOPYABLE(CConsole);
46 :
47 : public:
48 : CConsole();
49 : ~CConsole();
50 :
51 : void Init();
52 :
53 : void UpdateScreenSize(int w, int h);
54 :
55 : void ToggleVisible();
56 : void SetVisible(bool visible);
57 :
58 : /**
59 : * @param deltaRealTime Elapsed real time since the last frame.
60 : */
61 : void Update(const float deltaRealTime);
62 :
63 : void Render(CCanvas2D& canvas);
64 :
65 : void InsertChar(const int szChar, const wchar_t cooked);
66 :
67 : void InsertMessage(const std::string& message);
68 :
69 : void SetBuffer(const wchar_t* szMessage);
70 :
71 : // Only returns a pointer to the buffer; copy out of here if you want to keep it.
72 : const wchar_t* GetBuffer();
73 : void FlushBuffer();
74 :
75 0 : bool IsActive() const { return m_Visible; }
76 :
77 : private:
78 : // Lock for all state modified by InsertMessage
79 : std::mutex m_Mutex;
80 :
81 : int m_FontHeight;
82 : int m_FontWidth;
83 : int m_FontOffset; // distance to move up before drawing
84 : size_t m_CharsPerPage;
85 :
86 : float m_X;
87 : float m_Y;
88 : float m_Height;
89 : float m_Width;
90 :
91 : // "position" in show/hide animation, how visible the console is (0..1).
92 : // allows implementing other animations than sliding, e.g. fading in/out.
93 : float m_VisibleFrac;
94 :
95 : std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex
96 : std::deque<std::wstring> m_BufHistory;
97 :
98 : int m_MsgHistPos;
99 :
100 : std::unique_ptr<wchar_t[]> m_Buffer;
101 : int m_BufferPos;
102 : int m_BufferLength;
103 :
104 : VfsPath m_HistoryFile;
105 : int m_MaxHistoryLines;
106 :
107 : bool m_Visible; // console is to be drawn
108 : bool m_Toggle; // show/hide animation is currently active
109 : double m_PrevTime; // the previous time the cursor draw state changed (used for blinking cursor)
110 : bool m_CursorVisState; // if the cursor should be drawn or not
111 : bool m_QuitHotkeyWasShown; // show console.toggle hotkey values at first time
112 : double m_CursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0
113 :
114 : void DrawWindow(CCanvas2D& canvas);
115 : void DrawHistory(CTextRenderer& textRenderer);
116 : void DrawBuffer(CTextRenderer& textRenderer);
117 : void DrawCursor(CTextRenderer& textRenderer);
118 :
119 : // Is end of Buffer?
120 : bool IsEOB() const;
121 : // Is beginning of Buffer?
122 : bool IsBOB() const;
123 : bool IsFull() const;
124 : bool IsEmpty() const;
125 :
126 : void ProcessBuffer(const wchar_t* szLine);
127 :
128 : void LoadHistory();
129 : void SaveHistory();
130 : void ShowQuitHotkeys();
131 : };
132 :
133 : extern CConsole* g_Console;
134 :
135 : extern InReaction conInputHandler(const SDL_Event_* ev);
136 :
137 : #endif // INCLUDED_CCONSOLE
|