imgui console: use JetBrains (redistributable font); use font size, not pixel scaling
This commit is contained in:
parent
4ead2f25fe
commit
0207595db0
3 changed files with 97 additions and 13 deletions
|
|
@ -83,6 +83,10 @@ GameEngine::GameEngine(const McRogueFaceConfig& cfg)
|
|||
// Initialize ImGui for the window
|
||||
if (ImGui::SFML::Init(*window)) {
|
||||
imguiInitialized = true;
|
||||
// Register settings handler before .ini is loaded (happens on first frame)
|
||||
ImGuiConsole::registerSettingsHandler();
|
||||
// Load JetBrains Mono for crisp console text (will be overridden by .ini if present)
|
||||
ImGuiConsole::reloadFont(16.0f);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#include "ImGuiConsole.h"
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h" // For ImGuiSettingsHandler, ImHashStr, MarkIniSettingsDirty
|
||||
#include "imgui-SFML.h"
|
||||
#include "McRFPy_API.h"
|
||||
#include <Python.h>
|
||||
#include <sstream>
|
||||
|
|
@ -8,6 +10,74 @@
|
|||
|
||||
// Static member initialization
|
||||
bool ImGuiConsole::enabled = true;
|
||||
float ImGuiConsole::s_currentFontSize = 16.0f;
|
||||
|
||||
void ImGuiConsole::reloadFont(float size) {
|
||||
// Clamp size to reasonable bounds
|
||||
size = std::max(8.0f, std::min(32.0f, size));
|
||||
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
// Clear existing fonts
|
||||
io.Fonts->Clear();
|
||||
|
||||
// Load JetBrains Mono at the new size
|
||||
io.Fonts->AddFontFromFileTTF("./assets/JetbrainsMono.ttf", size);
|
||||
|
||||
// Rebuild the font texture
|
||||
if (!ImGui::SFML::UpdateFontTexture()) {
|
||||
// Font texture update failed - revert to default
|
||||
io.Fonts->Clear();
|
||||
io.Fonts->AddFontDefault();
|
||||
(void)ImGui::SFML::UpdateFontTexture(); // Cast to void - can't fail on default font
|
||||
return;
|
||||
}
|
||||
|
||||
s_currentFontSize = size;
|
||||
|
||||
// Mark imgui.ini as dirty so font size gets saved
|
||||
ImGui::MarkIniSettingsDirty();
|
||||
}
|
||||
|
||||
// Settings handler callbacks for imgui.ini persistence
|
||||
static void* ConsoleSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name) {
|
||||
// We only have one console, so just return a non-null pointer
|
||||
if (strcmp(name, "Main") == 0) {
|
||||
return (void*)1; // Non-null to indicate valid entry
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void ConsoleSettingsHandler_ReadLine(ImGuiContext*, ImGuiSettingsHandler*, void* entry, const char* line) {
|
||||
float size;
|
||||
if (sscanf(line, "FontSize=%f", &size) == 1) {
|
||||
// Don't reload font here - just store the value
|
||||
// Font will be loaded after settings are applied
|
||||
ImGuiConsole::s_currentFontSize = std::max(8.0f, std::min(32.0f, size));
|
||||
}
|
||||
}
|
||||
|
||||
static void ConsoleSettingsHandler_ApplyAll(ImGuiContext*, ImGuiSettingsHandler*) {
|
||||
// After all settings are read, reload the font at the saved size
|
||||
ImGuiConsole::reloadFont(ImGuiConsole::s_currentFontSize);
|
||||
}
|
||||
|
||||
static void ConsoleSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler* handler, ImGuiTextBuffer* buf) {
|
||||
buf->appendf("[%s][Main]\n", handler->TypeName);
|
||||
buf->appendf("FontSize=%.0f\n", ImGuiConsole::s_currentFontSize);
|
||||
buf->append("\n");
|
||||
}
|
||||
|
||||
void ImGuiConsole::registerSettingsHandler() {
|
||||
ImGuiSettingsHandler ini_handler;
|
||||
ini_handler.TypeName = "Console";
|
||||
ini_handler.TypeHash = ImHashStr("Console");
|
||||
ini_handler.ReadOpenFn = ConsoleSettingsHandler_ReadOpen;
|
||||
ini_handler.ReadLineFn = ConsoleSettingsHandler_ReadLine;
|
||||
ini_handler.ApplyAllFn = ConsoleSettingsHandler_ApplyAll;
|
||||
ini_handler.WriteAllFn = ConsoleSettingsHandler_WriteAll;
|
||||
ImGui::GetCurrentContext()->SettingsHandlers.push_back(ini_handler);
|
||||
}
|
||||
|
||||
ImGuiConsole::ImGuiConsole() {
|
||||
addOutput("McRogueFace Python Console", false);
|
||||
|
|
@ -161,23 +231,27 @@ void ImGuiConsole::render() {
|
|||
return;
|
||||
}
|
||||
|
||||
// Apply font scale
|
||||
ImGui::SetWindowFontScale(fontScale);
|
||||
|
||||
// Menu bar with toolbar buttons
|
||||
if (ImGui::BeginMenuBar()) {
|
||||
// Font size controls
|
||||
// Font size controls (adjust by 2 pixels, reload font)
|
||||
// Use static s_currentFontSize since font is shared across all ImGui
|
||||
if (ImGui::SmallButton("-")) {
|
||||
fontScale = std::max(0.5f, fontScale - 0.1f);
|
||||
float newSize = std::max(8.0f, s_currentFontSize - 2.0f);
|
||||
if (newSize != s_currentFontSize) {
|
||||
reloadFont(newSize);
|
||||
}
|
||||
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Decrease text size");
|
||||
}
|
||||
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Decrease font size");
|
||||
|
||||
ImGui::Text("%.0f%%", fontScale * 100);
|
||||
ImGui::Text("%.0fpx", s_currentFontSize);
|
||||
|
||||
if (ImGui::SmallButton("+")) {
|
||||
fontScale = std::min(2.0f, fontScale + 0.1f);
|
||||
float newSize = std::min(32.0f, s_currentFontSize + 2.0f);
|
||||
if (newSize != s_currentFontSize) {
|
||||
reloadFont(newSize);
|
||||
}
|
||||
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Increase text size");
|
||||
}
|
||||
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Increase font size");
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
|
|
@ -331,9 +405,6 @@ void ImGuiConsole::renderCodeEditor() {
|
|||
return;
|
||||
}
|
||||
|
||||
// Apply same font scale as console
|
||||
ImGui::SetWindowFontScale(fontScale);
|
||||
|
||||
// Menu bar
|
||||
if (ImGui::BeginMenuBar()) {
|
||||
// Run button
|
||||
|
|
|
|||
|
|
@ -27,6 +27,16 @@ public:
|
|||
// Input handling
|
||||
bool wantsKeyboardInput() const; // Returns true if ImGui wants keyboard
|
||||
|
||||
// Font management - reload ImGui font at specified pixel size
|
||||
static void reloadFont(float size);
|
||||
static float getCurrentFontSize() { return s_currentFontSize; }
|
||||
|
||||
// Settings persistence via imgui.ini
|
||||
static void registerSettingsHandler();
|
||||
|
||||
// Allow settings handler callbacks to access font size
|
||||
static float s_currentFontSize; // Track current loaded font size
|
||||
|
||||
private:
|
||||
void executeCommand(const std::string& command);
|
||||
void addOutput(const std::string& text, bool isError = false);
|
||||
|
|
@ -40,7 +50,6 @@ private:
|
|||
bool editorVisible = false; // Multi-line editor window
|
||||
bool consoleLocked = false; // Prevent console dragging
|
||||
bool editorLocked = false; // Prevent editor dragging
|
||||
float fontScale = 1.0f; // Text size multiplier (0.5 - 2.0)
|
||||
|
||||
// Input buffers
|
||||
char inputBuffer[1024] = {0};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue