llfloaterjoystick.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /**
  2. * @file llfloaterjoystick.cpp
  3. * @brief Joystick preferences panel
  4. *
  5. * $LicenseInfo:firstyear=2007&license=viewergpl$
  6. *
  7. * Copyright (c) 2007-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 "llfloaterjoystick.h"
  34. #include "llbutton.h"
  35. #include "llcheckboxctrl.h"
  36. #include "llstat.h"
  37. #include "llstatview.h"
  38. #include "lltextbox.h"
  39. #include "lluictrlfactory.h"
  40. #include "llappviewer.h"
  41. #include "llstartup.h"
  42. #include "llviewercontrol.h"
  43. #include "llviewerjoystick.h"
  44. LLFloaterJoystick::LLFloaterJoystick(const LLSD& data)
  45. : LLFloater("joystick"),
  46. mAxisStatsView(NULL)
  47. {
  48. LLUICtrlFactory::getInstance()->buildFloater(this, "floater_joystick.xml");
  49. center();
  50. }
  51. //virtual
  52. bool LLFloaterJoystick::postBuild()
  53. {
  54. mCheckJoystickEnabled = getChild<LLCheckBoxCtrl>("enable_joystick");
  55. childSetCommitCallback("enable_joystick", onCommitJoystickEnabled, this);
  56. mJoystickType = getChild<LLTextBox>("joystick_type");
  57. mCheckFlycamEnabled = getChild<LLCheckBoxCtrl>("JoystickFlycamEnabled");
  58. mCheckFlycamEnabled->setCommitCallback(onCommitJoystickEnabled);
  59. mCheckFlycamEnabled->setCallbackUserData(this);
  60. mInitButon = getChild<LLButton>("init_btn");
  61. mInitButon->setClickedCallback(onClickInit, this);
  62. childSetAction("cancel_btn", onClickCancel, this);
  63. childSetAction("ok_btn", onClickOK, this);
  64. childSetCommitCallback("defaults_btn", onClickRestoreDefaults, this);
  65. refresh();
  66. if (!LLStartUp::isLoggedIn())
  67. {
  68. // If not logged in, the joystick is not read in the main loop and the
  69. // monitor cannot be updated, so do not build the latter at all !
  70. return true;
  71. }
  72. childSetVisible("no_monitor", false);
  73. LLUIString joystick = getString("JoystickMonitor");
  74. // Use this child to get relative positioning info; we will place the
  75. // joystick monitor on its right, vertically aligned to it.
  76. LLRect rect;
  77. LLView* child = getChild<LLView>("FlycamAxisScale1");
  78. LLRect r = child->getRect();
  79. // Note: the stats view height is automatically adjusted (thus the final 0)
  80. rect = LLRect(r.mRight + 10, r.mTop, r.mRight + 170, 0);
  81. mAxisStatsView = new LLStatView("axis values", joystick, "", rect);
  82. mAxisStatsView->setDisplayChildren(true);
  83. mAxisStatsView->setCanCollapse(false);
  84. F32 range = gSavedSettings.getBool("Cursor3D") ? 1024.f : 2.f;
  85. LLUIString axis = getString("Axis");
  86. for (U32 i = 0; i < 6; ++i)
  87. {
  88. axis.setArg("[NUM]", llformat("%d", i));
  89. mAxisStats[i] = new LLStat(4);
  90. mAxisStatsBar[i] = mAxisStatsView->addStat(axis, mAxisStats[i], "",
  91. true); // Display the bar
  92. mAxisStatsBar[i]->mNoResize = true;
  93. mAxisStatsBar[i]->mMinBar = -range;
  94. mAxisStatsBar[i]->mMaxBar = range;
  95. mAxisStatsBar[i]->mLabelSpacing = range > 100.f ? range : range * 0.5f;
  96. mAxisStatsBar[i]->mTickSpacing = range * 0.25f;
  97. }
  98. addChild(mAxisStatsView);
  99. for (S32 i = 0; i < 16; ++i)
  100. {
  101. mJoystickButtons[i] =
  102. getChild<LLTextBox>(llformat("btn%d_text", i).c_str());
  103. mJoystickButtons[i]->setVisible(true);
  104. }
  105. return true;
  106. }
  107. //virtual
  108. void LLFloaterJoystick::draw()
  109. {
  110. LLViewerJoystick* joystick = LLViewerJoystick::getInstance();
  111. bool init_done = joystick->isJoystickInitialized();
  112. mCheckJoystickEnabled->setEnabled(init_done);
  113. std::string desc = joystick->getDescription();
  114. if (desc.empty())
  115. {
  116. desc = getString("NoDevice");
  117. }
  118. mJoystickType->setValue(desc);
  119. mJoystickType->setEnabled(init_done);
  120. mInitButon->setEnabled(!init_done);
  121. if (mAxisStatsView)
  122. {
  123. static LLCachedControl<bool> cursor_3d(gSavedSettings, "Cursor3D");
  124. F32 range = cursor_3d ? 1024.f : 2.f;
  125. static F32 old_range = 0.f;
  126. for (S32 i = 0; i < 6; ++i)
  127. {
  128. F32 value = joystick->getJoystickAxis(i);
  129. mAxisStats[i]->addValue(value * gFrameIntervalSeconds);
  130. if (mAxisStatsBar[i]->mMinBar > value)
  131. {
  132. mAxisStatsBar[i]->mMinBar = value;
  133. }
  134. if (mAxisStatsBar[i]->mMaxBar < value)
  135. {
  136. mAxisStatsBar[i]->mMaxBar = value;
  137. }
  138. if (old_range != range)
  139. {
  140. mAxisStatsBar[i]->mMinBar = -range;
  141. mAxisStatsBar[i]->mMaxBar = range;
  142. mAxisStatsBar[i]->mLabelSpacing = range > 100.f ? range
  143. : range * 0.5f;
  144. mAxisStatsBar[i]->mTickSpacing = range * 0.25f;
  145. }
  146. }
  147. old_range = range;
  148. for (S32 i = 0; i < 16; ++i)
  149. {
  150. bool pressed = joystick->getJoystickButton(i);
  151. if (pressed)
  152. {
  153. mJoystickButtons[i]->setColor(LLColor4::white);
  154. }
  155. else
  156. {
  157. mJoystickButtons[i]->setColor(LLColor4::grey4);
  158. }
  159. }
  160. }
  161. LLFloater::draw();
  162. }
  163. //virtual
  164. void LLFloaterJoystick::refresh()
  165. {
  166. LLFloater::refresh();
  167. mJoystickEnabled = gSavedSettings.getBool("JoystickEnabled");
  168. mJoystickAxis[0] = gSavedSettings.getS32("JoystickAxis0");
  169. mJoystickAxis[1] = gSavedSettings.getS32("JoystickAxis1");
  170. mJoystickAxis[2] = gSavedSettings.getS32("JoystickAxis2");
  171. mJoystickAxis[3] = gSavedSettings.getS32("JoystickAxis3");
  172. mJoystickAxis[4] = gSavedSettings.getS32("JoystickAxis4");
  173. mJoystickAxis[5] = gSavedSettings.getS32("JoystickAxis5");
  174. mJoystickAxis[6] = gSavedSettings.getS32("JoystickAxis6");
  175. mJoystickButtonFlyCam = gSavedSettings.getS32("JoystickButtonFlyCam");
  176. mJoystickButtonJump = gSavedSettings.getS32("JoystickButtonJump");
  177. m3DCursor = gSavedSettings.getBool("Cursor3D");
  178. mAutoLeveling = gSavedSettings.getBool("AutoLeveling");
  179. mZoomDirect = gSavedSettings.getBool("ZoomDirect");
  180. mAvatarEnabled = gSavedSettings.getBool("JoystickAvatarEnabled");
  181. mBuildEnabled = gSavedSettings.getBool("JoystickBuildEnabled");
  182. mFlycamEnabled = gSavedSettings.getBool("JoystickFlycamEnabled");
  183. mAvatarAxisScale[0] = gSavedSettings.getF32("AvatarAxisScale0");
  184. mAvatarAxisScale[1] = gSavedSettings.getF32("AvatarAxisScale1");
  185. mAvatarAxisScale[2] = gSavedSettings.getF32("AvatarAxisScale2");
  186. mAvatarAxisScale[3] = gSavedSettings.getF32("AvatarAxisScale3");
  187. mAvatarAxisScale[4] = gSavedSettings.getF32("AvatarAxisScale4");
  188. mAvatarAxisScale[5] = gSavedSettings.getF32("AvatarAxisScale5");
  189. mBuildAxisScale[0] = gSavedSettings.getF32("BuildAxisScale0");
  190. mBuildAxisScale[1] = gSavedSettings.getF32("BuildAxisScale1");
  191. mBuildAxisScale[2] = gSavedSettings.getF32("BuildAxisScale2");
  192. mBuildAxisScale[3] = gSavedSettings.getF32("BuildAxisScale3");
  193. mBuildAxisScale[4] = gSavedSettings.getF32("BuildAxisScale4");
  194. mBuildAxisScale[5] = gSavedSettings.getF32("BuildAxisScale5");
  195. mFlycamAxisScale[0] = gSavedSettings.getF32("FlycamAxisScale0");
  196. mFlycamAxisScale[1] = gSavedSettings.getF32("FlycamAxisScale1");
  197. mFlycamAxisScale[2] = gSavedSettings.getF32("FlycamAxisScale2");
  198. mFlycamAxisScale[3] = gSavedSettings.getF32("FlycamAxisScale3");
  199. mFlycamAxisScale[4] = gSavedSettings.getF32("FlycamAxisScale4");
  200. mFlycamAxisScale[5] = gSavedSettings.getF32("FlycamAxisScale5");
  201. mFlycamAxisScale[6] = gSavedSettings.getF32("FlycamAxisScale6");
  202. mAvatarAxisDeadZone[0] = gSavedSettings.getF32("AvatarAxisDeadZone0");
  203. mAvatarAxisDeadZone[1] = gSavedSettings.getF32("AvatarAxisDeadZone1");
  204. mAvatarAxisDeadZone[2] = gSavedSettings.getF32("AvatarAxisDeadZone2");
  205. mAvatarAxisDeadZone[3] = gSavedSettings.getF32("AvatarAxisDeadZone3");
  206. mAvatarAxisDeadZone[4] = gSavedSettings.getF32("AvatarAxisDeadZone4");
  207. mAvatarAxisDeadZone[5] = gSavedSettings.getF32("AvatarAxisDeadZone5");
  208. mBuildAxisDeadZone[0] = gSavedSettings.getF32("BuildAxisDeadZone0");
  209. mBuildAxisDeadZone[1] = gSavedSettings.getF32("BuildAxisDeadZone1");
  210. mBuildAxisDeadZone[2] = gSavedSettings.getF32("BuildAxisDeadZone2");
  211. mBuildAxisDeadZone[3] = gSavedSettings.getF32("BuildAxisDeadZone3");
  212. mBuildAxisDeadZone[4] = gSavedSettings.getF32("BuildAxisDeadZone4");
  213. mBuildAxisDeadZone[5] = gSavedSettings.getF32("BuildAxisDeadZone5");
  214. mFlycamAxisDeadZone[0] = gSavedSettings.getF32("FlycamAxisDeadZone0");
  215. mFlycamAxisDeadZone[1] = gSavedSettings.getF32("FlycamAxisDeadZone1");
  216. mFlycamAxisDeadZone[2] = gSavedSettings.getF32("FlycamAxisDeadZone2");
  217. mFlycamAxisDeadZone[3] = gSavedSettings.getF32("FlycamAxisDeadZone3");
  218. mFlycamAxisDeadZone[4] = gSavedSettings.getF32("FlycamAxisDeadZone4");
  219. mFlycamAxisDeadZone[5] = gSavedSettings.getF32("FlycamAxisDeadZone5");
  220. mFlycamAxisDeadZone[6] = gSavedSettings.getF32("FlycamAxisDeadZone6");
  221. mAvatarFeathering = gSavedSettings.getF32("AvatarFeathering");
  222. mBuildFeathering = gSavedSettings.getF32("BuildFeathering");
  223. mFlycamFeathering = gSavedSettings.getF32("FlycamFeathering");
  224. mRunThreshold = gSavedSettings.getF32("JoystickRunThreshold");
  225. }
  226. void LLFloaterJoystick::cancel()
  227. {
  228. gSavedSettings.setBool("JoystickEnabled", mJoystickEnabled);
  229. gSavedSettings.setS32("JoystickAxis0", mJoystickAxis[0]);
  230. gSavedSettings.setS32("JoystickAxis1", mJoystickAxis[1]);
  231. gSavedSettings.setS32("JoystickAxis2", mJoystickAxis[2]);
  232. gSavedSettings.setS32("JoystickAxis3", mJoystickAxis[3]);
  233. gSavedSettings.setS32("JoystickAxis4", mJoystickAxis[4]);
  234. gSavedSettings.setS32("JoystickAxis5", mJoystickAxis[5]);
  235. gSavedSettings.setS32("JoystickAxis6", mJoystickAxis[6]);
  236. gSavedSettings.setS32("JoystickButtonFlyCam", mJoystickButtonFlyCam);
  237. gSavedSettings.setS32("JoystickButtonJump", mJoystickButtonJump);
  238. gSavedSettings.setBool("Cursor3D", m3DCursor);
  239. gSavedSettings.setBool("AutoLeveling", mAutoLeveling);
  240. gSavedSettings.setBool("ZoomDirect", mZoomDirect );
  241. gSavedSettings.setBool("JoystickAvatarEnabled", mAvatarEnabled);
  242. gSavedSettings.setBool("JoystickBuildEnabled", mBuildEnabled);
  243. gSavedSettings.setBool("JoystickFlycamEnabled", mFlycamEnabled);
  244. gSavedSettings.setF32("AvatarAxisScale0", mAvatarAxisScale[0]);
  245. gSavedSettings.setF32("AvatarAxisScale1", mAvatarAxisScale[1]);
  246. gSavedSettings.setF32("AvatarAxisScale2", mAvatarAxisScale[2]);
  247. gSavedSettings.setF32("AvatarAxisScale3", mAvatarAxisScale[3]);
  248. gSavedSettings.setF32("AvatarAxisScale4", mAvatarAxisScale[4]);
  249. gSavedSettings.setF32("AvatarAxisScale5", mAvatarAxisScale[5]);
  250. gSavedSettings.setF32("BuildAxisScale0", mBuildAxisScale[0]);
  251. gSavedSettings.setF32("BuildAxisScale1", mBuildAxisScale[1]);
  252. gSavedSettings.setF32("BuildAxisScale2", mBuildAxisScale[2]);
  253. gSavedSettings.setF32("BuildAxisScale3", mBuildAxisScale[3]);
  254. gSavedSettings.setF32("BuildAxisScale4", mBuildAxisScale[4]);
  255. gSavedSettings.setF32("BuildAxisScale5", mBuildAxisScale[5]);
  256. gSavedSettings.setF32("FlycamAxisScale0", mFlycamAxisScale[0]);
  257. gSavedSettings.setF32("FlycamAxisScale1", mFlycamAxisScale[1]);
  258. gSavedSettings.setF32("FlycamAxisScale2", mFlycamAxisScale[2]);
  259. gSavedSettings.setF32("FlycamAxisScale3", mFlycamAxisScale[3]);
  260. gSavedSettings.setF32("FlycamAxisScale4", mFlycamAxisScale[4]);
  261. gSavedSettings.setF32("FlycamAxisScale5", mFlycamAxisScale[5]);
  262. gSavedSettings.setF32("FlycamAxisScale6", mFlycamAxisScale[6]);
  263. gSavedSettings.setF32("AvatarAxisDeadZone0", mAvatarAxisDeadZone[0]);
  264. gSavedSettings.setF32("AvatarAxisDeadZone1", mAvatarAxisDeadZone[1]);
  265. gSavedSettings.setF32("AvatarAxisDeadZone2", mAvatarAxisDeadZone[2]);
  266. gSavedSettings.setF32("AvatarAxisDeadZone3", mAvatarAxisDeadZone[3]);
  267. gSavedSettings.setF32("AvatarAxisDeadZone4", mAvatarAxisDeadZone[4]);
  268. gSavedSettings.setF32("AvatarAxisDeadZone5", mAvatarAxisDeadZone[5]);
  269. gSavedSettings.setF32("BuildAxisDeadZone0", mBuildAxisDeadZone[0]);
  270. gSavedSettings.setF32("BuildAxisDeadZone1", mBuildAxisDeadZone[1]);
  271. gSavedSettings.setF32("BuildAxisDeadZone2", mBuildAxisDeadZone[2]);
  272. gSavedSettings.setF32("BuildAxisDeadZone3", mBuildAxisDeadZone[3]);
  273. gSavedSettings.setF32("BuildAxisDeadZone4", mBuildAxisDeadZone[4]);
  274. gSavedSettings.setF32("BuildAxisDeadZone5", mBuildAxisDeadZone[5]);
  275. gSavedSettings.setF32("FlycamAxisDeadZone0", mFlycamAxisDeadZone[0]);
  276. gSavedSettings.setF32("FlycamAxisDeadZone1", mFlycamAxisDeadZone[1]);
  277. gSavedSettings.setF32("FlycamAxisDeadZone2", mFlycamAxisDeadZone[2]);
  278. gSavedSettings.setF32("FlycamAxisDeadZone3", mFlycamAxisDeadZone[3]);
  279. gSavedSettings.setF32("FlycamAxisDeadZone4", mFlycamAxisDeadZone[4]);
  280. gSavedSettings.setF32("FlycamAxisDeadZone5", mFlycamAxisDeadZone[5]);
  281. gSavedSettings.setF32("FlycamAxisDeadZone6", mFlycamAxisDeadZone[6]);
  282. gSavedSettings.setF32("AvatarFeathering", mAvatarFeathering);
  283. gSavedSettings.setF32("BuildFeathering", mBuildFeathering);
  284. gSavedSettings.setF32("FlycamFeathering", mFlycamFeathering);
  285. gSavedSettings.setF32("JoystickRunThreshold", mRunThreshold);
  286. }
  287. //static
  288. void LLFloaterJoystick::onCommitJoystickEnabled(LLUICtrl*, void* userdata)
  289. {
  290. LLFloaterJoystick* self = (LLFloaterJoystick*)userdata;
  291. if (!self) return;
  292. bool joystick_enabled = self->mCheckJoystickEnabled->get();
  293. bool flycam_enabled = self->mCheckFlycamEnabled->get();
  294. if (!joystick_enabled || !flycam_enabled)
  295. {
  296. // Turn off flycam
  297. LLViewerJoystick* joystick(LLViewerJoystick::getInstance());
  298. if (joystick->getOverrideCamera())
  299. {
  300. joystick->toggleFlycam();
  301. }
  302. }
  303. }
  304. //static
  305. void LLFloaterJoystick::onClickInit(void*)
  306. {
  307. if (!LLViewerJoystick::getInstance()->isJoystickInitialized())
  308. {
  309. LLViewerJoystick::getInstance()->init(true);
  310. }
  311. }
  312. //static
  313. void LLFloaterJoystick::onClickCancel(void* userdata)
  314. {
  315. LLFloaterJoystick* self = (LLFloaterJoystick*)userdata;
  316. if (self)
  317. {
  318. self->cancel();
  319. self->close();
  320. }
  321. }
  322. //static
  323. void LLFloaterJoystick::onClickOK(void* userdata)
  324. {
  325. LLFloaterJoystick* self = (LLFloaterJoystick*)userdata;
  326. if (self)
  327. {
  328. self->close();
  329. }
  330. }
  331. //static
  332. void LLFloaterJoystick::onClickRestoreDefaults(LLUICtrl* ctrl, void* userdata)
  333. {
  334. LLFloaterJoystick* self = (LLFloaterJoystick*)userdata;
  335. if (!self || !ctrl)
  336. {
  337. return;
  338. }
  339. std::string setting = ctrl->getValue().asString();
  340. if (setting == "previous")
  341. {
  342. self->cancel();
  343. }
  344. else if (setting == "spacenavigator")
  345. {
  346. LLViewerJoystick::getInstance()->setSNDefaults();
  347. }
  348. else
  349. {
  350. LLViewerJoystick::getInstance()->setToDefaults();
  351. }
  352. }