File: CConsole.h

package info (click to toggle)
0ad 0.0.26-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 130,460 kB
  • sloc: cpp: 261,824; ansic: 198,392; javascript: 19,067; python: 14,557; sh: 7,629; perl: 4,072; xml: 849; makefile: 741; java: 533; ruby: 229; php: 190; pascal: 30; sql: 21; tcl: 4
file content (137 lines) | stat: -rw-r--r-- 3,507 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (C) 2022 Wildfire Games.
 * This file is part of 0 A.D.
 *
 * 0 A.D. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * 0 A.D. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Implements the in-game console with scripting support.
 */

#ifndef INCLUDED_CCONSOLE
#define INCLUDED_CCONSOLE

#include "lib/file/vfs/vfs_path.h"
#include "lib/input.h"

#include <deque>
#include <memory>
#include <mutex>
#include <string>

class CCanvas2D;
class CTextRenderer;

/**
 * In-game console.
 *
 * Thread-safety:
 * - Expected to be constructed/destructed in the main thread.
 * - InsertMessage may be called from any thread while the object is alive.
 */
class CConsole
{
	NONCOPYABLE(CConsole);

public:
	CConsole();
	~CConsole();

	void Init();

	void UpdateScreenSize(int w, int h);

	void ToggleVisible();
	void SetVisible(bool visible);

	/**
	 * @param deltaRealTime Elapsed real time since the last frame.
	 */
	void Update(const float deltaRealTime);

	void Render(CCanvas2D& canvas);

	void InsertChar(const int szChar, const wchar_t cooked);

	void InsertMessage(const std::string& message);

	void SetBuffer(const wchar_t* szMessage);

	// Only returns a pointer to the buffer; copy out of here if you want to keep it.
	const wchar_t* GetBuffer();
	void FlushBuffer();

	bool IsActive() const { return m_Visible; }

private:
	// Lock for all state modified by InsertMessage
	std::mutex m_Mutex;

	int m_FontHeight;
	int m_FontWidth;
	int m_FontOffset; // distance to move up before drawing
	size_t m_CharsPerPage;

	float m_X;
	float m_Y;
	float m_Height;
	float m_Width;

	// "position" in show/hide animation, how visible the console is (0..1).
	// allows implementing other animations than sliding, e.g. fading in/out.
	float m_VisibleFrac;

	std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex
	std::deque<std::wstring> m_BufHistory;

	int m_MsgHistPos;

	std::unique_ptr<wchar_t[]> m_Buffer;
	int m_BufferPos;
	int m_BufferLength;

	VfsPath m_HistoryFile;
	int m_MaxHistoryLines;

	bool m_Visible;	// console is to be drawn
	bool m_Toggle;		// show/hide animation is currently active
	double m_PrevTime;	// the previous time the cursor draw state changed (used for blinking cursor)
	bool m_CursorVisState;	// if the cursor should be drawn or not
	bool m_QuitHotkeyWasShown;	// show console.toggle hotkey values at first time
	double m_CursorBlinkRate;	// cursor blink rate in seconds, if greater than 0.0

	void DrawWindow(CCanvas2D& canvas);
	void DrawHistory(CTextRenderer& textRenderer);
	void DrawBuffer(CTextRenderer& textRenderer);
	void DrawCursor(CTextRenderer& textRenderer);

	// Is end of Buffer?
	bool IsEOB() const;
	// Is beginning of Buffer?
	bool IsBOB() const;
	bool IsFull() const;
	bool IsEmpty() const;

	void ProcessBuffer(const wchar_t* szLine);

	void LoadHistory();
	void SaveHistory();
	void ShowQuitHotkeys();
};

extern CConsole* g_Console;

extern InReaction conInputHandler(const SDL_Event_* ev);

#endif // INCLUDED_CCONSOLE