1 /*---------------------------------------------------------------------------* 2 Project: NintendoWare 3 File: demo_Pad.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:$ 14 *---------------------------------------------------------------------------*/ 15 16 #include <nw/demo/demo_Pad.h> 17 18 namespace nw { 19 namespace demo { 20 21 Pad* PadFactory::s_pPad = NULL; 22 23 //-------------------------------------------------------------------------- 24 Pad* Create(os::IAllocator * allocator)25Pad::Create(os::IAllocator* allocator) 26 { 27 NW_NULL_ASSERT( allocator ); 28 29 void* memory = allocator->Alloc( sizeof(Pad) ); 30 NW_ASSERT( memory != NULL ); 31 Pad* pad = new(memory) Pad(); 32 33 pad->m_Allocator = allocator; 34 35 return pad; 36 } 37 38 //-------------------------------------------------------------------------- 39 void Destroy()40Pad::Destroy() 41 { 42 os::IAllocator* allocator = this->m_Allocator; 43 44 NW_ASSERT( allocator != NULL ); 45 46 this->~Pad(); 47 allocator->Free(this); 48 } 49 50 //-------------------------------------------------------------------------- 51 void Initialize()52Pad::Initialize() 53 { 54 55 nn::Result result = nn::hid::CTR::Initialize(); 56 // 下記マクロを使用すると、他所でnn::hid::CTR::Initialize()が呼ばれると 57 // エラーを出すため、コメントアウトしています。 58 //NN_UTIL_PANIC_IF_FAILED( result ); 59 60 Reset(); 61 } 62 63 //-------------------------------------------------------------------------- 64 void Finalize()65Pad::Finalize() 66 { 67 // 注意:PadをFinalizeすると、hidのFinalize()を行うため、 68 // これ以降でhidを使用できなくなります。 69 nn::hid::CTR::Finalize(); 70 } 71 72 //-------------------------------------------------------------------------- 73 void Update()74Pad::Update() 75 { 76 if (m_IsTestMode) 77 { 78 m_PadButton.Update( m_TestPadStatus ); 79 80 m_AnalogStickX = 0; 81 m_AnalogStickY = 0; 82 m_AnalogStick.x = 0; 83 m_AnalogStick.y = 0; 84 } 85 else 86 { 87 nn::hid::PadReader padReader; 88 padReader.ReadLatest( &m_PadStatus ); 89 m_PadButton.Update( static_cast<u32>( m_PadStatus.hold ) ); 90 91 m_AnalogStickX = m_PadStatus.stick.x; 92 m_AnalogStickY = m_PadStatus.stick.y; 93 m_AnalogStick.x = static_cast<f32>( m_PadStatus.stick.x ) / nn::hid::CTR::LIMIT_OF_STICK_CLAMP_MAX; 94 m_AnalogStick.y = static_cast<f32>( m_PadStatus.stick.y ) / nn::hid::CTR::LIMIT_OF_STICK_CLAMP_MAX; 95 } 96 } 97 98 //-------------------------------------------------------------------------- 99 void Reset()100Pad::Reset() 101 { 102 m_PadButton.Reset(); 103 } 104 105 //-------------------------------------------------------------------------- 106 bool IsButtonPress(Button button) const107Pad::IsButtonPress( Button button ) const 108 { 109 return m_PadButton.IsButtonPress( button ); 110 } 111 112 //-------------------------------------------------------------------------- 113 bool IsButtonDown(Button button) const114Pad::IsButtonDown( Button button ) const 115 { 116 return m_PadButton.IsButtonDown( button ); 117 } 118 119 //-------------------------------------------------------------------------- 120 bool IsButtonUp(Button button) const121Pad::IsButtonUp( Button button ) const 122 { 123 return m_PadButton.IsButtonUp( button ); 124 } 125 126 //-------------------------------------------------------------------------- 127 bool IsButtonRepeat(Button button) const128Pad::IsButtonRepeat( Button button ) const 129 { 130 return m_PadButton.IsButtonRepeat( button ); 131 } 132 133 //-------------------------------------------------------------------------- 134 bool IsButtonRepeatFast(Button button) const135Pad::IsButtonRepeatFast( Button button ) const 136 { 137 return m_PadButton.IsButtonRepeatFast( button ); 138 } 139 140 141 //-------------------------------------------------------------------------- 142 void Initialize(os::IAllocator * allocator)143PadFactory::Initialize(os::IAllocator* allocator) 144 { 145 NW_ASSERT( s_pPad == NULL ); 146 147 s_pPad = Pad::Create( allocator ); 148 s_pPad->Initialize(); 149 } 150 151 //-------------------------------------------------------------------------- 152 void Finalize()153PadFactory::Finalize() 154 { 155 NW_NULL_ASSERT( s_pPad ); 156 157 s_pPad->Finalize(); 158 159 s_pPad->Destroy(); 160 s_pPad = NULL; 161 } 162 163 //-------------------------------------------------------------------------- 164 Pad* GetPad()165PadFactory::GetPad() 166 { 167 NW_NULL_ASSERT( s_pPad ); 168 169 return s_pPad; 170 } 171 172 } // namespace nw::demo 173 } // namespace nw 174