Skip to content

Commit

Permalink
add in center text
Browse files Browse the repository at this point in the history
  • Loading branch information
kjblanchard committed Nov 9, 2024
1 parent e358fcc commit d672569
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/engine/include/Supergoon/UI/UIText.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ class UIText : public UIObject {
UIText(Panel* parent, std::string text);
virtual void Draw() override;
virtual void OnDirty() override;
inline void SetCenter(bool center) {
CenterText = center;
Dirty = true;
}
void UpdateText(std::string text);
std::shared_ptr<Text> TextPtr;
// Point TextBounds = {0, 0};
int currentLetters = 0;
RectangleF TextDrawRect = RectangleF();
RectangleF TextSrcRect = RectangleF();
bool WordWrap;
bool CenterText;
bool CenterText = false;
std::string DisplayText;

private:
Expand Down
18 changes: 14 additions & 4 deletions src/engine/src/UI/UIText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ void UIText::Draw() {
// We always want to draw the full text, if possible, otherwise we should cut off.
// Src rect should either be the full text, or the size of the text that we decide.
// Dst rect should always be the size of the src rect.
auto dst = RectangleF(Bounds.X, Bounds.Y, TextSrcRect.W, TextSrcRect.H);
TextPtr->Draw(TextSrcRect, dst);
TextPtr->Draw(TextSrcRect, TextDrawRect);
}

void UIText::OnDirty() {
Expand All @@ -35,6 +34,10 @@ void UIText::OnDirty() {
TextPtr->SetTextBounds({(int)Bounds.W, (int)Bounds.H});
TextPtr->SetLetterCount(currentLetters);
TextPtr->SetWordWrap(WordWrap);
// If we should center, adjust our X and Y accordingly.
// if (CenterText) {
// }

// If our bounds are set to 0, then we should use the full size.
if (Bounds.W == 0 && Bounds.H == 0) {
TextSrcRect = RectangleF(0, 0, TextPtr->Size().X, TextPtr->Size().Y);
Expand All @@ -44,6 +47,11 @@ void UIText::OnDirty() {
auto height = Bounds.H ? std::min((int)Bounds.H, TextPtr->Size().Y) : TextPtr->Size().Y;
TextSrcRect = RectangleF(0, 0, width, height);
}
auto x = Bounds.X;
if (CenterText) {
x = Bounds.X + ((Bounds.W / 2) - (TextPtr->Size().X / 2));
}
TextDrawRect = RectangleF(x, Bounds.Y, TextSrcRect.W, TextSrcRect.H);
}

void UIText::UpdateText(std::string text) {
Expand All @@ -52,8 +60,10 @@ void UIText::UpdateText(std::string text) {
}
TextPtr = ContentRegistry::CreateContent<Text, std::string, int>(text, "commodore", 16);
TextPtr->LoadContent();
Bounds.W = TextPtr->Size().X;
Bounds.H = TextPtr->Size().Y;
// Bounds.W = TextPtr->Size().X;
// Bounds.H = TextPtr->Size().Y;
// Bounds.W = Bounds
// Bounds.H = TextPtr->Size().Y;
// create new text?
// TextBounds = TextPtr->TextBounds();
currentLetters = TextPtr->_lettersToDraw;
Expand Down
15 changes: 15 additions & 0 deletions src/engine/src/Widgets/UIWidget.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <SupergoonEngine/imgui/imgui.h>

#include <Supergoon/UI/Panel.hpp>
#include <Supergoon/Graphics/Graphics.hpp>
#include <Supergoon/UI/UI.hpp>
#include <Supergoon/UI/UIImage.hpp>
#include <Supergoon/UI/UIText.hpp>
#include <Supergoon/Widgets/UIWidget.hpp>
#include <Supergoon/Widgets/Widgets.hpp>
using namespace Supergoon;
static bool shouldDrawDebugBox = false;
void UIWidget::DrawPanel(Panel *panel, std::string panelName) {
if (ImGui::TreeNode((panelName + "- panel").c_str())) {
auto panelOffsetLabel = "Offset X ##" + panelName;
Expand Down Expand Up @@ -64,7 +66,9 @@ void UIWidget::DrawPanel(Panel *panel, std::string panelName) {
std::string childXBounds = "TextBoundsX##" + key;
std::string childYBounds = "TextBoundsY##" + key;
std::string childWordWrapLabel = "WordWrap##" + key;
std::string childCenterLabel = "Center##" + key;
std::string childLettersToDraw = "Letters To Draw##" + key;
std::string childDebugBoxCheckbox = "DebugBox##" + key;
if (ImGui::TreeNode((key + "- text").c_str())) {
if (ImGui::DragFloat(childX_label.c_str(), &value->Offset.X, 1.0f)) {
value->Dirty = true;
Expand All @@ -81,6 +85,17 @@ void UIWidget::DrawPanel(Panel *panel, std::string panelName) {
if (ImGui::Checkbox(childWordWrapLabel.c_str(), &textUIObject->WordWrap)) {
value->Dirty = true;
};
ImGui::SameLine();
if (ImGui::Checkbox(childCenterLabel.c_str(), &textUIObject->CenterText)) {
value->Dirty = true;
};
ImGui::SameLine();
ImGui::Checkbox(childDebugBoxCheckbox.c_str(), &shouldDrawDebugBox);

if(shouldDrawDebugBox) {
Graphics::Instance()->DrawRect(textUIObject->Bounds, Color(255, 0, 0, 255));

}
if (ImGui::DragInt(childLettersToDraw.c_str(), &textUIObject->currentLetters, 1, 0, textUIObject->TextPtr->_text.length())) {
value->Dirty = true;
}
Expand Down
6 changes: 5 additions & 1 deletion src/game/BlackjackGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void setupUINameChangeBox() {
auto textPanel = std::make_shared<Panel>(ui);
textPanel->Offset = {145, 15};
auto text = std::make_shared<UIText>(textPanel.get(), "Hello world!");
text->Offset = {40, 13};
text->Offset = {0, 13};
textPanel->Children["textman"] = text;
ui->Children["textTesting"] = textPanel;
// Test creating the uitextbox
Expand All @@ -76,6 +76,10 @@ static void setupUINameChangeBox() {
// Create ui text image of the right size as a render target
float fullSizeX = 200;
float fullSizeY = 48;
text->Bounds.W = fullSizeX;
text->Bounds.H = fullSizeY;
text->CenterText = true;
// text->SetCenter(true);
auto textBoxImage = ContentRegistry::CreateContent<Image, int, int>("uitextbox", (int)fullSizeX, (int)fullSizeY);
textBoxImage->LoadContent();
// Set the background
Expand Down

0 comments on commit d672569

Please sign in to comment.