1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_Pad.h
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 #ifndef NW_DEMO_PAD_H_
19 #define NW_DEMO_PAD_H_
20 
21 #include <nn/types.h>
22 #include <nn/hid.h>
23 
24 #include <nw/demo/demo_PadButton.h>
25 #include <nw/demo/demo_Memory.h>
26 #include <nw/math/math_Types.h>
27 #include <nw/ut/ut_Preprocessor.h>
28 
29 namespace nw {
30 namespace demo {
31 
32 class PadFactory;
33 
34 //---------------------------------------------------------------------------
35 //! @brief        デモ用のゲームパッドを表すクラスです。
36 //---------------------------------------------------------------------------
37 class Pad
38 {
39 private:
40     NW_DISALLOW_COPY_AND_ASSIGN(Pad);
41 
42 public:
43     friend class PadFactory;
44 
45     //! @brief ボタンを表すビット値です。
46     enum Button
47     {
48         BUTTON_A               = nn::hid::CTR::BUTTON_A,            //!< @brief A ボタンです。
49         BUTTON_B               = nn::hid::CTR::BUTTON_B,            //!< @brief B ボタンです。
50         BUTTON_START           = nn::hid::CTR::BUTTON_START,        //!< @brief START ボタンです。
51         BUTTON_RIGHT           = nn::hid::CTR::BUTTON_RIGHT,        //!< @brief 十字ボタンの右です。
52         BUTTON_LEFT            = nn::hid::CTR::BUTTON_LEFT,         //!< @brief 十字ボタンの左です。
53         BUTTON_UP              = nn::hid::CTR::BUTTON_UP,           //!< @brief 十字ボタンの上です。
54         BUTTON_DOWN            = nn::hid::CTR::BUTTON_DOWN,         //!< @brief 十字ボタンの下です。
55         BUTTON_R               = nn::hid::CTR::BUTTON_R,            //!< @brief R ボタンです。
56         BUTTON_L               = nn::hid::CTR::BUTTON_L,            //!< @brief L ボタンです。
57         BUTTON_X               = nn::hid::CTR::BUTTON_X,            //!< @brief X ボタンです。
58         BUTTON_Y               = nn::hid::CTR::BUTTON_Y,            //!< @brief Y ボタンです。
59         BUTTON_DEBUG           = nn::hid::CTR::BUTTON_DEBUG,        //!< @brief DEBUG ボタンです。
60         // BUTTON_HEADPHONE       = nn::hid::CTR::BUTTON_HEADPHONE,
61         BUTTON_EMULATION_RIGHT = nn::hid::CTR::BUTTON_RIGHT,        //!< @brief アナログスティックによる十字ボタンエミュレーションの右です。
62         BUTTON_EMULATION_LEFT  = nn::hid::CTR::BUTTON_RIGHT,        //!< @brief アナログスティックによる十字ボタンエミュレーションの左です。
63         BUTTON_EMULATION_UP    = nn::hid::CTR::BUTTON_RIGHT,        //!< @brief アナログスティックによる十字ボタンエミュレーションの上です。
64         BUTTON_EMULATION_DOWN  = nn::hid::CTR::BUTTON_RIGHT,        //!< @brief アナログスティックによる十字ボタンエミュレーションの下です。
65         BUTTON_SELECT          = nn::hid::CTR::PSEUDO_BUTTON_SELECT //!< @brief SELECT ボタンです。
66     };
67 
68     //! @brief パッド状態を初期化します。
69     //!
70     void Initialize();
71 
72     //! @brief パッド状態の終了処理を行います。
73     //!
74     void Finalize();
75 
76     //! @brief パッド状態を更新します。
77     //!
78     void Update();
79 
80     //! @brief パッドの状態を初期状態にします。
81     //!
82     void Reset();
83 
84 
85     //! @brief ボタンが押されている間を判定します。
86     //!
87     //! @param[in]    button  ボタンを表すビット列です。
88     //!
89     //! @return       ボタンを押している間、trueを返します。
90     //!
91     bool IsButtonPress( Button button ) const;
92 
93     //! @brief ボタンが押された時を判定します。
94     //!
95     //! @param[in]    button  ボタンを表すビット列です。
96     //!
97     //! @return       ボタンを押した時だけ、trueを返します。
98     //!
99     bool IsButtonDown( Button button ) const;
100 
101     //! @brief ボタンが離された時を判定します。
102     //!
103     //! @param[in]    button  ボタンを表すビット列です。
104     //!
105     //! @return       ボタンを離した時だけ、trueを返します。
106     //!
107     bool IsButtonUp( Button button ) const;
108 
109     //! @brief ボタンが押され続けられている間、一定間隔でtrueを返します。
110     //!
111     //! @param[in]    button  ボタンを表すビット列です。
112     //!
113     //! @return       ボタンが押され続けられている間、trueを返します。
114     //!
115     bool IsButtonRepeat( Button button ) const;
116 
117     //! @brief ボタンが押され続けられている間、一定間隔でtrueを返します。
118     //!        IsButtonRepeat()より早い間隔となります。
119     //!
120     //! @param[in]    button  ボタンを表すビット列です。
121     //!
122     //! @return       ボタンが押され続けられている間、trueを返します。
123     //!
124     bool IsButtonRepeatFast( Button button ) const;
125 
126     //! @brief アナログスティックのx値を得ます。
127     //!
128     //! @return       アナログスティックのx値を返します。
129     //!
GetAnalogStickX()130     s16 GetAnalogStickX() const { return m_AnalogStickX; }
131 
132     //! @brief アナログスティックのy値を得ます。
133     //!
134     //! @return       アナログスティックのy値を返します。
135     //!
GetAnalogStickY()136     s16 GetAnalogStickY() const { return m_AnalogStickY; }
137 
138     //! @brief アナログスティックの値を得ます。
139     //!
140     //! @return       アナログスティックの値をnw::math::VEC2型で返します。
141     //!               各要素の値の範囲は -1 ~ 1 です。
142     //!
GetAnalogStick()143     nw::math::VEC2 GetAnalogStick() const { return m_AnalogStick; }
144 
145     //! @brief      テストモードを切り替えます。
146     //!
147     //!             テストモードにすると、このクラスはパッドの出力を無視して、
148     //!             SetTestPadStatus の入力を使用するようになります。
149     //!
150     //! @param[in]  enabled true を指定すると、テストモードを有効にします。
EnableTestMode(bool enabled)151     void EnableTestMode(bool enabled) { m_IsTestMode = enabled; }
152 
153     //! @brief      テストモードのパッド入力を設定します。
154     //!
155     //! @param[in]  status nn::hid::PadStatus.hold に相当する値です。
SetTestPadStatus(u32 status)156     void SetTestPadStatus(u32 status) { m_TestPadStatus = status; }
157 
158 protected:
159     //! @brief デモ用ゲームパッドを生成します。
160     //!
161     //! @return 生成したデモ用ゲームパッドを返します。
162     //!
163     static Pad* Create(os::IAllocator* allocator);
164 
165     //! @brief デモ用ゲームパッドを破棄します。
166     //!
167     void Destroy();
168 
169 private:
170     //! @brief コンストラクタです。
171     //!
Pad()172     Pad()
173     : m_AnalogStickX(0),
174       m_AnalogStickY(0),
175       m_AnalogStick(math::VEC2::Zero()),
176       m_IsTestMode(false),
177       m_TestPadStatus(0)
178     {}
179 
180 private:
181     internal::PadButton m_PadButton;
182     os::IAllocator* m_Allocator;
183     nn::hid::PadStatus m_PadStatus;
184 
185     s16 m_AnalogStickX;
186     s16 m_AnalogStickY;
187     nw::math::VEC2 m_AnalogStick;
188 
189     bool m_IsTestMode;
190     u32 m_TestPadStatus;
191 
192 };
193 
194 //---------------------------------------------------------------------------
195 //! @brief        デモ用のゲームパッドをひとつだけ生成するクラスです。
196 //---------------------------------------------------------------------------
197 class PadFactory
198 {
199 public:
200     //! @brief 初期化します。
201     //!
202     //! @param[in]    allocator アロケーターです。
203     //!
204     static void Initialize(os::IAllocator* allocator);
205 
206     //! @brief 終了処理を行います。
207     //!
208     static void Finalize();
209 
210     //! @brief ゲームパッドを取得します。
211     //!
212     //! @return ゲームパッドへのポインタを返します。
213     //!
214     static Pad* GetPad();
215 
216 private:
217     //! @brief コンストラクタです。
218     //!
PadFactory()219     PadFactory(){}
220     static Pad* s_pPad;
221 
222 };
223 
224 } // namespace nw::demo
225 } // namespace nw
226 
227 #endif /* NW_DEMO_PAD_H_ */
228 
229