Skip to content

Commit

Permalink
Work on destroying elements
Browse files Browse the repository at this point in the history
  • Loading branch information
JustDoom committed Feb 9, 2025
1 parent 786a5ce commit a7e451c
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/sdl/ui/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
target_sources(8ChocChip PRIVATE
ChildHolderElement.cpp
ChildHolderElement.h
Element.cpp
Element.h
ScrollBox.cpp
ScrollBox.h
Expand Down
21 changes: 21 additions & 0 deletions src/sdl/ui/ChildHolderElement.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "ChildHolderElement.h"

#include <algorithm>

void ChildHolderElement::setElements(std::vector<std::shared_ptr<Element>> elements) {
this->elements.clear();
this->elements = std::move(elements);
}

void ChildHolderElement::removeElement(Element* element) {
this->elements.erase(
std::remove_if(
this->elements.begin(),
this->elements.end(),
[element](const std::shared_ptr<Element>& e) {
return e.get() == element;
}
),
this->elements.end()
);
}
19 changes: 19 additions & 0 deletions src/sdl/ui/ChildHolderElement.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CHILD_HOLDER_ELEMENT_H
#define CHILD_HOLDER_ELEMENT_H

#include <memory>
#include <vector>

#include "Element.h"

class ChildHolderElement : public Element {
protected:
std::vector<std::shared_ptr<Element>> elements;
public:
explicit ChildHolderElement(const std::weak_ptr<ChildHolderElement>& parent) : Element(parent) {}

void setElements(std::vector<std::shared_ptr<Element>> elements);
void removeElement(Element* element);
};

#endif
11 changes: 11 additions & 0 deletions src/sdl/ui/Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "Element.h"

#include "ChildHolderElement.h"

void Element::destroy() {
if (const auto lock = this->parent.lock()) {
lock->removeElement(this);
} else {
std::cerr << "No" << std::endl;
}
}
15 changes: 13 additions & 2 deletions src/sdl/ui/Element.h
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
#ifndef ELEMENT_H
#define ELEMENT_H

#include <iostream>
#include <memory>
#include <ostream>

#include <SDL3/SDL.h>

class ChildHolderElement;
#include "../InputHandler.h"

class Element {
protected:
std::weak_ptr<Element> parent;
std::weak_ptr<ChildHolderElement> parent;
public:
explicit Element();
explicit Element(const std::weak_ptr<Element>& parent) : parent(parent) {}
explicit Element(const std::weak_ptr<ChildHolderElement>& parent) : parent(parent) {}
virtual ~Element() = default;
virtual void update(InputHandler& inputHandler) = 0;
virtual void draw(SDL_Renderer* window) = 0;
virtual bool handleEvent(SDL_Event &event) = 0;

void destroy();

virtual float getX() = 0;
virtual float getY() = 0;
virtual float getRealX() = 0; // Without any possible extra processing
virtual float getRealY() = 0;
virtual float getWidth() = 0;
virtual float getHeight() = 0;
virtual void setX(float x) = 0;
virtual void setY(float y) = 0;

virtual bool isPointInsideArea(SDL_FPoint& point) = 0;

bool operator==(const Element& other) const {
return parent.lock() == other.parent.lock() && this == &other;
}
};

#endif
24 changes: 16 additions & 8 deletions src/sdl/ui/ScrollBox.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "ScrollBox.h"

#include <utility>

void ScrollBox::draw(SDL_Renderer* window) {
SDL_SetRenderDrawColor(window, 200, 200, 200, 255);
SDL_RenderFillRect(window, &this->box);
Expand All @@ -19,9 +17,16 @@ void ScrollBox::draw(SDL_Renderer* window) {
}

void ScrollBox::update(InputHandler& inputHandler) {
for (const std::shared_ptr<Element>& element : this->elements) {
const int size = this->elements.size();
for (const std::shared_ptr<Element>& element : std::vector(this->elements)) {
element->update(inputHandler);
}
if (size != this->elements.size()) {
int count = 0;
for (const std::shared_ptr<Element>& element : this->elements) {
element->setY(25.0f * count++);
}
}
}

bool ScrollBox::handleEvent(SDL_Event& event) {
Expand Down Expand Up @@ -74,11 +79,14 @@ float ScrollBox::getHeight() {
return this->box.h;
}

bool ScrollBox::isPointInsideArea(SDL_FPoint &point) {
return SDL_PointInRectFloat(&point, &this->box);
void ScrollBox::setX(float x) {
this->box.x = x;
}

void ScrollBox::setElements(std::vector<std::shared_ptr<Element>> elements) {
this->elements.clear();
this->elements = std::move(elements);
void ScrollBox::setY(float y) {
this->box.y = y;
}

bool ScrollBox::isPointInsideArea(SDL_FPoint &point) {
return SDL_PointInRectFloat(&point, &this->box);
}
19 changes: 8 additions & 11 deletions src/sdl/ui/ScrollBox.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
#ifndef SCROLLBOX_H
#define SCROLLBOX_H

#include "Element.h"

#include <memory>
#include <utility>
#include <vector>

class ScrollBox : public Element {
#include "ChildHolderElement.h"

class ScrollBox : public ChildHolderElement {
private:
std::vector<std::shared_ptr<Element>> elements;
SDL_FRect box;
float scrollPosition = 0;
float scrollSpeed = 20;
public:
ScrollBox(float x, float y, float width, float height) : ScrollBox(x, y, width, height, std::weak_ptr<Element>()) {}
ScrollBox(float x, float y, float width, float height, const std::weak_ptr<Element>& parent) :
Element(parent), elements(std::move(elements)), box({x, y, width, height}) {}
ScrollBox(float x, float y, float width, float height) : ScrollBox(x, y, width, height, std::weak_ptr<ChildHolderElement>()) {}
ScrollBox(float x, float y, float width, float height, const std::weak_ptr<ChildHolderElement>& parent) :
ChildHolderElement(parent), box({x, y, width, height}) {}

void draw(SDL_Renderer *window) override;
void update(InputHandler &inputHandler) override;
Expand All @@ -28,10 +25,10 @@ class ScrollBox : public Element {
float getRealY() override;
float getWidth() override;
float getHeight() override;
void setX(float x) override;
void setY(float y) override;

bool isPointInsideArea(SDL_FPoint& point) override;

void setElements(std::vector<std::shared_ptr<Element>> elements);
};

#endif
33 changes: 23 additions & 10 deletions src/sdl/ui/TextButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <iostream>

TextButton::TextButton(const float x, const float y, const float width, const float height, TTF_Text *text, const std::weak_ptr<Element>& parent, SDL_Color idleColour) :
TextButton::TextButton(const float x, const float y, const float width, const float height, TTF_Text *text, const std::weak_ptr<ChildHolderElement>& parent, SDL_Color idleColour) :
Element(parent), text(text), idleColor(idleColour),
hoverColor(SDL_Color{128, 128, 128, 255}), activeColor(SDL_Color{64, 64, 64, 255}),
deleteColor(SDL_Color{255, 0, 0, 255}) {
Expand Down Expand Up @@ -48,16 +48,21 @@ void TextButton::update(InputHandler& inputHandler) {
this->isHovered = this->isHovered && lock->isPointInsideArea(pos);
}

if (this->isHovered && inputHandler.isJustClicked(SDL_BUTTON_LEFT)) {
this->isPressed = true;
updateColour(this->activeColor);
if (this->onClick) {
this->onClick();
if (this->isHovered) {
if (inputHandler.isPressed(SDL_SCANCODE_LSHIFT)) {
updateColour(this->deleteColor);
if (inputHandler.isJustClicked(SDL_BUTTON_LEFT)) {
destroy();
}
} else if (inputHandler.isJustClicked(SDL_BUTTON_LEFT)) {
this->isPressed = true;
updateColour(this->activeColor);
if (this->onClick) {
this->onClick();
}
} else {
updateColour(this->hoverColor);
}
} else if (this->isHovered && inputHandler.isPressed(SDL_SCANCODE_LSHIFT)) {
updateColour(this->deleteColor);
} else if (this->isHovered) {
updateColour(this->hoverColor);
} else {
this->isPressed = false;
updateColour(this->idleColor);
Expand Down Expand Up @@ -120,6 +125,14 @@ float TextButton::getHeight() {
return this->originalButton.h;
}

void TextButton::setX(float x) {
this->originalButton.x = x;
}

void TextButton::setY(float y) {
this->originalButton.y = y;
}

bool TextButton::isPointInsideArea(SDL_FPoint& point) {
return SDL_PointInRectFloat(&point, &this->renderButton);
}
Expand Down
12 changes: 7 additions & 5 deletions src/sdl/ui/TextButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


#include "../InputHandler.h"
#include "Element.h"
#include "ChildHolderElement.h"

#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
Expand All @@ -31,10 +31,10 @@ class TextButton : public Element {
std::function<void()> onClick;

public:
TextButton(float x, float y, float width, float height, TTF_Text* text) : TextButton(x, y, width, height, text, std::weak_ptr<Element>()) {}
TextButton(float x, float y, float width, float height, TTF_Text* text, const std::weak_ptr<Element>& parent) : TextButton(x, y, width, height, text, parent, SDL_Color{192, 192, 192, 255}) {}
TextButton(float x, float y, float width, float height, TTF_Text* text, SDL_Color idleColour) : TextButton(x, y, width, height, text, std::weak_ptr<Element>(), idleColour) {}
TextButton(float x, float y, float width, float height, TTF_Text* text, const std::weak_ptr<Element>& parent, SDL_Color idleColour);
TextButton(float x, float y, float width, float height, TTF_Text* text) : TextButton(x, y, width, height, text, std::weak_ptr<ChildHolderElement>()) {}
TextButton(float x, float y, float width, float height, TTF_Text* text, const std::weak_ptr<ChildHolderElement>& parent) : TextButton(x, y, width, height, text, parent, SDL_Color{192, 192, 192, 255}) {}
TextButton(float x, float y, float width, float height, TTF_Text* text, SDL_Color idleColour) : TextButton(x, y, width, height, text, std::weak_ptr<ChildHolderElement>(), idleColour) {}
TextButton(float x, float y, float width, float height, TTF_Text* text, const std::weak_ptr<ChildHolderElement>& parent, SDL_Color idleColour);

void updateSize(const SDL_Point & originalSize, const SDL_Point & updatedSize);

Expand All @@ -59,6 +59,8 @@ class TextButton : public Element {
float getRealY() override;
float getWidth() override;
float getHeight() override;
void setX(float x) override;
void setY(float y) override;

bool isPointInsideArea(SDL_FPoint& point) override;

Expand Down

0 comments on commit a7e451c

Please sign in to comment.