/indra/newview/llcolorswatch.cpp

https://bitbucket.org/lindenlab/viewer-beta/ · C++ · 373 lines · 269 code · 48 blank · 56 comment · 35 complexity · 4a21a84c3d3378a4be42ca2c8293559a MD5 · raw file

  1. /**
  2. * @file llcolorswatch.cpp
  3. * @brief LLColorSwatch 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. // File include
  28. #include "llcolorswatch.h"
  29. // Linden library includes
  30. #include "v4color.h"
  31. #include "llwindow.h" // setCursor()
  32. // Project includes
  33. #include "llui.h"
  34. #include "llrender.h"
  35. #include "lluiconstants.h"
  36. #include "llviewercontrol.h"
  37. #include "llbutton.h"
  38. #include "lltextbox.h"
  39. #include "llfloatercolorpicker.h"
  40. #include "llviewborder.h"
  41. #include "llviewertexturelist.h"
  42. #include "llfocusmgr.h"
  43. static LLDefaultChildRegistry::Register<LLColorSwatchCtrl> r("color_swatch");
  44. LLColorSwatchCtrl::Params::Params()
  45. : color("color", LLColor4::white),
  46. can_apply_immediately("can_apply_immediately", false),
  47. alpha_background_image("alpha_background_image"),
  48. border_color("border_color"),
  49. label_width("label_width", -1),
  50. label_height("label_height", -1),
  51. caption_text("caption_text"),
  52. border("border")
  53. {
  54. }
  55. LLColorSwatchCtrl::LLColorSwatchCtrl(const Params& p)
  56. : LLUICtrl(p),
  57. mValid( TRUE ),
  58. mColor(p.color()),
  59. mCanApplyImmediately(p.can_apply_immediately),
  60. mAlphaGradientImage(p.alpha_background_image),
  61. mOnCancelCallback(p.cancel_callback()),
  62. mOnSelectCallback(p.select_callback()),
  63. mBorderColor(p.border_color()),
  64. mLabelWidth(p.label_width),
  65. mLabelHeight(p.label_height)
  66. {
  67. LLTextBox::Params tp = p.caption_text;
  68. // use custom label height if it is provided
  69. mLabelHeight = mLabelHeight != -1 ? mLabelHeight : BTN_HEIGHT_SMALL;
  70. // label_width is specified, not -1
  71. if(mLabelWidth!= -1)
  72. {
  73. tp.rect(LLRect( 0, mLabelHeight, mLabelWidth, 0 ));
  74. }
  75. else
  76. {
  77. tp.rect(LLRect( 0, mLabelHeight, getRect().getWidth(), 0 ));
  78. }
  79. tp.initial_value(p.label());
  80. mCaption = LLUICtrlFactory::create<LLTextBox>(tp);
  81. addChild( mCaption );
  82. LLRect border_rect = getLocalRect();
  83. border_rect.mTop -= 1;
  84. border_rect.mRight -=1;
  85. border_rect.mBottom += mLabelHeight;
  86. LLViewBorder::Params params = p.border;
  87. params.rect(border_rect);
  88. mBorder = LLUICtrlFactory::create<LLViewBorder> (params);
  89. addChild(mBorder);
  90. }
  91. LLColorSwatchCtrl::~LLColorSwatchCtrl ()
  92. {
  93. // parent dialog is destroyed so we are too and we need to cancel selection
  94. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  95. if (pickerp)
  96. {
  97. pickerp->cancelSelection();
  98. pickerp->closeFloater();
  99. }
  100. }
  101. BOOL LLColorSwatchCtrl::handleDoubleClick(S32 x, S32 y, MASK mask)
  102. {
  103. return handleMouseDown(x, y, mask);
  104. }
  105. BOOL LLColorSwatchCtrl::handleHover(S32 x, S32 y, MASK mask)
  106. {
  107. getWindow()->setCursor(UI_CURSOR_HAND);
  108. return TRUE;
  109. }
  110. BOOL LLColorSwatchCtrl::handleUnicodeCharHere(llwchar uni_char)
  111. {
  112. if( ' ' == uni_char )
  113. {
  114. showPicker(TRUE);
  115. }
  116. return LLUICtrl::handleUnicodeCharHere(uni_char);
  117. }
  118. // forces color of this swatch and any associated floater to the input value, if currently invalid
  119. void LLColorSwatchCtrl::setOriginal(const LLColor4& color)
  120. {
  121. mColor = color;
  122. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  123. if (pickerp)
  124. {
  125. pickerp->setOrigRgb(mColor.mV[VRED], mColor.mV[VGREEN], mColor.mV[VBLUE]);
  126. }
  127. }
  128. void LLColorSwatchCtrl::set(const LLColor4& color, BOOL update_picker, BOOL from_event)
  129. {
  130. mColor = color;
  131. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  132. if (pickerp && update_picker)
  133. {
  134. pickerp->setCurRgb(mColor.mV[VRED], mColor.mV[VGREEN], mColor.mV[VBLUE]);
  135. }
  136. if (!from_event)
  137. {
  138. setControlValue(mColor.getValue());
  139. }
  140. }
  141. void LLColorSwatchCtrl::setLabel(const std::string& label)
  142. {
  143. mCaption->setText(label);
  144. }
  145. BOOL LLColorSwatchCtrl::handleMouseDown(S32 x, S32 y, MASK mask)
  146. {
  147. // Route future Mouse messages here preemptively. (Release on mouse up.)
  148. // No handler is needed for capture lost since this object has no state that depends on it.
  149. gFocusMgr.setMouseCapture( this );
  150. return TRUE;
  151. }
  152. BOOL LLColorSwatchCtrl::handleMouseUp(S32 x, S32 y, MASK mask)
  153. {
  154. // We only handle the click if the click both started and ended within us
  155. if( hasMouseCapture() )
  156. {
  157. // Release the mouse
  158. gFocusMgr.setMouseCapture( NULL );
  159. // If mouseup in the widget, it's been clicked
  160. if ( pointInView(x, y) )
  161. {
  162. llassert(getEnabled());
  163. llassert(getVisible());
  164. // Focus the widget now in order to return the focus
  165. // after the color picker is closed.
  166. setFocus(TRUE);
  167. showPicker(FALSE);
  168. }
  169. }
  170. return TRUE;
  171. }
  172. // assumes GL state is set for 2D
  173. void LLColorSwatchCtrl::draw()
  174. {
  175. // If we're in a focused floater, don't apply the floater's alpha to the color swatch (STORM-676).
  176. F32 alpha = getTransparencyType() == TT_ACTIVE ? 1.0f : getCurrentTransparency();
  177. mBorder->setKeyboardFocusHighlight(hasFocus());
  178. // Draw border
  179. LLRect border( 0, getRect().getHeight(), getRect().getWidth(), mLabelHeight );
  180. gl_rect_2d( border, mBorderColor.get(), FALSE );
  181. LLRect interior = border;
  182. interior.stretch( -1 );
  183. // Check state
  184. if ( mValid )
  185. {
  186. if (!mColor.isOpaque())
  187. {
  188. // Draw checker board.
  189. gl_rect_2d_checkerboard(interior, alpha);
  190. }
  191. // Draw the color swatch
  192. gl_rect_2d(interior, mColor % alpha, TRUE);
  193. if (!mColor.isOpaque())
  194. {
  195. // Draw semi-transparent center area in filled with mColor.
  196. LLColor4 opaque_color = mColor;
  197. opaque_color.mV[VALPHA] = alpha;
  198. gGL.color4fv(opaque_color.mV);
  199. if (mAlphaGradientImage.notNull())
  200. {
  201. gGL.pushMatrix();
  202. {
  203. mAlphaGradientImage->draw(interior, mColor % alpha);
  204. }
  205. gGL.popMatrix();
  206. }
  207. }
  208. }
  209. else
  210. {
  211. if (!mFallbackImageName.empty())
  212. {
  213. LLPointer<LLViewerFetchedTexture> fallback_image = LLViewerTextureManager::getFetchedTextureFromFile(mFallbackImageName, TRUE,
  214. LLViewerTexture::BOOST_NONE, LLViewerTexture::LOD_TEXTURE);
  215. if( fallback_image->getComponents() == 4 )
  216. {
  217. gl_rect_2d_checkerboard( interior );
  218. }
  219. gl_draw_scaled_image( interior.mLeft, interior.mBottom, interior.getWidth(), interior.getHeight(), fallback_image, LLColor4::white % alpha);
  220. fallback_image->addTextureStats( (F32)(interior.getWidth() * interior.getHeight()) );
  221. }
  222. else
  223. {
  224. // Draw grey and an X
  225. gl_rect_2d(interior, LLColor4::grey % alpha, TRUE);
  226. gl_draw_x(interior, LLColor4::black % alpha);
  227. }
  228. }
  229. LLUICtrl::draw();
  230. }
  231. void LLColorSwatchCtrl::setEnabled( BOOL enabled )
  232. {
  233. mCaption->setEnabled( enabled );
  234. LLView::setEnabled( enabled );
  235. if (!enabled)
  236. {
  237. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  238. if (pickerp)
  239. {
  240. pickerp->cancelSelection();
  241. pickerp->closeFloater();
  242. }
  243. }
  244. }
  245. void LLColorSwatchCtrl::setValue(const LLSD& value)
  246. {
  247. set(LLColor4(value), TRUE, TRUE);
  248. }
  249. //////////////////////////////////////////////////////////////////////////////
  250. // called (infrequently) when the color changes so the subject of the swatch can be updated.
  251. void LLColorSwatchCtrl::onColorChanged ( void* data, EColorPickOp pick_op )
  252. {
  253. LLColorSwatchCtrl* subject = ( LLColorSwatchCtrl* )data;
  254. if ( subject )
  255. {
  256. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)subject->mPickerHandle.get();
  257. if (pickerp)
  258. {
  259. // move color across from selector to internal widget storage
  260. LLColor4 updatedColor ( pickerp->getCurR (),
  261. pickerp->getCurG (),
  262. pickerp->getCurB (),
  263. subject->mColor.mV[VALPHA] ); // keep current alpha
  264. subject->mColor = updatedColor;
  265. subject->setControlValue(updatedColor.getValue());
  266. if (pick_op == COLOR_CANCEL && subject->mOnCancelCallback)
  267. {
  268. subject->mOnCancelCallback( subject, LLSD());
  269. }
  270. else if (pick_op == COLOR_SELECT && subject->mOnSelectCallback)
  271. {
  272. subject->mOnSelectCallback( subject, LLSD() );
  273. }
  274. else
  275. {
  276. // just commit change
  277. subject->onCommit ();
  278. }
  279. }
  280. }
  281. }
  282. // This is called when the main floatercustomize panel is closed.
  283. // Since this class has pointers up to its parents, we need to cleanup
  284. // this class first in order to avoid a crash.
  285. void LLColorSwatchCtrl::closeFloaterColorPicker()
  286. {
  287. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  288. if (pickerp)
  289. {
  290. pickerp->setSwatch(NULL);
  291. pickerp->closeFloater();
  292. }
  293. mPickerHandle.markDead();
  294. }
  295. void LLColorSwatchCtrl::setValid(BOOL valid )
  296. {
  297. mValid = valid;
  298. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  299. if (pickerp)
  300. {
  301. pickerp->setActive(valid);
  302. }
  303. }
  304. void LLColorSwatchCtrl::showPicker(BOOL take_focus)
  305. {
  306. LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)mPickerHandle.get();
  307. if (!pickerp)
  308. {
  309. pickerp = new LLFloaterColorPicker(this, mCanApplyImmediately);
  310. LLFloater* parent = gFloaterView->getParentFloater(this);
  311. if (parent)
  312. {
  313. parent->addDependentFloater(pickerp);
  314. }
  315. mPickerHandle = pickerp->getHandle();
  316. }
  317. // initialize picker with current color
  318. pickerp->initUI ( mColor.mV [ VRED ], mColor.mV [ VGREEN ], mColor.mV [ VBLUE ] );
  319. // display it
  320. pickerp->showUI ();
  321. if (take_focus)
  322. {
  323. pickerp->setFocus(TRUE);
  324. }
  325. }