Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
console.hpp
Go to the documentation of this file.
1
3#pragma once
11#include <aegis2d/ui/export.hpp>
12
13#include <cstddef>
14#include <cstdint>
15#include <optional>
16#include <span>
17#include <string>
18#include <string_view>
19#include <vector>
20
21namespace aegis2d::ui {
22
26 enum class Kind : std::uint8_t {
28 Input,
30 Output,
32 Error,
33 };
35 std::string text{};
38};
39
43 float height = 0.5F;
45 float text_scale = 2.0F;
47 float margin = 8.0F;
49 std::int32_t layer = 1'000'010;
51 render::Color panel = render::rgb(0x000000, 0.85F);
58};
59
88class AEGIS2D_UI_EXPORT Console {
89public:
92 static constexpr platform::Key toggle_key = platform::Key::Grave;
93
97 explicit Console(std::size_t capacity = 500);
98
100 static constexpr std::size_t history_capacity = 100;
101 Console(const Console&) = delete;
102 Console& operator=(const Console&) = delete;
103 Console(Console&&) = delete;
105 ~Console() = default;
106
109 [[nodiscard]] core::Commands& commands() noexcept;
110
113 [[nodiscard]] bool open() const noexcept;
114
117 void set_open(bool open) noexcept;
118
126 bool update(const platform::Keyboard& keyboard, const platform::TextInput& input);
127
134 core::CommandResult submit(std::string_view line);
135
140 [[nodiscard]] std::span<const std::string> history() const noexcept;
141
145 void recall_previous();
146
148 void recall_next();
149
156 std::vector<std::string> complete();
157
161 void print(std::string_view text, ConsoleLine::Kind kind = ConsoleLine::Kind::Output);
162
164 void clear() noexcept;
165
168 [[nodiscard]] const std::string& input() const noexcept;
169
172 void set_input(std::string_view text);
173
176 [[nodiscard]] std::span<const ConsoleLine> lines() const noexcept;
177
180 [[nodiscard]] std::size_t scroll() const noexcept;
181
184 void scroll_by(std::ptrdiff_t lines) noexcept;
185
194 void draw(render::IRenderer& renderer, const render::BitmapFont& font, Vec2 screen,
195 const ConsoleStyle& style = {}) const;
196
197private:
198 core::Commands commands_;
199 std::vector<ConsoleLine> lines_;
200 std::size_t capacity_;
201 std::string input_;
202 std::vector<std::string> history_;
203 std::optional<std::size_t> recalled_;
204 std::string draft_;
205 std::size_t scroll_ = 0;
206 bool open_ = false;
207};
208
209} // namespace aegis2d::ui
Text drawn with a bitmap font, through the sprite batcher.
The commands a console runs: each has a name, its arguments and a help.
Definition commands.hpp:140
The in-game console: the key under Escape opens it over the game, a command typed on its input line r...
Definition console.hpp:88
Console(const Console &)=delete
Console(std::size_t capacity=500)
Closed console, with help and clear.
core::Commands & commands() noexcept
Commands the console runs.
Console & operator=(const Console &)=delete
Console & operator=(Console &&)=delete
Console(Console &&)=delete
RGBA color used by the renderer.
Console commands: named actions with typed arguments and a help, run from a line.
Keyboard keys, their names, and the state of the keyboard during a frame.
Vector and matrix types of the engine (GLM behind engine names).
Key
Keyboard key, independent of SDL.
Definition keyboard.hpp:24
constexpr Color rgb(std::uint32_t hex, float alpha=1.0F) noexcept
Color from its hexadecimal sRGB code, as design tools and palettes give it.
Definition color.hpp:25
Menus and forms: widgets and their focus, moved by keyboard, gamepad and mouse (client only).
Definition console.hpp:21
glm::vec2 Vec2
2D vector of floats: positions, velocities, sizes (glm::vec2).
Definition math.hpp:19
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
A line of the console's output.
Definition console.hpp:24
Kind
What the line is.
Definition console.hpp:26
@ Output
What a command showed.
@ Input
A line typed and run, echoed after the prompt.
@ Error
Why a line did not run.
Kind kind
Its kind, which picks its color.
Definition console.hpp:37
std::string text
Its text, without a line break.
Definition console.hpp:35
Where and how the console is drawn.
Definition console.hpp:41
render::Color input
Color of the input line and of the lines typed.
Definition console.hpp:55
float height
Part of the screen's height the console covers, from the top.
Definition console.hpp:43
float text_scale
Scale of the text (render::TextStyle::scale).
Definition console.hpp:45
std::int32_t layer
Layer of the panel; the text is drawn one layer above.
Definition console.hpp:49
render::Color error
Color of the errors, told from the others with any color vision.
Definition console.hpp:57
float margin
Space between the edges of the panel and the text, in logical pixels.
Definition console.hpp:47
render::Color text
Color of the output.
Definition console.hpp:53
render::Color panel
Color of the panel behind.
Definition console.hpp:51
UTF-8 text typed during a frame, following the user's keyboard layout.