diff --git a/src/engine/include/Supergoon/UI/Panel.hpp b/src/engine/include/Supergoon/UI/Panel.hpp index 112b117..ef2dd1b 100644 --- a/src/engine/include/Supergoon/UI/Panel.hpp +++ b/src/engine/include/Supergoon/UI/Panel.hpp @@ -8,7 +8,7 @@ class UIWidget; class Panel : public UIObject { public: Panel(); - Panel(Panel* parent); + Panel(Panel* parent, std::string name); std::unordered_map> Children; void OnDirty() override; diff --git a/src/engine/include/Supergoon/UI/UIImage.hpp b/src/engine/include/Supergoon/UI/UIImage.hpp index 7e5a723..dbe5452 100644 --- a/src/engine/include/Supergoon/UI/UIImage.hpp +++ b/src/engine/include/Supergoon/UI/UIImage.hpp @@ -5,13 +5,13 @@ #include namespace Supergoon { class Panel; -class ImageObject : public UIObject { +class UIImage : public UIObject { public: // If you want to initialize everything about this Object. - ImageObject(Panel* parent); + UIImage(Panel* parent, std::string id); // If you are loading from a ui json file. - ImageObject(Panel* parent, nlohmann::json& json); - RectangleF ImageSourceRect = {0,0,0,0}; + UIImage(Panel* parent, nlohmann::json& json); + RectangleF ImageSourceRect = {0, 0, 0, 0}; std::shared_ptr ImagePtr = nullptr; virtual void Draw() override; virtual void OnDirty() override; diff --git a/src/engine/include/Supergoon/UI/UIText.hpp b/src/engine/include/Supergoon/UI/UIText.hpp index 03053f8..fca7534 100644 --- a/src/engine/include/Supergoon/UI/UIText.hpp +++ b/src/engine/include/Supergoon/UI/UIText.hpp @@ -7,7 +7,7 @@ namespace Supergoon { class UIText : public UIObject { public: - UIText(Panel* parent, std::string text); + UIText(Panel* parent, std::string text, std::string uiName = ""); virtual void Draw() override; virtual void OnDirty() override; inline void SetCenter(bool center) { diff --git a/src/engine/src/Content/Font.cpp b/src/engine/src/Content/Font.cpp index 6a37cfa..ce3ff2f 100644 --- a/src/engine/src/Content/Font.cpp +++ b/src/engine/src/Content/Font.cpp @@ -30,7 +30,10 @@ void Font::Load() { } void Font::Unload() { - FT_Done_Face(_face); + if (_face) { + FT_Done_Face(_face); + _face = nullptr; + } } const std::string Font::Filepath() { diff --git a/src/engine/src/UI/Panel.cpp b/src/engine/src/UI/Panel.cpp index 17c8a50..6b8565a 100644 --- a/src/engine/src/UI/Panel.cpp +++ b/src/engine/src/UI/Panel.cpp @@ -2,8 +2,9 @@ using namespace Supergoon; Panel::Panel() : UIObject() { } -Panel::Panel(Panel* parent) : UIObject(parent) { +Panel::Panel(Panel* parent, std::string name) : UIObject(parent) { WidgetType = (int)BuiltinWidgetTypes::Panel; + parent->Children[name] = std::shared_ptr(this); } void Panel::OnDirty() { auto parentBoundsX = Parent ? Parent->Bounds.X : 0; diff --git a/src/engine/src/UI/UI.cpp b/src/engine/src/UI/UI.cpp index 60ccce0..9835bf4 100644 --- a/src/engine/src/UI/UI.cpp +++ b/src/engine/src/UI/UI.cpp @@ -31,27 +31,27 @@ Panel* UI::Initialize() { auto graphics = Graphics::Instance(); rootPanel->Bounds = RectangleF{0, 0, (float)graphics->LogicalWidth(), (float)graphics->LogicalHeight()}; UIInstance = rootPanel; - auto name = "fadePanel"; - auto fadePanel = std::make_shared(rootPanel); - fadePanel->SetAlpha(0); + auto name = "fadeImage"; + auto fadeImage = new UIImage(rootPanel, "Fade Panel"); + fadeImage->SetAlpha(0); auto path = std::string(SDL_GetBasePath()) + "assets/img/null.png"; - fadePanel->ImagePtr = ContentRegistry::CreateContent(path); - fadePanel->ImagePtr->SetImageColor({0, 0, 0, 255}); - fadePanel->SetVisible(true); - fadePanel->Bounds = RectangleF{0, 0, (float)graphics->LogicalWidth(), (float)graphics->LogicalHeight()}; - fadePanel->Offset.X = 0; - fadePanel->Offset.Y = 0; + fadeImage->ImagePtr = ContentRegistry::CreateContent(path); + fadeImage->ImagePtr->SetImageColor({0, 0, 0, 255}); + fadeImage->SetVisible(true); + fadeImage->Bounds = RectangleF{0, 0, (float)graphics->LogicalWidth(), (float)graphics->LogicalHeight()}; + fadeImage->Offset.X = 0; + fadeImage->Offset.Y = 0; auto fadeInAnimator = std::make_shared("fadein"); auto fadeOutAnimator = std::make_shared("fadeout"); - auto fadeOutTween = new Tween(0, 255, 0.3, fadePanel->AlphaHandle(), Supergoon::Easings::Linear); - auto fadeInTween = new Tween(255, 0, 0.3, fadePanel->AlphaHandle(), Supergoon::Easings::Linear); - fadeInAnimator->AddUIObjectTween(fadeInTween, fadePanel.get()); - fadeOutAnimator->AddUIObjectTween(fadeOutTween, fadePanel.get()); - fadePanel->Animators.push_back(fadeInAnimator); - fadePanel->Animators.push_back(fadeOutAnimator); + auto fadeOutTween = new Tween(0, 255, 0.3, fadeImage->AlphaHandle(), Supergoon::Easings::Linear); + auto fadeInTween = new Tween(255, 0, 0.3, fadeImage->AlphaHandle(), Supergoon::Easings::Linear); + fadeInAnimator->AddUIObjectTween(fadeInTween, fadeImage); + fadeOutAnimator->AddUIObjectTween(fadeOutTween, fadeImage); + fadeImage->Animators.push_back(fadeInAnimator); + fadeImage->Animators.push_back(fadeOutAnimator); _fadeOutAnimator = fadeOutAnimator.get(); _fadeInAnimator = fadeInAnimator.get(); - rootPanel->Children[name] = fadePanel; + // rootPanel->Children[name] = fadePanel; return rootPanel; } @@ -80,7 +80,7 @@ void UI::LoadUIFromFile(std::string filename, Panel* parentPanel) { for (auto& jsonChild : j) { if (jsonChild["type"] == "Image") { auto name = jsonChild["name"].get(); - auto child = std::make_shared(parentPanel, jsonChild); + auto child = std::make_shared(parentPanel, jsonChild); parentPanel->Children[name] = child; } } diff --git a/src/engine/src/UI/UIImage.cpp b/src/engine/src/UI/UIImage.cpp index 0050eab..92b0272 100644 --- a/src/engine/src/UI/UIImage.cpp +++ b/src/engine/src/UI/UIImage.cpp @@ -8,18 +8,19 @@ using namespace Supergoon; using json = nlohmann::json; -void ImageObject::OnDirty() { +void UIImage::OnDirty() { auto parentBoundsX = Parent ? Parent->Bounds.X : 0; auto parentBoundsY = Parent ? Parent->Bounds.Y : 0; Bounds.X = Offset.X + parentBoundsX; Bounds.Y = Offset.Y + parentBoundsY; ImagePtr->SetAlpha(EffectiveAlpha()); } -ImageObject::ImageObject(Panel* parent) : UIObject(parent) { +UIImage::UIImage(Panel* parent, std::string id) : UIObject(parent) { + parent->Children[id] = std::shared_ptr(this); WidgetType = (int)BuiltinWidgetTypes::Image; } -ImageObject::ImageObject(Panel* parent, json& imageJson) : UIObject(parent) { +UIImage::UIImage(Panel* parent, json& imageJson) : UIObject(parent) { WidgetType = (int)BuiltinWidgetTypes::Image; _visible = imageJson["visible"].get(); _alpha = imageJson["alpha"].get(); @@ -46,6 +47,6 @@ ImageObject::ImageObject(Panel* parent, json& imageJson) : UIObject(parent) { Bounds.H = destinationData["height"].get(); }; -void ImageObject::Draw() { +void UIImage::Draw() { ImagePtr->Draw(ImageSourceRect, Bounds); } diff --git a/src/engine/src/UI/UIText.cpp b/src/engine/src/UI/UIText.cpp index cda1fec..36d369d 100644 --- a/src/engine/src/UI/UIText.cpp +++ b/src/engine/src/UI/UIText.cpp @@ -2,7 +2,9 @@ #include using namespace Supergoon; -UIText::UIText(Panel* parent, std::string text) : UIObject(parent), DisplayText(text) { +UIText::UIText(Panel* parent, std::string text, std::string uiName) : UIObject(parent), DisplayText(text) { + auto uiname = uiName.empty() ? uiName : text; + parent->Children[uiName] = std::shared_ptr(this); WidgetType = (int)BuiltinWidgetTypes::Text; TextPtr = ContentRegistry::CreateContent(text, "commodore", 16); // Start the bounds to be the size of textptr, for ease of use. diff --git a/src/engine/src/Widgets/UIWidget.cpp b/src/engine/src/Widgets/UIWidget.cpp index a1909ec..bbd7219 100644 --- a/src/engine/src/Widgets/UIWidget.cpp +++ b/src/engine/src/Widgets/UIWidget.cpp @@ -32,7 +32,7 @@ void UIWidget::DrawPanel(Panel *panel, std::string panelName) { assert((Panel *)value.get()); DrawPanel((Panel *)value.get(), key); } else if (value->WidgetType == (int)BuiltinWidgetTypes::Image) { - auto imageObject = std::dynamic_pointer_cast(value); + auto imageObject = std::dynamic_pointer_cast(value); std::string childX_label = "Offset X##" + key; std::string childY_label = "Offset Y##" + key; std::string childWLabel = "Child W##" + key; diff --git a/src/game/BlackjackGame.cpp b/src/game/BlackjackGame.cpp index b7c3811..9479abc 100644 --- a/src/game/BlackjackGame.cpp +++ b/src/game/BlackjackGame.cpp @@ -32,6 +32,7 @@ static void loadLevel() { StartPlayers(); // Check if we should show the text at top auto display = Level::GetCurrentLevelProperty("display"); + auto ui = UI::UIInstance; auto textPanel = std::dynamic_pointer_cast(UI::UIInstance->Children["textTesting"]); assert(textPanel); if (display) { @@ -62,12 +63,12 @@ class BlackjackGame : public Game { // TODO this shouldn't be here static void setupUINameChangeBox() { auto ui = UI::UIInstance; - auto textPanel = std::make_shared(ui); + auto textPanel = new Panel(ui, "textTesting"); textPanel->Offset = {145, 15}; - auto text = std::make_shared(textPanel.get(), "Hello world!"); + auto text = new UIText(textPanel, "Hello world!", "textman"); text->Offset = {0, 13}; - textPanel->Children["textman"] = text; - ui->Children["textTesting"] = textPanel; + // textPanel->Children["textman"] = text; + // ui->Children["textTesting"] = textPanel; // Test creating the uitextbox // First, lets load in the picture for uiimage so that we can draw from it to the new one auto path = std::string(SDL_GetBasePath()) + "assets/img/uibase.png"; @@ -131,13 +132,13 @@ static void setupUINameChangeBox() { } // Add this image to the ui panel - auto textBoxUIImage = std::make_shared(textPanel.get()); + auto textBoxUIImage = new UIImage(textPanel, "textBoxImage"); textBoxUIImage->ImagePtr = textBoxImage; textBoxUIImage->SetVisible(true); textBoxUIImage->Bounds = RectangleF{0, 0, (float)textBoxImage->Width(), (float)textBoxImage->Height()}; textBoxUIImage->Offset.X = 0; textBoxUIImage->Offset.Y = 0; - textPanel->Children["uitextbox"] = textBoxUIImage; + // textPanel->Children["uitextbox"] = textBoxUIImage; // textPanel->Visible = true; // Setup the animators auto animator = std::make_shared("levelDisplayAnimator"); @@ -147,16 +148,16 @@ static void setupUINameChangeBox() { textPanel->SetVisible(false); textPanel->SetAlpha(255); }; - animator->AddUIObjectTween(waitTween, textPanel.get()); - animator->AddUIObjectTween(fadeOutTween, textPanel.get()); + animator->AddUIObjectTween(waitTween, textPanel); + animator->AddUIObjectTween(fadeOutTween, textPanel); textPanel->Animators.push_back(animator); } static void playLogos() { UI::LoadUIFromFile("logos"); auto ui = UI::UIInstance; - auto thing = (ImageObject *)ui->Children["logoImage"].get(); - auto thing2 = (ImageObject *)ui->Children["logoImage2"].get(); + auto thing = (UIImage *)ui->Children["logoImage"].get(); + auto thing2 = (UIImage *)ui->Children["logoImage2"].get(); auto animator = new UIObjectAnimatorBase("logo"); auto animator2 = new UIObjectAnimatorBase("logo2"); auto fadeInTween = new Tween(0, 255, 3.0, thing->AlphaHandle(), Supergoon::Easings::Linear);