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