1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     SmSliderBar.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/SmSliderBar.h"
21 #include "../include/SmTouchPanelStatus.h"
22 
23 namespace
24 {
25 #define SM_BAR_HALF_SIZE  (8.f)
26 #define SM_SLIDER_COL_HALF_SIZE  (10.f)
27 #define SM_SLIDER_HALF_SIZE  (8.f)
28 
29 
30 //----------------------------------------
31 // コンストラクタ
SmSliderBar(uint x,uint y,uint length,SmSliderBarType type)32 SmSliderBar::SmSliderBar( uint x, uint y, uint length, SmSliderBarType type )
33 : SmMessage( NULL ),
34   m_SliderType( type ),
35   m_Bar( NULL ),
36   m_Slider( NULL ),
37   m_Length( length ),
38   m_IsGrab( false ),
39   m_BaseX( x ),
40   m_BaseY( y ),
41   m_GrabX( 0 ),
42   m_GrabY( 0 ),
43   m_OffsetX( 0 ),
44   m_OffsetY( 0 ),
45   m_PreOffsetX( 0 ),
46   m_PreOffsetY( 0 )
47 {
48     NW_NULL_ASSERT( m_Allocator );
49 
50     // Barポリゴン生成
51     m_Bar = new Sm2DPrimPC( 4 );
52     NW_NULL_ASSERT( m_Bar );
53     {
54         m_Bar->SetVertex( 0, x - SM_BAR_HALF_SIZE, y,           nw::ut::Color8::BLACK );
55         m_Bar->SetVertex( 1, x - SM_BAR_HALF_SIZE, y + length,  nw::ut::Color8::BLACK );
56         m_Bar->SetVertex( 2, x + SM_BAR_HALF_SIZE, y,           nw::ut::Color8::BLACK );
57         m_Bar->SetVertex( 3, x + SM_BAR_HALF_SIZE, y + length,  nw::ut::Color8::BLACK );
58     }
59     m_Bar->Update();
60 
61     // Sliderポリゴン生成
62     m_Slider = new Sm2DPrimPC( 4 );
63     NW_NULL_ASSERT( m_Bar );
64     {
65         m_Slider->SetVertex( 0, x - SM_SLIDER_HALF_SIZE,   y - SM_SLIDER_HALF_SIZE,   nw::ut::Color8::RED );
66         m_Slider->SetVertex( 1, x - SM_SLIDER_HALF_SIZE,   y + SM_SLIDER_HALF_SIZE,   nw::ut::Color8::RED );
67         m_Slider->SetVertex( 2, x + SM_SLIDER_HALF_SIZE,   y - SM_SLIDER_HALF_SIZE,   nw::ut::Color8::RED );
68         m_Slider->SetVertex( 3, x + SM_SLIDER_HALF_SIZE,   y + SM_SLIDER_HALF_SIZE,   nw::ut::Color8::RED );
69     }
70 
71     // ユニフォーム(オフセット)の値がキャッシュと合わないので、キャッシュを生成しない
72 //    m_Slider->Update();
73 
74     // コリジョン形状を登録
75     m_Collision.SetRect(   x - SM_SLIDER_COL_HALF_SIZE,
76                            y - SM_SLIDER_COL_HALF_SIZE,
77                            SM_SLIDER_COL_HALF_SIZE * 2.f,
78                            SM_SLIDER_COL_HALF_SIZE * 2.f );
79 }
80 
81 
82 //----------------------------------------
83 // デストラクタ
~SmSliderBar()84 SmSliderBar::~SmSliderBar()
85 {
86     if( m_Bar ) delete m_Bar;
87     m_Bar = NULL;
88 
89     if( m_Slider ) delete m_Slider;
90     m_Slider = NULL;
91 }
92 
93 
94 //----------------------------------------
95 // 描画
Render()96 void SmSliderBar::Render()
97 {
98     m_Bar->Render( GL_TRIANGLE_STRIP );
99     m_Slider->Render( GL_TRIANGLE_STRIP, static_cast<f32>(m_OffsetX), static_cast<f32>(m_OffsetY) );
100 }
101 
102 
103 //----------------------------------------
104 // メッセージ受信
ReceveMessage(SmMessageType type,void * object,uint targetId)105 bool SmSliderBar::ReceveMessage( SmMessageType type, void* object, uint targetId )
106 {
107     NW_UNUSED_VARIABLE(targetId);
108 
109     SmTouchPanelStatus*  touchPanelStaus = NULL;
110 
111     switch( type )
112     {
113     case SM_MESSAGE_TUCHPANEL_PRESS:
114         touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
115         NW_NULL_ASSERT( touchPanelStaus );
116 
117         // コリジョン内であれば、
118         if( m_Collision.CheckInner( touchPanelStaus->GetX(), touchPanelStaus->GetY() ) )
119         {
120             if( !touchPanelStaus->IsGrab() )
121             {
122                 // 掴む
123                 touchPanelStaus->Grab();
124                 m_IsGrab = true;
125 
126                 // 掴んだ位置
127                 m_GrabX = touchPanelStaus->GetX();
128                 m_GrabY = touchPanelStaus->GetY();
129             }
130         }
131         break;
132 
133 
134     case SM_MESSAGE_TUCHPANEL_RELEASE:
135         touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
136         NW_NULL_ASSERT( touchPanelStaus );
137 
138         // 放す
139         if( m_IsGrab )
140         {
141             touchPanelStaus->UnGrab();
142             m_IsGrab = false;
143 
144             m_PreOffsetX = m_OffsetX;
145             m_PreOffsetY = m_OffsetY;
146         }
147         break;
148 
149 
150     case SM_MESSAGE_TUCHPANEL_MOTION:
151         touchPanelStaus = static_cast<SmTouchPanelStatus*>(object);
152         NW_NULL_ASSERT( touchPanelStaus );
153 
154         // 差分を計算し、制限をかける
155         if( m_IsGrab )
156         {
157             // オフセットを求める
158             m_OffsetX = 0;//m_GrabX + static_cast<s32>( touchPanelStaus->GetX() );
159             m_OffsetY = static_cast<s32>( touchPanelStaus->GetY() ) - m_GrabY + m_PreOffsetY;
160 
161             // 制限
162             if( m_OffsetY <= 0 ) m_OffsetY = 0;
163             if( m_Length < m_OffsetY ) m_OffsetY = m_Length;
164 
165             // コリジョン側も更新
166             m_Collision.SetXY( m_BaseX + m_OffsetX - SM_SLIDER_COL_HALF_SIZE,
167                                m_BaseY + m_OffsetY - SM_SLIDER_COL_HALF_SIZE );
168 
169             // Updateを送信
170             if( m_Target )
171             {
172                 m_Target->SendMessage( SM_MESSAGE_UPDATE_PARAM, NULL, m_Id );
173             }
174         }
175         break;
176 
177     }
178 
179     return 0;
180 }
181 
182 } // namespace
183 
184 
185