bplayer stub update + remove some tabs
[blender-staging.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 /** \file gameengine/BlenderRoutines/KX_BlenderKeyboardDevice.cpp
30  *  \ingroup blroutines
31  */
32
33
34 #if defined(WIN32) && !defined(FREE_WINDOWS)
35 // annoying warnings about truncated STL debug info
36 #pragma warning (disable :4786)
37 #endif 
38
39 #include "KX_BlenderKeyboardDevice.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
95
96 bool    KX_BlenderKeyboardDevice::ConvertBlenderEvent(unsigned short incode,short val)
97 {
98         bool result = false;
99         
100         // convert event
101         KX_EnumInputs kxevent = this->ToNative(incode);
102
103         // only process it, if it's a key
104         if (kxevent >= KX_BEGINKEY && kxevent <= KX_ENDKEY)
105         {
106                 int previousTable = 1-m_currentTable;
107
108                 if (val == KM_PRESS)
109                 {
110                         if (kxevent == KX_ESCKEY && val != 0 && !m_hookesc)
111                                 result = true;
112                         if (kxevent == KX_PAUSEKEY && val && (IsPressed(KX_LEFTCTRLKEY) || IsPressed(KX_RIGHTCTRLKEY)))
113                                 result = true;
114
115                         // todo: convert val ??
116                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
117
118                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
119                         {
120                         case SCA_InputEvent::KX_JUSTACTIVATED:
121                                 {
122                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
123                                         break;
124                                 }
125                         case SCA_InputEvent::KX_ACTIVE:
126                         
127                                 {
128                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
129                                         break;
130                                 }
131                         case SCA_InputEvent::KX_NO_INPUTSTATUS:
132                                 {
133                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
134                                                 break;
135                                 }
136                         default:
137                                 {
138                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
139                                 }
140                         }
141                         
142                 } else if (val == KM_RELEASE)
143                 {
144                         // blender eventval == 0
145                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
146                         {
147                         case SCA_InputEvent::KX_JUSTACTIVATED:
148                                 {
149                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
150                                         break;
151                                 }
152                         case SCA_InputEvent::KX_ACTIVE:
153                                 {
154                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
155                                         break;
156                                 }
157                         default:
158                                 {
159                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
160                                 }
161                         }
162                 }
163         }
164         return result;
165 }
166
167 void KX_BlenderKeyboardDevice::HookEscape()
168 {
169         m_hookesc = true;
170 }