Initial revision
[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
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 {
42
43 }
44 KX_BlenderKeyboardDevice::~KX_BlenderKeyboardDevice()
45 {
46
47 }
48
49 /**
50         IsPressed gives boolean information about keyboard status, true if pressed, false if not
51 */
52
53 bool KX_BlenderKeyboardDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
54 {
55         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
56         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
57                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
58         return pressed;
59 }
60 /*const SCA_InputEvent& KX_BlenderKeyboardDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
61 {
62         return m_eventStatusTables[m_currentTable][inputcode];
63 }
64 */
65 /** 
66         NextFrame toggles currentTable with previousTable,
67         and copy relevant event information from previous to current
68         (pressed keys need to be remembered)
69 */
70 void    KX_BlenderKeyboardDevice::NextFrame()
71 {
72         SCA_IInputDevice::NextFrame();
73         
74         // now convert justpressed keyevents into regular (active) keyevents
75         int previousTable = 1-m_currentTable;
76         for (int keyevent= KX_BEGINKEY; keyevent< KX_ENDKEY;keyevent++)
77         {
78                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][keyevent];
79                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
80                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
81                 {
82                         m_eventStatusTables[m_currentTable][keyevent] = oldevent;
83                         m_eventStatusTables[m_currentTable][keyevent].m_status = SCA_InputEvent::KX_ACTIVE;
84                 }
85         }
86 }
87
88 /** 
89         ConvertBlenderEvent translates blender keyboard events into ketsji kbd events
90         extra event information is stored, like ramp-mode (just released/pressed)
91 */
92
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 > 0)
107                 {
108                         if (kxevent == SCA_IInputDevice::KX_ESCKEY && val != 0)
109                                 result = true;
110
111                         // todo: convert val ??
112                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
113
114                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
115                         {
116                         case SCA_InputEvent::KX_JUSTACTIVATED:
117                                 {
118                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
119                                         break;
120                                 }
121                         case SCA_InputEvent::KX_ACTIVE:
122                         
123                                 {
124                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
125                                         break;
126                                 }
127                         case SCA_InputEvent::KX_NO_INPUTSTATUS:
128                                 {
129                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
130                                                 break;
131                                 }
132                         default:
133                                 {
134                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
135                                 }
136                         }
137                         
138                 } else
139                 {
140                         // blender eventval == 0
141                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
142                         {
143                         case SCA_InputEvent::KX_JUSTACTIVATED:
144                                 {
145                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
146                                         break;
147                                 }
148                         case SCA_InputEvent::KX_ACTIVE:
149                                 {
150                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
151                                         break;
152                                 }
153                         default:
154                                 {
155                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
156                                 }
157                         }
158                 }
159         }
160         return result;
161 }