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;
}
};
}

View File

@ -1,39 +0,0 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "prefab/SceneItemPrefab.hpp"
#include "scene/components/Components.hpp"
#include "display/mesh/CubeMesh.hpp"
namespace Dawn {
class SimpleCubePrefab : public SceneItemPrefab<SimpleCubePrefab> {
public:
MeshRenderer *renderer = nullptr;
MeshHost *host = nullptr;
Material *material = nullptr;
static std::vector<Asset*> prefabAssets() {
return std::vector<Asset*>();
}
SimpleCubePrefab(Scene *scene, sceneitemid_t id) :
SceneItemPrefab(scene, id)
{
}
void prefabInit() override {
this->renderer = this->addComponent<MeshRenderer>();
this->host = this->addComponent<MeshHost>();
this->material = this->addComponent<Material>();
this->host->mesh.createBuffers(CUBE_VERTICE_COUNT, CUBE_INDICE_COUNT);
CubeMesh::buffer(&this->host->mesh, glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec3(1, 1, 1), 0, 0);
std::cout << "Preab Init" << std::endl;
}
};
}

View File

@ -10,7 +10,7 @@
using namespace Dawn;
UICanvas * UICanvas::createCanvas(Scene *scene) {
UICanvas * UICanvas::create(Scene *scene) {
auto item = scene->createSceneItem();
return item->addComponent<UICanvas>();
}

View File

@ -32,7 +32,7 @@ namespace Dawn {
* @param scene Scene to create the UI Canvas for.
* @return Created UI Canvas.
*/
static UICanvas * createCanvas(Scene *scene);
static UICanvas * create(Scene *scene);
//
std::vector<UIComponent*> children;

View File

@ -19,4 +19,15 @@ namespace Dawn {
++it;
}
}
template<typename T>
void vectorAppend(std::vector<T> *list, std::vector<T> append) {
assertNotNull(list);
auto it = append.begin();
while(it != append.end()) {
list->push_back(*it);
++it;
}
}
}

View File

@ -22,7 +22,6 @@ void VisualNovelManager::onStart() {
this->fader = this->uiCanvas->findElement<VisualNovelFader>();
assertNotNull(this->textBox);
assertNotNull(this->fader);
this->getScene()->eventSceneUnpausedUpdate.addListener(this, &VisualNovelManager::onUnpausedUpdate);

View File

@ -9,7 +9,7 @@ using namespace Dawn;
SimpleVisualNovelBackground * SimpleVisualNovelBackground::create(Scene *s) {
auto item = s->createSceneItem();
item->addComponent<MeshRenderer>();
// item->addComponent<MeshRenderer>();
item->addComponent<MeshHost>();
item->addComponent<Material>();
auto background = item->addComponent<SimpleVisualNovelBackground>();

View File

@ -14,23 +14,26 @@ SimpleVNScene::SimpleVNScene(DawnGame *game) : Scene(game) {
std::vector<Asset*> SimpleVNScene::getRequiredAssets() {
auto assMan = &this->game->assetManager;
// return PokerGameTextbox::getAssets(assMan);
std::vector<Asset*> assets;
vectorAppend(&assets, VisualNovelTextboxPrefab::getRequiredAssets(assMan));
return std::vector<Asset*>();
}
void SimpleVNScene::stage() {
auto assMan = &this->game->assetManager;
// Camera
this->camera = Camera::create(this);
this->camera->transform->lookAtPixelPerfect(
glm::vec3(0, 0, 0),
glm::vec3(0, 0, 0),
1.0f,
this->camera->getRenderTarget()->getHeight(),
camera->fov
);
// UI
this->canvas = UICanvas::createCanvas(this);
// this->textbox = Prefab::create<VisualNovelTextboxPrefab>(this)->getComponent<VisualNovelTextbox>();
this->canvas = UICanvas::create(this);
this->textbox = VisualNovelTextboxPrefab::create(this->canvas);
this->background = SimpleVisualNovelBackground::create(this);
// VN Manager

View File

@ -13,6 +13,7 @@
#include "visualnovel/events/VisualNovelPauseEvent.hpp"
#include "visualnovel/events/VisualNovelFadeEvent.hpp"
#include "visualnovel/events/VisualNovelChangeSimpleBackgroundEvent.hpp"
#include "prefabs/ui/VisualNovelTextboxPrefab.hpp"
namespace Dawn {
class SimpleVNScene : public Scene {