From 112e8e568c9eac8723a39cd2f9b05786f0d58fe9 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Thu, 4 Apr 2019 11:18:58 +0700 Subject: [PATCH] flipbook_player helper functions --- .../high/components/flipbook_player.hpp | 64 ++++++++++++---- samples/sources/sample_03/sample_03.cpp | 4 +- .../enduro2d/high/systems/flipbook_system.cpp | 9 +-- .../sources/untests_high/flipbook_player.cpp | 74 +++++++++++++++++++ 4 files changed, 129 insertions(+), 22 deletions(-) create mode 100644 untests/sources/untests_high/flipbook_player.cpp diff --git a/headers/enduro2d/high/components/flipbook_player.hpp b/headers/enduro2d/high/components/flipbook_player.hpp index c8bd64d6..5f251d5d 100644 --- a/headers/enduro2d/high/components/flipbook_player.hpp +++ b/headers/enduro2d/high/components/flipbook_player.hpp @@ -16,25 +16,38 @@ namespace e2d public: flipbook_player() = default; + // data access + flipbook_player& time(f32 value) noexcept; f32 time() const noexcept; flipbook_player& speed(f32 value) noexcept; f32 speed() const noexcept; - flipbook_player& paused(bool value) noexcept; - bool paused() const noexcept; - flipbook_player& looped(bool value) noexcept; bool looped() const noexcept; + flipbook_player& stopped(bool value) noexcept; + bool stopped() const noexcept; + + flipbook_player& playing(bool value) noexcept; + bool playing() const noexcept; + flipbook_player& sequence(str_hash value) noexcept; str_hash sequence() const noexcept; + + // helpers + + flipbook_player& stop(f32 ntime) noexcept; + flipbook_player& stop(str_hash nsequence) noexcept; + + flipbook_player& play(f32 ntime) noexcept; + flipbook_player& play(str_hash nsequence) noexcept; private: f32 time_{0.f}; f32 speed_{1.f}; - bool paused_{false}; bool looped_{false}; + bool playing_{false}; str_hash sequence_; }; } @@ -59,15 +72,6 @@ namespace e2d return speed_; } - inline flipbook_player& flipbook_player::paused(bool value) noexcept { - paused_ = value; - return *this; - } - - inline bool flipbook_player::paused() const noexcept { - return paused_; - } - inline flipbook_player& flipbook_player::looped(bool value) noexcept { looped_ = value; return *this; @@ -77,6 +81,24 @@ namespace e2d return looped_; } + inline flipbook_player& flipbook_player::stopped(bool value) noexcept { + playing_ = !value; + return *this; + } + + inline bool flipbook_player::stopped() const noexcept { + return !playing_; + } + + inline flipbook_player& flipbook_player::playing(bool value) noexcept { + playing_ = value; + return *this; + } + + inline bool flipbook_player::playing() const noexcept { + return playing_; + } + inline flipbook_player& flipbook_player::sequence(str_hash value) noexcept { sequence_ = value; return *this; @@ -85,4 +107,20 @@ namespace e2d inline str_hash flipbook_player::sequence() const noexcept { return sequence_; } + + inline flipbook_player& flipbook_player::stop(f32 ntime) noexcept { + return time(ntime).stopped(true); + } + + inline flipbook_player& flipbook_player::stop(str_hash nsequence) noexcept { + return sequence(nsequence).stop(0.f); + } + + inline flipbook_player& flipbook_player::play(f32 ntime) noexcept { + return time(ntime).playing(true); + } + + inline flipbook_player& flipbook_player::play(str_hash nsequence) noexcept { + return sequence(nsequence).play(0.f); + } } diff --git a/samples/sources/sample_03/sample_03.cpp b/samples/sources/sample_03/sample_03.cpp index 8c5754c7..895ec518 100644 --- a/samples/sources/sample_03/sample_03.cpp +++ b/samples/sources/sample_03/sample_03.cpp @@ -128,8 +128,8 @@ namespace .filtering(false)) .component(flipbook_res) .component(flipbook_player() - .looped(true) - .sequence("idle")); + .play("idle") + .looped(true)); node_iptr flipbook_n = flipbook_e.get_component().node(); flipbook_n->scale(v3f(2.f,2.f,1.f)); diff --git a/sources/enduro2d/high/systems/flipbook_system.cpp b/sources/enduro2d/high/systems/flipbook_system.cpp index 5df2f795..ae736bef 100644 --- a/sources/enduro2d/high/systems/flipbook_system.cpp +++ b/sources/enduro2d/high/systems/flipbook_system.cpp @@ -20,20 +20,16 @@ namespace flipbook_player& fp, const flipbook_source& fs) { - if ( fp.speed() <= 0.f || fp.paused() ) { + if ( fp.speed() <= 0.f || fp.stopped() ) { return; } const flipbook_asset::ptr& flipbook_res = fs.flipbook(); if ( !flipbook_res ) { - fp.time(0.f); - fp.paused(true); return; } const flipbook& flipbook = flipbook_res->content(); const flipbook::sequence* sequence = flipbook.find_sequence(fp.sequence()); if ( !sequence || sequence->frames.empty() ) { - fp.time(0.f); - fp.paused(true); return; } fp.time(fp.time() + dt * fp.speed()); @@ -43,8 +39,7 @@ namespace if ( fp.looped() ) { fp.time(math::mod(fp.time(), loop_time)); } else { - fp.time(loop_time); - fp.paused(true); + fp.stop(loop_time); } } } diff --git a/untests/sources/untests_high/flipbook_player.cpp b/untests/sources/untests_high/flipbook_player.cpp new file mode 100644 index 00000000..10399319 --- /dev/null +++ b/untests/sources/untests_high/flipbook_player.cpp @@ -0,0 +1,74 @@ +/******************************************************************************* + * This file is part of the "Enduro2D" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2018-2019 Matvey Cherevko + ******************************************************************************/ + +#include "_high.hpp" +using namespace e2d; + +TEST_CASE("flipbook_player") { + { + flipbook_player fp; + REQUIRE(math::approximately(fp.time(), 0.f)); + REQUIRE(math::approximately(fp.speed(), 1.f)); + REQUIRE_FALSE(fp.looped()); + REQUIRE(fp.stopped()); + REQUIRE_FALSE(fp.playing()); + REQUIRE(fp.sequence() == make_hash("")); + } + { + flipbook_player fp; + + fp.playing(true); + REQUIRE(fp.playing()); + REQUIRE_FALSE(fp.stopped()); + + fp.stopped(true); + REQUIRE_FALSE(fp.playing()); + REQUIRE(fp.stopped()); + + fp.stopped(false); + REQUIRE(fp.playing()); + REQUIRE_FALSE(fp.stopped()); + + fp.playing(false); + REQUIRE_FALSE(fp.playing()); + REQUIRE(fp.stopped()); + } + { + flipbook_player fp; + + REQUIRE(&fp.play(5.f) == &fp); + REQUIRE(math::approximately(fp.time(), 5.f)); + REQUIRE(math::approximately(fp.speed(), 1.f)); + REQUIRE_FALSE(fp.looped()); + REQUIRE_FALSE(fp.stopped()); + REQUIRE(fp.playing()); + REQUIRE(fp.sequence() == make_hash("")); + + REQUIRE(&fp.play("hello") == &fp); + REQUIRE(math::approximately(fp.time(), 0.f)); + REQUIRE(math::approximately(fp.speed(), 1.f)); + REQUIRE_FALSE(fp.looped()); + REQUIRE_FALSE(fp.stopped()); + REQUIRE(fp.playing()); + REQUIRE(fp.sequence() == make_hash("hello")); + + REQUIRE(&fp.stop(5.f) == &fp); + REQUIRE(math::approximately(fp.time(), 5.f)); + REQUIRE(math::approximately(fp.speed(), 1.f)); + REQUIRE_FALSE(fp.looped()); + REQUIRE(fp.stopped()); + REQUIRE_FALSE(fp.playing()); + REQUIRE(fp.sequence() == make_hash("hello")); + + REQUIRE(&fp.stop("") == &fp); + REQUIRE(math::approximately(fp.time(), 0.f)); + REQUIRE(math::approximately(fp.speed(), 1.f)); + REQUIRE_FALSE(fp.looped()); + REQUIRE(fp.stopped()); + REQUIRE_FALSE(fp.playing()); + REQUIRE(fp.sequence() == make_hash("")); + } +}