< Summary

Class:DCL.GifPlayer
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Gif/GifPlayer/GifPlayer.cs
Covered lines:0
Uncovered lines:38
Coverable lines:38
Total lines:109
Line coverage:0% (0 of 38)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GifPlayer(...)0%2100%
GifPlayer()0%2100%
SetGif(...)0%20400%
Play(...)0%20400%
Stop()0%6200%
Dispose()0%2100%
UpdateRoutine()0%20400%
IsValidAsset()0%20400%
UpdateFrame()0%12300%
SetFrame(...)0%6200%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/Gif/GifPlayer/GifPlayer.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using UnityEngine;
 4
 5namespace DCL
 6{
 7    /// <summary>
 8    /// Player for a Gif Asset.
 9    /// Player will stop if Gif Asset is disposed.
 10    /// Is not this player responsibility to dispose Gif Asset. Gif Asset should be explicitly disposed.
 11    /// </summary>
 12    public class GifPlayer : IDisposable
 13    {
 14        public event Action<Texture2D> OnFrameTextureChanged;
 15
 016        public bool isPlaying { get; private set; } = false;
 17
 18        private Asset_Gif gifAsset = null;
 19        private int currentFrameIdx = 0;
 20        private Coroutine updateRoutine = null;
 21        private float currentTimeDelay = 0;
 22
 023        public GifPlayer(Asset_Gif asset) { SetGif(asset); }
 24
 025        public GifPlayer() { }
 26
 27        /// <summary>
 28        /// Set gif asset for the player
 29        /// </summary>
 30        /// <param name="asset">gif asset</param>
 31        public void SetGif(Asset_Gif asset)
 32        {
 033            gifAsset = asset;
 34
 035            if (isPlaying && IsValidAsset())
 36            {
 037                if (currentFrameIdx >= asset.frames.Length)
 38                {
 039                    currentFrameIdx = 0;
 40                }
 041                SetFrame(currentFrameIdx);
 42            }
 043        }
 44
 45        public void Play(bool reset = false)
 46        {
 047            if (reset)
 48            {
 049                currentFrameIdx = 0;
 050                currentTimeDelay = 0;
 051                Stop();
 52            }
 53
 054            isPlaying = true;
 55
 056            if (updateRoutine == null && gifAsset != null)
 57            {
 058                updateRoutine = CoroutineStarter.Start(UpdateRoutine());
 59            }
 060        }
 61
 62        public void Stop()
 63        {
 064            isPlaying = false;
 65
 066            if (updateRoutine != null)
 67            {
 068                CoroutineStarter.Stop(updateRoutine);
 069                updateRoutine = null;
 70            }
 071        }
 72
 073        public void Dispose() { Stop(); }
 74
 75        private IEnumerator UpdateRoutine()
 76        {
 077            while (isPlaying)
 78            {
 079                yield return WaitForSecondsCache.Get(currentTimeDelay);
 080                UpdateFrame();
 81            }
 082        }
 83
 084        private bool IsValidAsset() { return gifAsset?.frames != null && gifAsset.frames.Length > 0; }
 85
 86        private void UpdateFrame()
 87        {
 088            if (!IsValidAsset())
 89            {
 090                return;
 91            }
 92
 093            currentFrameIdx++;
 94
 095            if (currentFrameIdx >= gifAsset.frames.Length)
 96            {
 097                currentFrameIdx = 0;
 98            }
 99
 0100            SetFrame(currentFrameIdx);
 0101        }
 102
 103        private void SetFrame(int frameIdx)
 104        {
 0105            currentTimeDelay = gifAsset.frames[frameIdx].delay;
 0106            OnFrameTextureChanged?.Invoke(gifAsset.frames[frameIdx].texture);
 0107        }
 108    }
 109}