Skip to content

Commit

Permalink
refactor some of ui, and fix font bug when unloading and then trying …
Browse files Browse the repository at this point in the history
…to delete again
  • Loading branch information
kjblanchard committed Nov 9, 2024
1 parent d672569 commit 8fee773
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/engine/include/Supergoon/UI/Panel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class UIWidget;
class Panel : public UIObject {
public:
Panel();
Panel(Panel* parent);
Panel(Panel* parent, std::string name);
std::unordered_map<std::string, std::shared_ptr<UIObject>> Children;

void OnDirty() override;
Expand Down
8 changes: 4 additions & 4 deletions src/engine/include/Supergoon/UI/UIImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include <SupergoonEngine/nlohmann/json_fwd.hpp>
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<Image> ImagePtr = nullptr;
virtual void Draw() override;
virtual void OnDirty() override;
Expand Down
2 changes: 1 addition & 1 deletion src/engine/include/Supergoon/UI/UIText.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion src/engine/src/Content/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/engine/src/UI/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIObject>(this);
}
void Panel::OnDirty() {
auto parentBoundsX = Parent ? Parent->Bounds.X : 0;
Expand Down
34 changes: 17 additions & 17 deletions src/engine/src/UI/UI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageObject>(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<Image>(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<Image>(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<UIObjectAnimatorBase>("fadein");
auto fadeOutAnimator = std::make_shared<UIObjectAnimatorBase>("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;
}

Expand Down Expand Up @@ -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<std::string>();
auto child = std::make_shared<ImageObject>(parentPanel, jsonChild);
auto child = std::make_shared<UIImage>(parentPanel, jsonChild);
parentPanel->Children[name] = child;
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/engine/src/UI/UIImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UIObject>(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<bool>();
_alpha = imageJson["alpha"].get<float>();
Expand All @@ -46,6 +47,6 @@ ImageObject::ImageObject(Panel* parent, json& imageJson) : UIObject(parent) {
Bounds.H = destinationData["height"].get<float>();
};

void ImageObject::Draw() {
void UIImage::Draw() {
ImagePtr->Draw(ImageSourceRect, Bounds);
}
4 changes: 3 additions & 1 deletion src/engine/src/UI/UIText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
#include <Supergoon/UI/UIText.hpp>
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<UIObject>(this);
WidgetType = (int)BuiltinWidgetTypes::Text;
TextPtr = ContentRegistry::CreateContent<Text, std::string, int>(text, "commodore", 16);
// Start the bounds to be the size of textptr, for ease of use.
Expand Down
2 changes: 1 addition & 1 deletion src/engine/src/Widgets/UIWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImageObject>(value);
auto imageObject = std::dynamic_pointer_cast<UIImage>(value);
std::string childX_label = "Offset X##" + key;
std::string childY_label = "Offset Y##" + key;
std::string childWLabel = "Child W##" + key;
Expand Down
21 changes: 11 additions & 10 deletions src/game/BlackjackGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static void loadLevel() {
StartPlayers();
// Check if we should show the text at top
auto display = Level::GetCurrentLevelProperty<std::string>("display");
auto ui = UI::UIInstance;
auto textPanel = std::dynamic_pointer_cast<Panel>(UI::UIInstance->Children["textTesting"]);
assert(textPanel);
if (display) {
Expand Down Expand Up @@ -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<Panel>(ui);
auto textPanel = new Panel(ui, "textTesting");
textPanel->Offset = {145, 15};
auto text = std::make_shared<UIText>(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";
Expand Down Expand Up @@ -131,13 +132,13 @@ static void setupUINameChangeBox() {
}

// Add this image to the ui panel
auto textBoxUIImage = std::make_shared<ImageObject>(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<UIObjectAnimatorBase>("levelDisplayAnimator");
Expand All @@ -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);
Expand Down

0 comments on commit 8fee773

Please sign in to comment.