rebranch from 2.5
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderMouseDevice.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_BlenderMouseDevice.h"
39
40 KX_BlenderMouseDevice::KX_BlenderMouseDevice()
41 {
42
43 }
44 KX_BlenderMouseDevice::~KX_BlenderMouseDevice()
45 {
46
47 }
48
49 /**
50         IsPressed gives boolean information about mouse status, true if pressed, false if not
51 */
52
53 bool KX_BlenderMouseDevice::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_BlenderMouseDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
61 {
62         return m_eventStatusTables[m_currentTable][inputcode];
63 }
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_BlenderMouseDevice::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 mouseevent= KX_BEGINMOUSE; mouseevent< KX_ENDMOUSEBUTTONS;mouseevent++)
78         {
79                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mouseevent];
80                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
81                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
82                 {
83                         m_eventStatusTables[m_currentTable][mouseevent] = oldevent;
84                         m_eventStatusTables[m_currentTable][mouseevent].m_status = SCA_InputEvent::KX_ACTIVE;
85                 }
86         }
87         for (int mousemove= KX_ENDMOUSEBUTTONS; mousemove< KX_ENDMOUSE;mousemove++)
88         {
89                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mousemove];
90                 m_eventStatusTables[m_currentTable][mousemove] = oldevent;
91                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
92                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
93                 {
94                         
95                         m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_JUSTRELEASED;
96                 } else
97                 {
98                         if (oldevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
99                         {
100                                 
101                                 m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS ;
102                         }
103                 }
104         }
105 }
106
107
108 /** 
109         ConvertBlenderEvent translates blender mouse events into ketsji kbd events
110         extra event information is stored, like ramp-mode (just released/pressed)
111 */
112
113
114 bool    KX_BlenderMouseDevice::ConvertBlenderEvent(unsigned short incode,short val)
115 {
116         bool result = false;
117         
118         // convert event
119         KX_EnumInputs kxevent = this->ToNative(incode);
120
121         // only process it, if it's a key
122         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSE)
123         {
124                 int previousTable = 1-m_currentTable;
125
126
127                 if (val > 0)
128                 {
129                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
130
131                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
132                         {
133                         
134                         case SCA_InputEvent::KX_ACTIVE:
135                         case SCA_InputEvent::KX_JUSTACTIVATED:
136                                 {
137                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
138                                         break;
139                                 }
140                         case SCA_InputEvent::KX_JUSTRELEASED:
141                                 {
142                                         if ( kxevent > KX_BEGINMOUSEBUTTONS && kxevent < KX_ENDMOUSEBUTTONS)
143                                         {
144                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
145                                         } else
146                                         {
147                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
148                                                 
149                                         }
150                                         break;
151                                 }
152                         default:
153                                 {
154                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
155                                 }
156                         }
157                         
158                 } else
159                 {
160                         // blender eventval == 0
161                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
162                         {
163                         case SCA_InputEvent::KX_JUSTACTIVATED:
164                         case SCA_InputEvent::KX_ACTIVE:
165                                 {
166                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
167                                         break;
168                                 }
169                         default:
170                                 {
171                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
172                                 }
173                         }
174                 }
175         }
176         return result;
177 }