1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: SmLytButton.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/SmLytButton.h"
21 #include "../include/SmTouchPanelStatus.h"
22
23 namespace
24 {
25
26 //----------------------------------------
27 // コンストラクタ
SmLytButton(SmLayout * smLayout,const wchar_t * string,const char * bounding)28 SmLytButton::SmLytButton( SmLayout* smLayout, const wchar_t* string, const char* bounding )
29 : SmMessage( NULL ),
30 m_Layout( smLayout ),
31 m_LastX( 0 ),
32 m_LastY( 0 ),
33 m_IsPressed( false ),
34 m_IsGrab( false ),
35 m_IsSwitch( true )
36 {
37 NW_NULL_ASSERT( m_Allocator );
38 NW_NULL_ASSERT( m_Layout );
39
40 // コリジョン形状を登録
41 SmLayoutResource* layoutRes = m_Layout->GetLayoutResource();
42 nw::ut::Rect rectBounding = layoutRes->GetRect( bounding );
43 m_Collision.SetRect( rectBounding );
44
45 if ( string )
46 {
47 m_Layout->SetString( string );
48 }
49
50 #if 0
51 // 文字列を登録
52 // TextBox ペインか
53 const nw::lyt::Pane* pane = layoutRes->GetPane("TextBox_00");
54 if ( nw::lyt::TextBox* pTextBox = nw::ut::DynamicCast<nw::lyt::TextBox*>(pane) )
55 {
56 pTextBox->SetString( string );
57 }
58 #endif
59
60 // 初期状態
61 SetSwitch( m_IsSwitch );
62 }
63
64
65 //----------------------------------------
66 // デストラクタ
~SmLytButton()67 SmLytButton::~SmLytButton()
68 {
69
70 }
71
72
73 //----------------------------------------
74 // 位置
SetPosition(f32 x,f32 y)75 void SmLytButton::SetPosition( f32 x, f32 y )
76 {
77 m_Layout->SetPosition( x, y );
78 // m_Collision.SetXY( x, y );
79
80 // コリジョンの矩形を決定します。
81 m_Collision.SetXY( SM_LOWER_SCREEN_HALF_SIZE_W + x - m_Collision.GetWidth()/2.f,
82 SM_LOWER_SCREEN_HALF_SIZE_H + y - m_Collision.GetHeight()/2.f );
83 }
84
85
86
87 //----------------------------------------
88 // 描画
Render()89 void SmLytButton::Render()
90 {
91 return;
92 }
93
94
95 //----------------------------------------
96 // ボタン状態を設定する
SetSwitch(bool bswitch)97 void SmLytButton::SetSwitch( bool bswitch )
98 {
99 m_IsSwitch = bswitch;
100
101 if ( m_IsSwitch )
102 {
103 m_Layout->SetAnimationNo( 0 );
104 }
105 else
106 {
107 m_Layout->SetAnimationNo( 1 );
108 }
109 }
110
111
112
113 //----------------------------------------
114 // ボタン状態をスワップする
SwapSwitch()115 void SmLytButton::SwapSwitch()
116 {
117 if ( m_IsSwitch )
118 {
119 m_IsSwitch = false;
120 m_Layout->SetAnimationNo( 1 );
121 }
122 else
123 {
124 m_IsSwitch = true;
125 m_Layout->SetAnimationNo( 0 );
126 }
127 }
128
129
130 //----------------------------------------
131 // メッセージ受信
ReceveMessage(SmMessageType type,void * object,uint targetId)132 bool SmLytButton::ReceveMessage( SmMessageType type, void* object, uint targetId )
133 {
134 NW_UNUSED_VARIABLE(targetId);
135
136 SmTouchPanelStatus* touchPanelStaus = NULL;
137
138 switch( type )
139 {
140 case SM_MESSAGE_TUCHPANEL_PRESS:
141 {
142 touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
143 NW_NULL_ASSERT( touchPanelStaus );
144
145 // 他処理で使用中でなければ
146 if ( !touchPanelStaus->IsGrab() )
147 {
148 // コリジョン内であれば、
149 if ( m_Collision.CheckInner( touchPanelStaus->GetX(), touchPanelStaus->GetY() ) )
150 {
151 // 掴む
152 touchPanelStaus->Grab();
153 m_IsGrab = true;
154 m_IsPressed = true;
155
156 m_Layout->SetAnimationNo( 2 );
157 }
158 }
159 }
160 break;
161
162 case SM_MESSAGE_TUCHPANEL_MOTION:
163 {
164 touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
165 NW_NULL_ASSERT( touchPanelStaus );
166 if ( m_IsGrab )
167 {
168 m_LastX = touchPanelStaus->GetX();
169 m_LastY = touchPanelStaus->GetY();
170
171 // 選択中アニメを再生
172 m_Layout->AddAnimationFrame( 1.f );
173 }
174 }
175 break;
176
177 case SM_MESSAGE_TUCHPANEL_RELEASE:
178 {
179 touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
180 NW_NULL_ASSERT( touchPanelStaus );
181
182 // 放す
183 if ( m_IsGrab )
184 {
185 touchPanelStaus->UnGrab();
186 m_IsGrab = false;
187 m_IsPressed = false;
188
189 // 選択中アニメを0.fフレームに戻します。
190 m_Layout->SetAnimationFrame( 0.f );
191
192 // コリジョン範囲内であれば、指定されたターゲットに対してメッセージを送信します。
193 if ( m_Collision.CheckInner( m_LastX, m_LastY ) )
194 {
195 // 状態をスワップする
196 SwapSwitch();
197
198 // Updateを送信
199 if ( m_Target )
200 {
201 m_Target->SendMessage( SM_MESSAGE_UPDATE_PARAM, NULL, m_Id );
202 }
203 }
204 }
205 }
206 break;
207
208 default:
209 break;
210 }
211 return 0;
212 }
213
214 } // namespace
215
216
217