This commit is contained in:
2023-04-21 23:30:33 -07:00
parent cacda495d3
commit 72a0bb9192
8 changed files with 157 additions and 38 deletions

View File

@ -32,7 +32,7 @@ namespace Dawn {
this->events.push_back(event);
return event;
}
/**
* Sets the currently active visual novel event. This is assumed to be
* the only way to handle events (no multiples currently).

View File

@ -0,0 +1,49 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "VNEvent.hpp"
#include "display/animation/SimplerCallbackAnimation.hpp"
namespace Dawn {
template<typename T>
class VNAnimateEvent : public VNEvent {
public:
T from;
T to;
float_t duration;
protected:
SimplerCallbackAnimation<T> animation;
void onStart() override {
if(duration > 0) {
animation.clear();
animation.addKeyframe(0, from);
animation.addKeyframe(duration, to);
animation.callback = [&](T v){
this->setValue(v);
};
// On-end
useEvent([&]() {
this->next();
}, animation.event2AnimationEnd);
useEvent([&](float_t delta) {
animation.tick(delta);
}, getScene()->eventSceneUpdate);
} else {
this->setValue(to);
this->next();
}
}
virtual void setValue(T value) = 0;
};
}

View File

@ -5,6 +5,7 @@
#pragma once
#include "games/vn/components/VNManager.hpp"
#include "scene/SceneItem.hpp"
namespace Dawn {
class VNEvent : public StateOwner {

View File

@ -4,20 +4,27 @@
// https://opensource.org/licenses/MIT
#pragma once
#include "VNEvent.hpp"
#include "scene/SceneItem.hpp"
#include "display/animation/SimplerCallbackAnimation.hpp"
#include "VNAnimateEvent.hpp"
#define VN_POSITION_EVENT_VALUE_UNCHANGED -123456789
#define VN_POSITION_EVENT_VALUE_UNCHANGED std::numeric_limits<float>::min()
namespace Dawn {
class VNPositionEvent : public VNEvent {
class VNPositionEvent : public VNAnimateEvent<glm::vec3> {
public:
float_t x = VN_POSITION_EVENT_VALUE_UNCHANGED;
float_t y = VN_POSITION_EVENT_VALUE_UNCHANGED;
float_t z = VN_POSITION_EVENT_VALUE_UNCHANGED;
SceneItem *item = nullptr;
float_t duration;
VNPositionEvent() {
from = glm::vec3(
VN_POSITION_EVENT_VALUE_UNCHANGED,
VN_POSITION_EVENT_VALUE_UNCHANGED,
VN_POSITION_EVENT_VALUE_UNCHANGED
);
to = glm::vec3(
VN_POSITION_EVENT_VALUE_UNCHANGED,
VN_POSITION_EVENT_VALUE_UNCHANGED,
VN_POSITION_EVENT_VALUE_UNCHANGED
);
}
protected:
SimplerCallbackAnimation<glm::vec3> animation;
@ -26,33 +33,19 @@ namespace Dawn {
assertNotNull(item);
auto start = item->transform.getLocalPosition();
auto destination = glm::vec3(
x == VN_POSITION_EVENT_VALUE_UNCHANGED ? start.x : x,
y == VN_POSITION_EVENT_VALUE_UNCHANGED ? start.y : y,
z == VN_POSITION_EVENT_VALUE_UNCHANGED ? start.z : z
);
if(from.x == VN_POSITION_EVENT_VALUE_UNCHANGED) start.x = from.x;
if(from.y == VN_POSITION_EVENT_VALUE_UNCHANGED) start.y = from.y;
if(from.z == VN_POSITION_EVENT_VALUE_UNCHANGED) start.z = from.z;
if(duration > 0) {
animation.clear();
if(to.x == VN_POSITION_EVENT_VALUE_UNCHANGED) to.x = start.x;
if(to.y == VN_POSITION_EVENT_VALUE_UNCHANGED) to.y = start.y;
if(to.z == VN_POSITION_EVENT_VALUE_UNCHANGED) to.z = start.z;
VNAnimateEvent::onStart();
}
animation.addKeyframe(0, start);
animation.addKeyframe(duration, destination);
animation.callback = [&](glm::vec3 v){
this->item->transform.setLocalPosition(v);
};
useEvent([&]() {
this->next();
}, animation.event2AnimationEnd);
useEvent([&](float_t delta) {
animation.tick(delta);
}, getScene()->eventSceneUpdate);
} else {
this->item->transform.setLocalPosition(destination);
this->next();
}
void setValue(glm::vec3 value) override {
this->item->transform.setLocalPosition(value);
}
};
}

View File

@ -0,0 +1,27 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "VNAnimateEvent.hpp"
namespace Dawn {
template<typename T>
class VNSetEvent : public VNAnimateEvent<T> {
public:
T *modifies = nullptr;
protected:
void onStart() override {
assertNotNull(this->modifies);
this->from = *modifies;
VNAnimateEvent<T>::onStart();
}
void setValue(T value) override {
*modifies = value;
}
};
}

View File

@ -0,0 +1,20 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "VNEvent.hpp"
namespace Dawn {
class VNTextEvent : public VNEvent {
public:
std::string text;
protected:
void onStart() override {
std::cout << "TEXT: " << text << std::endl;
this->next();
}
};
}

View File

@ -0,0 +1,18 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "VNAnimateEvent.hpp"
namespace Dawn {
class VNWaitEvent : public VNAnimateEvent<float_t> {
public:
protected:
void setValue(T value) override {
// Do nothing
}
};
}

View File

@ -9,7 +9,9 @@
#include "prefabs/SimpleSpinningCubePrefab.hpp"
#include "games/vn/components/VNManager.hpp"
#include "games/vn/events/VNDummyEvent.hpp"
#include "games/vn/events/VNTextEvent.hpp"
#include "games/vn/events/VNPositionEvent.hpp"
#include "games/vn/events/VNSetEvent.hpp"
namespace Dawn {
class HelloWorldScene : public Scene {
@ -17,6 +19,8 @@ namespace Dawn {
Camera *camera;
UICanvas *canvas;
int32_t test = 0;
void stage() override {
canvas = UICanvas::create(this);
@ -32,14 +36,21 @@ namespace Dawn {
auto eventTest = vnManager->createEvent<VNDummyEvent>();
auto positionEvent = vnManager->createEvent<VNPositionEvent>();
positionEvent->x = 2.0f;
positionEvent->to.x = 2.0f;
positionEvent->item = cube;
positionEvent->duration = 3.0f;
auto vnTextEvent = vnManager->createEvent<VNTextEvent>();
vnTextEvent->text = "Hello World!";
auto setPropertyEvent = vnManager->createEvent<VNSetEvent<int32_t>>();
setPropertyEvent->modifies = &test;
setPropertyEvent->to = 10;
eventTest
->then(positionEvent)
->then(vnManager->createEvent<VNDummyEvent>())
->then(vnManager->createEvent<VNDummyEvent>())
->then(vnTextEvent)
->then(setPropertyEvent)
;
vnManager->setEvent(eventTest);
}