186 lines
4.8 KiB
C++
186 lines
4.8 KiB
C++
// Copyright (c) 2016-2018 Kiwano - Nomango
|
||
//
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
// of this software and associated documentation files (the "Software"), to deal
|
||
// in the Software without restriction, including without limitation the rights
|
||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
// copies of the Software, and to permit persons to whom the Software is
|
||
// furnished to do so, subject to the following conditions:
|
||
//
|
||
// The above copyright notice and this permission notice shall be included in
|
||
// all copies or substantial portions of the Software.
|
||
//
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
// THE SOFTWARE.
|
||
|
||
#pragma once
|
||
#include <kiwano/2d/Actor.h>
|
||
#include <kiwano/core/Resource.h>
|
||
#include <kiwano/renderer/RenderTarget.h>
|
||
#include <kiwano/renderer/GifImage.h>
|
||
|
||
namespace kiwano
|
||
{
|
||
KGE_DECLARE_SMART_PTR(GifSprite);
|
||
|
||
/**
|
||
* \addtogroup Actors
|
||
* @{
|
||
*/
|
||
|
||
/**
|
||
* \~chinese
|
||
* @brief GIF ¾«Áé
|
||
*/
|
||
class KGE_API GifSprite
|
||
: public Actor
|
||
{
|
||
public:
|
||
/// \~chinese
|
||
/// @brief GIF²¥·ÅÑ»·½áÊø»Øµ÷
|
||
using LoopDoneCallback = Function<void(int /* times */)>;
|
||
|
||
/// \~chinese
|
||
/// @brief GIF²¥·Å½áÊø»Øµ÷
|
||
using DoneCallback = Function<void()>;
|
||
|
||
GifSprite();
|
||
|
||
/// \~chinese
|
||
/// @brief ¹¹ÔìGIF¾«Áé
|
||
/// @param file_path GIFͼƬ·¾¶
|
||
GifSprite(String const& file_path);
|
||
|
||
/// \~chinese
|
||
/// @brief ¹¹ÔìGIF¾«Áé
|
||
/// @param res GIFͼƬ×ÊÔ´
|
||
GifSprite(Resource const& res);
|
||
|
||
/// \~chinese
|
||
/// @brief ¹¹ÔìGIF¾«Áé
|
||
/// @param gif GIFͼƬ
|
||
GifSprite(GifImagePtr gif);
|
||
|
||
/// \~chinese
|
||
/// @brief ¼ÓÔØGIFͼƬ
|
||
/// @param file_path GIFͼƬ·¾¶
|
||
bool Load(String const& file_path);
|
||
|
||
/// \~chinese
|
||
/// @brief ¼ÓÔØGIFͼƬ
|
||
/// @param res GIFͼƬ×ÊÔ´
|
||
bool Load(Resource const& res);
|
||
|
||
/// \~chinese
|
||
/// @brief ¼ÓÔØGIFͼƬ
|
||
/// @param gif GIFͼƬ
|
||
bool Load(GifImagePtr gif);
|
||
|
||
/// \~chinese
|
||
/// @brief ÉèÖà GIF ¶¯»Ñ»·´ÎÊý
|
||
void SetLoopCount(int loops);
|
||
|
||
/// \~chinese
|
||
/// @brief ÉèÖà GIF ¶¯»Ã¿´ÎÑ»·½áÊø»Øµ÷º¯Êý
|
||
void SetLoopDoneCallback(LoopDoneCallback const& cb);
|
||
|
||
/// \~chinese
|
||
/// @brief ÉèÖà GIF ¶¯»½áÊø»Øµ÷º¯Êý
|
||
void SetDoneCallback(DoneCallback const& cb);
|
||
|
||
/// \~chinese
|
||
/// @brief ÉèÖà GIF ͼÏñ
|
||
void SetGifImage(GifImagePtr gif);
|
||
|
||
/// \~chinese
|
||
/// @brief ÖØÐ²¥·Å GIF ¶¯»
|
||
void RestartAnimation();
|
||
|
||
/// \~chinese
|
||
/// @brief »ñÈ¡ GIF ¶¯»Ñ»·½áÊø»Øµ÷
|
||
LoopDoneCallback GetLoopDoneCallback() const;
|
||
|
||
/// \~chinese
|
||
/// @brief »ñÈ¡ GIF ¶¯»²¥·Å½áÊø»Øµ÷
|
||
DoneCallback GetDoneCallback() const;
|
||
|
||
/// \~chinese
|
||
/// @brief »ñÈ¡ GIF ͼƬ
|
||
GifImagePtr GetGifImage() const;
|
||
|
||
void OnRender(RenderTarget* rt) override;
|
||
|
||
private:
|
||
void Update(Duration dt) override;
|
||
|
||
/// \~chinese
|
||
/// @brief ÊÇ·ñÊÇ×îºóÒ»Ö¡
|
||
bool IsLastFrame() const;
|
||
|
||
/// \~chinese
|
||
/// @brief ¶¯»ÊÇ·ñÒѽáÊø
|
||
bool EndOfAnimation() const;
|
||
|
||
/// \~chinese
|
||
/// @brief ºÏ³ÉÏÂÒ»Ö¡
|
||
void ComposeNextFrame();
|
||
|
||
/// \~chinese
|
||
/// @brief ½âÎöµ±Ç°Í¼ÏñÖ¡
|
||
void DisposeCurrentFrame();
|
||
|
||
/// \~chinese
|
||
/// @brief ¸²¸ÇÏÂÒ»Ö¡
|
||
void OverlayNextFrame();
|
||
|
||
/// \~chinese
|
||
/// @brief ±£´æºÏ³ÉºóµÄͼÏñÖ¡
|
||
void SaveComposedFrame();
|
||
|
||
/// \~chinese
|
||
/// @brief »Ö¸´Òѱ£´æµÄͼÏñÖ¡
|
||
void RestoreSavedFrame();
|
||
|
||
/// \~chinese
|
||
/// @brief Çå¿Õµ±Ç°Í¼ÏñÇøÓò
|
||
void ClearCurrentFrameArea();
|
||
|
||
private:
|
||
bool animating_;
|
||
int total_loop_count_;
|
||
int loop_count_;
|
||
size_t next_index_;
|
||
Duration frame_elapsed_;
|
||
LoopDoneCallback loop_cb_;
|
||
DoneCallback done_cb_;
|
||
GifImagePtr gif_;
|
||
GifImage::Frame frame_;
|
||
TexturePtr saved_frame_;
|
||
TexturePtr frame_to_render_;
|
||
TextureRenderTargetPtr frame_rt_;
|
||
};
|
||
|
||
/** @} */
|
||
|
||
inline void GifSprite::SetLoopCount(int loops) { total_loop_count_ = loops; }
|
||
|
||
inline void GifSprite::SetLoopDoneCallback(LoopDoneCallback const& cb) { loop_cb_ = cb; }
|
||
|
||
inline void GifSprite::SetDoneCallback(DoneCallback const& cb) { done_cb_ = cb; }
|
||
|
||
inline GifSprite::LoopDoneCallback GifSprite::GetLoopDoneCallback() const { return loop_cb_; }
|
||
|
||
inline GifSprite::DoneCallback GifSprite::GetDoneCallback() const { return done_cb_; }
|
||
|
||
inline GifImagePtr GifSprite::GetGifImage() const { return gif_; }
|
||
|
||
inline bool GifSprite::IsLastFrame() const { return (next_index_ == 0); }
|
||
|
||
inline bool GifSprite::EndOfAnimation() const { return IsLastFrame() && loop_count_ == total_loop_count_ + 1; }
|
||
}
|