rebranch from 2.5
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #endif
32
33 #ifdef WIN32
34 // annoying warnings about truncated STL debug info
35 #pragma warning (disable :4786)
36 #endif 
37
38 #include "KX_BlenderKeyboardDevice.h"
39
40 KX_BlenderKeyboardDevice::KX_BlenderKeyboardDevice()
41         : m_hookesc(false)
42 {
43
44 }
45 KX_BlenderKeyboardDevice::~KX_BlenderKeyboardDevice()
46 {
47
48 }
49
50 /**
51         IsPressed gives boolean information about keyboard status, true if pressed, false if not
52 */
53
54 bool KX_BlenderKeyboardDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
55 {
56         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
57         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
58                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
59         return pressed;
60 }
61 /*const SCA_InputEvent& KX_BlenderKeyboardDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
62 {
63         return m_eventStatusTables[m_currentTable][inputcode];
64 }
65 */
66 /** 
67         NextFrame toggles currentTable with previousTable,
68         and copy relevant event information from previous to current
69         (pressed keys need to be remembered)
70 */
71 void    KX_BlenderKeyboardDevice::NextFrame()
72 {
73         SCA_IInputDevice::NextFrame();
74         
75         // now convert justpressed keyevents into regular (active) keyevents
76         int previousTable = 1-m_currentTable;
77         for (int keyevent= KX_BEGINKEY; keyevent< KX_ENDKEY;keyevent++)
78         {
79                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][keyevent];
80                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
81                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
82                 {
83                         m_eventStatusTables[m_currentTable][keyevent] = oldevent;
84                         m_eventStatusTables[m_currentTable][keyevent].m_status = SCA_InputEvent::KX_ACTIVE;
85                 }
86         }
87 }
88
89 /** 
90         ConvertBlenderEvent translates blender keyboard events into ketsji kbd events
91         extra event information is stored, like ramp-mode (just released/pressed)
92 */
93
94
95 bool    KX_BlenderKeyboardDevice::ConvertBlenderEvent(unsigned short incode,short val)
96 {
97         bool result = false;
98         
99         // convert event
100         KX_EnumInputs kxevent = this->ToNative(incode);
101
102         // only process it, if it's a key
103         if (kxevent >= KX_BEGINKEY && kxevent < KX_ENDKEY)
104         {
105                 int previousTable = 1-m_currentTable;
106
107                 if (val > 0)
108                 {
109                         if (kxevent == KX_ESCKEY && val != 0 && !m_hookesc)
110                                 result = true;
111                         if (kxevent == KX_PAUSEKEY && val && (IsPressed(KX_LEFTCTRLKEY) || IsPressed(KX_RIGHTCTRLKEY)))
112                                 result = true;
113
114                         // todo: convert val ??
115                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
116
117                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
118                         {
119                         case SCA_InputEvent::KX_JUSTACTIVATED:
120                                 {
121                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
122                                         break;
123                                 }
124                         case SCA_InputEvent::KX_ACTIVE:
125                         
126                                 {
127                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
128                                         break;
129                                 }
130                         case SCA_InputEvent::KX_NO_INPUTSTATUS:
131                                 {
132                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
133                                                 break;
134                                 }
135                         default:
136                                 {
137                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
138                                 }
139                         }
140                         
141                 } else
142                 {
143                         // blender eventval == 0
144                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
145                         {
146                         case SCA_InputEvent::KX_JUSTACTIVATED:
147                                 {
148                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
149                                         break;
150                                 }
151                         case SCA_InputEvent::KX_ACTIVE:
152                                 {
153                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
154                                         break;
155                                 }
156                         default:
157                                 {
158                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
159                                 }
160                         }
161                 }
162         }
163         return result;
164 }
165
166 void KX_BlenderKeyboardDevice::HookEscape()
167 {
168         m_hookesc = true;
169 }