1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     SmLytSliderBar.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 
18 #include "../include/SmLytSliderBar.h"
19 #include "../include/SmTouchPanelStatus.h"
20 
21 namespace
22 {
23 
24 //----------------------------------------
25 // コンストラクタ
SmLytSliderBar(f32 x,f32 y,SmLayout * smLayout)26 SmLytSliderBar::SmLytSliderBar( f32 x, f32 y, SmLayout* smLayout )
27 : SmMessage( NULL ),
28   m_Layout( smLayout ),
29   m_HandlePane( NULL ),
30   m_Param( 0.0f ),
31   m_IsGrab( false ),
32   m_RangeMinY( 0 ),
33   m_RangeMaxY( 0 ),
34   m_BaseY( y )
35 {
36     NW_NULL_ASSERT( m_Layout );
37 
38     SmLayoutResource* layoutRes = m_Layout->GetLayoutResource();
39     nw::ut::Rect rectBase   = layoutRes->GetRect( "SliderBase" );
40     nw::ut::Rect rectHandle = layoutRes->GetRect( "SliderBounding" );
41     m_HandlePane            = layoutRes->GetPane( "SliderHandle00" );
42 
43     m_Collision.SetRect( rectHandle );
44 
45     m_Layout->SetPosition( x, y );
46 
47     // コリジョンの矩形を決定します。
48     m_Collision.SetXY(  SM_LOWER_SCREEN_HALF_SIZE_W + x - m_Collision.GetWidth()/2.f,
49                         SM_LOWER_SCREEN_HALF_SIZE_H + y - m_Collision.GetHeight()/2.f );
50 
51     // ハンドルの上限下限値を求めます。
52     m_RangeMinY = SM_LOWER_SCREEN_HALF_SIZE_H + y - rectBase.GetHeight()/2.f;
53     m_RangeMaxY = SM_LOWER_SCREEN_HALF_SIZE_H + y + rectBase.GetHeight()/2.f;
54 
55     // パラメータを初期化し、ハンドルとコリジョンを更新します。
56     m_Param = 0.0f;
57     SetParam( m_Param );
58 }
59 
60 
61 //----------------------------------------
62 // デストラクタ
~SmLytSliderBar()63 SmLytSliderBar::~SmLytSliderBar()
64 {
65 
66 }
67 
68 
69 //----------------------------------------
70 // 描画
Render()71 void SmLytSliderBar::Render()
72 {
73     return;
74 }
75 
76 
77 //----------------------------------------
78 // パラメータ値をセットする
SetParam(f32 param)79 void SmLytSliderBar::SetParam( f32 param )
80 {
81      if ( param < 0.0f ) return;
82      if ( param > 1.0f ) return;
83 
84     s32 posY = ( m_RangeMaxY - m_RangeMinY ) * param + m_RangeMinY;
85 
86     // ハンドルの表示位置を更新
87     m_Layout->SetPanePosition( m_HandlePane, 0.f, SM_LOWER_SCREEN_HALF_SIZE_H - posY + m_BaseY );
88 
89     // コリジョンを更新
90     m_Collision.SetXY( m_Collision.GetX(), posY - m_Collision.GetHeight()/2.f );
91 
92     // todo
93     m_Param = param;
94 }
95 
96 
97 //----------------------------------------
98 // メッセージ受信
ReceveMessage(SmMessageType type,void * object,uint targetId)99 bool SmLytSliderBar::ReceveMessage( SmMessageType type, void* object, uint targetId )
100 {
101     NW_UNUSED_VARIABLE(targetId);
102 
103     SmTouchPanelStatus*  touchPanelStaus = NULL;
104 
105     switch( type )
106     {
107     case SM_MESSAGE_TUCHPANEL_PRESS:
108         touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
109         NW_NULL_ASSERT( touchPanelStaus );
110 
111          if ( m_Collision.CheckInner( touchPanelStaus->GetX(), touchPanelStaus->GetY() ) )
112         {
113              if ( !touchPanelStaus->IsGrab() )
114             {
115                 touchPanelStaus->Grab();
116                 m_IsGrab = true;
117             }
118         }
119         break;
120 
121     case SM_MESSAGE_TUCHPANEL_RELEASE:
122         touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
123         NW_NULL_ASSERT( touchPanelStaus );
124 
125          if ( m_IsGrab )
126         {
127             touchPanelStaus->UnGrab();
128             m_IsGrab = false;
129         }
130         break;
131 
132 
133     case SM_MESSAGE_TUCHPANEL_MOTION:
134         touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
135         NW_NULL_ASSERT( touchPanelStaus );
136 
137          if ( m_IsGrab )
138         {
139             // オフセットを求める
140             s32 currentY = touchPanelStaus->GetY();
141 
142             // 制限
143              if ( currentY < m_RangeMinY )    currentY = m_RangeMinY;
144              if ( m_RangeMaxY < currentY )    currentY = m_RangeMaxY;
145 
146             // パラメータを計算しセットします。
147             m_Param = static_cast<f32>( currentY - m_RangeMinY );
148             m_Param /= static_cast<f32>( m_RangeMaxY - m_RangeMinY );
149             SetParam( m_Param );
150 
151             // ターゲットに更新メッセージを送信します。
152              if ( m_Target )
153             {
154                 m_Target->SendMessage( SM_MESSAGE_UPDATE_PARAM, NULL, m_Id );
155             }
156         }
157         break;
158 
159     }
160 
161     return 0;
162 }
163 
164 } // namespace
165 
166 
167