llxyvector.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /**
  2. * @file llxyvector.cpp
  3. * @brief LLXYVector class definition
  4. *
  5. * $LicenseInfo:firstyear=2018&license=viewergpl$
  6. *
  7. * Copyright (c) 2018, 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 "linden_common.h"
  33. #include <math.h> // For fabs()
  34. #include "llxyvector.h"
  35. #include "lllineeditor.h"
  36. #include "llpanel.h"
  37. #include "llrender.h"
  38. #include "lltextbox.h"
  39. static const std::string LL_XY_VECTOR_TAG = "xy_vector";
  40. static LLRegisterWidget<LLXYVector> r34(LL_XY_VECTOR_TAG);
  41. // UI elements constants
  42. constexpr S32 EDIT_BAR_HEIGHT = 20;
  43. constexpr S32 XY_VECTOR_PADDING = 4;
  44. constexpr S32 XY_VECTOR_LABEL_WIDTH = 16;
  45. constexpr S32 XY_VECTOR_WIDTH = 120;
  46. constexpr S32 XY_VECTOR_HEIGHT = 140;
  47. // Drawing constants
  48. constexpr F32 CENTER_CIRCLE_RADIUS = 2.f;
  49. constexpr F32 ARROW_ANGLE = 30.f * DEG_TO_RAD;
  50. constexpr S32 ARROW_LENGTH_LONG = 10;
  51. constexpr S32 ARROW_LENGTH_SHORT = 6;
  52. // Helper function
  53. void draw_arrow(S32 tail_x, S32 tail_y, S32 tip_x, S32 tip_y,
  54. const LLColor4& color)
  55. {
  56. gl_line_2d(tail_x, tail_y, tip_x, tip_y, color);
  57. S32 dx = tip_x - tail_x;
  58. S32 dy = tip_y - tail_y;
  59. S32 length;
  60. if (fabs(dx) < ARROW_LENGTH_LONG && fabs(dy) < ARROW_LENGTH_LONG)
  61. {
  62. length = ARROW_LENGTH_SHORT;
  63. }
  64. else
  65. {
  66. length = ARROW_LENGTH_LONG;
  67. }
  68. F32 theta = atan2f(dy, dx);
  69. F32 x = tip_x - length * cosf(theta + ARROW_ANGLE);
  70. F32 y = tip_y - length * sinf(theta + ARROW_ANGLE);
  71. F32 x2 = tip_x - length * cosf(theta - ARROW_ANGLE);
  72. F32 y2 = tip_y - length * sinf(theta - ARROW_ANGLE);
  73. gl_triangle_2d(tip_x, tip_y, x, y, x2, y2, color, true);
  74. }
  75. LLXYVector::LLXYVector(const std::string& name, const LLRect& ui_rect,
  76. void (*commit_cb)(LLUICtrl*, void*), void* userdata)
  77. : LLUICtrl(name, ui_rect, true, commit_cb, userdata,
  78. FOLLOWS_TOP | FOLLOWS_LEFT),
  79. mGhostX(0),
  80. mGhostY(0),
  81. mValueX(0.f),
  82. mValueY(0.f),
  83. mMinValueX(-1.f),
  84. mMinValueY(-1.f),
  85. mMaxValueX(1.f),
  86. mMaxValueY(1.f),
  87. mLogScaleX(1.f),
  88. mLogScaleY(1.f),
  89. mIncrementX(0.05f),
  90. mIncrementY(0.05f),
  91. mArrowColor(LLColor4::white),
  92. mAreaColor(LLColor4::grey4),
  93. mGridColor(LLColor4::grey % 0.25f),
  94. mGhostColor(LLColor4::white % 0.3f),
  95. mLogarithmic(false)
  96. {
  97. LLRect border_rect = getLocalRect();
  98. mBorder = new LLViewBorder("border", border_rect);
  99. addChild(mBorder);
  100. LLRect rect = LLRect(XY_VECTOR_PADDING,
  101. border_rect.mTop - XY_VECTOR_PADDING,
  102. XY_VECTOR_LABEL_WIDTH,
  103. border_rect.getHeight() - EDIT_BAR_HEIGHT);
  104. mXLabel = new LLTextBox("x_label", rect, "X:");
  105. addChild(mXLabel);
  106. rect = LLRect(XY_VECTOR_PADDING + XY_VECTOR_LABEL_WIDTH,
  107. border_rect.mTop - XY_VECTOR_PADDING,
  108. border_rect.getCenterX(),
  109. border_rect.getHeight() - EDIT_BAR_HEIGHT);
  110. mXEntry = new LLLineEditor("x_entry", rect);
  111. mXEntry->setPrevalidate(LLLineEditor::prevalidateFloat);
  112. mXEntry->setCommitCallback(onEditChange);
  113. mXEntry->setCallbackUserData(this);
  114. addChild(mXEntry);
  115. rect = LLRect(border_rect.getCenterX() + XY_VECTOR_PADDING,
  116. border_rect.mTop - XY_VECTOR_PADDING,
  117. border_rect.getCenterX() + XY_VECTOR_LABEL_WIDTH,
  118. border_rect.getHeight() - EDIT_BAR_HEIGHT);
  119. mYLabel = new LLTextBox("y_label", rect, "Y:");
  120. addChild(mYLabel);
  121. rect = LLRect(border_rect.getCenterX() + XY_VECTOR_PADDING +
  122. XY_VECTOR_LABEL_WIDTH,
  123. border_rect.getHeight() - XY_VECTOR_PADDING,
  124. border_rect.getWidth() - XY_VECTOR_PADDING,
  125. border_rect.getHeight() - EDIT_BAR_HEIGHT);
  126. mYEntry = new LLLineEditor("y_entry", rect);
  127. mYEntry->setPrevalidate(LLLineEditor::prevalidateFloat);
  128. mYEntry->setCommitCallback(onEditChange);
  129. mYEntry->setCallbackUserData(this);
  130. addChild(mYEntry);
  131. rect = LLRect(XY_VECTOR_PADDING,
  132. border_rect.mTop - EDIT_BAR_HEIGHT - XY_VECTOR_PADDING,
  133. border_rect.getWidth() - XY_VECTOR_PADDING,
  134. XY_VECTOR_PADDING);
  135. mTouchArea = new LLPanel("touch area", rect);
  136. addChild(mTouchArea);
  137. }
  138. //virtual
  139. bool LLXYVector::postBuild()
  140. {
  141. if (mMaxValueX != 0.f && mMaxValueY != 0.f)
  142. {
  143. mLogScaleX = 2.f * logf(mMaxValueX) /
  144. (F32)mTouchArea->getRect().getWidth();
  145. mLogScaleY = 2.f * logf(mMaxValueY) /
  146. (F32)mTouchArea->getRect().getHeight();
  147. }
  148. return true;
  149. }
  150. //virtual
  151. void LLXYVector::draw()
  152. {
  153. const LLRect& rect = mTouchArea->getRect();
  154. S32 center_x = rect.getCenterX();
  155. S32 center_y = rect.getCenterY();
  156. S32 point_x = center_x;
  157. S32 point_y = center_y;
  158. if (mMaxValueX != 0.f && mMaxValueY != 0.f)
  159. {
  160. if (mLogarithmic)
  161. {
  162. if (mValueX >= 0.f)
  163. {
  164. point_x += logf(1.f + mValueX) / mLogScaleX;
  165. }
  166. else
  167. {
  168. point_x += -logf(1.f - mValueX) / mLogScaleX;
  169. }
  170. if (mValueY >= 0.f)
  171. {
  172. point_y += logf(1.f + mValueY) / mLogScaleY;
  173. }
  174. else
  175. {
  176. point_y += -logf(1.f - mValueY) / mLogScaleY;
  177. }
  178. }
  179. else
  180. {
  181. point_x += mValueX * rect.getWidth() / (2.f * mMaxValueX);
  182. point_y += mValueY * rect.getHeight() / (2.f * mMaxValueY);
  183. }
  184. }
  185. // Fill up touch area
  186. gl_rect_2d(rect, mAreaColor, true);
  187. // Draw the grid
  188. gl_line_2d(center_x, rect.mTop, center_x, rect.mBottom, mGridColor);
  189. gl_line_2d(rect.mLeft, center_y, rect.mRight, center_y, mGridColor);
  190. // Draw the ghost
  191. if (hasMouseCapture())
  192. {
  193. draw_arrow(center_x, center_y, mGhostX, mGhostY, mGhostColor);
  194. }
  195. else
  196. {
  197. mGhostX = point_x;
  198. mGhostY = point_y;
  199. }
  200. if (fabs(mValueX) >= mIncrementX || fabs(mValueY) >= mIncrementY)
  201. {
  202. // Draw the vector arrow
  203. draw_arrow(center_x, center_y, point_x, point_y, mArrowColor);
  204. }
  205. else
  206. {
  207. // Skip the arrow and set the color for the center circle
  208. gGL.color4fv(mArrowColor.mV);
  209. }
  210. // Draw the center circle
  211. gl_circle_2d(center_x, center_y, CENTER_CIRCLE_RADIUS, 12, true);
  212. bool enabled = isInEnabledChain();
  213. mXEntry->setEnabled(enabled);
  214. mYEntry->setEnabled(enabled);
  215. LLView::draw();
  216. }
  217. //virtual
  218. bool LLXYVector::handleHover(S32 x, S32 y, MASK mask)
  219. {
  220. if (hasMouseCapture())
  221. {
  222. const LLRect& rect = mTouchArea->getRect();
  223. F32 value_x, value_y;
  224. if (mLogarithmic)
  225. {
  226. value_x = llfastpow(F_E,
  227. mLogScaleX * abs(x - rect.getCenterX())) - 1.f;
  228. if (x < mTouchArea->getRect().getCenterX())
  229. {
  230. value_x = -value_x;
  231. }
  232. value_y = llfastpow(F_E,
  233. mLogScaleY * abs(y - rect.getCenterY())) - 1.f;
  234. if (y < mTouchArea->getRect().getCenterY())
  235. {
  236. value_y = -value_y;
  237. }
  238. }
  239. else
  240. {
  241. value_x = 2.f * mMaxValueX * F32(x - rect.getCenterX()) /
  242. F32(rect.getWidth());
  243. value_y = 2.f * mMaxValueY * F32(y - rect.getCenterY()) /
  244. F32(rect.getHeight());
  245. }
  246. setValueAndCommit(value_x, value_y);
  247. }
  248. return true;
  249. }
  250. //virtual
  251. bool LLXYVector::handleMouseUp(S32 x, S32 y, MASK mask)
  252. {
  253. if (hasMouseCapture())
  254. {
  255. gFocusMgr.setMouseCapture(NULL);
  256. make_ui_sound("UISndClickRelease");
  257. }
  258. if (mTouchArea->getRect().pointInRect(x, y))
  259. {
  260. return true;
  261. }
  262. return LLUICtrl::handleMouseUp(x, y, mask);
  263. }
  264. //virtual
  265. bool LLXYVector::handleMouseDown(S32 x, S32 y, MASK mask)
  266. {
  267. if (mTouchArea->getRect().pointInRect(x, y))
  268. {
  269. gFocusMgr.setMouseCapture(this);
  270. make_ui_sound("UISndClick");
  271. return true;
  272. }
  273. return LLUICtrl::handleMouseDown(x, y, mask);
  274. }
  275. //virtual
  276. LLSD LLXYVector::getValue() const
  277. {
  278. LLSD value;
  279. value.append(mValueX);
  280. value.append(mValueY);
  281. return value;
  282. }
  283. void LLXYVector::update()
  284. {
  285. mXEntry->setValue(mValueX);
  286. mYEntry->setValue(mValueY);
  287. }
  288. void LLXYVector::setValue(F32 x, F32 y)
  289. {
  290. mValueX = ll_round(llclamp(x, mMinValueX, mMaxValueX), mIncrementX);
  291. mValueY = ll_round(llclamp(y, mMinValueY, mMaxValueY), mIncrementY);
  292. update();
  293. }
  294. //virtual
  295. void LLXYVector::setValue(const LLSD& value)
  296. {
  297. if (value.isArray())
  298. {
  299. setValue(value[0].asReal(), value[1].asReal());
  300. }
  301. }
  302. void LLXYVector::setValueAndCommit(F32 x, F32 y)
  303. {
  304. if (mValueX != x || mValueY != y)
  305. {
  306. setValue(x, y);
  307. onCommit();
  308. }
  309. }
  310. //static
  311. void LLXYVector::onEditChange(LLUICtrl*, void* userdata)
  312. {
  313. LLXYVector* self = (LLXYVector*)userdata;
  314. if (self && self->getEnabled())
  315. {
  316. self->setValueAndCommit(self->mXEntry->getValue().asReal(),
  317. self->mYEntry->getValue().asReal());
  318. }
  319. }
  320. //virtual
  321. const std::string& LLXYVector::getTag() const
  322. {
  323. return LL_XY_VECTOR_TAG;
  324. }
  325. //virtual
  326. LLXMLNodePtr LLXYVector::getXML(bool save_children) const
  327. {
  328. LLXMLNodePtr node = LLUICtrl::getXML();
  329. node->setName(LL_XY_VECTOR_TAG);
  330. node->createChild("min_val_x", true)->setFloatValue(mMinValueX);
  331. node->createChild("max_val_x", true)->setFloatValue(mMaxValueX);
  332. node->createChild("increment_x", true)->setFloatValue(mIncrementX);
  333. node->createChild("min_val_y", true)->setFloatValue(mMinValueY);
  334. node->createChild("max_val_y", true)->setFloatValue(mMaxValueY);
  335. node->createChild("increment_y", true)->setFloatValue(mIncrementY);
  336. node->createChild("logarithmic", true)->setBoolValue(mLogarithmic);
  337. return node;
  338. }
  339. //static
  340. LLView* LLXYVector::fromXML(LLXMLNodePtr node, LLView* parent,
  341. LLUICtrlFactory* factory)
  342. {
  343. std::string name = LL_XY_VECTOR_TAG;
  344. node->getAttributeString("name", name);
  345. LLRect rect;
  346. createRect(node, rect, parent,
  347. LLRect(0, XY_VECTOR_HEIGHT, XY_VECTOR_WIDTH, 0));
  348. LLUICtrlCallback callback = NULL;
  349. LLXYVector* xy_vector = new LLXYVector(name, rect, callback, NULL);
  350. F32 min_val_x = -1.f;
  351. node->getAttributeF32("min_val_x", min_val_x);
  352. xy_vector->mMinValueX = min_val_x;
  353. F32 max_val_x = 1.f;
  354. node->getAttributeF32("max_val_x", max_val_x);
  355. xy_vector->mMaxValueX = max_val_x;
  356. if (max_val_x == 0.f)
  357. {
  358. llerrs << "Zero max X value for: " << name << llendl;
  359. }
  360. F32 min_val_y = -1.f;
  361. node->getAttributeF32("min_val_y", min_val_y);
  362. xy_vector->mMinValueY = min_val_y;
  363. F32 max_val_y = 1.f;
  364. node->getAttributeF32("max_val_y", max_val_y);
  365. xy_vector->mMaxValueY = max_val_y;
  366. if (max_val_y == 0.f)
  367. {
  368. llerrs << "Zero max Y value for: " << name << llendl;
  369. }
  370. F32 increment_x = 0.05f;
  371. node->getAttributeF32("increment_x", increment_x);
  372. xy_vector->mIncrementX = increment_x;
  373. F32 increment_y = 0.05f;
  374. node->getAttributeF32("increment_y", increment_y);
  375. xy_vector->mIncrementY = increment_y;
  376. bool logarithmic = false;
  377. node->getAttributeBool("logarithmic", logarithmic);
  378. xy_vector->mLogarithmic = logarithmic;
  379. xy_vector->initFromXML(node, parent);
  380. xy_vector->postBuild();
  381. return xy_vector;
  382. }