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