style cleanup: comment format
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderKeyboardDevice.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/BlenderRoutines/KX_BlenderKeyboardDevice.cpp
29  *  \ingroup blroutines
30  */
31
32
33 #ifdef _MSC_VER
34    /* annoying warnings about truncated STL debug info */
35 #  pragma warning (disable:4786)
36 #endif 
37
38 #include "KX_BlenderKeyboardDevice.h"
39 #include "KX_KetsjiEngine.h"
40
41 KX_BlenderKeyboardDevice::KX_BlenderKeyboardDevice()
42         : m_hookesc(false)
43 {
44
45 }
46 KX_BlenderKeyboardDevice::~KX_BlenderKeyboardDevice()
47 {
48
49 }
50
51 /**
52  * IsPressed gives boolean information about keyboard status, true if pressed, false if not
53  */
54
55 bool KX_BlenderKeyboardDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
56 {
57         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
58         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
59                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
60         return pressed;
61 }
62 /*const SCA_InputEvent& KX_BlenderKeyboardDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
63 {
64         return m_eventStatusTables[m_currentTable][inputcode];
65 }
66 */
67 /**
68  * NextFrame toggles currentTable with previousTable,
69  * and copy relevant event information from previous to current
70  * (pressed keys need to be remembered)
71  */
72 void    KX_BlenderKeyboardDevice::NextFrame()
73 {
74         SCA_IInputDevice::NextFrame();
75         
76         // now convert justpressed keyevents into regular (active) keyevents
77         int previousTable = 1-m_currentTable;
78         for (int keyevent= KX_BEGINKEY; keyevent<= KX_ENDKEY;keyevent++)
79         {
80                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][keyevent];
81                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
82                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
83                 {
84                         m_eventStatusTables[m_currentTable][keyevent] = oldevent;
85                         m_eventStatusTables[m_currentTable][keyevent].m_status = SCA_InputEvent::KX_ACTIVE;
86                 }
87         }
88 }
89
90 /**
91  * ConvertBlenderEvent translates blender keyboard events into ketsji kbd events
92  * extra event information is stored, like ramp-mode (just released/pressed)
93 */
94 bool KX_BlenderKeyboardDevice::ConvertBlenderEvent(unsigned short incode, short val)
95 {
96         bool result = false;
97         
98         // convert event
99         KX_EnumInputs kxevent = this->ToNative(incode);
100
101         // only process it, if it's a key
102         if (kxevent >= KX_BEGINKEY && kxevent <= KX_ENDKEY)
103         {
104                 int previousTable = 1-m_currentTable;
105
106                 if (val == KM_PRESS || val == KM_DBL_CLICK)
107                 {
108                         if (kxevent == KX_KetsjiEngine::GetExitKey() && val != 0 && !m_hookesc)
109                                 result = true;
110                         if (kxevent == KX_PAUSEKEY && val && (IsPressed(KX_LEFTCTRLKEY) || IsPressed(KX_RIGHTCTRLKEY)))
111                                 result = true;
112
113                         // todo: convert val ??
114                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
115
116                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
117                         {
118                         case SCA_InputEvent::KX_JUSTACTIVATED:
119                                 {
120                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
121                                         break;
122                                 }
123                         case SCA_InputEvent::KX_ACTIVE:
124                         
125                                 {
126                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
127                                         break;
128                                 }
129                         case SCA_InputEvent::KX_NO_INPUTSTATUS:
130                                 {
131                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
132                                                 break;
133                                 }
134                         default:
135                                 {
136                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
137                                 }
138                         }
139                         
140                 } else if (val == KM_RELEASE)
141                 {
142                         // blender eventval == 0
143                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
144                         {
145                         case SCA_InputEvent::KX_JUSTACTIVATED:
146                                 {
147                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
148                                         break;
149                                 }
150                         case SCA_InputEvent::KX_ACTIVE:
151                                 {
152                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
153                                         break;
154                                 }
155                         default:
156                                 {
157                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
158                                 }
159                         }
160                 }
161         }
162         return result;
163 }
164
165 void KX_BlenderKeyboardDevice::HookEscape()
166 {
167         m_hookesc = true;
168 }