lltoolmgr.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * @file lltoolmgr.cpp
  3. * @brief LLToolMgr class implementation
  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 "lltoolmgr.h"
  34. #include "llagent.h"
  35. #include "llapp.h"
  36. #include "llfirstuse.h"
  37. #include "llfloatertools.h"
  38. //MK
  39. #include "mkrlinterface.h"
  40. //mk
  41. #include "llselectmgr.h"
  42. #include "lltool.h"
  43. #include "lltoolbrushland.h"
  44. #include "lltoolcomp.h"
  45. #include "lltoolfocus.h"
  46. #include "lltoolgrab.h"
  47. #include "lltoolpie.h"
  48. #include "lltoolpipette.h"
  49. #include "llviewercontrol.h"
  50. #include "llviewerjoystick.h"
  51. #include "llviewermenu.h"
  52. #include "llviewerwindow.h"
  53. // Globals
  54. LLToolMgr gToolMgr;
  55. // Used when app not active to avoid processing hover.
  56. LLTool* gToolNull = NULL;
  57. LLToolset* gBasicToolset = NULL;
  58. LLToolset* gCameraToolset = NULL;
  59. LLToolset* gMouselookToolset = NULL;
  60. LLToolset* gFaceEditToolset = NULL;
  61. /////////////////////////////////////////////////////
  62. // LLToolMgr
  63. LLToolMgr::LLToolMgr()
  64. : mBaseTool(NULL),
  65. mSavedTool(NULL),
  66. mTransientTool(NULL),
  67. mOverrideTool(NULL),
  68. mSelectedTool(NULL),
  69. mCurrentToolset(NULL)
  70. {
  71. gToolNull = new LLTool(LLStringUtil::null); // Does nothing
  72. setCurrentTool(gToolNull);
  73. gBasicToolset = new LLToolset();
  74. gCameraToolset = new LLToolset();
  75. gMouselookToolset = new LLToolset();
  76. gFaceEditToolset = new LLToolset();
  77. }
  78. void LLToolMgr::initTools()
  79. {
  80. static bool initialized = false;
  81. if (initialized)
  82. {
  83. return;
  84. }
  85. initialized = true;
  86. gBasicToolset->addTool(&gToolPie);
  87. gBasicToolset->addTool(&gToolFocus);
  88. gCameraToolset->addTool(&gToolFocus);
  89. gBasicToolset->addTool(&gToolGrab);
  90. gBasicToolset->addTool(&gToolCompTranslate);
  91. gBasicToolset->addTool(&gToolCompCreate);
  92. gBasicToolset->addTool(&gToolBrushLand);
  93. gMouselookToolset->addTool(&gToolCompGun);
  94. gBasicToolset->addTool(&gToolCompInspect);
  95. gFaceEditToolset->addTool(&gToolFocus);
  96. // In case focus was lost before we got here
  97. clearSavedTool();
  98. // On startup, use "select" tool
  99. setCurrentToolset(gBasicToolset);
  100. gBasicToolset->selectTool(&gToolPie);
  101. }
  102. LLToolMgr::~LLToolMgr()
  103. {
  104. delete gBasicToolset;
  105. gBasicToolset = NULL;
  106. delete gMouselookToolset;
  107. gMouselookToolset = NULL;
  108. delete gFaceEditToolset;
  109. gFaceEditToolset = NULL;
  110. delete gCameraToolset;
  111. gCameraToolset = NULL;
  112. delete gToolNull;
  113. gToolNull = NULL;
  114. }
  115. void LLToolMgr::setCurrentToolset(LLToolset* current)
  116. {
  117. if (!current) return;
  118. // Switching toolsets ?
  119. if (current != mCurrentToolset)
  120. {
  121. // Deselect current tool
  122. if (mSelectedTool)
  123. {
  124. mSelectedTool->handleDeselect();
  125. }
  126. mCurrentToolset = current;
  127. // Select first tool of new toolset only if toolset changed
  128. mCurrentToolset->selectFirstTool();
  129. }
  130. // Update current tool based on new toolset
  131. setCurrentTool(mCurrentToolset->getSelectedTool());
  132. }
  133. void LLToolMgr::setCurrentTool(LLTool* tool)
  134. {
  135. if (mTransientTool)
  136. {
  137. mTransientTool = NULL;
  138. }
  139. mBaseTool = tool;
  140. updateToolStatus();
  141. }
  142. LLTool* LLToolMgr::getCurrentTool()
  143. {
  144. MASK override_mask = gKeyboardp ? gKeyboardp->currentMask(true) : 0;
  145. LLTool* cur_tool = NULL;
  146. // Always use transient tools if available
  147. if (mTransientTool)
  148. {
  149. mOverrideTool = NULL;
  150. cur_tool = mTransientTool;
  151. }
  152. // Tools currently grabbing mouse input will stay active
  153. else if (mSelectedTool && mSelectedTool->hasMouseCapture())
  154. {
  155. cur_tool = mSelectedTool;
  156. }
  157. else
  158. {
  159. // Do not override gToolNull
  160. mOverrideTool = mBaseTool && mBaseTool != gToolNull ?
  161. mBaseTool->getOverrideTool(override_mask) : NULL;
  162. // Use override tool if available otherwise drop back to base tool
  163. cur_tool = mOverrideTool ? mOverrideTool : mBaseTool;
  164. }
  165. LLTool* prev_tool = mSelectedTool;
  166. // Set the selected tool to avoid infinite recursion
  167. mSelectedTool = cur_tool;
  168. // Update tool selection status
  169. if (prev_tool != cur_tool)
  170. {
  171. if (prev_tool)
  172. {
  173. prev_tool->handleDeselect();
  174. }
  175. if (cur_tool)
  176. {
  177. cur_tool->handleSelect();
  178. }
  179. }
  180. return mSelectedTool;
  181. }
  182. bool LLToolMgr::inEdit()
  183. {
  184. return mBaseTool != &gToolPie && mBaseTool != gToolNull;
  185. }
  186. void LLToolMgr::toggleBuildMode()
  187. {
  188. if (!gViewerWindowp) return;
  189. if (LLFloaterTools::isVisible())
  190. {
  191. if (gSavedSettings.getBool("EditCameraMovement"))
  192. {
  193. // Just reset the view, will pull us out of edit mode
  194. handle_reset_view();
  195. }
  196. else
  197. {
  198. // Manually disable edit mode, but do not affect the camera
  199. gAgent.resetView(false);
  200. gFloaterToolsp->close();
  201. gViewerWindowp->showCursor();
  202. }
  203. // Avoid spurious avatar movements pulling out of edit mode
  204. LLViewerJoystick::getInstance()->setNeedsReset();
  205. }
  206. else
  207. {
  208. //MK
  209. if (gRLenabled &&
  210. (gRLInterface.mContainsRez || gRLInterface.mContainsEdit))
  211. {
  212. return;
  213. }
  214. //mk
  215. ECameraMode cam_mode = gAgent.getCameraMode();
  216. if (cam_mode == CAMERA_MODE_MOUSELOOK ||
  217. cam_mode == CAMERA_MODE_CUSTOMIZE_AVATAR)
  218. {
  219. // Pull the user out of mouselook or appearance mode when entering
  220. // build mode
  221. handle_reset_view();
  222. }
  223. if (gSavedSettings.getBool("EditCameraMovement"))
  224. {
  225. // Camera should be set
  226. if (LLViewerJoystick::getInstance()->getOverrideCamera())
  227. {
  228. LLViewerJoystick::getInstance()->toggleFlycam();
  229. }
  230. if (gAgent.getFocusOnAvatar())
  231. {
  232. // zoom in if we're looking at the avatar
  233. gAgent.setFocusOnAvatar(false);
  234. gAgent.setFocusGlobal(gAgent.getPositionGlobal() +
  235. 2.0 * LLVector3d(gAgent.getAtAxis()));
  236. gAgent.cameraZoomIn(0.666f);
  237. gAgent.cameraOrbitOver(30.f * DEG_TO_RAD);
  238. }
  239. }
  240. setCurrentToolset(gBasicToolset);
  241. getCurrentToolset()->selectTool(&gToolCompCreate);
  242. // Could be first use
  243. LLFirstUse::useBuild();
  244. gAgent.resetView(false);
  245. // Avoid spurious avatar movements
  246. LLViewerJoystick::getInstance()->setNeedsReset();
  247. }
  248. }
  249. bool LLToolMgr::inBuildMode()
  250. {
  251. // When entering mouselook inEdit() immediately returns true before
  252. // cameraMouselook() actually starts returning true. Also, appearance edit
  253. // sets build mode to true, so let's exclude that.
  254. return inEdit() && mCurrentToolset != gFaceEditToolset &&
  255. #if 0
  256. LLFloaterTools::isVisible() &&
  257. #endif
  258. !gAgent.cameraMouselook();
  259. }
  260. void LLToolMgr::setTransientTool(LLTool* tool)
  261. {
  262. if (!tool)
  263. {
  264. clearTransientTool();
  265. }
  266. else
  267. {
  268. if (mTransientTool)
  269. {
  270. mTransientTool = NULL;
  271. }
  272. mTransientTool = tool;
  273. }
  274. updateToolStatus();
  275. }
  276. void LLToolMgr::clearTransientTool()
  277. {
  278. if (mTransientTool)
  279. {
  280. mTransientTool = NULL;
  281. if (!mBaseTool)
  282. {
  283. llwarns << "mBaseTool is NULL" << llendl;
  284. }
  285. }
  286. updateToolStatus();
  287. }
  288. void LLToolMgr::onAppFocusLost()
  289. {
  290. if (LLApp::isExiting())
  291. {
  292. return;
  293. }
  294. if (mSelectedTool)
  295. {
  296. mSelectedTool->handleDeselect();
  297. }
  298. updateToolStatus();
  299. }
  300. void LLToolMgr::onAppFocusGained()
  301. {
  302. if (mSelectedTool)
  303. {
  304. mSelectedTool->handleSelect();
  305. }
  306. updateToolStatus();
  307. }
  308. /////////////////////////////////////////////////////
  309. // LLToolset
  310. void LLToolset::addTool(LLTool* tool)
  311. {
  312. mToolList.push_back(tool);
  313. if (!mSelectedTool)
  314. {
  315. mSelectedTool = tool;
  316. }
  317. }
  318. void LLToolset::selectTool(LLTool* tool)
  319. {
  320. mSelectedTool = tool;
  321. gToolMgr.setCurrentTool(mSelectedTool);
  322. }
  323. void LLToolset::selectToolByIndex(U32 index)
  324. {
  325. if (index < (U32)mToolList.size())
  326. {
  327. LLTool* tool = mToolList[index];
  328. if (tool)
  329. {
  330. mSelectedTool = tool;
  331. gToolMgr.setCurrentTool(tool);
  332. }
  333. }
  334. }
  335. void LLToolset::selectNextTool()
  336. {
  337. LLTool* next = NULL;
  338. for (tool_list_t::iterator iter = mToolList.begin();
  339. iter != mToolList.end();)
  340. {
  341. LLTool* cur = *iter++;
  342. if (cur == mSelectedTool && iter != mToolList.end())
  343. {
  344. next = *iter;
  345. break;
  346. }
  347. }
  348. if (next)
  349. {
  350. mSelectedTool = next;
  351. gToolMgr.setCurrentTool(mSelectedTool);
  352. }
  353. else
  354. {
  355. selectFirstTool();
  356. }
  357. }
  358. void LLToolset::selectPrevTool()
  359. {
  360. LLTool* prev = NULL;
  361. for (tool_list_t::reverse_iterator iter = mToolList.rbegin();
  362. iter != mToolList.rend(); )
  363. {
  364. LLTool* cur = *iter++;
  365. if (cur == mSelectedTool && iter != mToolList.rend())
  366. {
  367. prev = *iter;
  368. break;
  369. }
  370. }
  371. if (prev)
  372. {
  373. mSelectedTool = prev;
  374. gToolMgr.setCurrentTool(mSelectedTool);
  375. }
  376. else
  377. {
  378. S32 count = mToolList.size();
  379. if (count)
  380. {
  381. selectToolByIndex(count - 1);
  382. }
  383. }
  384. }