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