1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2019 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2010 Robert TIMM (rti) <mail@rtti.de>
6 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
7 // Copyright (C) 2014 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
9 // This program is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU Affero General Public License as
11 // published by the Free Software Foundation, either version 3 of the
12 // License, or (at your option) any later version.
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 Affero General Public License for more details.
19 // You should have received a copy of the GNU Affero General Public License
20 // along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "interface_v3/interface_manager.h"
33 #include "interface_v3/input_handler_manager.h"
34 #include "client_cfg.h"
35 #include "time_client.h"
37 #include "nel/3d/u_driver.h"
39 #include "nel/misc/mouse_smoother.h"
40 #include "nel/misc/system_utils.h"
48 using namespace NLMISC
;
54 extern UDriver
*Driver
;
55 extern UMaterial GenericMat
;
56 extern CActionsManager Actions
; // Actions Manager.
62 bool MouseHardware
= false;
63 bool MouseFreeLook
= false;
64 float MouseCursorSpeed
= 1.f
;
65 uint MouseCursorAcceleration
= 0;
66 bool InitMouseFirstTime
= true;
67 bool SetMousePosFirstTime
= true;
69 // mask for mouse buttons that are known to be down
70 uint DownMouseButtons
= 0;
76 // *********************************************************************************
77 uint
GetMouseButtonsState()
79 return DownMouseButtons
;
82 // *********************************************************************************
83 // Initialize the mouse
84 bool InitMouseWithCursor (bool hardware
)
86 Driver
->showCursor(false);
88 // Get the new mouse state
89 MouseHardware
= hardware
;
90 CViewPointer::setHWMouse( hardware
);
92 if (InitMouseFirstTime
)
94 InitMouseFirstTime
= false;
100 // Get the current mouse position
101 CInterfaceManager
*pIm
= CInterfaceManager::getInstance();
102 CViewPointer
*vp
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
103 Driver
->showCursor(hardware
);
106 float x
= (float) vp
->getX();
107 float y
= (float) vp
->getY();
108 // First, hide the hardware mouse
109 uint width
= Driver
->getWindowWidth();
110 uint height
= Driver
->getWindowHeight();
111 if (SetMousePosFirstTime
)
113 SetMousePosFirstTime
= false;
115 else if (width
!= 0 && height
!= 0)
117 nlwarning("Mouse pos %f, %f", x
, y
);
118 Driver
->setMousePos(x
/ width
, y
/ height
);
127 // *********************************************************************************
128 // Is mouse cursor hardware ?
129 bool IsMouseCursorHardware ()
131 return MouseHardware
;
134 // *********************************************************************************
135 // Use this method to toggle the mouse (freelook <- cursor)
136 void SetMouseFreeLook ()
140 MouseFreeLook
= true;
143 Driver
->showCursor(false);
147 CInterfaceManager
*im
= CInterfaceManager::getInstance();
150 CViewPointer
*pointer
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
152 pointer
->show (false);
158 // *********************************************************************************
159 bool IsMouseFreeLook()
161 return MouseFreeLook
;
164 // *********************************************************************************
165 // Use this method to toggle the mouse (freelook -> cursor)
166 void SetMouseCursor (bool updatePos
)
170 // Get the last cursor
171 float x
= 0.5f
, y
= 0.5f
;
175 CViewRenderer::getInstance()->getScreenSize(width
, height
);
177 // Update the interface pointer
178 CInterfaceManager
*instance
= CInterfaceManager::getInstance();
181 // Get the cursor instance
182 CViewPointer
*cursor
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
186 cursor
->getPointerPos (ix
, iy
);
187 if (ix
!= CViewPointer::InvalidCoord
&& iy
!= CViewPointer::InvalidCoord
)
189 x
= (float)ix
/ (float)width
;
190 y
= (float)iy
/ (float)height
;
195 MouseFreeLook
= false;
197 // Integer coordinates
198 sint ix
= (sint
)(x
*(float)width
+0.5f
);
199 sint iy
= (sint
)(y
*(float)height
+0.5f
);
203 Driver
->setMousePos(x
, y
);
206 // Update the interface pointer
209 // Get the cursor instance
210 CViewPointer
*cursor
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
213 cursor
->setPointerPos(ix
, iy
);
214 cursor
->setPointerDispPos(ix
, iy
);
217 CInputHandlerManager
*IHM
= CInputHandlerManager::getInstance ();
220 IHM
->resetPos (ix
, iy
);
226 Driver
->showCursor(true);
230 CInterfaceManager
*im
= CInterfaceManager::getInstance();
233 CViewPointer
*pointer
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
235 pointer
->show (true);
241 // *********************************************************************************
242 // Use this method to set the cursor speed
243 void SetMouseSpeed (float speed
)
245 MouseCursorSpeed
= speed
;
248 // *********************************************************************************
249 // Use this method to set the cursor acceleration
250 void SetMouseAcceleration (uint accel
)
252 MouseCursorAcceleration
= accel
;
255 // *********************************************************************************
256 void HandleSystemCursorCapture(const CEvent
&event
)
258 static bool mouseCaptured
= false;
260 // capture on first move event after button is held down or free look is activated
261 if (event
== EventMouseMoveId
&& !mouseCaptured
&& (MouseFreeLook
|| DownMouseButtons
!= 0))
263 mouseCaptured
= true;
264 Driver
->setCapture(true);
267 // release when button is released and not in free look
268 if (mouseCaptured
&& !MouseFreeLook
&& DownMouseButtons
== 0)
270 mouseCaptured
= false;
271 Driver
->setCapture(false);
274 if (event
== EventMouseDownId
)
276 CEventMouseDown
&em
= (CEventMouseDown
&) event
;
277 DownMouseButtons
|= em
.Button
& (leftButton
| middleButton
| rightButton
);
279 CViewPointer
*cursor
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
282 cursor
->setPointerDown(em
.Button
== leftButton
);
283 cursor
->setPointerMiddleDown(em
.Button
== middleButton
);
284 cursor
->setPointerRightDown(em
.Button
== rightButton
);
288 if (event
== EventMouseUpId
)
291 CEventMouseDown
&em
= (CEventMouseDown
&) event
;
292 DownMouseButtons
&= ~(em
.Button
& (leftButton
| middleButton
| rightButton
));
293 if (DownMouseButtons
== 0)
295 CViewPointer
*cursor
= static_cast< CViewPointer
* >( CWidgetManager::getInstance()->getPointer() );
298 cursor
->setPointerDown(false);
299 cursor
->setPointerMiddleDown(false);
300 cursor
->setPointerRightDown(false);
305 // if focus was lost then says that all buttons are up from this app viewpoint
306 if (event
== EventSetFocusId
)
308 DownMouseButtons
= 0;
312 sint
CNiceInputAuto::_Count
= 0;
315 CNiceInputAuto::CNiceInputAuto()
319 Driver
->setCursor("curs_default.tga", CRGBA::White
, 0, 0x15, 0x18);
320 Driver
->showCursor(true); // keep cursor visible in windowed mode
325 CNiceInputAuto::~CNiceInputAuto()
328 nlassert(_Count
>= 0);
331 InitMouseWithCursor(ClientCfg
.HardwareCursor
&& !StereoDisplayAttached
);