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