Updated the display controllers to use state controls

This commit is contained in:
2023-03-01 08:43:59 -08:00
parent 2121124f79
commit 3a4c87fc37
7 changed files with 79 additions and 137 deletions

View File

@ -19,19 +19,14 @@ void AnimationController::addAnimation(Animation *animation) {
this->animations.push_back(animation);
}
void AnimationController::onSceneUpdate() {
auto it = this->animations.begin();
while(it != this->animations.end()) {
(*it)->tick(this->getGame()->timeManager.delta);
++it;
}
}
void AnimationController::onStart() {
SceneItemComponent::onStart();
getScene()->eventSceneUnpausedUpdate.addListener(this, &AnimationController::onSceneUpdate);
}
void AnimationController::onDispose() {
getScene()->eventSceneUnpausedUpdate.removeListener(this, &AnimationController::onSceneUpdate);
useEventLegacy([&]{
auto it = this->animations.begin();
while(it != this->animations.end()) {
(*it)->tick(this->getGame()->timeManager.delta);
++it;
}
}, getScene()->eventSceneUnpausedUpdate);
}

View File

@ -1,24 +1,27 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/animation/Animation.hpp"
namespace Dawn {
class AnimationController : public SceneItemComponent {
private:
std::vector<Animation*> animations;
void onSceneUpdate();
public:
AnimationController(SceneItem *item);
void addAnimation(Animation *animation);
void onStart() override;
void onDispose() override;
};
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/animation/Animation.hpp"
namespace Dawn {
class AnimationController : public SceneItemComponent {
private:
std::vector<Animation*> animations;
public:
AnimationController(SceneItem *item);
/**
* Add an animation to this controller.
*
* @param animation Pointer to the animation to add.
*/
void addAnimation(Animation *animation);
void onStart() override;
};
}

View File

@ -77,59 +77,34 @@ glm::vec3 Camera::getRayDirectionFromScreenSpace(glm::vec2 screenSpace) {
}
void Camera::onStart() {
// Render Target
useEffect([&]{
if(renderTarget.previous != nullptr) {
renderTarget.previous->eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
}
this->projectionNeedsUpdating = true;
useEffectWithTeardown([&]{
auto rt = this->getRenderTarget();
rt->eventRenderTargetResized.addListener(
this, &Camera::onRenderTargetResize
);
this->eventRenderTargetResized.invoke(
rt->getWidth(), rt->getHeight()
);
this->projectionNeedsUpdating = true;
this->eventRenderTargetResized.invoke(rt->getWidth(), rt->getHeight());
this->event2RenderTargetResized.invoke(rt->getWidth(), rt->getHeight());
// Subscribe to new render target resized.
return evtResized = useEventLegacy([&](RenderTarget *rt, float_t w, float_t h){
this->projectionNeedsUpdating = true;
this->eventRenderTargetResized.invoke(w, h);
this->event2RenderTargetResized.invoke(w, h);
}, rt->eventRenderTargetResized);
}, renderTarget);
// All regular properties.
useEffect([&]{
this->projectionNeedsUpdating = true;
}, {
&fov,
&type,
&orthoLeft,
&orthoRight,
&orthoBottom,
&orthoTop,
&clipNear,
&clipFar
&fov, &type,
&orthoLeft, &orthoRight, &orthoBottom, &orthoTop,
&clipNear, &clipFar
});
// useEffect(fov, cbUpdateProj);
// useEffect(type, cbUpdateProj);
// useEffect(orthoLeft, cbUpdateProj);
// useEffect(orthoRight, cbUpdateProj);
// useEffect(orthoBottom, cbUpdateProj);
// useEffect(orthoTop, cbUpdateProj);
// useEffect(clipNear, cbUpdateProj);
// useEffect(clipFar, cbUpdateProj);
getRenderTarget()->eventRenderTargetResized.addListener(this, &Camera::onRenderTargetResize);
}
void Camera::onRenderTargetResize(RenderTarget *target, float_t w, float_t h) {
this->projectionNeedsUpdating = true;
this->eventRenderTargetResized.invoke(w, h);
this->event2RenderTargetResized.invoke(w, h);
}
void Camera::onDispose() {
this->getRenderTarget()->eventRenderTargetResized.removeListener(
this, &Camera::onRenderTargetResize
);
// Sub to evt legacy, we don't invoke the useTeardown to avoid invoking
// the state event for this camera when we don't need to.
evtResized = useEventLegacy([&](RenderTarget *rt, float_t w, float_t h){
this->projectionNeedsUpdating = true;
this->eventRenderTargetResized.invoke(w, h);
this->event2RenderTargetResized.invoke(w, h);
}, getRenderTarget()->eventRenderTargetResized);
}

View File

@ -19,8 +19,7 @@ namespace Dawn {
protected:
bool_t projectionNeedsUpdating = true;
glm::mat4 projection;
void onRenderTargetResize(RenderTarget *target, float_t w, float_t h);
std::function<void()> evtResized;
public:
static Camera * create(Scene *scene) {
@ -89,6 +88,5 @@ namespace Dawn {
* Event triggered by the scene item when the item is added to the scene.
*/
void onStart() override;
void onDispose() override;
};
}

View File

@ -13,23 +13,6 @@ SimpleRenderTargetQuad::SimpleRenderTargetQuad(SceneItem *i) :
{
}
void SimpleRenderTargetQuad::onRenderTargetResized(
RenderTarget *target, float_t w, float_t h
) {
assertTrue(target == this->renderTarget);
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(
((RenderTarget*)this->renderTarget)->getWidth(),
((RenderTarget*)this->renderTarget)->getHeight()
),
glm::vec2(1, 1),
0, 0
);
}
std::vector<SceneItemComponent*> SimpleRenderTargetQuad::getDependencies() {
return std::vector<SceneItemComponent*>{
(this->meshHost = this->item->getComponent<MeshHost>())
@ -42,14 +25,10 @@ void SimpleRenderTargetQuad::onStart() {
// Create quad mesh
this->meshHost->mesh.createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
useEffect([&]{
if(this->renderTarget.previous != nullptr) {
this->renderTarget.previous->eventRenderTargetResized.addListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
if(this->renderTarget == nullptr) return;
// Create teardown effect and keep track.
auto initial = useEffectWithTeardown([&]{
// No teardown needed.
if(this->renderTarget == nullptr) return evtResized = [&]{ };
// Update mesh
QuadMesh::bufferQuadMesh(
@ -63,30 +42,20 @@ void SimpleRenderTargetQuad::onStart() {
0, 0
);
((RenderTarget*)this->renderTarget)->eventRenderTargetResized.addListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
// Subscribe to resize event.
evtResized = useEventLegacy([&](RenderTarget *target, float_t w, float_t h){
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(w, h), glm::vec2(1, 1),
0, 0
);
}, this->renderTarget->eventRenderTargetResized);
// Teardown
return evtResized;
}, this->renderTarget);
// Perform first resize.
if(this->renderTarget != nullptr) {
QuadMesh::bufferQuadMesh(
&this->meshHost->mesh,
glm::vec2(0, 0), glm::vec2(0, 0),
glm::vec2(
((RenderTarget*)this->renderTarget)->getWidth(),
((RenderTarget*)this->renderTarget)->getHeight()
),
glm::vec2(1, 1),
0, 0
);
}
}
void SimpleRenderTargetQuad::onDispose() {
if(this->renderTarget != nullptr) {
((RenderTarget*)this->renderTarget)->eventRenderTargetResized.removeListener(
this, &SimpleRenderTargetQuad::onRenderTargetResized
);
}
// Invoke immediate
initial();
}

View File

@ -12,8 +12,7 @@ namespace Dawn {
class SimpleRenderTargetQuad : public SceneItemComponent {
protected:
MeshHost *meshHost = nullptr;
void onRenderTargetResized(RenderTarget *target, float_t w, float_t h);
std::function<void()> evtResized;
public:
StateProperty<RenderTarget*> renderTarget;
@ -29,6 +28,5 @@ namespace Dawn {
std::vector<SceneItemComponent*> getDependencies() override;
void onStart() override;
void onDispose() override;
};
}

View File

@ -58,7 +58,7 @@ namespace Dawn {
* Creates a new state property and listens for its change.
* @param initial The initial value of this state.
*/
StateProperty(V initial) : _realValue(initial) {}
StateProperty(V initial) : _realValue(initial), previous(initial) {}
const StateProperty& operator += (const V &value) {
this->setInternal(this->_realValue + value);
@ -78,6 +78,10 @@ namespace Dawn {
return *this;
}
const V operator->() const {
return this->_realValue;
}
operator V() const {
return this->_realValue;
}