Bevel Factor Mapping
[blender.git] / source / gameengine / GamePlayer / common / GPC_MouseDevice.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/GamePlayer/common/GPC_MouseDevice.cpp
29  *  \ingroup player
30  */
31
32
33 #include "GPC_MouseDevice.h"
34
35 GPC_MouseDevice::GPC_MouseDevice()
36 {
37
38 }
39 GPC_MouseDevice::~GPC_MouseDevice()
40 {
41
42 }
43
44 /**
45  * IsPressed gives boolean information about mouse status, true if pressed, false if not.
46  */
47 bool GPC_MouseDevice::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
48 {
49         const SCA_InputEvent & inevent =  m_eventStatusTables[m_currentTable][inputcode];
50         bool pressed = (inevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED || 
51                 inevent.m_status == SCA_InputEvent::KX_ACTIVE);
52         return pressed;
53 }
54
55
56 /** 
57  * NextFrame toggles currentTable with previousTable,
58  * and copies relevant event information from previous to current table
59  * (pressed keys need to be remembered).
60  */
61 void GPC_MouseDevice::NextFrame()
62 {
63         SCA_IInputDevice::NextFrame();
64         
65         // Convert just pressed events into regular (active) events
66         int previousTable = 1-m_currentTable;
67         for (int mouseevent= KX_BEGINMOUSE; mouseevent< KX_ENDMOUSEBUTTONS; mouseevent++) {
68                 SCA_InputEvent& oldevent = m_eventStatusTables[previousTable][mouseevent];
69                 if (oldevent.m_status == SCA_InputEvent::KX_JUSTACTIVATED ||
70                     oldevent.m_status == SCA_InputEvent::KX_ACTIVE)
71                 {
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                 {
82                         m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_JUSTRELEASED;
83                 }
84                 else {
85                         if (oldevent.m_status == SCA_InputEvent::KX_JUSTRELEASED) {
86                                 m_eventStatusTables[m_currentTable][mousemove].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
87                         }
88                 }
89         }
90 }
91
92
93 bool GPC_MouseDevice::ConvertButtonEvent(TButtonId button, bool isDown)
94 {
95         bool result = false;
96
97         switch (button)
98         {
99         case buttonLeft:
100                 result = ConvertEvent(KX_LEFTMOUSE, isDown, 0);
101                 break;
102         case buttonMiddle:
103                 result = ConvertEvent(KX_MIDDLEMOUSE, isDown, 0);
104                 break;
105         case buttonRight:
106                 result = ConvertEvent(KX_RIGHTMOUSE, isDown, 0);
107                 break;
108         case buttonWheelUp:
109                 result = ConvertEvent(KX_WHEELUPMOUSE, isDown, 0);
110                 break;
111         case buttonWheelDown:
112                 result = ConvertEvent(KX_WHEELDOWNMOUSE, isDown, 0);
113                 break;
114         default:
115                 // Should not happen!
116                 break;
117         }
118
119         return result;
120 }
121
122 /**
123  * Splits combined button and x,y cursor move events into separate Ketsji
124  * x and y move and button events.
125  */
126 bool GPC_MouseDevice::ConvertButtonEvent(TButtonId button, bool isDown, int x, int y)
127 {
128         // First update state tables for cursor move.
129         bool result = ConvertMoveEvent(x, y);
130
131         // Now update for button state.
132         if (result) {
133                 result = ConvertButtonEvent(button, isDown);
134         }
135
136         return result;
137 }
138
139 /**
140  * Splits combined x,y move into separate Ketsji x and y move events.
141  */
142 bool GPC_MouseDevice::ConvertMoveEvent(int x, int y)
143 {
144         bool result;
145
146         // Convert to local coordinates?
147         result = ConvertEvent(KX_MOUSEX, x, 0);
148         if (result) {
149                 result = ConvertEvent(KX_MOUSEY, y, 0);
150         }
151
152         return result;
153 }
154
155
156 bool GPC_MouseDevice::ConvertEvent(KX_EnumInputs kxevent, int eventval, unsigned int unicode)
157 {
158         bool result = true;
159         
160         // Only process it, if it's a mouse event
161         if (kxevent > KX_BEGINMOUSE && kxevent < KX_ENDMOUSE) {
162                 int previousTable = 1-m_currentTable;
163
164                 if (eventval > 0) {
165                         m_eventStatusTables[m_currentTable][kxevent].m_eventval = eventval;
166
167                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
168                         {
169                         case SCA_InputEvent::KX_ACTIVE:
170                         case SCA_InputEvent::KX_JUSTACTIVATED:
171                                 {
172                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
173                                         break;
174                                 }
175                         case SCA_InputEvent::KX_JUSTRELEASED:
176                                 {
177                                         if ( kxevent > KX_BEGINMOUSEBUTTONS && kxevent < KX_ENDMOUSEBUTTONS)
178                                         {
179                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
180                                         } else
181                                         {
182                                                 m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_ACTIVE;
183                                                 
184                                         }
185                                         break;
186                                 }
187                         default:
188                                 {
189                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTACTIVATED;
190                                 }
191                         }
192                         
193                 } 
194                 else {
195                         switch (m_eventStatusTables[previousTable][kxevent].m_status)
196                         {
197                         case SCA_InputEvent::KX_JUSTACTIVATED:
198                         case SCA_InputEvent::KX_ACTIVE:
199                                 {
200                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_JUSTRELEASED;
201                                         break;
202                                 }
203                         default:
204                                 {
205                                         m_eventStatusTables[m_currentTable][kxevent].m_status = SCA_InputEvent::KX_NO_INPUTSTATUS;
206                                 }
207                         }
208                 }
209         }
210         else {
211                 result = false;
212         }
213         return result;
214 }