Added basic prefabs finally

This commit is contained in:
2023-01-03 16:44:16 -08:00
parent 562e6644ef
commit 659ab3d760
29 changed files with 193 additions and 108 deletions

View File

@ -1,74 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "SceneItem.hpp"
#include "SceneItemComponent.hpp"
#include "scene/components/Components.hpp"
#include "util/array.hpp"
namespace Dawn {
class Prefab {
protected:
virtual std::vector<Asset*> prefabAssets(AssetManager *man) = 0;
virtual SceneItem * prefabItem(Scene *scene) = 0;
public:
/**
* Returns a list of assets that this prefab requires to be loaded for the
* instanciation to work.
*
* @param man Asset Manager to retreive the assets from.
* @return List of required assets, includes sibling/child assets.
*/
template<class T>
static std::vector<Asset*> getAssets(AssetManager *man) {
return T::prefabAssets(man);
}
/**
* Create a scene item from this prefab.
*
* @param scene Scene to add the item to.
* @return The created scene item for this prefab.
*/
template<class T>
static SceneItem * create(Scene *scene) {
return T::prefabItem(scene);
}
};
class UIPrefab : public Prefab {
protected:
/** @deprecated */
SceneItem * prefabItem(Scene *scene) override {
assertUnreachable();
}
virtual void prefabUIApply(T *existing) = 0;
public:
/**
* Applies a UI Prefab styling to an existing UI Element.
*
* @param existing Existing item to apply styling to.
*/
static virtual void uiApply(T *existing) {
V::prefabUIApply(existing);
}
/**
* Creates a UI Item from this prefab.
*
* @param canvas Canvas to create this item on to.
* @return Pointer to the created UI Item.
*/
static virtual T * uiCreate(UICanvas *canvas) {
auto item = canvas->addElement<T>();
V::uiApply(item);
return item;
}
}
}

View File

@ -33,11 +33,7 @@ void Scene::update() {
}
SceneItem * Scene::createSceneItem() {
sceneitemid_t id = this->nextId++;
auto item = new SceneItem(this, id);
assertNotNull(item);
this->itemsNotInitialized[id] = item;
return item;
return this->createSceneItemOfType<SceneItem>();
}
Scene::~Scene() {

View File

@ -35,6 +35,20 @@ namespace Dawn {
*/
void update();
/**
* Create a Scene Item object and add to the current scene.
*
* @return A shared pointer to the created SceneItem.
*/
template<class T>
T * createSceneItemOfType() {
sceneitemid_t id = this->nextId++;
auto item = new T(this, id);
assertNotNull(item);
this->itemsNotInitialized[id] = item;
return item;
}
/**
* Create a Scene Item object and add to the current scene.
*