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