lltoolfocus.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /**
  2. * @file lltoolfocus.cpp
  3. * @brief A tool to set the build focus point.
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewergpl$
  6. *
  7. * Copyright (c) 2001-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "lltoolfocus.h"
  34. #include "llagent.h"
  35. #include "lldrawable.h"
  36. #include "llhoverview.h"
  37. #include "llhudmanager.h"
  38. #include "llfloatertools.h"
  39. #include "llmorphview.h"
  40. #include "llpipeline.h" // For LLPipeline::sFreezeTime
  41. //MK
  42. #include "mkrlinterface.h"
  43. //mk
  44. #include "llselectmgr.h"
  45. #include "llstatusbar.h"
  46. #include "lltoolmgr.h"
  47. #include "llviewercamera.h"
  48. #include "llviewercontrol.h"
  49. #include "llviewerobject.h"
  50. #include "llviewerwindow.h"
  51. #include "llvoavatarself.h"
  52. // Globals
  53. bool gCameraBtnZoom = true;
  54. bool gCameraBtnOrbit = false;
  55. bool gCameraBtnPan = false;
  56. constexpr S32 SLOP_RANGE = 4;
  57. LLToolFocus gToolFocus;
  58. LLToolFocus::LLToolFocus()
  59. : LLTool("Focus"),
  60. mAccumX(0),
  61. mAccumY(0),
  62. mMouseDownX(0),
  63. mMouseDownY(0),
  64. mOutsideSlopX(false),
  65. mOutsideSlopY(false),
  66. mValidClickPoint(false),
  67. mMouseSteering(false),
  68. mMouseUpX(0),
  69. mMouseUpY(0),
  70. mMouseUpMask(MASK_NONE)
  71. {
  72. }
  73. //virtual
  74. void LLToolFocus::handleSelect()
  75. {
  76. if (gFloaterToolsp)
  77. {
  78. gFloaterToolsp->setStatusText("camera");
  79. }
  80. }
  81. //virtual
  82. void LLToolFocus::handleDeselect()
  83. {
  84. }
  85. bool LLToolFocus::handleMouseDown(S32 x, S32 y, MASK mask)
  86. {
  87. // Ensure a mouseup
  88. setMouseCapture(true);
  89. // Call the base class to propogate info to sim
  90. LLTool::handleMouseDown(x, y, mask);
  91. mAccumX = mAccumY = 0;
  92. mOutsideSlopX = mOutsideSlopY = mValidClickPoint = false;
  93. // If mouse capture gets ripped away, claim we moused up at the point we
  94. // clicked down. JC
  95. mMouseUpX = x;
  96. mMouseUpY = y;
  97. mMouseUpMask = mask;
  98. gViewerWindowp->hideCursor();
  99. gViewerWindowp->pickAsync(x, y, mask, pickCallback);
  100. // Do not steal focus from UI
  101. return false;
  102. }
  103. //static
  104. void LLToolFocus::pickCallback(const LLPickInfo& pick_info)
  105. {
  106. LLToolFocus* self = &gToolFocus;
  107. if (!self->hasMouseCapture())
  108. {
  109. return;
  110. }
  111. self->mMouseDownX = pick_info.mMousePt.mX;
  112. self->mMouseDownY = pick_info.mMousePt.mY;
  113. gViewerWindowp->moveCursorToCenter();
  114. // Potentially recenter if click outside rectangle
  115. LLViewerObject* hit_obj = pick_info.getObject();
  116. // Check for hit the sky, or some other invalid point
  117. if (!hit_obj && pick_info.mPosGlobal.isExactlyZero())
  118. {
  119. self->mValidClickPoint = false;
  120. return;
  121. }
  122. // check for hud attachments
  123. if (hit_obj && hit_obj->isHUDAttachment())
  124. {
  125. LLObjectSelectionHandle selection = gSelectMgr.getSelection();
  126. if (!selection->getObjectCount() ||
  127. selection->getSelectType() != SELECT_TYPE_HUD)
  128. {
  129. self->mValidClickPoint = false;
  130. return;
  131. }
  132. }
  133. if (gAgent.getCameraMode() == CAMERA_MODE_CUSTOMIZE_AVATAR)
  134. {
  135. bool good_customize_avatar_hit = false;
  136. if (hit_obj)
  137. {
  138. if (hit_obj == gAgentAvatarp)
  139. {
  140. // It's you
  141. good_customize_avatar_hit = true;
  142. }
  143. else if (hit_obj->isAttachment() && hit_obj->permYouOwner())
  144. {
  145. // It's an attachment that you're wearing
  146. good_customize_avatar_hit = true;
  147. }
  148. }
  149. if (!good_customize_avatar_hit)
  150. {
  151. self->mValidClickPoint = false;
  152. return;
  153. }
  154. if (gMorphViewp)
  155. {
  156. gMorphViewp->setCameraDrivenByKeys(false);
  157. }
  158. }
  159. // RN: check to see if this is mouse-driving as opposed to ALT-zoom or
  160. // Focus tool
  161. else if (pick_info.mKeyMask & MASK_ALT ||
  162. gToolMgr.getCurrentTool()->getName() == "Focus")
  163. {
  164. //MK
  165. if (gRLenabled &&
  166. (gRLInterface.contains("camunlock") ||
  167. gRLInterface.contains("setcam_unlock")))
  168. {
  169. if (!(pick_info.mKeyMask & MASK_ALT) &&
  170. gAgent.cameraThirdPerson() &&
  171. gViewerWindowp->getLeftMouseDown() &&
  172. !LLPipeline::sFreezeTime &&
  173. (hit_obj == gAgentAvatarp ||
  174. (hit_obj && hit_obj->isAttachment() &&
  175. LLVOAvatar::findAvatarFromAttachment(hit_obj)->isSelf())))
  176. {
  177. // Do nothing, we are steering with the mouse... but the
  178. // condition is so complex it is better to check for its
  179. // negative.
  180. }
  181. else
  182. {
  183. self->mValidClickPoint = false;
  184. return;
  185. }
  186. }
  187. //mk
  188. LLViewerObject* hit_obj = pick_info.getObject();
  189. if (hit_obj)
  190. {
  191. // ...clicked on a world object, so focus at its position
  192. if (!hit_obj->isHUDAttachment())
  193. {
  194. gAgent.setFocusOnAvatar(false);
  195. gAgent.setFocusGlobal(pick_info);
  196. }
  197. }
  198. else if (!pick_info.mPosGlobal.isExactlyZero())
  199. {
  200. // Hit the ground
  201. gAgent.setFocusOnAvatar(false);
  202. gAgent.setFocusGlobal(pick_info);
  203. }
  204. if (!(pick_info.mKeyMask & MASK_ALT) && gAgent.cameraThirdPerson() &&
  205. gViewerWindowp->getLeftMouseDown() && !LLPipeline::sFreezeTime &&
  206. (hit_obj == gAgentAvatarp ||
  207. (hit_obj && hit_obj->isAttachment() &&
  208. LLVOAvatar::findAvatarFromAttachment(hit_obj)->isSelf())))
  209. {
  210. self->mMouseSteering = true;
  211. }
  212. }
  213. self->mValidClickPoint = true;
  214. if (CAMERA_MODE_CUSTOMIZE_AVATAR == gAgent.getCameraMode())
  215. {
  216. gAgent.setFocusOnAvatar(false, false);
  217. LLVector3d cam_pos = gAgent.getCameraPositionGlobal();
  218. cam_pos -= LLVector3d(gViewerCamera.getLeftAxis() *
  219. gAgent.calcCustomizeAvatarUIOffset(cam_pos));
  220. gAgent.setCameraPosAndFocusGlobal(cam_pos, pick_info.mPosGlobal,
  221. pick_info.mObjectID);
  222. }
  223. }
  224. // "Let go" of the mouse, for example on mouse up or when we loose mouse
  225. // capture. This ensures that cursor becomes visible if a modal dialog pops up
  226. // during Alt-Zoom. JC
  227. void LLToolFocus::releaseMouse()
  228. {
  229. // Need to tell the sim that the mouse button is up, since this tool is no
  230. // longer working and cursor is visible (despite actual mouse button status).
  231. LLTool::handleMouseUp(mMouseUpX, mMouseUpY, mMouseUpMask);
  232. gViewerWindowp->showCursor();
  233. gToolMgr.clearTransientTool();
  234. mMouseSteering = false;
  235. mValidClickPoint = false;
  236. mOutsideSlopX = false;
  237. mOutsideSlopY = false;
  238. }
  239. bool LLToolFocus::handleMouseUp(S32 x, S32 y, MASK mask)
  240. {
  241. // Claim that we're mousing up somewhere
  242. mMouseUpX = x;
  243. mMouseUpY = y;
  244. mMouseUpMask = mask;
  245. if (hasMouseCapture())
  246. {
  247. if (mValidClickPoint)
  248. {
  249. if (CAMERA_MODE_CUSTOMIZE_AVATAR == gAgent.getCameraMode())
  250. {
  251. LLCoordGL mouse_pos;
  252. LLVector3 focus_pos;
  253. focus_pos = gAgent.getPosAgentFromGlobal(gAgent.getFocusGlobal());
  254. bool success = gViewerCamera.projectPosAgentToScreen(focus_pos,
  255. mouse_pos);
  256. if (success)
  257. {
  258. LLUI::setCursorPositionScreen(mouse_pos.mX, mouse_pos.mY);
  259. }
  260. }
  261. else if (mMouseSteering)
  262. {
  263. LLUI::setCursorPositionScreen(mMouseDownX, mMouseDownY);
  264. }
  265. else
  266. {
  267. gViewerWindowp->moveCursorToCenter();
  268. }
  269. }
  270. else
  271. {
  272. // not a valid zoomable object
  273. LLUI::setCursorPositionScreen(mMouseDownX, mMouseDownY);
  274. }
  275. // calls releaseMouse() internally
  276. setMouseCapture(false);
  277. }
  278. else
  279. {
  280. releaseMouse();
  281. }
  282. return true;
  283. }
  284. bool LLToolFocus::handleHover(S32 x, S32 y, MASK mask)
  285. {
  286. S32 dx = gViewerWindowp->getCurrentMouseDX();
  287. S32 dy = gViewerWindowp->getCurrentMouseDY();
  288. if (hasMouseCapture() && mValidClickPoint)
  289. {
  290. mAccumX += abs(dx);
  291. mAccumY += abs(dy);
  292. if (mAccumX >= SLOP_RANGE)
  293. {
  294. mOutsideSlopX = true;
  295. }
  296. if (mAccumY >= SLOP_RANGE)
  297. {
  298. mOutsideSlopY = true;
  299. }
  300. }
  301. if (mOutsideSlopX || mOutsideSlopY)
  302. {
  303. if (!mValidClickPoint)
  304. {
  305. LL_DEBUGS("UserInput") << "hover handled by LLToolFocus [invalid point]"
  306. << LL_ENDL;
  307. gViewerWindowp->setCursor(UI_CURSOR_NO);
  308. gViewerWindowp->showCursor();
  309. return true;
  310. }
  311. if (gCameraBtnOrbit || mask == MASK_ORBIT ||
  312. mask == (MASK_ALT | MASK_ORBIT))
  313. {
  314. // Orbit tool
  315. if (hasMouseCapture())
  316. {
  317. const F32 radians_per_pixel = 360.f * DEG_TO_RAD /
  318. gViewerWindowp->getWindowWidth();
  319. if (dx != 0)
  320. {
  321. gAgent.cameraOrbitAround(-dx * radians_per_pixel);
  322. }
  323. if (dy != 0)
  324. {
  325. gAgent.cameraOrbitOver(-dy * radians_per_pixel);
  326. }
  327. gViewerWindowp->moveCursorToCenter();
  328. }
  329. LL_DEBUGS("UserInput") << "hover handled by LLToolFocus [active]"
  330. << LL_ENDL;
  331. }
  332. else if (gCameraBtnPan || mask == MASK_PAN ||
  333. mask == (MASK_PAN | MASK_ALT))
  334. {
  335. // Pan tool
  336. if (hasMouseCapture())
  337. {
  338. LLVector3d camera_to_focus = gAgent.getCameraPositionGlobal();
  339. camera_to_focus -= gAgent.getFocusGlobal();
  340. F32 dist = (F32)camera_to_focus.normalize();
  341. // Fudge factor for pan
  342. F32 meters_per_pixel = 3.f * dist / gViewerWindowp->getWindowWidth();
  343. if (dx != 0)
  344. {
  345. gAgent.cameraPanLeft(dx * meters_per_pixel);
  346. }
  347. if (dy != 0)
  348. {
  349. gAgent.cameraPanUp(-dy * meters_per_pixel);
  350. }
  351. gViewerWindowp->moveCursorToCenter();
  352. }
  353. LL_DEBUGS("UserInput") << "hover handled by LLToolPan" << LL_ENDL;
  354. }
  355. else if (gCameraBtnZoom)
  356. {
  357. // Zoom tool
  358. if (hasMouseCapture())
  359. {
  360. const F32 radians_per_pixel = 360.f * DEG_TO_RAD /
  361. gViewerWindowp->getWindowWidth();
  362. if (dx != 0)
  363. {
  364. gAgent.cameraOrbitAround(-dx * radians_per_pixel);
  365. }
  366. constexpr F32 IN_FACTOR = 0.99f;
  367. if (dy != 0 && mOutsideSlopY)
  368. {
  369. if (mMouseSteering)
  370. {
  371. gAgent.cameraOrbitOver(-dy * radians_per_pixel);
  372. }
  373. else
  374. {
  375. gAgent.cameraZoomIn(powf(IN_FACTOR, dy));
  376. }
  377. }
  378. gViewerWindowp->moveCursorToCenter();
  379. }
  380. LL_DEBUGS("UserInput") << "hover handled by LLToolZoom" << LL_ENDL;
  381. }
  382. }
  383. if (gCameraBtnOrbit || mask == MASK_ORBIT ||
  384. mask == (MASK_ALT | MASK_ORBIT))
  385. {
  386. gViewerWindowp->setCursor(UI_CURSOR_TOOLCAMERA);
  387. }
  388. else if (gCameraBtnPan || mask == MASK_PAN ||
  389. mask == (MASK_PAN | MASK_ALT))
  390. {
  391. gViewerWindowp->setCursor(UI_CURSOR_TOOLPAN);
  392. }
  393. else
  394. {
  395. gViewerWindowp->setCursor(UI_CURSOR_TOOLZOOMIN);
  396. }
  397. return true;
  398. }
  399. void LLToolFocus::onMouseCaptureLost()
  400. {
  401. releaseMouse();
  402. }