Undo revision 23130 which was a merge with 2.5, a messy one because I did something...
[blender.git] / source / gameengine / GamePlayer / common / GPC_MouseDevice.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "GPC_MouseDevice.h"
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 GPC_MouseDevice::GPC_MouseDevice()
37 {
38
39 }
40 GPC_MouseDevice::~GPC_MouseDevice()
41 {
42
43 }
44
45 /**
46  * IsPressed gives boolean information about mouse status, true if pressed, false if not.
47  */
48 bool GPC_MouseDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
49 {
50         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
51         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
52                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
53         return pressed;
54 }
55
56
57 /** 
58  * NextFrame toggles currentTable with previousTable,
59  * and copies relevant event information from previous to current table
60  * (pressed keys need to be remembered).
61  */
62 void GPC_MouseDevice::NextFrame()
63 {
64         SCA_IInputDevice::NextFrame();
65         
66         // Convert just pressed events into regular (active) events
67         int previousTable = 1-m_currentTable;
68         for (int mouseevent= KX_BEGINMOUSE; mouseevent< KX_ENDMOUSEBUTTONS; mouseevent++) {
69                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mouseevent];
70                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
71                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE) {
72                         m_eventStatusTables[m_currentTable][mouseevent] = oldevent;
73                         m_eventStatusTables[m_currentTable][mouseevent].m_status = SCA_InputEvent::KX_ACTIVE;
74                 }
75         }
76         for (int mousemove= KX_ENDMOUSEBUTTONS; mousemove< KX_ENDMOUSE; mousemove++) {
77                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mousemove];
78                 m_eventStatusTables[m_currentTable][mousemove] = oldevent;
79                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
80                         oldevent.m_status == SCA_InputEvent::KX_ACTIVE) {       
81                         m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_JUSTRELEASED;
82                 }
83                 else {
84                         if (oldevent.m_status == SCA_InputEvent::KX_JUSTRELEASED) {
85                                 m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS ;
86                         }
87                 }
88         }
89 }
90
91
92 bool GPC_MouseDevice::ConvertButtonEvent(TButtonId button, bool isDown)
93 {
94         bool result = false;
95
96         switch (button)
97         {
98         case buttonLeft:
99                 result = ConvertEvent(KX_LEFTMOUSE, isDown);
100                 break;
101         case buttonMiddle:
102                 result = ConvertEvent(KX_MIDDLEMOUSE, isDown);
103                 break;
104         case buttonRight:
105                 result = ConvertEvent(KX_RIGHTMOUSE, isDown);
106                 break;
107         case buttonWheelUp:
108                 result = ConvertEvent(KX_WHEELUPMOUSE, isDown);
109                 break;
110         case buttonWheelDown:
111                 result = ConvertEvent(KX_WHEELDOWNMOUSE, isDown);
112                 break;
113         default:
114                 // Should not happen!
115                 break;
116         }
117
118         return result;
119 }
120
121 /**
122  * Splits combined button and x,y cursor move events into separate Ketsji
123  * x and y move and button events.
124  */
125 bool GPC_MouseDevice::ConvertButtonEvent(TButtonId button, bool isDown, int x, int y)
126 {
127         // First update state tables for cursor move.
128         bool result = ConvertMoveEvent(x, y);
129
130         // Now update for button state.
131         if (result) {
132                 result = ConvertButtonEvent(button, isDown);
133         }
134
135         return result;
136 }
137
138 /**
139  * Splits combined x,y move into separate Ketsji x and y move events.
140  */
141 bool GPC_MouseDevice::ConvertMoveEvent(int x, int y)
142 {
143         bool result;
144
145         // Convert to local coordinates?
146         result = ConvertEvent(KX_MOUSEX, x);
147         if (result) {
148                 result = ConvertEvent(KX_MOUSEY, y);
149         }
150
151         return result;
152 }
153
154
155 bool GPC_MouseDevice::ConvertEvent(KX_EnumInputs kxevent, int eventval)
156 {
157         bool result = true;
158         
159         // Only process it, if it's a mouse event
160         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSE) {
161                 int previousTable = 1-m_currentTable;
162
163                 if (eventval > 0) {
164                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = eventval;
165
166                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
167                         {
168                         case SCA_InputEvent::KX_ACTIVE:
169                         case SCA_InputEvent::KX_JUSTACTIVATED:
170                                 {
171                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
172                                         break;
173                                 }
174                         case SCA_InputEvent::KX_JUSTRELEASED:
175                                 {
176                                         if ( kxevent > KX_BEGINMOUSEBUTTONS && kxevent < KX_ENDMOUSEBUTTONS)
177                                         {
178                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
179                                         } else
180                                         {
181                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
182                                                 
183                                         }
184                                         break;
185                                 }
186                         default:
187                                 {
188                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
189                                 }
190                         }
191                         
192                 } 
193                 else {
194                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
195                         {
196                         case SCA_InputEvent::KX_JUSTACTIVATED:
197                         case SCA_InputEvent::KX_ACTIVE:
198                                 {
199                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
200                                         break;
201                                 }
202                         default:
203                                 {
204                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
205                                 }
206                         }
207                 }
208         }
209         else {
210                 result = false;
211         }
212         return result;
213 }