1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     demo_PadButton.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_PadButton.h>
17 #include <algorithm>
18 
19 namespace nw {
20 namespace demo {
21 namespace internal {
22 
23 //--------------------------------------------------------------------------
PadButton()24 PadButton::PadButton()
25 {
26     Reset();
27 }
28 
29 //--------------------------------------------------------------------------
30 void
Update(u32 buttonStatus)31 PadButton::Update( u32 buttonStatus )
32 {
33     if ( m_ResetFlag )
34     {
35         m_ButtonDown = m_ButtonUp = m_ButtonRepeat = m_ButtonRepeatFast = 0;
36         m_ResetFlag = false;
37     }
38     else
39     {
40         m_ButtonDown = ( m_ButtonPress ^ buttonStatus ) & buttonStatus;
41         m_ButtonUp = ( m_ButtonPress ^ buttonStatus ) & m_ButtonPress;
42         m_ButtonRepeat = m_ButtonDown;
43         m_ButtonRepeatFast = m_ButtonDown;
44     }
45     m_ButtonPress = buttonStatus;
46 
47     u32 mask = 1;
48     for ( int i = 0; i < BUTTON_COUNT_MAX; ++i, mask <<= 1 )
49     {
50         if ( m_ButtonPress & mask )
51         {
52             --m_RepeatCounter[ i ];
53             --m_RepeatCounterFast[ i ];
54 
55             if ( m_RepeatCounter[ i ] == 0 )
56             {
57                 m_ButtonRepeat |= mask;
58                 m_RepeatCounter[ i ] = REPEAT_SPAN;
59             }
60             if ( m_RepeatCounterFast[ i ] == 0 )
61             {
62                 m_ButtonRepeatFast |= mask;
63                 m_RepeatCounterFast[ i ] = 1;
64             }
65         }
66         else
67         {
68             m_RepeatCounter[ i ] = REPEAT_START;
69             m_RepeatCounterFast[ i ] = REPEAT_START;
70         }
71     }
72 }
73 
74 //--------------------------------------------------------------------------
75 void
Reset()76 PadButton::Reset()
77 {
78     m_ResetFlag = true;
79 
80     m_ButtonPress = 0;
81     m_ButtonDown = 0;
82     m_ButtonUp = 0;
83     m_ButtonRepeat = 0;
84     m_ButtonRepeatFast = 0;
85 
86     std::fill_n(m_RepeatCounter, BUTTON_COUNT_MAX, static_cast<u8>(REPEAT_START));
87     std::fill_n(m_RepeatCounterFast, BUTTON_COUNT_MAX, static_cast<u8>(REPEAT_START));
88 }
89 
90 //--------------------------------------------------------------------------
91 void
ClearTriggerFlag()92 PadButton::ClearTriggerFlag()
93 {
94     m_ButtonDown = 0;
95     m_ButtonUp = 0;
96     m_ButtonRepeat = 0;
97     m_ButtonRepeatFast = 0;
98 }
99 
100 } // namespace nw::demo::internal
101 } // namespace nw::demo
102 } // namespace nw
103