1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     SmLayout.cpp
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: 1 $
14  *---------------------------------------------------------------------------*/
15 #include <nn/os.h>
16 
17 #include <nw/types.h>
18 #include <nw/ut.h>
19 
20 #include "../include/SmLayout.h"
21 
22 namespace
23 {
24 
25 /*!--------------------------------------------------------------------------*
26   @brief        指定Paneの矩形(スクリーン座標)を取得します。
27  *---------------------------------------------------------------------------*/
GetRect(const char * name)28 nw::ut::Rect SmLayoutResource::GetRect( const char* name )
29 {
30     nw::ut::Rect rect;
31     const nw::lyt::Pane* pane = GetPane( name );
32      if ( !pane ) return rect;
33 
34     rect = pane->GetPaneRect();
35     nw::math::VEC3 pos = pane->GetTranslate();
36 
37     // スクリーン座標系に修正します。
38     rect.top    = -rect.top;
39     rect.bottom = -rect.bottom;
40 
41     // 矩形の中心を考慮します。
42     f32 posx = 0.f;
43     f32 posy = 0.f;
44 
45     switch( pane->GetBasePositionH() )
46     {
47     case nw::lyt::HORIZONTALPOSITION_LEFT:
48         posx = SM_LOWER_SCREEN_HALF_SIZE_W + pos.x;
49         break;
50 
51     case nw::lyt::HORIZONTALPOSITION_CENTER:
52         posx = SM_LOWER_SCREEN_HALF_SIZE_W + pos.x - rect.GetWidth()/2.f;
53         break;
54 
55     case nw::lyt::HORIZONTALPOSITION_RIGHT:
56         posx = SM_LOWER_SCREEN_HALF_SIZE_W + pos.x;
57         break;
58     }
59 
60     switch( pane->GetBasePositionV() )
61     {
62     case nw::lyt::VERTICALPOSITION_TOP:
63         posy = SM_LOWER_SCREEN_HALF_SIZE_H - pos.y;
64         break;
65 
66     case nw::lyt::VERTICALPOSITION_CENTER:
67         posy = SM_LOWER_SCREEN_HALF_SIZE_H - pos.y - rect.GetHeight()/2.f;
68         break;
69 
70     case nw::lyt::VERTICALPOSITION_BOTTOM:
71         posy = SM_LOWER_SCREEN_HALF_SIZE_H - pos.y;
72         break;
73     }
74 
75     rect.MoveTo( posx, posy );
76 
77     return rect;
78 }
79 
80 
81 
82 
83 /*!--------------------------------------------------------------------------*
84   @brief        コンストラクタ。
85  *---------------------------------------------------------------------------*/
SmLayout(SmLayoutResource * layoutResource)86 SmLayout::SmLayout( SmLayoutResource* layoutResource )
87 : m_LayoutResource( layoutResource ),
88   m_OffsetPane( NULL ),
89   m_AnimationFrame( 0.0f ),
90   m_AnimationNo( 0 ),
91   m_Visible( true ),
92   m_String( NULL )
93 {
94     SetPosition( 0.f, 0.f );
95 }
96 
97 
98 /*!--------------------------------------------------------------------------*
99   @brief        デストラクタ。
100  *---------------------------------------------------------------------------*/
~SmLayout()101 SmLayout::~SmLayout()
102 {
103 
104 }
105 
106 
107 /*!--------------------------------------------------------------------------*
108   @brief        指定ペインの表示位置(レイアウト座標系)を設定します。
109  *---------------------------------------------------------------------------*/
110 void
SetPosition(f32 x,f32 y)111 SmLayout::SetPosition( f32 x, f32 y )
112 {
113     m_Position.x = x;
114     m_Position.y = - y;
115     m_Position.z = 0.0f;
116 }
117 
118 
119 /*!--------------------------------------------------------------------------*
120   @brief        再生するアニメーションを設定します。
121  *---------------------------------------------------------------------------*/
122 bool
SetAnimationNo(uint animTransformNo)123 SmLayout::SetAnimationNo( uint animTransformNo )
124 {
125     if ( animTransformNo > m_LayoutResource->GetAnimationNum() ) return false;
126     m_AnimationNo = animTransformNo;
127     return true;
128 }
129 
130 
131 /*!--------------------------------------------------------------------------*
132   @brief        アニメーションフレームを設定します。
133  *---------------------------------------------------------------------------*/
134 void
SetAnimationFrame(f32 setFrame)135 SmLayout::SetAnimationFrame( f32 setFrame )
136 {
137     m_AnimationFrame = setFrame;
138 }
139 
140 
141 /*!--------------------------------------------------------------------------*
142   @brief        アニメーションフレームを指定フレームだけ進めます。
143  *---------------------------------------------------------------------------*/
144 bool
AddAnimationFrame(f32 addFrame,bool loop)145 SmLayout::AddAnimationFrame( f32 addFrame, bool loop )
146 {
147     bool ret = false;
148     if ( m_AnimationNo >= m_LayoutResource->GetAnimationNum() ) return ret;
149     f32 frameSize = m_LayoutResource->GetAnimationFrameSize( m_AnimationNo );
150 
151     // フレームに加算します。
152     m_AnimationFrame += addFrame;
153 
154     // 最終フレーム
155     if ( m_AnimationFrame > frameSize )
156     {
157         if ( loop )
158         {
159             m_AnimationFrame = 0.f;
160         }
161         else
162         {
163             m_AnimationFrame -= frameSize;
164         }
165 
166         ret = true;
167     }
168     return ret;
169 }
170 
171 
172 /*!--------------------------------------------------------------------------*
173   @brief        レイアウトリソースに現在のデータを反映します。
174  *---------------------------------------------------------------------------*/
175 bool
SetupLayout()176 SmLayout::SetupLayout()
177 {
178     nw::lyt::Layout* layout = m_LayoutResource->GetLayout();
179      if ( !layout ) return false;
180 
181     // レイアウト全体の表示位置を設定します。
182     nw::lyt::Pane* pane = layout->GetRootPane();
183     if ( pane )
184     {
185         pane->SetTranslate( m_Position );
186     }
187 
188     // さらにオフセットをかけるPaneが指定されていれば、位置を設定します。
189     if ( m_OffsetPane )
190     {
191         m_OffsetPane->SetTranslate( m_OffsetPosition );
192     }
193 
194     // アニメーションを設定します。
195     if ( m_AnimationNo < m_LayoutResource->GetAnimationNum() )
196     {
197         SmLayoutResource::AnimationArray& animArray = m_LayoutResource->GetAnimationArray();
198 
199         // アニメーションを設定する
200         if ( !animArray.empty() )
201         {
202             layout->UnbindAnimation( NULL );
203             layout->BindAnimation( animArray[m_AnimationNo] );
204 
205             animArray[m_AnimationNo]->SetFrame( m_AnimationFrame );
206         }
207     }
208 
209     return true;
210 }
211 
212 
213 /*!--------------------------------------------------------------------------*
214   @brief        レイアウトリソースに反映したデータを戻します。
215  *---------------------------------------------------------------------------*/
216 void
ResetLayout()217 SmLayout::ResetLayout()
218 {
219     // オフセットをかけたPaneを戻します。
220     if ( m_OffsetPane )
221     {
222         m_OffsetPane->SetTranslate( nw::math::VEC3::Zero() );
223     }
224 }
225 
226 
227 
228 } // namespace
229 
230 
231