Adding some components.
This commit is contained in:
@@ -3,15 +3,58 @@
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "assert/assert.hpp"
|
||||
#include "util/Flag.hpp"
|
||||
#include "SceneComponent.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void SceneComponent::init(const std::shared_ptr<SceneItem> item) {
|
||||
assertFlagOff(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT,
|
||||
"SceneComponent is already initialized!"
|
||||
);
|
||||
Flag::turnOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
);
|
||||
this->item = item;
|
||||
this->onInit();
|
||||
}
|
||||
|
||||
void SceneComponent::dispose() {
|
||||
assertFlagOn(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT,
|
||||
"SceneComponent is not initialized!"
|
||||
);
|
||||
assertFlagOff(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED,
|
||||
"SceneComponent is already disposed!"
|
||||
);
|
||||
Flag::turnOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED
|
||||
);
|
||||
this->onDispose();
|
||||
this->item.reset();
|
||||
}
|
||||
|
||||
std::shared_ptr<SceneItem> SceneComponent::getItem() {
|
||||
return this->item.lock();
|
||||
}
|
||||
|
||||
SceneComponent::~SceneComponent() {
|
||||
if(Flag::isOn<uint_fast8_t>(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_INIT
|
||||
)) {
|
||||
assertFlagOn(
|
||||
sceneComponentState,
|
||||
SCENE_COMPONENT_STATE_DISPOSED,
|
||||
"SceneComponent is initialized but was not properly disposed!"
|
||||
);
|
||||
}
|
||||
}
|
@@ -6,12 +6,16 @@
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
#define SCENE_COMPONENT_STATE_INIT 0x01
|
||||
#define SCENE_COMPONENT_STATE_DISPOSED 0x02
|
||||
|
||||
namespace Dawn {
|
||||
class SceneItem;
|
||||
|
||||
class SceneComponent : std::enable_shared_from_this<SceneComponent> {
|
||||
private:
|
||||
std::weak_ptr<SceneItem> item;
|
||||
uint_fast8_t sceneComponentState = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
@@ -20,6 +24,12 @@ namespace Dawn {
|
||||
*/
|
||||
virtual void onInit() = 0;
|
||||
|
||||
/**
|
||||
* Custom component listener that is invoked when the component is meant
|
||||
* to dispose.
|
||||
*/
|
||||
virtual void onDispose() = 0;
|
||||
|
||||
public:
|
||||
/**
|
||||
* Initializes this scene component.
|
||||
@@ -28,11 +38,21 @@ namespace Dawn {
|
||||
*/
|
||||
void init(const std::shared_ptr<SceneItem> item);
|
||||
|
||||
/**
|
||||
* Disposes this scene component.
|
||||
*/
|
||||
void dispose();
|
||||
|
||||
/**
|
||||
* Returns the scene item that this scene component belongs to.
|
||||
*
|
||||
* @return Reference to the scene item that this component belongs to.
|
||||
*/
|
||||
std::shared_ptr<SceneItem> getItem();
|
||||
|
||||
/**
|
||||
* Disposes this scene component.
|
||||
*/
|
||||
virtual ~SceneComponent();
|
||||
};
|
||||
}
|
@@ -24,5 +24,11 @@ std::shared_ptr<Scene> SceneItem::getScene() {
|
||||
}
|
||||
|
||||
SceneItem::~SceneItem() {
|
||||
|
||||
std::for_each(
|
||||
components.begin(),
|
||||
components.end(),
|
||||
[](auto &component) {
|
||||
component->dispose();
|
||||
}
|
||||
);
|
||||
}
|
@@ -16,6 +16,7 @@ void SceneItemComponents::removeComponent(
|
||||
) {
|
||||
auto it = std::find(components.begin(), components.end(), component);
|
||||
if(it == components.end()) return; //Not found?
|
||||
it->get()->dispose();
|
||||
components.erase(it);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user