Files
dt/Console.h
T
2026-11-03 20:05:02 -05:00

68 lines
1.8 KiB
C++

#ifndef CONSOLE_CLASS
#define CONSOLE_CLASS
#include <deque>
#include <functional>
#include <map>
#include <string>
#include <vector>
class Font;
class Box;
class TextBox;
// The in-game console. Commands get registered by name and run with whatever words follow them.
class Console {
public:
using Arguments = std::vector<std::string>; // the words after the command name
using Command = std::function<void(const Arguments&)>;
explicit Console(Font* newFont);
// usage is shown by help, like "give <item> [count]". Names aren't case sensitive.
void Register(const std::string& name, const std::string& usage, const std::string& description, Command run);
void Execute(const std::string& line);
void Print(const std::string& message);
void Open();
void Close();
void Toggle() { open ? Close() : Open(); }
bool IsOpen() const { return open; }
// Feed these from the GLFW callbacks while it's open
void OnChar(unsigned int codepoint);
void OnKey(int key);
// Positive goes back toward older lines
void Scroll(int lines);
// Once a frame, refreshes what's on screen
void Update(float dt);
private:
struct Entry {
std::string usage;
std::string description;
Command run;
};
void CreateDisplay();
Font* font = nullptr;
std::map<std::string, Entry> commands; // sorted, so help lists them alphabetically
std::deque<std::string> log;
std::vector<std::string> history;
int historyIndex = 0; // history.size() means the line being typed
std::string input;
int scroll = 0; // how many lines up from the newest
float caretTimer = 0.0f;
bool open = false;
Box* background = nullptr;
Box* inputBackground = nullptr;
TextBox* logText = nullptr;
TextBox* inputText = nullptr;
};
#endif