2018-10-07 03:38:44 +00:00
|
|
|
#pragma once
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2019-09-04 21:22:05 +00:00
|
|
|
#include <functional>
|
2018-01-11 09:35:27 +00:00
|
|
|
#include <string>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
2019-09-04 21:22:05 +00:00
|
|
|
|
|
|
|
#include <boo/System.hpp>
|
|
|
|
#include <logvisor/logvisor.hpp>
|
|
|
|
|
|
|
|
namespace boo {
|
|
|
|
class IWindow;
|
|
|
|
|
|
|
|
enum class EModifierKey;
|
|
|
|
enum class ESpecialKey;
|
|
|
|
|
|
|
|
struct IGraphicsCommandQueue;
|
|
|
|
} // namespace boo
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
namespace hecl {
|
2018-01-15 12:34:02 +00:00
|
|
|
class CVarManager;
|
2018-01-22 02:40:11 +00:00
|
|
|
class CVar;
|
2018-12-08 05:18:42 +00:00
|
|
|
struct SConsoleCommand {
|
|
|
|
enum class ECommandFlags { Normal = 0, Cheat = (1 << 0), Developer = (1 << 1) };
|
|
|
|
std::string m_displayName;
|
|
|
|
std::string m_helpString;
|
|
|
|
std::string m_usage;
|
|
|
|
std::function<void(class Console*, const std::vector<std::string>&)> m_func;
|
|
|
|
ECommandFlags m_flags;
|
2018-01-11 09:35:27 +00:00
|
|
|
};
|
2018-04-30 03:11:37 +00:00
|
|
|
ENABLE_BITWISE_ENUM(SConsoleCommand::ECommandFlags)
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
class Console {
|
|
|
|
friend class LogVisorAdapter;
|
|
|
|
struct LogVisorAdapter : logvisor::ILogger {
|
|
|
|
Console* m_con;
|
2020-12-31 21:56:20 +00:00
|
|
|
LogVisorAdapter(Console* con) : logvisor::ILogger(log_typeid(LogVisorAdapter)), m_con(con) {}
|
2018-01-11 16:26:33 +00:00
|
|
|
|
2019-08-21 22:20:07 +00:00
|
|
|
~LogVisorAdapter() override = default;
|
|
|
|
void report(const char* modName, logvisor::Level severity, fmt::string_view format, fmt::format_args args) override;
|
|
|
|
void report(const char* modName, logvisor::Level severity, fmt::wstring_view format,
|
|
|
|
fmt::wformat_args args) override;
|
2018-12-08 05:18:42 +00:00
|
|
|
void reportSource(const char* modName, logvisor::Level severity, const char* file, unsigned linenum,
|
2019-08-21 22:20:07 +00:00
|
|
|
fmt::string_view format, fmt::format_args args) override;
|
2018-12-08 05:18:42 +00:00
|
|
|
void reportSource(const char* modName, logvisor::Level severity, const char* file, unsigned linenum,
|
2019-08-21 22:20:07 +00:00
|
|
|
fmt::wstring_view format, fmt::wformat_args args) override;
|
2018-12-08 05:18:42 +00:00
|
|
|
};
|
2018-01-11 16:26:33 +00:00
|
|
|
|
2018-01-11 09:35:27 +00:00
|
|
|
public:
|
2018-12-08 05:18:42 +00:00
|
|
|
static Console* m_instance;
|
|
|
|
enum class Level {
|
|
|
|
Info, /**< Non-error informative message */
|
|
|
|
Warning, /**< Non-error warning message */
|
|
|
|
Error, /**< Recoverable error message */
|
|
|
|
Fatal /**< Non-recoverable error message (Kept for compatibility with logvisor) */
|
|
|
|
};
|
2018-01-14 02:39:32 +00:00
|
|
|
|
2019-10-21 05:53:03 +00:00
|
|
|
enum class State { Closed, Closing, Opened, Opening };
|
2018-01-14 02:39:32 +00:00
|
|
|
|
2018-01-11 09:35:27 +00:00
|
|
|
private:
|
2018-12-10 05:12:10 +00:00
|
|
|
CVarManager* m_cvarMgr = nullptr;
|
|
|
|
boo::IWindow* m_window = nullptr;
|
2018-12-08 05:18:42 +00:00
|
|
|
std::unordered_map<std::string, SConsoleCommand> m_commands;
|
|
|
|
std::vector<std::pair<std::string, Level>> m_log;
|
2019-04-07 04:54:58 +00:00
|
|
|
int m_logOffset = 0;
|
2018-12-08 05:18:42 +00:00
|
|
|
std::string m_commandString;
|
|
|
|
std::vector<std::string> m_commandHistory;
|
|
|
|
int m_cursorPosition = -1;
|
|
|
|
int m_currentCommand = -1;
|
|
|
|
size_t m_maxLines = 0;
|
|
|
|
bool m_overwrite : 1;
|
|
|
|
bool m_cursorAtEnd : 1;
|
|
|
|
State m_state = State::Closed;
|
|
|
|
CVar* m_conSpeed;
|
|
|
|
CVar* m_conHeight;
|
|
|
|
float m_cachedConSpeed;
|
|
|
|
float m_cachedConHeight;
|
|
|
|
bool m_showCursor = true;
|
|
|
|
float m_cursorTime = 0.f;
|
|
|
|
|
2018-01-11 09:35:27 +00:00
|
|
|
public:
|
2018-12-08 05:18:42 +00:00
|
|
|
Console(CVarManager*);
|
|
|
|
void registerCommand(std::string_view name, std::string_view helpText, std::string_view usage,
|
2019-10-21 05:03:27 +00:00
|
|
|
std::function<void(Console*, const std::vector<std::string>&)>&& func,
|
2018-12-08 05:18:42 +00:00
|
|
|
SConsoleCommand::ECommandFlags cmdFlags = SConsoleCommand::ECommandFlags::Normal);
|
|
|
|
void unregisterCommand(std::string_view name);
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void executeString(const std::string& strToExec);
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2018-12-08 05:18:42 +00:00
|
|
|
void help(Console* con, const std::vector<std::string>& args);
|
|
|
|
void listCommands(Console* con, const std::vector<std::string>& args);
|
2019-10-21 05:30:16 +00:00
|
|
|
bool commandExists(std::string_view cmd) const;
|
2018-01-11 09:35:27 +00:00
|
|
|
|
2019-07-20 04:22:58 +00:00
|
|
|
void vreport(Level level, fmt::string_view format, fmt::format_args args);
|
|
|
|
template <typename S, typename... Args, typename Char = fmt::char_t<S>>
|
|
|
|
void report(Level level, const S& format, Args&&... args) {
|
|
|
|
vreport(level, fmt::to_string_view<Char>(format),
|
|
|
|
fmt::basic_format_args<fmt::buffer_context<Char>>(
|
2021-04-06 16:55:23 +00:00
|
|
|
fmt::make_args_checked<Args...>(format, args...)));
|
2019-07-20 04:22:58 +00:00
|
|
|
}
|
2018-01-14 02:39:32 +00:00
|
|
|
|
2018-12-10 05:12:10 +00:00
|
|
|
void init(boo::IWindow* ctx);
|
2018-12-08 05:18:42 +00:00
|
|
|
void proc();
|
|
|
|
void draw(boo::IGraphicsCommandQueue* gfxQ);
|
|
|
|
void handleCharCode(unsigned long chr, boo::EModifierKey mod, bool repeat);
|
|
|
|
void handleSpecialKeyDown(boo::ESpecialKey sp, boo::EModifierKey mod, bool repeat);
|
|
|
|
void handleSpecialKeyUp(boo::ESpecialKey sp, boo::EModifierKey mod);
|
|
|
|
void dumpLog();
|
|
|
|
static Console* instance();
|
|
|
|
static void RegisterLogger(Console* con);
|
2019-10-21 05:49:19 +00:00
|
|
|
bool isOpen() const { return m_state == State::Opened; }
|
2018-01-11 09:35:27 +00:00
|
|
|
};
|
2018-12-08 05:18:42 +00:00
|
|
|
} // namespace hecl
|