Lots of UI Component Updates

This commit is contained in:
2022-10-27 08:16:55 -07:00
parent 57b3354d4c
commit 942de96e16
25 changed files with 387 additions and 56 deletions

View File

@ -16,7 +16,42 @@ SceneItem::SceneItem(Scene &scene, sceneitemid_t id) :
}
void SceneItem::init() {
// Keep checking all components until they have all inited
int32_t waitingOn;
do {
waitingOn = 0;
// For each component
auto it = this->components.begin();
while(it != this->components.end()) {
// Has this component already inited?
if((*it)->hasInitialized) {
++it;
continue;
}
// For each dependency.
auto deps = (*it)->getDependencies();
bool_t waiting = false;
auto it2 = deps.begin();
while(it2 != deps.end()) {
// Has the dep not yet inited?
if(!(*it2)->hasInitialized) {
waiting = true;
break;
}
++it2;
}
// Are we waiting for a dep?
if(waiting) {
waitingOn++;
} else {
(*it)->init();
}
++it;
}
} while(waitingOn != 0);
}
SceneItem::~SceneItem() {

View File

@ -6,6 +6,7 @@
#pragma once
#include "SceneItemComponent.hpp"
#include "display/Transform.hpp"
#include "event/Event.hpp"
namespace Dawn {
typedef int32_t sceneitemid_t;

View File

@ -16,6 +16,15 @@ SceneItemComponent::SceneItemComponent(SceneItem &item) :
{
}
void SceneItemComponent::init() {
this->onStart();
this->hasInitialized = true;
}
std::vector<SceneItemComponent*> SceneItemComponent::getDependencies() {
return std::vector<SceneItemComponent*>();
}
Scene & SceneItemComponent::getScene() {
return this->item.scene;
}
@ -24,6 +33,10 @@ DawnGame & SceneItemComponent::getGame() {
return this->item.scene.game;
}
void SceneItemComponent::onStart() {
}
SceneItemComponent::~SceneItemComponent() {
}

View File

@ -16,6 +16,7 @@ namespace Dawn {
public:
SceneItem &item;
Transform &transform;
bool_t hasInitialized = false;
/**
* Constructs a new SceneItemComponent. Components are attached to
@ -28,9 +29,17 @@ namespace Dawn {
/**
* Requested on the first frame that the parent scene item has become
* active.
* active, after all of my dependencies are ready.
*/
virtual void start() = 0;
void init();
/**
* Optionally return all dependencies for this component to wait for init
* for before the component will be initialized.
*
* @return Array of dependencies.
*/
virtual std::vector<SceneItemComponent*> getDependencies();
/**
* Shorthand to return the scene that this component's item belongs to.
@ -44,6 +53,11 @@ namespace Dawn {
*/
DawnGame & getGame();
/**
* Same as init, but intended for your subclass to override.
*/
virtual void onStart();
/**
* Cleanup the SceneItemComponent.
*/

View File

@ -65,7 +65,7 @@ float_t Camera::getAspect() {
return target.getWidth() / target.getHeight();
}
void Camera::start() {
void Camera::onStart() {
this->updateProjection();
}

View File

@ -73,7 +73,7 @@ namespace Dawn {
/**
* Event triggered by the scene item when the item is added to the scene.
*/
void start() override;
void onStart() override;
/**
* Disposes a previously initialized camera.

View File

@ -65,7 +65,4 @@ std::shared_ptr<Shader> Material::getShader() {
void Material::setShader(std::shared_ptr<Shader> shader) {
this->shader = shader;
this->updateShaderParameters();
}
void Material::start() {
}

View File

@ -58,10 +58,5 @@ namespace Dawn {
* This method assumes that the shader has already been bound.
*/
void setShaderParameters();
/**
* Overrides the default SceneItemComponent start method.
*/
void start() override;
};
}

View File

@ -11,8 +11,4 @@ MeshRenderer::MeshRenderer(SceneItem &item) :
SceneItemComponent(item)
{
}
void MeshRenderer::start() {
}

View File

@ -18,10 +18,5 @@ namespace Dawn {
* @param item Scene Item this mesh renderer belongs to.
*/
MeshRenderer(SceneItem &item);
/**
* Overrides the default SceneItemComponent start method.
*/
void start() override;
};
}

View File

@ -16,7 +16,6 @@ std::shared_ptr<UICanvas> UICanvas::createCanvas(std::shared_ptr<Scene> scene) {
}
UICanvas::UICanvas(SceneItem &item) : SceneItemComponent(item) {
}
float_t UICanvas::getWidth() {
@ -27,6 +26,27 @@ float_t UICanvas::getHeight() {
return this->getGame().renderManager.getBackBuffer().getHeight();
}
void UICanvas::start() {
void UICanvas::onStart() {
std::cout << "Canvas event" << std::endl;
this->getGame().renderManager.getBackBuffer()
.eventRenderTargetResized.addListener(this, &UICanvas::onBackBufferResize)
;
}
void UICanvas::onBackBufferResize(
RenderTarget &target,
float_t width,
float_t height
) {
auto it = this->children.begin();
while(it != this->children.end()) {
(*it)->updatePositions();
++it;
}
}
UICanvas::~UICanvas() {
this->getGame().renderManager.getBackBuffer()
.eventRenderTargetResized.removeListener(this, &UICanvas::onBackBufferResize)
;
}

View File

@ -5,6 +5,7 @@
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
namespace Dawn {
enum UIDrawType {
@ -16,6 +17,13 @@ namespace Dawn {
class UIComponent;
class UICanvas : public SceneItemComponent {
protected:
void onBackBufferResize(
RenderTarget &target,
float_t width,
float_t height
);
public:
static std::shared_ptr<UICanvas> createCanvas(
std::shared_ptr<Scene> scene
@ -37,6 +45,8 @@ namespace Dawn {
float_t getWidth();
float_t getHeight();
void start() override;
void onStart() override;
~UICanvas();
};
}