BGE: Fixing the double-click issue for mouse events too. The previous fix only fixed...
[blender.git] / source / gameengine / BlenderRoutines / KX_BlenderMouseDevice.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/BlenderRoutines/KX_BlenderMouseDevice.cpp
29  *  \ingroup blroutines
30  */
31
32 #ifdef _MSC_VER
33    /* annoying warnings about truncated STL debug info */
34 #  pragma warning (disable:4786)
35 #endif 
36
37 #include "KX_BlenderMouseDevice.h"
38
39 KX_BlenderMouseDevice::KX_BlenderMouseDevice()
40 {
41
42 }
43 KX_BlenderMouseDevice::~KX_BlenderMouseDevice()
44 {
45
46 }
47
48 /**
49         IsPressed gives boolean information about mouse status, true if pressed, false if not
50 */
51
52 bool KX_BlenderMouseDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
53 {
54         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
55         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
56                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
57         return pressed;
58 }
59 /*const SCA_InputEvent& KX_BlenderMouseDevice::GetEventValue(SCA_IInputDevice::KX_EnumInputs inputcode)
60 {
61         return m_eventStatusTables[m_currentTable][inputcode];
62 }
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_BlenderMouseDevice::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 mouseevent= KX_BEGINMOUSE; mouseevent< KX_ENDMOUSEBUTTONS;mouseevent++)
77         {
78                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mouseevent];
79                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
80                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
81                 {
82                         m_eventStatusTables[m_currentTable][mouseevent] = oldevent;
83                         m_eventStatusTables[m_currentTable][mouseevent].m_status = SCA_InputEvent::KX_ACTIVE;
84                 }
85         }
86         for (int mousemove= KX_ENDMOUSEBUTTONS; mousemove< KX_ENDMOUSE;mousemove++)
87         {
88                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mousemove];
89                 m_eventStatusTables[m_currentTable][mousemove] = oldevent;
90                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
91                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE  )
92                 {
93                         
94                         m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_JUSTRELEASED;
95                 } else
96                 {
97                         if (oldevent.m_status == SCA_InputEvent::KX_JUSTRELEASED)
98                         {
99                                 
100                                 m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
101                         }
102                 }
103         }
104 }
105
106
107 /** 
108         ConvertBlenderEvent translates blender mouse events into ketsji kbd events
109         extra event information is stored, like ramp-mode (just released/pressed)
110 */
111
112
113 bool    KX_BlenderMouseDevice::ConvertBlenderEvent(unsigned short incode,short val)
114 {
115         bool result = false;
116         
117         // convert event
118         KX_EnumInputs kxevent = this->ToNative(incode);
119         int previousTable = 1-m_currentTable;
120
121         // only process it, if it's a key
122         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSEBUTTONS)
123         {
124                 if (val == KM_PRESS || val == KM_DBL_CLICK)
125                 {
126                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
127
128                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
129                         {
130                         
131                         case SCA_InputEvent::KX_ACTIVE:
132                         case SCA_InputEvent::KX_JUSTACTIVATED:
133                                 {
134                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
135                                         break;
136                                 }
137                         case SCA_InputEvent::KX_JUSTRELEASED:
138                                 {
139                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
140                                         break;
141                                 }
142                         default:
143                                 {
144                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
145                                 }
146                         }
147                         
148                 } else if (val == KM_RELEASE)
149                 {
150                         // blender eventval == 0
151                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
152                         {
153                         case SCA_InputEvent::KX_JUSTACTIVATED:
154                         case SCA_InputEvent::KX_ACTIVE:
155                                 {
156                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
157                                         break;
158                                 }
159                         default:
160                                 {
161                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
162                                 }
163                         }
164                 }
165         }
166
167         if (kxevent > KX_ENDMOUSEBUTTONS && kxevent < KX_ENDMOUSE)
168         {
169                 m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //remember mouse position
170
171                 switch (m_eventStatusTables[previousTable][kxevent].m_status)
172                 {
173                         
174                 case SCA_InputEvent::KX_ACTIVE:
175                 case SCA_InputEvent::KX_JUSTACTIVATED:
176                         {
177                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
178                                 break;
179                         }
180                 case SCA_InputEvent::KX_JUSTRELEASED:
181                         {
182                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
183                                 break;
184                         }
185                 default:
186                         {
187                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
188                         }
189                 }
190         }
191
192
193         return result;
194 }