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