MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
mxvk_console.hpp
Go to the documentation of this file.
1/**
2 * @file mxvk_console.hpp
3 * @brief In-game command console for MXVK / Vulkan windows.
4 */
5#pragma once
6
7#include <SDL3/SDL.h>
8
9#include <cstddef>
10#include <deque>
11#include <functional>
12#include <iosfwd>
13#include <limits>
14#include <string>
15#include <vector>
16
17#include "mxvk_text.hpp"
18
19namespace mxvk {
20
21 class VK_Window;
22 class VK_Sprite;
23
24 /**
25 * @class VK_Console
26 * @brief Lightweight command console renderer for `mxvk::VK_Window`.
27 *
28 * The console captures keyboard input, keeps command history, renders buffered
29 * lines via `VK_Window::printText`, and allows applications to register a
30 * command callback.
31 */
32 class VK_Console {
33 public:
35 std::function<bool(VK_Window &window, const std::vector<std::string> &args, std::ostream &output)>;
36
37 VK_Console() = default;
38
39 /** @brief Attach this console to a window and configure console font rendering. */
40 void attach(VK_Window &window, const std::string &fontPath, int fontSize = 18);
41
42 /** @brief Change only the console font, leaving the window default font unchanged. */
43 void setFont(const std::string &fontPath, int fontSize);
44
45 /** @brief Handle keyboard/text events for the console. */
46 void handleEvent(const SDL_Event &event);
47
48 /** @brief Draw the current console overlay. Call once per frame from `proc()`. */
49 void draw();
50
51 /**
52 * @brief Append one line to console output.
53 * @param line Text to append. Embedded newlines are split into separate lines.
54 * @param color Optional text color for this output line. Defaults to white.
55 */
56 void printLine(const std::string &line, SDL_Color color = SDL_Color{255, 255, 255, 255});
57
58 /** @brief Clear all buffered output lines. */
59 void clear();
60
61 /** @brief Register a command callback. Return true to indicate the command was handled. */
63
64 /** @brief Set the input prompt text. */
65 void setPrompt(const std::string &prompt);
66
67 /** @brief Show or hide the console. */
68 void setVisible(bool value) noexcept;
69
70 /** @brief Show console. */
71 void show() noexcept;
72
73 /** @brief Hide console. */
74 void hide() noexcept;
75
76 /** @brief Toggle console visibility. */
77 void toggle() noexcept;
78
79 /** @return true when the console is visible. */
80 [[nodiscard]] bool isVisible() const noexcept;
81
82 /** @brief Set max number of retained output lines. */
83 void setMaxLines(std::size_t maxLines);
84
85 /** @brief Set max number of lines rendered on screen. */
86 void setMaxVisibleLines(std::size_t maxVisibleLines);
87
88 /** @brief Recompute cached line wrapping after font or layout metrics change. */
90
91 /** @brief Select whether console sprite elements use top-left Y coordinates. */
92 void setSpriteYOriginTopLeft(bool enabled) noexcept { sprite_y_origin_top_left = enabled; }
93
94 /** @return Current input buffer. */
95 [[nodiscard]] const std::string &inputBuffer() const noexcept;
96
97 private:
98 struct OutputLine {
99 std::string text;
100 SDL_Color color;
101 };
102
103 void pushOutputLine(std::string line);
104 void pushOutputLine(std::string line, SDL_Color color);
105 void trimOutputToLimits();
106 void submitInput();
107 void historyUp();
108 void historyDown();
109 void scrollUp(std::size_t amount = 1);
110 void scrollDown(std::size_t amount = 1);
111 void ensurePanelSprite();
112 void ensureCursorSprite();
113 void ensureScrollSprites();
114 void updatePanelLayout();
115 void refreshVisibleLineCount();
116 void updateScrollbarGeometry();
117 void invalidateWrappedCache();
118 void ensureWrappedCache(int maxWidth) const;
119 [[nodiscard]] int measureTextWidth(const std::string &text) const;
120 [[nodiscard]] int usableTextWidth() const;
121 [[nodiscard]] std::vector<std::string> wrapTextToWidth(const std::string &text, int maxWidth) const;
122 [[nodiscard]] bool isPointInScrollbar(int x, int y) const noexcept;
123 [[nodiscard]] bool isPointInScrollbarThumb(int x, int y) const noexcept;
124 void updateScrollFromThumbY(int thumbY);
125 [[nodiscard]] std::size_t effectiveVisibleLineCount() const noexcept;
126 [[nodiscard]] std::size_t maxScrollOffset() const noexcept;
127 [[nodiscard]] static std::vector<std::string> tokenize(const std::string &line);
128 [[nodiscard]] bool handleDefaultCommand(const std::vector<std::string> &args, std::ostream &output);
129
130 VK_Window *windowPtr = nullptr;
131 VK_Sprite *panel_sprite = nullptr;
132 VK_Sprite *cursor_sprite = nullptr;
133 VK_Sprite *scroll_track_sprite = nullptr;
134 VK_Sprite *scroll_thumb_sprite = nullptr;
135 bool visible = false;
136 bool fade_active = false;
137 float fade_alpha = 0.0f;
138 float fade_start_alpha = 0.0f;
139 float fade_target_alpha = 0.0f;
140 Uint64 fade_start_ns = 0;
141 Uint64 fade_duration_ns = 220000000ULL;
142 bool cursor_visible = true;
143 Uint64 last_cursor_toggle_ns = 0;
144 std::size_t cursor_pos = 0;
145 int panel_x = 12;
146 int panel_y = 12;
147 int panel_w = 640;
148 int panel_h = 320;
149 int content_top_y = 0;
150 int content_bottom_y = 0;
151 int scrollbar_x = 0;
152 int scrollbar_y = 0;
153 int scrollbar_w = 10;
154 int scrollbar_h = 0;
155 int scrollbar_thumb_y = 0;
156 int scrollbar_thumb_h = 0;
157 bool scrollbar_dragging = false;
158 bool sprite_y_origin_top_left = false;
159 int scrollbar_drag_offset = 0;
160 std::size_t scroll_offset = 0;
161 std::size_t last_visible_line_count = 16;
162 bool follow_tail = true;
163 std::string promptText = "> ";
164 std::string input{};
165 std::deque<OutputLine> lines{};
166 std::size_t total_line_chars = 0;
167 mutable bool wrapped_cache_dirty = true;
168 mutable int wrapped_cache_width = -1;
169 mutable std::vector<OutputLine> wrapped_cache{};
170 mutable std::size_t wrapped_cache_line_count = 0;
171 std::vector<std::string> history{};
172 int history_index = -1;
173 std::size_t max_lines = 1200;
174 std::size_t max_total_chars = 128 * 1024;
175 std::size_t max_visible_lines = std::numeric_limits<std::size_t>::max();
176 SDL_Color text_color{220, 235, 255, 255};
177 SDL_Color prompt_color{120, 220, 160, 255};
178 SDL_Color info_color{170, 170, 210, 255};
179 Font console_font{};
180 CommandCallback commandCallback{};
181 };
182
184
185} // namespace mxvk
186
187namespace console {
189} // namespace console
Lightweight command console renderer for mxvk::VK_Window.
void setVisible(bool value) noexcept
Show or hide the console.
std::function< bool(VK_Window &window, const std::vector< std::string > &args, std::ostream &output)> CommandCallback
void draw()
Draw the current console overlay.
void invalidateLayoutCache()
Recompute cached line wrapping after font or layout metrics change.
VK_Console()=default
void setSpriteYOriginTopLeft(bool enabled) noexcept
Select whether console sprite elements use top-left Y coordinates.
const std::string & inputBuffer() const noexcept
void toggle() noexcept
Toggle console visibility.
void handleEvent(const SDL_Event &event)
Handle keyboard/text events for the console.
bool isVisible() const noexcept
void hide() noexcept
Hide console.
void setFont(const std::string &fontPath, int fontSize)
Change only the console font, leaving the window default font unchanged.
void setPrompt(const std::string &prompt)
Set the input prompt text.
void show() noexcept
Show console.
void clear()
Clear all buffered output lines.
void setMaxVisibleLines(std::size_t maxVisibleLines)
Set max number of lines rendered on screen.
void setCommandCallback(CommandCallback callback)
Register a command callback.
void printLine(const std::string &line, SDL_Color color=SDL_Color{255, 255, 255, 255})
Append one line to console output.
void attach(VK_Window &window, const std::string &fontPath, int fontSize=18)
Attach this console to a window and configure console font rendering.
void setMaxLines(std::size_t maxLines)
Set max number of retained output lines.
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:37
Vulkan SDL_ttf text renderer.
mxvk::VK_Console Console
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
VK_Console Console