Magic_Game/src/kiwano/2d/GifSprite.h

209 lines
5.3 KiB
C
Raw Normal View History

2019-07-31 16:22:33 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
2020-01-21 10:09:55 +08:00
//
2019-07-31 16:22:33 +08:00
// 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:
2020-01-21 10:09:55 +08:00
//
2019-07-31 16:22:33 +08:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2020-01-21 10:09:55 +08:00
//
2019-07-31 16:22:33 +08:00
// 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
2019-10-11 21:55:29 +08:00
#include <kiwano/2d/Actor.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/Resource.h>
2020-01-17 16:55:47 +08:00
#include <kiwano/render/GifImage.h>
2020-02-08 00:17:31 +08:00
#include <kiwano/render/TextureRenderContext.h>
2019-07-31 16:22:33 +08:00
namespace kiwano
{
2020-01-21 10:09:55 +08:00
KGE_DECLARE_SMART_PTR(GifSprite);
/**
* \addtogroup Actors
* @{
*/
/**
* \~chinese
2020-02-10 17:32:04 +08:00
* @brief GIF
2020-01-21 10:09:55 +08:00
*/
class KGE_API GifSprite : public Actor
{
public:
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief GIF播放循环结束回调
2020-01-21 10:09:55 +08:00
using LoopDoneCallback = Function<void(int /* times */)>;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief GIF播放结束回调
2020-01-21 10:09:55 +08:00
using DoneCallback = Function<void()>;
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建GIF精灵
/// @param file_path GIF图片路径
2020-02-19 12:09:50 +08:00
static GifSpritePtr Create(const String& file_path);
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建GIF精灵
/// @param res GIF图片资源
2020-02-19 12:09:50 +08:00
static GifSpritePtr Create(const Resource& res);
2020-02-06 16:54:47 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 创建GIF精灵
/// @param gif GIF图片
2020-02-06 16:54:47 +08:00
static GifSpritePtr Create(GifImagePtr gif);
2020-01-21 10:09:55 +08:00
GifSprite();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 加载GIF图片
/// @param file_path GIF图片路径
2020-02-19 12:09:50 +08:00
bool Load(const String& file_path);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 加载GIF图片
/// @param res GIF图片资源
2020-02-19 12:09:50 +08:00
bool Load(const Resource& res);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 加载GIF图片
/// @param gif GIF图片
2020-01-21 10:09:55 +08:00
bool Load(GifImagePtr gif);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置 GIF 动画循环次数
2020-01-21 10:09:55 +08:00
void SetLoopCount(int loops);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置 GIF 动画每次循环结束回调函数
2020-02-19 12:09:50 +08:00
void SetLoopDoneCallback(const LoopDoneCallback& cb);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置 GIF 动画结束回调函数
2020-02-19 12:09:50 +08:00
void SetDoneCallback(const DoneCallback& cb);
2020-01-21 10:09:55 +08:00
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 设置 GIF 图像
2020-01-21 10:09:55 +08:00
void SetGifImage(GifImagePtr gif);
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 重新播放 GIF 动画
2020-01-21 10:09:55 +08:00
void RestartAnimation();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 GIF 动画循环结束回调
2020-01-21 10:09:55 +08:00
LoopDoneCallback GetLoopDoneCallback() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 GIF 动画播放结束回调
2020-01-21 10:09:55 +08:00
DoneCallback GetDoneCallback() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 获取 GIF 图片
2020-01-21 10:09:55 +08:00
GifImagePtr GetGifImage() const;
void OnRender(RenderContext& ctx) override;
private:
void Update(Duration dt) override;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 是否是最后一帧
2020-01-21 10:09:55 +08:00
bool IsLastFrame() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 动画是否已结束
2020-01-21 10:09:55 +08:00
bool EndOfAnimation() const;
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 合成下一帧
2020-01-21 10:09:55 +08:00
void ComposeNextFrame();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 解析当前图像帧
2020-01-21 10:09:55 +08:00
void DisposeCurrentFrame();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 覆盖下一帧
2020-01-21 10:09:55 +08:00
void OverlayNextFrame();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 保存合成后的图像帧
2020-01-21 10:09:55 +08:00
void SaveComposedFrame();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 恢复已保存的图像帧
2020-01-21 10:09:55 +08:00
void RestoreSavedFrame();
/// \~chinese
2020-02-10 17:32:04 +08:00
/// @brief 清空当前图像区域
2020-01-21 10:09:55 +08:00
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_;
TextureRenderContextPtr frame_rt_;
};
/** @} */
inline void GifSprite::SetLoopCount(int loops)
{
total_loop_count_ = loops;
}
2019-12-23 18:05:08 +08:00
2020-02-19 12:09:50 +08:00
inline void GifSprite::SetLoopDoneCallback(const LoopDoneCallback& cb)
2020-01-21 10:09:55 +08:00
{
loop_cb_ = cb;
}
2019-12-23 18:05:08 +08:00
2020-02-19 12:09:50 +08:00
inline void GifSprite::SetDoneCallback(const DoneCallback& cb)
2020-01-21 10:09:55 +08:00
{
done_cb_ = cb;
}
2019-12-23 18:05:08 +08:00
2020-01-21 10:09:55 +08:00
inline GifSprite::LoopDoneCallback GifSprite::GetLoopDoneCallback() const
{
return loop_cb_;
}
2019-12-23 18:05:08 +08:00
2020-01-21 10:09:55 +08:00
inline GifSprite::DoneCallback GifSprite::GetDoneCallback() const
{
return done_cb_;
}
2019-12-23 18:05:08 +08:00
2020-01-21 10:09:55 +08:00
inline GifImagePtr GifSprite::GetGifImage() const
{
return gif_;
}
2019-12-23 18:05:08 +08:00
2020-01-21 10:09:55 +08:00
inline bool GifSprite::IsLastFrame() const
{
return (next_index_ == 0);
}
2019-12-23 18:05:08 +08:00
2020-01-21 10:09:55 +08:00
inline bool GifSprite::EndOfAnimation() const
{
return IsLastFrame() && loop_count_ == total_loop_count_ + 1;
2019-07-31 16:22:33 +08:00
}
2020-01-21 10:09:55 +08:00
} // namespace kiwano