Core/ApplicationBase: Add support for updaters with intervals

This commit is contained in:
SirLynix
2023-04-23 19:45:33 +02:00
parent a55560d1ca
commit de5e7bd8a8
17 changed files with 253 additions and 39 deletions

View File

@@ -13,9 +13,59 @@ namespace Nz
}
template<typename F>
void ApplicationUpdaterFunctor<F>::Update(Time elapsedTime)
Time ApplicationUpdaterFunctor<F>::Update(Time elapsedTime)
{
m_functor(elapsedTime);
if constexpr (std::is_invocable_v<F, Time>)
return TriggerFunctor(elapsedTime);
else if constexpr (std::is_invocable_v<F>)
return TriggerFunctor();
else
static_assert(AlwaysFalse<F>(), "updater functor must be callable with a Time or nothing");
}
template<typename F>
template<typename... Args>
Time ApplicationUpdaterFunctor<F>::TriggerFunctor(Args&&... args)
{
if constexpr (std::is_same_v<std::invoke_result_t<F, Args...>, Time>)
return m_functor(std::forward<Args>(args)...);
else if constexpr (std::is_same_v<std::invoke_result_t<F, Args...>, void>)
{
m_functor(std::forward<Args>(args)...);
return Time::Zero();
}
else
static_assert(AlwaysFalse<F>(), "updater functor must either return Time or void");
}
template<typename F, bool FixedInterval>
ApplicationUpdaterFunctorWithInterval<F, FixedInterval>::ApplicationUpdaterFunctorWithInterval(F functor, Time interval) :
m_interval(interval),
m_functor(std::move(functor))
{
}
template<typename F, bool FixedInterval>
Time ApplicationUpdaterFunctorWithInterval<F, FixedInterval>::Update(Time elapsedTime)
{
if constexpr (std::is_invocable_v<F, Time>)
{
if constexpr (FixedInterval)
m_functor(m_interval);
else
m_functor(elapsedTime);
}
else
{
static_assert(std::is_invocable_v<F>, "updater functor must be callable with a Time or nothing");
m_functor();
}
if constexpr (FixedInterval)
return -m_interval;
else
return m_interval;
}
}