synched with trunk at revision 36569
[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 /** \file gameengine/GameLogic/SCA_MouseManager.cpp
34  *  \ingroup gamelogic
35  */
36
37
38 #if defined(WIN32) && !defined(FREE_WINDOWS)
39 // This warning tells us about truncation of __long__ stl-generated names.
40 // It can occasionally cause DevStudio to have internal compiler warnings.
41 #pragma warning( disable : 4786 )     
42 #endif
43
44 #include "BoolValue.h"
45 #include "SCA_MouseManager.h"
46 #include "SCA_MouseSensor.h"
47 #include "IntValue.h"
48 #include "RAS_ICanvas.h"
49
50
51 SCA_MouseManager::SCA_MouseManager(SCA_LogicManager* logicmgr,
52                                                                    SCA_IInputDevice* mousedev,
53                                                                    RAS_ICanvas* canvas)
54         :       SCA_EventManager(logicmgr, MOUSE_EVENTMGR),
55                 m_mousedevice (mousedev),
56                 m_canvas(canvas)
57 {
58         m_xpos = 0;
59         m_ypos = 0;
60 }
61
62
63
64 SCA_MouseManager::~SCA_MouseManager()
65 {
66 }
67
68
69
70 SCA_IInputDevice* SCA_MouseManager::GetInputDevice()
71 {
72         return m_mousedevice;
73 }
74
75
76
77 void SCA_MouseManager::NextFrame()
78 {
79         if (m_mousedevice)
80         {
81                 SG_DList::iterator<SCA_ISensor> it(m_sensors);
82                 for (it.begin();!it.end();++it)
83                 {
84                         SCA_MouseSensor* mousesensor = (SCA_MouseSensor*)(*it);
85                         // (0,0) is the Upper Left corner in our local window
86                         // coordinates
87                         if (!mousesensor->IsSuspended())
88                         {
89                                 const SCA_InputEvent& event1 = 
90                                         m_mousedevice->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
91                                 const SCA_InputEvent& event2 = 
92                                         m_mousedevice->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
93
94                                 int mx = this->m_canvas->GetMouseX(event1.m_eventval);
95                                 int my = this->m_canvas->GetMouseY(event2.m_eventval);
96                                 
97                                 mousesensor->setX(mx);
98                                 mousesensor->setY(my);
99                                 
100                                 mousesensor->Activate(m_logicmgr);
101                         }
102                 }
103         }
104 }
105
106 bool SCA_MouseManager::IsPressed(SCA_IInputDevice::KX_EnumInputs inputcode)
107 {
108         /* We should guard for non-mouse events maybe? A rather silly side       */
109         /* effect here is that position-change events are considered presses as  */
110         /* well.                                                                 */
111         
112         return m_mousedevice->IsPressed(inputcode);
113 }