JNR
laMultiKey.cpp
1 /*
2 
3 Jump'n'Run Engine
4 http://www.atanaslaskov.com/jnr/
5 
6 BSD LICENSE
7 Copyright (c) 2007-2013, Atanas Laskov
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12 1. Redistributions of source code must retain the above copyright notice,
13 this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright notice,
15 this list of conditions and the following disclaimer in the documentation
16 and/or other materials provided with the distribution.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 */
29 
30 //
31 // FILE: laMultiKey.cpp
32 //
33 // This class provides the functionality for detecting multiple key presses
34 // within a specified time interval.
35 //
36 // The number of key presses (n) and interval in seconds (sec) can be specified
37 //
38 // This functionality can be used as an easy way to add more responsiveness
39 // to player controls (e.g. single jump, double jump).
40 //
41 // Copyright (C) 2007-2013 Atanas Laskov, <latanas@gmail.com>
42 //
43 #include "stdafx.h"
44 #include "Core.h"
45 
46 // Constructor
47 //
48 laMultiKey::laMultiKey()
49 {
50  _key = KEY_SPACE;
51  _dHitInterval = 1;
52  _nPressesRequired = 2;
53 
54  reset();
55 }
56 
57 // Set multikey parameters
58 //
59 void laMultiKey::set(KEY key, double sec, unsigned n, M_BOOL first_down)
60 {
61  _key = key;
62  _dHitInterval = sec;
63  _nPressesRequired = n;
64  _bExpectedFirstState = first_down;
65 
66  reset();
67 }
68 
69 // Get parameters
70 //
71 void laMultiKey::reset()
72 {
73  _bNextDown = _bExpectedFirstState;
74  _nPressed = 0;
75  _dInterval = 0;
76  _bActivated = M_FALSE;
77 }
78 
79 // Animate the multikey ( get input and update internal state)
80 //
81 M_BOOL laMultiKey::animate(laInputManager* pi, laTimer &t)
82 {
83  if(_bActivated) return M_TRUE;
84 
85  // Expecting key press
86  if(_bNextDown)
87  {
88  //Key is pressed
89  if( pi->key(_key) )
90  {
91  //Nullify counter if it comes too late
92  //Except for the inital key press that triggers the sequence
93  if( (_dInterval>_dHitInterval) && (_nPressed!=0) ) reset();
94 
95  //Otherwise wait for a key release
96  else
97  {
98  _bNextDown = M_FALSE;
99  _dInterval = 0;
100 
101  //or set the flag if we already have enough key presses
102  if( (_nPressed >= _nPressesRequired) && (!_bExpectedFirstState) )
103  _bActivated = M_TRUE;
104  }
105  }
106  else _dInterval += t.delta();
107 
108  }
109 
110  //Expecting key release
111  else
112  {
113  //Key is released
114  if( !(pi->key(_key)) )
115  {
116  //Nullify counter if it comes too late
117  if( (_dInterval>_dHitInterval) ) reset();
118 
119  //Otherwise increment counter and wait for the next key press
120  else
121  {
122  _nPressed ++;
123  _bNextDown = M_TRUE;
124  _dInterval = 0;
125 
126  //or set the flag if we already have enough key presses
127  if( (_nPressed >= _nPressesRequired) && _bExpectedFirstState)
128  _bActivated = M_TRUE;
129  }
130  }
131  /*else*/ _dInterval += t.delta();
132  }
133 
134  return isActivated();
135 }