Kiwano Engine v1.3.x
FrameAnimation.h
1// Copyright (c) 2016-2018 Kiwano - Nomango
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21#pragma once
22#include <kiwano/2d/animation/TweenAnimation.h>
23#include <kiwano/render/Bitmap.h>
24
25namespace kiwano
26{
27
37struct KGE_API SpriteFrame
38{
39 SpriteFrame() = default;
40
45 explicit SpriteFrame(RefPtr<Image> image, const Rect& src_rect = Rect(), bool reset_sprite_size = true);
46
55 inline static Vector<SpriteFrame> SplitBitmap(RefPtr<Bitmap> bitmap, int cols, int rows, int max_num = -1,
56 float padding_x = 0, float padding_y = 0)
57 {
58 return SpriteFrame::SplitBitmap(bitmap, Rect(Point(), bitmap->GetSize()), cols, rows, max_num, padding_x,
59 padding_y);
60 }
61
71 static Vector<SpriteFrame> SplitBitmap(RefPtr<Bitmap> bitmap, const Rect& src_rect, int cols, int rows,
72 int max_num = -1, float padding_x = 0, float padding_y = 0);
73
74 bool reset_sprite_size = true;
75 RefPtr<Image> image;
76 RefPtr<Bitmap> bitmap;
77 Rect src_rect;
78};
79
84struct KGE_API FrameSequence
85{
88 FrameSequence() = default;
89
94 inline FrameSequence(Duration dur, const Vector<SpriteFrame>& frames)
95 : duration(dur)
96 , frames(frames)
97 {
98 }
99
102 FrameSequence Reverse() const;
103
104 Duration duration;
105 Vector<SpriteFrame> frames;
106};
107
110class KGE_API FrameAnimation : public TweenAnimation
111{
112public:
114
118 explicit FrameAnimation(const Vector<FrameSequence>& frame_seq_list);
119
122 const Vector<FrameSequence>& GetFrameSequences() const;
123
126 const FrameSequence& GetCurrentFrameSequence() const;
127
131 void SetFrameSequences(const Vector<FrameSequence>& frame_seq_list);
132
135 FrameAnimation* Clone() const override;
136
139 FrameAnimation* Reverse() const override;
140
141protected:
142 void Init(Actor* target) override;
143
144 void UpdateTween(Actor* target, float percent) override;
145
146 void SetTargetFrame(Actor* target, const SpriteFrame& frame);
147
148private:
149 size_t current_index_;
150 Vector<float> progress_;
151 Vector<FrameSequence> frame_seq_list_;
152};
153
156inline const Vector<FrameSequence>& FrameAnimation::GetFrameSequences() const
157{
158 return frame_seq_list_;
159}
160
162{
163 return frame_seq_list_[current_index_];
164}
165
166} // namespace kiwano
角色
Definition: Actor.h:63
帧动画
Definition: FrameAnimation.h:111
const Vector< FrameSequence > & GetFrameSequences() const
获取序列帧
Definition: FrameAnimation.h:156
const FrameSequence & GetCurrentFrameSequence() const
获取序列帧
Definition: FrameAnimation.h:161
引用计数智能指针
Definition: RefBasePtr.hpp:35
补间动画
Definition: TweenAnimation.h:37
时间段
Definition: Duration.h:48
序列帧
Definition: FrameAnimation.h:85
FrameSequence()=default
构建空序列帧
FrameSequence(Duration dur, const Vector< SpriteFrame > &frames)
创建序列帧
Definition: FrameAnimation.h:94
精灵帧
Definition: FrameAnimation.h:38
static Vector< SpriteFrame > SplitBitmap(RefPtr< Bitmap > bitmap, int cols, int rows, int max_num=-1, float padding_x=0, float padding_y=0)
按行列分割位图
Definition: FrameAnimation.h:55