synched with trunk at revision 32129
[blender.git] / source / gameengine / GameLogic / SCA_MouseManager.cpp
1 /**
2  * Manager for mouse events
3  *
4  *
5  * $Id$
6  *
7  * ***** BEGIN GPL LICENSE BLOCK *****
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL LICENSE BLOCK *****
31  */
32
33 #if defined(WIN32) && !defined(FREE_WINDOWS)
34 // This warning tells us about truncation of __long__ stl-generated names.
35 // It can occasionally cause DevStudio to have internal compiler warnings.
36 #pragma warning( disable : 4786 )     
37 #endif
38
39 #include "BoolValue.h"
40 #include "SCA_MouseManager.h"
41 #include "SCA_MouseSensor.h"
42 #include "IntValue.h"
43 #include "RAS_ICanvas.h"
44
45
46 SCA_MouseManager::SCA_MouseManager(SCA_LogicManager* logicmgr,
47                                                                    SCA_IInputDevice* mousedev,
48                                                                    RAS_ICanvas* canvas)
49         :       SCA_EventManager(logicmgr, MOUSE_EVENTMGR),
50                 m_mousedevice (mousedev),
51                 m_canvas(canvas)
52 {
53         m_xpos = 0;
54         m_ypos = 0;
55 }
56
57
58
59 SCA_MouseManager::~SCA_MouseManager()
60 {
61 }
62
63
64
65 SCA_IInputDevice* SCA_MouseManager::GetInputDevice()
66 {
67         return m_mousedevice;
68 }
69
70
71
72 void SCA_MouseManager::NextFrame()
73 {
74         if (m_mousedevice)
75         {
76                 SG_DList::iterator<SCA_ISensor> it(m_sensors);
77                 for (it.begin();!it.end();++it)
78                 {
79                         SCA_MouseSensor* mousesensor = (SCA_MouseSensor*)(*it);
80                         // (0,0) is the Upper Left corner in our local window
81                         // coordinates
82                         if (!mousesensor->IsSuspended())
83                         {
84                                 const SCA_InputEvent& event1 = 
85                                         m_mousedevice->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
86                                 const SCA_InputEvent& event2 = 
87                                         m_mousedevice->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
88
89                                 int mx = this->m_canvas->GetMouseX(event1.m_eventval);
90                                 int my = this->m_canvas->GetMouseY(event2.m_eventval);
91                                 
92                                 mousesensor->setX(mx);
93                                 mousesensor->setY(my);
94                                 
95                                 mousesensor->Activate(m_logicmgr);
96                         }
97                 }
98         }
99 }
100
101 bool SCA_MouseManager::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
102 {
103         /* We should guard for non-mouse events maybe? A rather silly side       */
104         /* effect here is that position-change events are considered presses as  */
105         /* well.                                                                 */
106         
107         return m_mousedevice->IsPressed(inputcode);
108 }