From 96fc1bee6b078d53015916e6a629f78bbb3acefb Mon Sep 17 00:00:00 2001 From: openshwprojects Date: Sun, 4 Dec 2022 00:23:40 +0100 Subject: [PATCH] sim: textfields now support variable expansion --- src/sim/Simulator.cpp | 8 ++++++-- src/sim/Text.cpp | 6 +++++- src/sim/sim_local.h | 4 ++++ src/sim/sim_sdl.cpp | 5 ++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/sim/Simulator.cpp b/src/sim/Simulator.cpp index 7871f8611..04fed0113 100644 --- a/src/sim/Simulator.cpp +++ b/src/sim/Simulator.cpp @@ -148,8 +148,8 @@ void CSimulator::drawWindow() { glLoadIdentity(); glOrtho(0.0f, WinWidth, WinHeight, 0.0f, 0.0f, 1.0f); - int h = 20; - h = drawText(10, h, "Demo %i", 1); + int h = 40; + h = drawText(10, h, "OpenBeken Simulator"); if (sim != 0) { h = sim->drawTextStats(h); } @@ -157,6 +157,9 @@ void CSimulator::drawWindow() { h = drawText(10, h, "Active Tool: %s", activeTool->getName()); h = activeTool->drawTextStats(h); } + if (currentlyEditingText) { + h = drawText(10, h, "You are currently editing a text field."); + } glColor3f(0.7f, 0.7f, 0.7f); glLineWidth(0.25f); @@ -269,6 +272,7 @@ bool CSimulator::loadSimulation(const char *s) { SIM_ClearOBK(); SIM_SetupFlashFileReading(memPath.c_str()); SIM_DoFreshOBKBoot(); + sim->recalcBounds(); bSchematicModified = false; return false; diff --git a/src/sim/Text.cpp b/src/sim/Text.cpp index bc3c83ccb..7fead6ae8 100644 --- a/src/sim/Text.cpp +++ b/src/sim/Text.cpp @@ -5,8 +5,12 @@ void CText::recalcBoundsSelf() { bounds.clear(); int line = 0; int ch = 0;; - const char *p = txt.c_str(); + char buffer[512]; + CMD_ExpandConstantsWithinString(txt.c_str(), buffer, sizeof(buffer)); + const char *p = buffer; bounds.addPoint(Coord(0, -8)); + // do not go smaller than that + bounds.addPoint(Coord(9, 5)); while (*p) { if (p[0] == '\n') { line++; diff --git a/src/sim/sim_local.h b/src/sim/sim_local.h index dd5d97119..015a901a1 100644 --- a/src/sim/sim_local.h +++ b/src/sim/sim_local.h @@ -44,6 +44,10 @@ extern int WinWidth; extern int WinHeight; extern int gridSize; +extern "C" { + void CMD_ExpandConstantsWithinString(const char *in, char *out, int outLen); +} + template class TArray : public std::vector { public: diff --git a/src/sim/sim_sdl.cpp b/src/sim/sim_sdl.cpp index 139e6e7a5..0babbcd1c 100644 --- a/src/sim/sim_sdl.cpp +++ b/src/sim/sim_sdl.cpp @@ -48,12 +48,15 @@ int drawTextInternal(float x, float y, const char *buffer) { y += 15; return y; } + int drawText(int x, int y, const char* fmt, ...) { va_list argList; + char buffer2[512]; char buffer[512]; va_start(argList, fmt); - vsnprintf(buffer, sizeof(buffer), fmt, argList); + vsnprintf(buffer2, sizeof(buffer2), fmt, argList); va_end(argList); + CMD_ExpandConstantsWithinString(buffer2, buffer, sizeof(buffer)); g_style_text.apply(); float ret = drawTextInternal(x, y, buffer); drawTextInternal(x + 0.5f, y + 0.5f, buffer);