This commit is contained in:
2023-01-06 21:01:06 -08:00
parent d13984dddc
commit d6b7895cab
21 changed files with 190 additions and 190 deletions

View File

@ -8,26 +8,28 @@
#include "scene/Scene.hpp"
namespace Dawn {
template<class T>
template<class T, typename J, class P = T>
class Prefab {
public:
/**
* Returns the list of assets required for this prefab.
*
* @param man Asset Manasger for getting required assets from.
* @return List of required assets this prefab.
*/
static std::vector<Asset*> getRequiredAssets() {
return T::prefabAssets();
static std::vector<Asset*> getRequiredAssets(AssetManager *man) {
return P::prefabAssets(man);
}
/**
* Creates a new instance of this asset.
* Creates a new instance of this prefabricated asset.
*
* @param scene Scene this item belongs to.
* @param context Custom context that this prefab needs to initialize.
* @return The instance of the created prefab.
*/
static T * create(Scene *scene, UICanvas *canvas) {
return T::prefabCreate(scene, canvas);
static T * create(J *context) {
assertNotNull(context);
return P::prefabCreate(context);
}
};
}

View File

@ -11,17 +11,27 @@ namespace Dawn {
template<class T>
class SceneItemPrefab :
public SceneItem,
public Prefab<T>
public Prefab<T, Scene>
{
protected:
public:
static T * prefabCreate(Scene *scene, UICanvas *canvas) {
/**
* Creates an instance of this prefab for the given scene.
*
* @param scene Scene that this prefab is going to be added to.
* @return The created prefab instance.
*/
static T * prefabCreate(Scene *scene) {
T *item = scene->createSceneItemOfType<T>();
item->prefabInit();
item->prefabInit(&scene->game->assetManager);
return item;
}
/**
* Constructor for this SceneItemPrefab.
*
* @param scene Scene that this prefab belongs to.
* @param id ID of this scene item.
*/
SceneItemPrefab(Scene *scene, sceneitemid_t id) :
SceneItem(scene, id)
{
@ -32,6 +42,6 @@ namespace Dawn {
* Virtual method called by the subclass for initialization after the
* prefab has been created.
*/
virtual void prefabInit() = 0;
virtual void prefabInit(AssetManager *man) = 0;
};
}

View File

@ -5,19 +5,23 @@
#pragma once
#include "Prefab.hpp"
#include "ui/UIComponent.hpp"
namespace Dawn {
template<class T>
template<class T, class P>
class UIPrefab :
public Prefab<T>
public Prefab<T, UICanvas, P>
{
public:
static T * prefabCreate(Scene *scene) {
}
static void apply(T *item) {
assertNotNull(item);
P::prefabApply(&item->getGame()->assetManager , item);
}
static T * prefabCreate(UICanvas *canvas) {
T * item = canvas->addElement<T>();
P::apply(item);
return item;
}
};
}