Resolve "Toggle Free Look with Hotkey"
[ryzomcore.git] / ryzom / client / src / input.cpp
blobcdd813a4a3dbd6f4a14461efbd8d514c46f249da
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010-2019 Winch Gate Property Limited
3 //
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>
8 //
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/>.
24 /////////////
25 // INCLUDE //
26 /////////////
27 #include "stdpch.h"
28 // Client
29 #include "global.h"
30 #include "actions.h"
31 #include "input.h"
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"
36 // 3D
37 #include "nel/3d/u_driver.h"
38 // Misc
39 #include "nel/misc/mouse_smoother.h"
40 #include "nel/misc/system_utils.h"
41 // Game Share
44 ///////////
45 // USING //
46 ///////////
47 using namespace NL3D;
48 using namespace NLMISC;
51 ////////////
52 // EXTERN //
53 ////////////
54 extern UDriver *Driver;
55 extern UMaterial GenericMat;
56 extern CActionsManager Actions; // Actions Manager.
59 ////////////
60 // GLOBAL //
61 ////////////
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;
72 //////////////
73 // FUNCTION //
74 //////////////
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;
96 else
98 if (!MouseFreeLook)
100 // Get the current mouse position
101 CInterfaceManager *pIm = CInterfaceManager::getInstance();
102 CViewPointer *vp = static_cast< CViewPointer* >( CWidgetManager::getInstance()->getPointer() );
103 Driver->showCursor(hardware);
104 if (vp)
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);
124 return true;
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 ()
138 if (!MouseFreeLook)
140 MouseFreeLook = true;
141 if (MouseHardware)
143 Driver->showCursor(false);
145 else
147 CInterfaceManager *im = CInterfaceManager::getInstance();
148 if (im)
150 CViewPointer *pointer = static_cast< CViewPointer* >( CWidgetManager::getInstance()->getPointer() );
151 if (pointer)
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)
168 if (MouseFreeLook)
170 // Get the last cursor
171 float x = 0.5f, y = 0.5f;
173 // Screen size
174 uint width, height;
175 CViewRenderer::getInstance()->getScreenSize(width, height);
177 // Update the interface pointer
178 CInterfaceManager *instance = CInterfaceManager::getInstance();
179 if (instance)
181 // Get the cursor instance
182 CViewPointer *cursor = static_cast< CViewPointer* >( CWidgetManager::getInstance()->getPointer() );
183 if (cursor)
185 sint32 ix, iy;
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);
201 if (updatePos)
203 Driver->setMousePos(x, y);
206 // Update the interface pointer
207 if (instance)
209 // Get the cursor instance
210 CViewPointer *cursor = static_cast< CViewPointer* >( CWidgetManager::getInstance()->getPointer() );
211 if (cursor)
213 cursor->setPointerPos(ix, iy);
214 cursor->setPointerDispPos(ix, iy);
217 CInputHandlerManager *IHM = CInputHandlerManager::getInstance ();
218 if (IHM)
220 IHM->resetPos (ix, iy);
223 // Show the cursor
224 if (MouseHardware)
226 Driver->showCursor(true);
228 else
230 CInterfaceManager *im = CInterfaceManager::getInstance();
231 if (im)
233 CViewPointer *pointer = static_cast< CViewPointer* >( CWidgetManager::getInstance()->getPointer() );
234 if (pointer)
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() );
280 if (cursor)
282 cursor->setPointerDown(em.Button == leftButton);
283 cursor->setPointerMiddleDown(em.Button == middleButton);
284 cursor->setPointerRightDown(em.Button == rightButton);
288 if (event == EventMouseUpId)
290 // if all buttons up
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() );
296 if (cursor)
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()
317 if (_Count == 0)
319 Driver->setCursor("curs_default.tga", CRGBA::White, 0, 0x15, 0x18);
320 Driver->showCursor(true); // keep cursor visible in windowed mode
322 ++_Count;
325 CNiceInputAuto::~CNiceInputAuto()
327 --_Count;
328 nlassert(_Count >= 0);
329 if (_Count == 0)
331 InitMouseWithCursor(ClientCfg.HardwareCursor && !StereoDisplayAttached);