-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
121 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters