From a845d0d630691643483197a5b8cc92ea4874aab4 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Mon, 29 Aug 2022 13:54:06 +0300 Subject: [PATCH] Rearranged TTextBox immediate mode draw. --- SpaceCadetPinball/TTextBox.cpp | 39 +++++++++++++++++++++++++++++++++- SpaceCadetPinball/TTextBox.h | 1 + SpaceCadetPinball/gdrv.cpp | 38 ++++++--------------------------- SpaceCadetPinball/gdrv.h | 2 +- SpaceCadetPinball/winmain.cpp | 8 +------ 5 files changed, 48 insertions(+), 40 deletions(-) diff --git a/SpaceCadetPinball/TTextBox.cpp b/SpaceCadetPinball/TTextBox.cpp index bf87099..e3c371c 100644 --- a/SpaceCadetPinball/TTextBox.cpp +++ b/SpaceCadetPinball/TTextBox.cpp @@ -145,6 +145,43 @@ void TTextBox::Display(const char* text, float time) } } +void TTextBox::DrawImGui() +{ + // Do nothing when using a font (the text will be rendered to VScreen in TTextBox::Draw) + if (Font || !Message1) + return; + + char windowName[64]; + SDL_Rect rect; + ImGuiWindowFlags window_flags = + ImGuiWindowFlags_NoBackground | + ImGuiWindowFlags_NoDecoration | + ImGuiWindowFlags_NoSavedSettings | + ImGuiWindowFlags_NoFocusOnAppearing | + ImGuiWindowFlags_NoInputs; + + rect.x = OffsetX; + rect.y = OffsetY; + rect.w = Width; + rect.h = Height; + + rect = fullscrn::GetScreenRectFromPinballRect(rect); + + ImGui::SetNextWindowPos(ImVec2(rect.x, rect.y)); + ImGui::SetNextWindowSize(ImVec2(rect.w, rect.h)); + + // Use the pointer to generate a window unique name per text box + snprintf(windowName, sizeof(windowName), "TTextBox_%p", this); + if (ImGui::Begin(windowName, nullptr, window_flags)) + { + ImGui::SetWindowFontScale(fullscrn::GetScreenToPinballRatio()); + + // ToDo: centered text in FT + ImGui::TextWrapped("%s", Message1->Text); + } + ImGui::End(); +} + void TTextBox::Draw() { auto bmp = BgBmp; @@ -189,7 +226,7 @@ void TTextBox::Draw() { if (!Font) { - // Handled by gdrv::grtext_draw_ttext_in_box() + // Immediate mode drawing using system font is handled by TTextBox::DrawImGui return; } diff --git a/SpaceCadetPinball/TTextBox.h b/SpaceCadetPinball/TTextBox.h index ad2b908..e009703 100644 --- a/SpaceCadetPinball/TTextBox.h +++ b/SpaceCadetPinball/TTextBox.h @@ -22,6 +22,7 @@ public: int Message(int code, float value) override; void Clear(); void Display(const char* text, float time); + void DrawImGui(); private: struct LayoutResult diff --git a/SpaceCadetPinball/gdrv.cpp b/SpaceCadetPinball/gdrv.cpp index 355c0cb..46dfb3a 100644 --- a/SpaceCadetPinball/gdrv.cpp +++ b/SpaceCadetPinball/gdrv.cpp @@ -8,6 +8,7 @@ #include "winmain.h" #include "TTextBox.h" #include "fullscrn.h" +#include "pinball.h" ColorRgba gdrv::current_palette[256]{}; @@ -271,40 +272,15 @@ void gdrv::ScrollBitmapHorizontal(gdrv_bitmap8* bmp, int xStart) } -void gdrv::grtext_draw_ttext_in_box(TTextBox* textBox) +void gdrv::grtext_draw_ttext_in_box() { - // Do nothing when using a font (the text will be rendered in TTextBox::Draw) - if(textBox->Font) - return; - - char windowName[64]; - SDL_Rect rect; - ImGuiWindowFlags window_flags = - ImGuiWindowFlags_NoBackground | - ImGuiWindowFlags_NoDecoration | - ImGuiWindowFlags_NoSavedSettings | - ImGuiWindowFlags_NoFocusOnAppearing | - ImGuiWindowFlags_NoInputs; - - rect.x = textBox->OffsetX; - rect.y = textBox->OffsetY; - rect.w = textBox->Width; - rect.h = textBox->Height; - - rect = fullscrn::GetScreenRectFromPinballRect(rect); - - ImGui::SetNextWindowPos(ImVec2(rect.x, rect.y)); - ImGui::SetNextWindowSize(ImVec2(rect.w, rect.h)); - - // Use the pointer to generate a window unique name per text box - snprintf(windowName, sizeof(windowName), "TTextBox_%p", textBox); - if (ImGui::Begin(windowName, nullptr, window_flags)) + for (const auto textBox : { pinball::InfoTextBox, pinball::MissTextBox }) { - ImGui::SetWindowFontScale(fullscrn::GetScreenToPinballRatio()); - if(textBox->Message1) - ImGui::TextWrapped("%s", textBox->Message1->Text); + if (textBox) + { + textBox->DrawImGui(); + } } - ImGui::End(); } void gdrv::ApplyPalette(gdrv_bitmap8& bmp) diff --git a/SpaceCadetPinball/gdrv.h b/SpaceCadetPinball/gdrv.h index afc11e3..44635b6 100644 --- a/SpaceCadetPinball/gdrv.h +++ b/SpaceCadetPinball/gdrv.h @@ -82,7 +82,7 @@ public: static void copy_bitmap_w_transparency(gdrv_bitmap8* dstBmp, int width, int height, int xOff, int yOff, gdrv_bitmap8* srcBmp, int srcXOff, int srcYOff); static void ScrollBitmapHorizontal(gdrv_bitmap8* bmp, int xStart); - static void grtext_draw_ttext_in_box(TTextBox* textBox); + static void grtext_draw_ttext_in_box(); static void ApplyPalette(gdrv_bitmap8& bmp); static void CreatePreview(gdrv_bitmap8& bmp); diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index 0a767e2..c3064ea 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -714,13 +714,7 @@ void winmain::RenderUi() RenderFrameTimeDialog(); // Print game texts on the sidebar - for(auto textToDraw : {pinball::InfoTextBox, pinball::MissTextBox}) - { - if(textToDraw) - { - gdrv::grtext_draw_ttext_in_box(textToDraw); - } - } + gdrv::grtext_draw_ttext_in_box(); } int winmain::event_handler(const SDL_Event* event)