Timeout and Interval events

This commit is contained in:
2023-11-25 23:57:07 -06:00
parent 94941b1c14
commit 3920dd34a3
15 changed files with 418 additions and 10 deletions

View File

@ -6,4 +6,7 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
ITimeManager.cpp
)
)
# Subdirs
add_subdirectory(event)

View File

@ -0,0 +1,10 @@
# Copyright (c) 2023 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
target_sources(${DAWN_TARGET_NAME}
PRIVATE
IntervalEvent.cpp
TimeoutEvent.cpp
)

View File

@ -0,0 +1,41 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "IntervalEvent.hpp"
using namespace Dawn;
IntervalEvent::IntervalEvent() : CustomEvent() {
internalData = 0.0f;
}
enum CustomEventResult IntervalEvent::shouldEmit(
const struct IntervalEventData &listener
) {
if(listener.nextInvoke > internalData) return CustomEventResult::NOTHING;
return CustomEventResult::INVOKE;
}
struct IntervalEventData IntervalEvent::transformData(const float_t &interval) {
return {
.interval = interval,
.nextInvoke = internalData + interval
};
}
struct IntervalEventData IntervalEvent::transformDataAfterEmit(
const struct IntervalEventData &listenerData
) {
return {
.interval = listenerData.interval,
.nextInvoke = listenerData.nextInvoke + listenerData.interval
};
}
void IntervalEvent::tick(const float_t delta) {
internalData += delta;
emit();
}

View File

@ -0,0 +1,38 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/CustomEvent.hpp"
namespace Dawn {
struct IntervalEventData {
float_t interval;
float_t nextInvoke;
};
class IntervalEvent : public CustomEvent<
float_t, struct IntervalEventData, float_t
> {
protected:
enum CustomEventResult shouldEmit(
const struct IntervalEventData &listener
) override;
struct IntervalEventData transformData(const float_t &interval) override;
struct IntervalEventData transformDataAfterEmit(
const struct IntervalEventData &listenerData
) override;
public:
IntervalEvent();
/**
* Ticks this interval event by a time delta amount. Will also emit all
* events that are ready to be emitted.
*
* @param delta Delta time.
*/
void tick(const float_t delta);
};
}

View File

@ -0,0 +1,26 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TimeoutEvent.hpp"
using namespace Dawn;
TimeoutEvent::TimeoutEvent() : CustomEvent() {
internalData = 0.0f;
}
enum CustomEventResult TimeoutEvent::shouldEmit(const float_t &listener) {
if(listener > internalData) return CustomEventResult::NOTHING;
return CustomEventResult::INVOKE_AND_REMOVE;
}
float_t TimeoutEvent::transformData(const float_t &interval) {
return interval;
}
void TimeoutEvent::tick(const float_t delta) {
internalData += delta;
emit();
}

View File

@ -0,0 +1,26 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/CustomEvent.hpp"
namespace Dawn {
class TimeoutEvent : public CustomEvent<float_t, float_t, float_t> {
protected:
enum CustomEventResult shouldEmit(const float_t &listener) override;
float_t transformData(const float_t &timeout) override;
public:
TimeoutEvent();
/**
* Ticks this timeout event by a time delta amount. Will also emit all
* events that are ready to be emitted.
*
* @param delta Delta time.
*/
void tick(const float_t delta);
};
}