Bugfix [#32410] Create New Group (CTRL+G) tried to activate even when nothing is
[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
33 #if defined(WIN32) && !defined(FREE_WINDOWS)
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         int previousTable = 1-m_currentTable;
121
122         // only process it, if it's a key
123         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSEBUTTONS)
124         {
125                 if (val == KM_PRESS)
126                 {
127                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //???
128
129                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
130                         {
131                         
132                         case SCA_InputEvent::KX_ACTIVE:
133                         case SCA_InputEvent::KX_JUSTACTIVATED:
134                                 {
135                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
136                                         break;
137                                 }
138                         case SCA_InputEvent::KX_JUSTRELEASED:
139                                 {
140                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
141                                         break;
142                                 }
143                         default:
144                                 {
145                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
146                                 }
147                         }
148                         
149                 } else if (val == KM_RELEASE)
150                 {
151                         // blender eventval == 0
152                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
153                         {
154                         case SCA_InputEvent::KX_JUSTACTIVATED:
155                         case SCA_InputEvent::KX_ACTIVE:
156                                 {
157                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
158                                         break;
159                                 }
160                         default:
161                                 {
162                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
163                                 }
164                         }
165                 }
166         }
167
168         if (kxevent > KX_ENDMOUSEBUTTONS && kxevent < KX_ENDMOUSE)
169         {
170                 m_eventStatusTables[m_currentTable][kxevent].m_eventval = val ; //remember mouse position
171
172                 switch (m_eventStatusTables[previousTable][kxevent].m_status)
173                 {
174                         
175                 case SCA_InputEvent::KX_ACTIVE:
176                 case SCA_InputEvent::KX_JUSTACTIVATED:
177                         {
178                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
179                                 break;
180                         }
181                 case SCA_InputEvent::KX_JUSTRELEASED:
182                         {
183                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
184                                 break;
185                         }
186                 default:
187                         {
188                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
189                         }
190                 }
191         }
192
193
194         return result;
195 }