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