update for MingW/CMake
[blender-staging.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., 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 #if defined(WIN32) && !defined(FREE_WINDOWS)
30 // annoying warnings about truncated STL debug info
31 #pragma warning (disable :4786)
32 #endif 
33
34 #include "KX_BlenderMouseDevice.h"
35
36 KX_BlenderMouseDevice::KX_BlenderMouseDevice()
37 {
38
39 }
40 KX_BlenderMouseDevice::~KX_BlenderMouseDevice()
41 {
42
43 }
44
45 /**
46         IsPressed gives boolean information about mouse status, true if pressed, false if not
47 */
48
49 bool KX_BlenderMouseDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
50 {
51         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
52         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
53                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
54         return pressed;
55 }
56 /*const SCA_InputEvent& KX_BlenderMouseDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
57 {
58         return m_eventStatusTables[m_currentTable][inputcode];
59 }
60 */
61
62 /** 
63         NextFrame toggles currentTable with previousTable,
64         and copy relevant event information from previous to current
65         (pressed keys need to be remembered)
66 */
67 void    KX_BlenderMouseDevice::NextFrame()
68 {
69         SCA_IInputDevice::NextFrame();
70         
71         // now convert justpressed keyevents into regular (active) keyevents
72         int previousTable = 1-m_currentTable;
73         for (int mouseevent= KX_BEGINMOUSE; mouseevent< KX_ENDMOUSEBUTTONS;mouseevent++)
74         {
75                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mouseevent];
76                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
77                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
78                 {
79                         m_eventStatusTables[m_currentTable][mouseevent] = oldevent;
80                         m_eventStatusTables[m_currentTable][mouseevent].m_status = SCA_InputEvent::KX_ACTIVE;
81                 }
82         }
83         for (int mousemove= KX_ENDMOUSEBUTTONS; mousemove< KX_ENDMOUSE;mousemove++)
84         {
85                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mousemove];
86                 m_eventStatusTables[m_currentTable][mousemove] = oldevent;
87                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
88                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
89                 {
90                         
91                         m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_JUSTRELEASED;
92                 } else
93                 {
94                         if (oldevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
95                         {
96                                 
97                                 m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS ;
98                         }
99                 }
100         }
101 }
102
103
104 /** 
105         ConvertBlenderEvent translates blender mouse events into ketsji kbd events
106         extra event information is stored, like ramp-mode (just released/pressed)
107 */
108
109
110 bool    KX_BlenderMouseDevice::ConvertBlenderEvent(unsigned short incode,short val)
111 {
112         bool result = false;
113         
114         // convert event
115         KX_EnumInputs kxevent = this->ToNative(incode);
116         int previousTable = 1-m_currentTable;
117
118         // only process it, if it's a key
119         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSEBUTTONS)
120         {
121                 if (val == KM_PRESS)
122                 {
123                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
124
125                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
126                         {
127                         
128                         case SCA_InputEvent::KX_ACTIVE:
129                         case SCA_InputEvent::KX_JUSTACTIVATED:
130                                 {
131                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
132                                         break;
133                                 }
134                         case SCA_InputEvent::KX_JUSTRELEASED:
135                                 {
136                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
137                                         break;
138                                 }
139                         default:
140                                 {
141                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
142                                 }
143                         }
144                         
145                 } else if (val == KM_RELEASE)
146                 {
147                         // blender eventval == 0
148                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
149                         {
150                         case SCA_InputEvent::KX_JUSTACTIVATED:
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
164         if (kxevent > KX_ENDMOUSEBUTTONS && kxevent < KX_ENDMOUSE)
165         {
166                 m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //remember mouse position
167
168                 switch (m_eventStatusTables[previousTable][kxevent].m_status)
169                 {
170                         
171                 case SCA_InputEvent::KX_ACTIVE:
172                 case SCA_InputEvent::KX_JUSTACTIVATED:
173                         {
174                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
175                                 break;
176                         }
177                 case SCA_InputEvent::KX_JUSTRELEASED:
178                         {
179                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
180                                 break;
181                         }
182                 default:
183                         {
184                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
185                         }
186                 }
187         }
188
189
190         return result;
191 }