1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_Animation.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 23638 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_LYT_ANIMATION_H_
17 #define NW_LYT_ANIMATION_H_
18 
19 #include <nw/ut/ut_LinkList.h>
20 
21 #include <nw/lyt/lyt_Types.h>
22 
23 namespace nw
24 {
25 namespace lyt
26 {
27 namespace res
28 {
29 
30 struct BinaryFileHeader;
31 struct AnimationBlock;
32 struct AnimationTagBlock;
33 struct AnimationShareBlock;
34 
35 } // namespace res
36 
37 namespace internal
38 {
39 
40 class AnimPaneTreeLink;
41 
42 } // namespace internal
43 
44 class Pane;
45 class Layout;
46 class Group;
47 class Material;
48 class ResourceAccessor;
49 class TextureInfo;
50 class AnimationLink;
51 
52 //---------------------------------------------------------------------------
53 //! :category アニメーション
54 //!
55 //! @brief アニメーションの基底クラスです。
56 //!
57 //! @since 2009/09/18 初版。
58 //---------------------------------------------------------------------------
59 class AnimTransform
60 {
61 public:
62     //----------------------------------------
63     //! @name コンストラクタ/デストラクタ
64     //@{
65 
66     //! @brief コンストラクタです。
67     //!
68     //! @since 2009/09/18 初版。
69     //!
70     AnimTransform();
71 
72     //! @brief デストラクタです。
73     //!
74     //! @since 2009/09/18 初版。
75     //!
76     virtual ~AnimTransform();
77 
78     //@}
79 
80     //----------------------------------------
81     //! @name 設定/取得
82     //@{
83 
84     //! @brief 現在のフレーム位置を取得します。
85     //!
86     //! @return 現在のフレーム位置を返します。
87     //!
88     //! @sa SetFrame
89     //!
90     //! @since 2009/09/18 初版。
91     //!
GetFrame()92     f32 GetFrame() const
93     {
94         return m_Frame;
95     }
96 
97     //! @brief フレーム位置を設定します。
98     //!
99     //! @param frame  フレーム位置です。
100     //!
101     //! @sa GetFrame
102     //!
103     //! @since 2009/09/18 初版。
104     //!
SetFrame(f32 frame)105     void SetFrame(f32 frame)
106     {
107         m_Frame = frame;
108     }
109 
110     //! @brief フレームの長さを取得します。
111     //!
112     //! @return フレームの長さを返します。
113     //!
114     //! @since 2009/09/18 初版。
115     //!
116     u16 GetFrameSize() const;
117 
118     //! @brief フレーム位置の最大値を取得します。
119     //!
120     //! @return フレーム位置の最大値を返します。
121     //!
122     //! @since 2009/09/18 初版。
123     //!
GetFrameMax()124     f32 GetFrameMax() const
125     {
126         return GetFrameSize();
127     }
128 
129     //! @brief アニメーションブロックを取得します。
130     //!
131     //! @return アニメーションブロックへのポインタを返します。
132     //!
133     //! @sa SetResource
134     //!
135     //! @since 2009/09/18 初版。
136     //!
GetAnimResource()137     const res::AnimationBlock* GetAnimResource() const
138     {
139         return m_pRes;
140     }
141 
142     //@}
143 
144     //----------------------------------------
145     //! @name アニメーション
146     //@{
147 
148     //! @brief ループ用のアニメーションデータか判定します。
149     //!
150     //! @details
151     //! アニメーションデータがループ用としてマークされているかどうかを
152     //! 判定します。
153     //!
154     //! @return
155     //! アニメーションデータがループ用としてマークされている場合は true を
156     //! 返します。
157     //!
158     //! @since 2009/09/18 初版。
159     //!
160     bool IsLoopData() const;
161 
162     //! @brief アニメーションを再生します。
163     //!
164     //! @details
165     //! idx で指定されたアニメーションを pPane で指定したペインで再生します。
166     //!
167     //! @param idx  アニメーションのインデックスです。
168     //! @param pPane  ペインです。
169     //!
170     //! @sa Bind
171     //!
172     //! @since 2009/09/18 初版。
173     //!
174     virtual void Animate(u32 idx, Pane* pPane) = 0;
175 
176     //! @brief アニメーションを再生します。
177     //!
178     //! @details
179     //! idx で指定されたアニメーションを pMaterial で指定したマテリアルで
180     //! 再生します。
181     //!
182     //! @param idx  アニメーションのインデックスです。
183     //! @param pMaterial  マテリアルです。
184     //!
185     //! @sa Bind
186     //!
187     //! @since 2009/09/18 初版。
188     //!
189     virtual void Animate(u32 idx, Material* pMaterial) = 0;
190 
191     //@}
192 
193     //----------------------------------------
194     //! @name リソース
195     //@{
196 
197     //! @brief アニメーションが使用するリソースを設定します。
198     //!
199     //! @param pRes  リソースのアニメーションブロックへのポインタです。
200     //! @param pResAccessor  リソースアクセサです。
201     //!
202     //! @details
203     //! Bind() でアニメーションに関連付けるアニメーションブロックデータと
204     //! アニメーションデータに必要なリソースを設定します。
205     //!
206     //! また、 Bind() で必要になる AnimationLink を
207     //! アニメーションブロックに含まれるアニメーション対象の個数分確保します。
208     //!
209     //! AnimationLink は、バインドされるアニメーションの対象(ペインや
210     //! マテリアル)毎に必要です。
211     //!
212     //! この関数の実装では SetAnimResource() を使用して、アニメーション
213     //! ブロックデータを登録しなければなりません。
214     //!
215     //! @sa Bind
216     //! @sa SetAnimResource
217     //! @sa lyt::AnimationLink
218     //!
219     //! @since 2009/09/18
220     //!
221     virtual void SetResource(
222         const res::AnimationBlock* pRes,
223         ResourceAccessor* pResAccessor) = 0;
224 
225     //! @brief アニメーションが使用するリソースを設定します。
226     //!
227     //! @param pRes  リソースのアニメーションブロックへのポインタです。
228     //! @param pResAccessor  リソースアクセサです。
229     //! @param animNum  確保する AnimationLink 数です。
230     //!
231     //! @details
232     //! Bind() でアニメーションに関連付けるアニメーションブロックデータと
233     //! アニメーションデータに必要なリソースを設定します。
234     //!
235     //! また、 Bind() で必要になる AnimationLink を
236     //! animNum で指定された個数、確保します。
237     //!
238     //! AnimationLink は、バインドされるアニメーションの対象(ペインや
239     //! マテリアル)毎に必要です。
240     //!
241     //! 本関数の実装では SetAnimResource() を使用して、アニメーション
242     //! ブロックデータを登録しなければなりません。
243     //!
244     //! @sa Bind
245     //! @sa SetAnimResource
246     //! @sa lyt::AnimationLink
247     //!
248     //! @since 2009/09/18
249     //!
250     virtual void SetResource(
251         const res::AnimationBlock* pRes,
252         ResourceAccessor* pResAccessor,
253         u16 animNum) = 0;
254 
255     //! @brief アニメーションの関連付けを行います。
256     //!
257     //! @details
258     //! pPane で指定したペインにアニメーションを関連付けます。
259     //!
260     //! bRecursive に true を渡して呼び出した場合は、ペインを pPane の子からも
261     //! 検索します。
262     //!
263     //! bDisable に true を渡して呼び出した場合は、アニメーションを無効状態で
264     //! 関連付けます。
265     //!
266     //! @param pPane  ペインへのポインタです。
267     //! @param bRecursive  再帰的に検索する場合は true を指定します。
268     //! @param bDisable  アニメーションを無効状態で関連付ける場合は true を
269     //!                  指定します。
270     //!
271     //! @since 2009/09/18 初版。
272     //!
273     virtual void  Bind(
274         Pane* pPane,
275         bool bRecursive,
276         bool bDisable = false) = 0;
277 
278     //! @brief アニメーションの関連付けを行います。
279     //!
280     //! @details
281     //! pMaterial で指定したマテリアルにアニメーションを関連付けます。
282     //!
283     //! bDisable に true を渡して呼び出した場合は、アニメーションを無効状態で
284     //! 関連付けます。
285     //!
286     //! @param pMaterial  マテリアルへのポインタです。
287     //! @param bDisable  アニメーションを無効状態で関連付ける場合は true を
288     //!                  指定します。
289     //!
290     //! @since 2009/09/18 初版。
291     //!
292     virtual void Bind(Material* pMaterial, bool bDisable = false) = 0;
293 
294     //@}
295 
296     //! :private
297     //! @brief リンク情報です。
298     ut::LinkListNode m_Link;
299 
300 protected:
301     //----------------------------------------
302     //! @name 設定/取得
303     //@{
304 
305     //! @brief アニメーションリソースへのポインタを設定します。
306     //!
307     //! @details
308     //! SetResource() の実装内で使用され、 GetAnimResource() の戻り値を設定します。
309     //!
310     //! @param pRes  アニメーションブロックへのポインタです。
311     //!
312     //! @sa SetResource
313     //! @sa GetAnimResource
314     //!
315     //! @since 2009/09/18 初版。
316     //!
SetAnimResource(const res::AnimationBlock * pRes)317     void SetAnimResource(const res::AnimationBlock* pRes)
318     {
319         m_pRes = pRes;
320     }
321 
322     //@}
323 
324 private:
325     // アニメーションリソースです。
326     const res::AnimationBlock* m_pRes;
327 
328     // カレントフレーム値です。
329     f32 m_Frame;
330 };
331 
332 //---------------------------------------------------------------------------
333 //! :category アニメーション
334 //!
335 //! @brief アニメーションの基礎クラスです。
336 //!
337 //! @since 2009/09/18 初版。
338 //---------------------------------------------------------------------------
339 class AnimTransformBasic : public AnimTransform
340 {
341 private:
342     typedef AnimTransform Base;
343 
344 public:
345     //----------------------------------------
346     //! @name コンストラクタ/デストラクタ
347     //@{
348 
349     //! @brief コンストラクタです。
350     //!
351     //! @since 2009/09/18 初版。
352     //!
353     AnimTransformBasic();
354 
355     //! @brief デストラクタです。
356     //!
357     //! @since 2009/09/18 初版。
358     //!
359     virtual ~AnimTransformBasic();
360 
361     //@}
362 
363     //----------------------------------------
364     //! @name アニメーション
365     //@{
366 
367     virtual void Animate(
368         u32 idx,
369         Pane* pPane);
370 
371     virtual void Animate(
372         u32 idx,
373         Material* pMaterial);
374 
375     //@}
376 
377     //----------------------------------------
378     //! @name リソース
379     //@{
380 
381     virtual void SetResource(
382         const res::AnimationBlock* pRes,
383         ResourceAccessor* pResAccessor);
384 
385     virtual void SetResource(
386         const res::AnimationBlock* pRes,
387         ResourceAccessor* pResAccessor,
388         u16 animNum);
389 
390     virtual void Bind(
391         Pane* pPane,
392         bool bRecursive,
393         bool bDisable = false);
394 
395     virtual void Bind(
396         Material* pMaterial,
397         bool bDisable = false);
398 
399     //@}
400 
401     //! :private
402     //! @brief アニメーションの関連付けを行います。
403     template<typename T>
404     AnimationLink* Bind(
405         T* pTarget,
406         AnimationLink* pAnimLink,
407         u16 idx,
408         bool bDisable);
409 
410 private:
411     AnimationLink* FindUnbindLink(AnimationLink* pLink) const;
412 
413     //! テクスチャの情報です。
414     TextureInfo* m_pTexAry;
415 
416     //! アニメーションリンクです。
417     AnimationLink* m_pAnimLinkAry;
418 
419     //! アニメーションリンクの数です。
420     u16 m_AnimLinkNum;
421 };
422 
423 //---------------------------------------------------------------------------
424 //! :category アニメーション
425 //!
426 //! @brief アニメーションリソースクラスです。
427 //!
428 //! @since 2009/09/18 初版。
429 //---------------------------------------------------------------------------
430 class AnimResource
431 {
432 public:
433     //----------------------------------------
434     //! @name コンストラクタ/デストラクタ
435     //@{
436 
437     //! @brief コンストラクタです。
438     //!
439     //! @details
440     //! リソースの関連付けは行われません。
441     //! 別途 Set() を使用して関連付けを行います。
442     //!
443     //! @sa Set
444     //!
445     //! @since 2009/09/18 初版。
446     //!
447     AnimResource();
448 
449     //! @brief コンストラクタです。
450     //!
451     //! @details
452     //! anmResBuf で指定したアニメーションリソースが関連付けられます。
453     //!
454     //! @param anmResBuf  アニメーションリソースへのポインタです。
455     //!
456     //! @since 2009/09/18 初版。
457     //!
AnimResource(const void * anmResBuf)458     explicit AnimResource(const void* anmResBuf)
459     {
460         Set(anmResBuf);
461     }
462 
463     //@}
464 
465     //----------------------------------------
466     //! @name 設定/取得
467     //@{
468 
469     //! @brief アニメーションリソースを関連付けます。
470     //!
471     //! @param anmResBuf アニメーションリソースへのポインタです。
472     //!
473     //! @since 2009/09/18 初版。
474     //!
475     void Set(const void* anmResBuf);
476 
477     //! @brief アニメーションリソースを取得します。
478     //!
479     //! @return ファイルヘッダへのポインタを返します。
480     //!
481     //! @since 2009/09/18 初版。
482     //!
GetFileHeader()483     const ut::BinaryFileHeader* GetFileHeader() const
484     {
485         return m_pFileHeader;
486     }
487 
488     //! @brief アニメーションブロックを取得します。
489     //!
490     //! @return アニメーションブロックへのポインタを返します。
491     //!
492     //! @since 2009/09/18 初版。
493     //!
GetResourceBlock()494     const res::AnimationBlock* GetResourceBlock() const
495     {
496         return m_pResBlock;
497     }
498 
499     //! @brief アニメーション区間タグブロックを取得します。
500     //!
501     //! @return アニメーション区間タグブロックへのポインタを返します。
502     //!
503     //! @since 2009/09/18 初版。
504     //!
GetTagBlock()505     const res::AnimationTagBlock* GetTagBlock() const
506     {
507         return m_pTagBlock;
508     }
509 
510     //! @brief アニメーション区間タグの通し番号を取得します。
511     //!
512     //! @return アニメーション区間タグの0から始まる通し番号を返します。
513     //!
514     //! @since 2009/09/18 初版。
515     //!
516     u16 GetTagOrder() const;
517 
518     //! @brief アニメーション区間タグの名前を取得します。
519     //!
520     //! @return アニメーション区間タグの名前を返します。
521     //!
522     //! @since 2009/09/18 初版。
523     //!
524     const char* GetTagName() const;
525 
526     //! @brief アニメーション区間タグのグループ数を取得します。
527     //!
528     //! @return アニメーション区間タグに設定されているグループ数を返します。
529     //!
530     //! @since 2009/09/18 初版。
531     //!
532     u16 GetGroupNum() const;
533 
534     //! @brief アニメーション区間タグのグループ配列を取得します。
535     //!
536     //! @return
537     //! アニメーション区間タグに設定されているグループ配列の
538     //! 先頭ポインタを返します。
539     //!
540     //! @since 2009/09/18 初版。
541     //!
542     const AnimationGroupRef* GetGroupArray() const;
543 
544     //! @brief
545     //! アニメーションをバインドするとき、ペインの子孫となるペインも対象に
546     //! するかどうかを判定します。
547     //!
548     //! @return
549     //! アニメーションをバインドするとき、子孫のペインも対象にするときは
550     //! true を返します。
551     //!
552     //! @since 2009/09/18 初版。
553     //!
554     bool IsDescendingBind() const;
555 
556     //! @brief アニメーション共有情報の個数を取得します。
557     //!
558     //! @return アニメーション共有情報の個数を返します。
559     //!
560     //! @since 2009/09/18 初版。
561     //!
562     u16 GetAnimationShareInfoNum() const;
563 
564     //! @brief アニメーション共有情報の配列を取得します。
565     //!
566     //! @return AnimationShareInfo オブジェクト配列の先頭ポインタを返します。
567     //!
568     //! @since 2009/09/18 初版。
569     //!
570     const AnimationShareInfo* GetAnimationShareInfoArray() const;
571 
572     //! @brief バインドされるアニメーションの個数を計算します。
573     //!
574     //! @details
575     //! pPane で指定したペインにバインドされるアニメーションの個数を計算します。
576     //!
577     //! bRecursive に true を指定した場合は、関連付けるペインを子ペインからも
578     //! 検索します。
579     //!
580     //! @param pPane ペインへのポインタです。
581     //! @param bRecursive 子ペインも検索するか指定します。
582     //!
583     //! @return バインドされるアニメーションの個数を返します。
584     //!
585     //! @since 2009/09/18 初版。
586     //!
587     u16 CalcAnimationNum(Pane* pPane, bool bRecursive) const;
588 
589     //! @brief バインドされるアニメーションの個数を計算します。
590     //!
591     //! @details
592     //! pMaterial で指定されたマテリアルにバインドされるアニメーションの
593     //! 個数を計算します。
594     //!
595     //! @param pMaterial マテリアルへのポインタです。
596     //!
597     //! @return バインドされるアニメーションの個数を返します。
598     //!
599     //! @since 2009/09/18 初版。
600     //!
601     u16 CalcAnimationNum(Material* pMaterial) const;
602 
603     //! @brief バインドされるアニメーションの個数を計算します。
604     //!
605     //! @details
606     //! pGroup で指定されたグループに含まれるペインにバインドされる
607     //! アニメーションの個数を計算します。
608     //!
609     //! bRecursive に true を指定した場合は、関連付けるペインを子ペインからも
610     //! 検索します。
611     //!
612     //! @param pGroup グループへのポインタです。
613     //! @param bRecursive 子ペインも検索するか指定します。
614     //!
615     //! @return バインドされるアニメーションの個数を返します。
616     //!
617     //! @since 2009/09/18 初版。
618     //!
619     u16 CalcAnimationNum(Group* pGroup, bool bRecursive) const;
620 
621     //@}
622 
623 private:
624     void Init();
625 
626     //! @brief アニメーションリソースが設定されているか確認します。
627     //!
628     //! @return 設定されていれば true を返します。
629     //!
630     //! @since 2009/09/18 初版。
631     //!
632     bool CheckResource() const;
633 
634     const ut::BinaryFileHeader* m_pFileHeader;
635     const res::AnimationBlock* m_pResBlock;
636     const res::AnimationTagBlock* m_pTagBlock;
637     const res::AnimationShareBlock* m_pShareBlock;
638 };
639 
640 namespace internal
641 {
642 
643 class AnimPaneTree
644 {
645 public:
646     AnimPaneTree();
647 
648     AnimPaneTree(
649         Pane* pTargetPane,
650         const AnimResource& animRes);
651 
652     void Set(
653         Pane* pTargetPane,
654         const AnimResource& animRes);
655 
656     AnimTransform* Bind(
657         Layout* pLayout,
658         Pane* pTargetPane,
659         ResourceAccessor* pResAccessor) const;
660 
IsEnabled()661     bool IsEnabled() const
662     {
663         return m_LinkNum > 0;
664     }
665 
GetAnimResource()666     const AnimResource& GetAnimResource() const
667     {
668         return m_AnimRes;
669     }
670 
671 private:
672     static u16 FindAnimContent(
673         const res::AnimationBlock* pAnimBlock,
674         const char* animContName,
675         u8 animContType);
676 
677     void Init();
678 
679     static const u16 NOBIND = u16(-1);
680     static const int MATERIAL_NUM_MAX = 1 + 4 + 4;
681 
682     AnimResource m_AnimRes;
683 
684     u16 m_AnimPaneIdx;
685     u16 m_LinkNum;
686 
687     u16 m_AnimMatIdxs[MATERIAL_NUM_MAX];
688     u8 m_AnimMatCnt;
689 };
690 
691 AnimationLink* FindAnimationLink(
692     AnimationList* pAnimList,
693     AnimTransform* pAnimTrans);
694 
695 AnimationLink* FindAnimationLink(
696     AnimationList* pAnimList,
697     const AnimResource& animRes);
698 
699 void UnbindAnimationLink(
700     AnimationList* pAnimList,
701     AnimTransform* pAnimTrans);
702 
703 } // namespace nw::lyt::internal
704 } // namespace nw::lyt
705 } // namespace nw
706 
707 #endif // NW_LYT_ANIMATION_H_
708