2025-05-15 23:13:47 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* Implements the in-game console with scripting support.
|
2007-05-07 09:33:24 -07:00
|
|
|
*/
|
2004-07-16 23:14:23 -07:00
|
|
|
|
2012-02-12 12:45:31 -08:00
|
|
|
#ifndef INCLUDED_CCONSOLE
|
|
|
|
|
#define INCLUDED_CCONSOLE
|
|
|
|
|
|
2025-08-03 08:30:17 -07:00
|
|
|
#include "lib/code_annotation.h"
|
2021-05-21 16:10:43 -07:00
|
|
|
#include "lib/file/vfs/vfs_path.h"
|
|
|
|
|
#include "lib/input.h"
|
|
|
|
|
|
2025-08-03 08:30:17 -07:00
|
|
|
#include <cstddef>
|
2004-06-02 13:40:07 -07:00
|
|
|
#include <deque>
|
2021-12-22 23:37:03 -08:00
|
|
|
#include <memory>
|
2019-06-06 12:30:48 -07:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <string>
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2021-05-30 12:10:10 -07:00
|
|
|
class CCanvas2D;
|
2012-02-12 12:45:31 -08:00
|
|
|
class CTextRenderer;
|
2025-08-03 08:30:17 -07:00
|
|
|
struct SDL_Event_;
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2010-10-31 15:00:28 -07:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2004-06-02 13:40:07 -07:00
|
|
|
class CConsole
|
|
|
|
|
{
|
2013-05-22 14:40:56 -07:00
|
|
|
NONCOPYABLE(CConsole);
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
public:
|
|
|
|
|
CConsole();
|
|
|
|
|
~CConsole();
|
|
|
|
|
|
2021-05-21 16:10:43 -07:00
|
|
|
void Init();
|
|
|
|
|
|
2008-01-07 12:03:19 -08:00
|
|
|
void UpdateScreenSize(int w, int h);
|
|
|
|
|
|
|
|
|
|
void ToggleVisible();
|
2012-02-12 12:45:31 -08:00
|
|
|
void SetVisible(bool visible);
|
2008-01-07 12:03:19 -08:00
|
|
|
|
2012-06-06 12:37:03 -07:00
|
|
|
/**
|
|
|
|
|
* @param deltaRealTime Elapsed real time since the last frame.
|
|
|
|
|
*/
|
|
|
|
|
void Update(const float deltaRealTime);
|
2008-01-07 12:03:19 -08:00
|
|
|
|
2022-02-25 00:14:11 -08:00
|
|
|
void Render(CCanvas2D& canvas);
|
2008-01-07 12:03:19 -08:00
|
|
|
|
|
|
|
|
void InsertChar(const int szChar, const wchar_t cooked);
|
|
|
|
|
|
2015-02-13 17:49:34 -08:00
|
|
|
void InsertMessage(const std::string& message);
|
2008-01-07 12:03:19 -08:00
|
|
|
|
2010-03-20 15:01:39 -07:00
|
|
|
void SetBuffer(const wchar_t* szMessage);
|
2008-01-07 12:03:19 -08:00
|
|
|
|
|
|
|
|
// Only returns a pointer to the buffer; copy out of here if you want to keep it.
|
|
|
|
|
const wchar_t* GetBuffer();
|
|
|
|
|
void FlushBuffer();
|
|
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
bool IsActive() const { return m_Visible; }
|
2008-01-07 12:03:19 -08:00
|
|
|
|
2021-05-21 16:10:43 -07:00
|
|
|
private:
|
|
|
|
|
// Lock for all state modified by InsertMessage
|
|
|
|
|
std::mutex m_Mutex;
|
|
|
|
|
|
2025-05-28 20:37:37 -07:00
|
|
|
float m_FontHeight;
|
|
|
|
|
float m_FontWidth;
|
|
|
|
|
float m_FontOffset; // distance to move up before drawing
|
2021-12-22 23:37:03 -08:00
|
|
|
size_t m_CharsPerPage;
|
2008-01-07 12:03:19 -08:00
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
float m_X;
|
|
|
|
|
float m_Y;
|
|
|
|
|
float m_Height;
|
|
|
|
|
float m_Width;
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2004-06-02 18:42:40 -07:00
|
|
|
// "position" in show/hide animation, how visible the console is (0..1).
|
|
|
|
|
// allows implementing other animations than sliding, e.g. fading in/out.
|
2021-12-22 23:37:03 -08:00
|
|
|
float m_VisibleFrac;
|
2016-11-23 03:18:37 -08:00
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
std::deque<std::wstring> m_MsgHistory; // protected by m_Mutex
|
|
|
|
|
std::deque<std::wstring> m_BufHistory;
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
int m_MsgHistPos;
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
std::unique_ptr<wchar_t[]> m_Buffer;
|
|
|
|
|
int m_BufferPos;
|
|
|
|
|
int m_BufferLength;
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
VfsPath m_HistoryFile;
|
2005-08-09 08:55:44 -07:00
|
|
|
int m_MaxHistoryLines;
|
2025-05-15 23:38:51 -07:00
|
|
|
bool m_HistoryIgnoreDuplicates;
|
2025-05-15 23:13:47 -07:00
|
|
|
std::string m_consoleFont;
|
2005-05-10 00:13:25 -07:00
|
|
|
|
2021-12-22 23:37:03 -08:00
|
|
|
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
|
2021-12-07 12:09:46 -08:00
|
|
|
bool m_QuitHotkeyWasShown; // show console.toggle hotkey values at first time
|
2021-12-22 23:37:03 -08:00
|
|
|
double m_CursorBlinkRate; // cursor blink rate in seconds, if greater than 0.0
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2021-05-30 12:10:10 -07:00
|
|
|
void DrawWindow(CCanvas2D& canvas);
|
2012-02-12 12:45:31 -08:00
|
|
|
void DrawHistory(CTextRenderer& textRenderer);
|
|
|
|
|
void DrawBuffer(CTextRenderer& textRenderer);
|
|
|
|
|
void DrawCursor(CTextRenderer& textRenderer);
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2021-05-21 16:10:43 -07:00
|
|
|
// Is end of Buffer?
|
|
|
|
|
bool IsEOB() const;
|
|
|
|
|
// Is beginning of Buffer?
|
|
|
|
|
bool IsBOB() const;
|
|
|
|
|
bool IsFull() const;
|
|
|
|
|
bool IsEmpty() const;
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2010-10-31 15:00:28 -07:00
|
|
|
void ProcessBuffer(const wchar_t* szLine);
|
2004-06-02 13:40:07 -07:00
|
|
|
|
2005-05-10 00:13:25 -07:00
|
|
|
void LoadHistory();
|
|
|
|
|
void SaveHistory();
|
2021-12-07 12:09:46 -08:00
|
|
|
void ShowQuitHotkeys();
|
2004-06-02 13:40:07 -07:00
|
|
|
};
|
|
|
|
|
|
2005-07-03 09:25:48 -07:00
|
|
|
extern CConsole* g_Console;
|
2004-06-10 15:24:03 -07:00
|
|
|
|
2006-08-26 14:52:18 -07:00
|
|
|
extern InReaction conInputHandler(const SDL_Event_* ev);
|
2005-08-14 16:34:37 -07:00
|
|
|
|
2021-05-21 16:10:43 -07:00
|
|
|
#endif // INCLUDED_CCONSOLE
|