PageRenderTime 163ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/newview/lltoolmgr.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 477 lines | 343 code | 74 blank | 60 comment | 55 complexity | f2e259a98159174937e4d815515d3afe MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file lltoolmgr.cpp
  3. * @brief LLToolMgr class implementation
  4. *
  5. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library 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 GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "llviewerprecompiledheaders.h"
  27. #include "lltoolmgr.h"
  28. #include "lluictrl.h"
  29. #include "llmenugl.h"
  30. #include "llfloaterreg.h"
  31. //#include "llfirstuse.h"
  32. // tools and manipulators
  33. #include "lltool.h"
  34. #include "llmanipscale.h"
  35. #include "llselectmgr.h"
  36. #include "lltoolbrush.h"
  37. #include "lltoolcomp.h"
  38. #include "lltooldraganddrop.h"
  39. #include "lltoolface.h"
  40. #include "lltoolfocus.h"
  41. #include "lltoolgrab.h"
  42. #include "lltoolindividual.h"
  43. #include "lltoolmorph.h"
  44. #include "lltoolpie.h"
  45. #include "lltoolselectland.h"
  46. #include "lltoolobjpicker.h"
  47. #include "lltoolpipette.h"
  48. #include "llagent.h"
  49. #include "llagentcamera.h"
  50. #include "llviewercontrol.h"
  51. #include "llviewerjoystick.h"
  52. #include "llviewermenu.h"
  53. #include "llviewerparcelmgr.h"
  54. // Used when app not active to avoid processing hover.
  55. LLTool* gToolNull = NULL;
  56. LLToolset* gBasicToolset = NULL;
  57. LLToolset* gCameraToolset = NULL;
  58. //LLToolset* gLandToolset = NULL;
  59. LLToolset* gMouselookToolset = NULL;
  60. LLToolset* gFaceEditToolset = NULL;
  61. /////////////////////////////////////////////////////
  62. // LLToolMgr
  63. LLToolMgr::LLToolMgr()
  64. :
  65. mBaseTool(NULL),
  66. mSavedTool(NULL),
  67. mTransientTool( NULL ),
  68. mOverrideTool( NULL ),
  69. mSelectedTool( NULL ),
  70. mCurrentToolset( NULL )
  71. {
  72. // Not a panel, register these callbacks globally.
  73. LLUICtrl::EnableCallbackRegistry::currentRegistrar().add("Build.Active", boost::bind(&LLToolMgr::inEdit, this));
  74. LLUICtrl::EnableCallbackRegistry::currentRegistrar().add("Build.Enabled", boost::bind(&LLToolMgr::canEdit, this));
  75. LLUICtrl::CommitCallbackRegistry::currentRegistrar().add("Build.Toggle", boost::bind(&LLToolMgr::toggleBuildMode, this));
  76. gToolNull = new LLTool(LLStringUtil::null); // Does nothing
  77. setCurrentTool(gToolNull);
  78. gBasicToolset = new LLToolset();
  79. gCameraToolset = new LLToolset();
  80. // gLandToolset = new LLToolset();
  81. gMouselookToolset = new LLToolset();
  82. gFaceEditToolset = new LLToolset();
  83. }
  84. void LLToolMgr::initTools()
  85. {
  86. static BOOL initialized = FALSE;
  87. if(initialized)
  88. {
  89. return;
  90. }
  91. initialized = TRUE;
  92. gBasicToolset->addTool( LLToolPie::getInstance() );
  93. gBasicToolset->addTool( LLToolCamera::getInstance() );
  94. gCameraToolset->addTool( LLToolCamera::getInstance() );
  95. gBasicToolset->addTool( LLToolGrab::getInstance() );
  96. gBasicToolset->addTool( LLToolCompTranslate::getInstance() );
  97. gBasicToolset->addTool( LLToolCompCreate::getInstance() );
  98. gBasicToolset->addTool( LLToolBrushLand::getInstance() );
  99. gMouselookToolset->addTool( LLToolCompGun::getInstance() );
  100. gBasicToolset->addTool( LLToolCompInspect::getInstance() );
  101. gFaceEditToolset->addTool( LLToolCamera::getInstance() );
  102. // On startup, use "select" tool
  103. setCurrentToolset(gBasicToolset);
  104. gBasicToolset->selectTool( LLToolPie::getInstance() );
  105. }
  106. LLToolMgr::~LLToolMgr()
  107. {
  108. delete gBasicToolset;
  109. gBasicToolset = NULL;
  110. delete gMouselookToolset;
  111. gMouselookToolset = NULL;
  112. delete gFaceEditToolset;
  113. gFaceEditToolset = NULL;
  114. delete gCameraToolset;
  115. gCameraToolset = NULL;
  116. delete gToolNull;
  117. gToolNull = NULL;
  118. }
  119. BOOL LLToolMgr::usingTransientTool()
  120. {
  121. return mTransientTool ? TRUE : FALSE;
  122. }
  123. void LLToolMgr::setCurrentToolset(LLToolset* current)
  124. {
  125. if (!current) return;
  126. // switching toolsets?
  127. if (current != mCurrentToolset)
  128. {
  129. // deselect current tool
  130. if (mSelectedTool)
  131. {
  132. mSelectedTool->handleDeselect();
  133. }
  134. mCurrentToolset = current;
  135. // select first tool of new toolset only if toolset changed
  136. mCurrentToolset->selectFirstTool();
  137. }
  138. // update current tool based on new toolset
  139. setCurrentTool( mCurrentToolset->getSelectedTool() );
  140. }
  141. LLToolset* LLToolMgr::getCurrentToolset()
  142. {
  143. return mCurrentToolset;
  144. }
  145. void LLToolMgr::setCurrentTool( LLTool* tool )
  146. {
  147. if (mTransientTool)
  148. {
  149. mTransientTool = NULL;
  150. }
  151. mBaseTool = tool;
  152. updateToolStatus();
  153. mSavedTool = NULL;
  154. }
  155. LLTool* LLToolMgr::getCurrentTool()
  156. {
  157. MASK override_mask = gKeyboard ? gKeyboard->currentMask(TRUE) : 0;
  158. LLTool* cur_tool = NULL;
  159. // always use transient tools if available
  160. if (mTransientTool)
  161. {
  162. mOverrideTool = NULL;
  163. cur_tool = mTransientTool;
  164. }
  165. // tools currently grabbing mouse input will stay active
  166. else if (mSelectedTool && mSelectedTool->hasMouseCapture())
  167. {
  168. cur_tool = mSelectedTool;
  169. }
  170. else
  171. {
  172. mOverrideTool = mBaseTool ? mBaseTool->getOverrideTool(override_mask) : NULL;
  173. // use override tool if available otherwise drop back to base tool
  174. cur_tool = mOverrideTool ? mOverrideTool : mBaseTool;
  175. }
  176. LLTool* prev_tool = mSelectedTool;
  177. // Set the selected tool to avoid infinite recursion
  178. mSelectedTool = cur_tool;
  179. //update tool selection status
  180. if (prev_tool != cur_tool)
  181. {
  182. if (prev_tool)
  183. {
  184. prev_tool->handleDeselect();
  185. }
  186. if (cur_tool)
  187. {
  188. cur_tool->handleSelect();
  189. }
  190. }
  191. return mSelectedTool;
  192. }
  193. LLTool* LLToolMgr::getBaseTool()
  194. {
  195. return mBaseTool;
  196. }
  197. void LLToolMgr::updateToolStatus()
  198. {
  199. // call getcurrenttool() to calculate active tool and call handleSelect() and handleDeselect() immediately
  200. // when active tool changes
  201. getCurrentTool();
  202. }
  203. bool LLToolMgr::inEdit()
  204. {
  205. return mBaseTool != LLToolPie::getInstance() && mBaseTool != gToolNull;
  206. }
  207. bool LLToolMgr::canEdit()
  208. {
  209. return LLViewerParcelMgr::getInstance()->allowAgentBuild();
  210. }
  211. void LLToolMgr::toggleBuildMode()
  212. {
  213. LLFloaterReg::toggleInstanceOrBringToFront("build");
  214. bool build_visible = LLFloaterReg::instanceVisible("build");
  215. if (build_visible)
  216. {
  217. ECameraMode camMode = gAgentCamera.getCameraMode();
  218. if (CAMERA_MODE_MOUSELOOK == camMode || CAMERA_MODE_CUSTOMIZE_AVATAR == camMode)
  219. {
  220. // pull the user out of mouselook or appearance mode when entering 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. handle_toggle_flycam();
  229. }
  230. if (gAgentCamera.getFocusOnAvatar())
  231. {
  232. // zoom in if we're looking at the avatar
  233. gAgentCamera.setFocusOnAvatar(FALSE, ANIMATE);
  234. gAgentCamera.setFocusGlobal(gAgent.getPositionGlobal() + 2.0 * LLVector3d(gAgent.getAtAxis()));
  235. gAgentCamera.cameraZoomIn(0.666f);
  236. gAgentCamera.cameraOrbitOver( 30.f * DEG_TO_RAD );
  237. }
  238. }
  239. setCurrentToolset(gBasicToolset);
  240. getCurrentToolset()->selectTool( LLToolCompCreate::getInstance() );
  241. // Could be first use
  242. //LLFirstUse::useBuild();
  243. gAgentCamera.resetView(false);
  244. // avoid spurious avatar movements
  245. LLViewerJoystick::getInstance()->setNeedsReset();
  246. }
  247. else
  248. {
  249. if (gSavedSettings.getBOOL("EditCameraMovement"))
  250. {
  251. // just reset the view, will pull us out of edit mode
  252. handle_reset_view();
  253. }
  254. else
  255. {
  256. // manually disable edit mode, but do not affect the camera
  257. gAgentCamera.resetView(false);
  258. LLFloaterReg::hideInstance("build");
  259. gViewerWindow->showCursor();
  260. }
  261. // avoid spurious avatar movements pulling out of edit mode
  262. LLViewerJoystick::getInstance()->setNeedsReset();
  263. }
  264. }
  265. bool LLToolMgr::inBuildMode()
  266. {
  267. // when entering mouselook inEdit() immediately returns true before
  268. // cameraMouselook() actually starts returning true. Also, appearance edit
  269. // sets build mode to true, so let's exclude that.
  270. bool b=(inEdit()
  271. && !gAgentCamera.cameraMouselook()
  272. && mCurrentToolset != gFaceEditToolset);
  273. return b;
  274. }
  275. void LLToolMgr::setTransientTool(LLTool* tool)
  276. {
  277. if (!tool)
  278. {
  279. clearTransientTool();
  280. }
  281. else
  282. {
  283. if (mTransientTool)
  284. {
  285. mTransientTool = NULL;
  286. }
  287. mTransientTool = tool;
  288. }
  289. updateToolStatus();
  290. }
  291. void LLToolMgr::clearTransientTool()
  292. {
  293. if (mTransientTool)
  294. {
  295. mTransientTool = NULL;
  296. if (!mBaseTool)
  297. {
  298. llwarns << "mBaseTool is NULL" << llendl;
  299. }
  300. }
  301. updateToolStatus();
  302. }
  303. void LLToolMgr::onAppFocusLost()
  304. {
  305. if (mSelectedTool)
  306. {
  307. mSelectedTool->handleDeselect();
  308. }
  309. updateToolStatus();
  310. }
  311. void LLToolMgr::onAppFocusGained()
  312. {
  313. if (mSelectedTool)
  314. {
  315. mSelectedTool->handleSelect();
  316. }
  317. updateToolStatus();
  318. }
  319. void LLToolMgr::clearSavedTool()
  320. {
  321. mSavedTool = NULL;
  322. }
  323. /////////////////////////////////////////////////////
  324. // LLToolset
  325. void LLToolset::addTool(LLTool* tool)
  326. {
  327. mToolList.push_back( tool );
  328. if( !mSelectedTool )
  329. {
  330. mSelectedTool = tool;
  331. }
  332. }
  333. void LLToolset::selectTool(LLTool* tool)
  334. {
  335. mSelectedTool = tool;
  336. LLToolMgr::getInstance()->setCurrentTool( mSelectedTool );
  337. }
  338. void LLToolset::selectToolByIndex( S32 index )
  339. {
  340. LLTool *tool = (index >= 0 && index < (S32)mToolList.size()) ? mToolList[index] : NULL;
  341. if (tool)
  342. {
  343. mSelectedTool = tool;
  344. LLToolMgr::getInstance()->setCurrentTool( tool );
  345. }
  346. }
  347. BOOL LLToolset::isToolSelected( S32 index )
  348. {
  349. LLTool *tool = (index >= 0 && index < (S32)mToolList.size()) ? mToolList[index] : NULL;
  350. return (tool == mSelectedTool);
  351. }
  352. void LLToolset::selectFirstTool()
  353. {
  354. mSelectedTool = (0 < mToolList.size()) ? mToolList[0] : NULL;
  355. LLToolMgr::getInstance()->setCurrentTool( mSelectedTool );
  356. }
  357. void LLToolset::selectNextTool()
  358. {
  359. LLTool* next = NULL;
  360. for( tool_list_t::iterator iter = mToolList.begin();
  361. iter != mToolList.end(); )
  362. {
  363. LLTool* cur = *iter++;
  364. if( cur == mSelectedTool && iter != mToolList.end() )
  365. {
  366. next = *iter;
  367. break;
  368. }
  369. }
  370. if( next )
  371. {
  372. mSelectedTool = next;
  373. LLToolMgr::getInstance()->setCurrentTool( mSelectedTool );
  374. }
  375. else
  376. {
  377. selectFirstTool();
  378. }
  379. }
  380. void LLToolset::selectPrevTool()
  381. {
  382. LLTool* prev = NULL;
  383. for( tool_list_t::reverse_iterator iter = mToolList.rbegin();
  384. iter != mToolList.rend(); )
  385. {
  386. LLTool* cur = *iter++;
  387. if( cur == mSelectedTool && iter != mToolList.rend() )
  388. {
  389. prev = *iter;
  390. break;
  391. }
  392. }
  393. if( prev )
  394. {
  395. mSelectedTool = prev;
  396. LLToolMgr::getInstance()->setCurrentTool( mSelectedTool );
  397. }
  398. else if (mToolList.size() > 0)
  399. {
  400. selectToolByIndex((S32)mToolList.size()-1);
  401. }
  402. }
  403. ////////////////////////////////////////////////////////////////////////////