llcolorswatch.cpp

Go to the documentation of this file.
00001 
00032 #include "llviewerprecompiledheaders.h"
00033 
00034 // File include
00035 #include "llcolorswatch.h"
00036 
00037 // Linden library includes
00038 #include "v4color.h"
00039 
00040 // Project includes
00041 #include "llui.h"
00042 #include "lluiconstants.h"
00043 #include "llviewerwindow.h"
00044 #include "llviewercontrol.h"
00045 #include "llbutton.h"
00046 #include "lltextbox.h"
00047 #include "llfloatercolorpicker.h"
00048 #include "llviewborder.h"
00049 #include "llviewerimagelist.h"
00050 #include "llfocusmgr.h"
00051 
00052 LLColorSwatchCtrl::LLColorSwatchCtrl(const std::string& name, const LLRect& rect, const LLColor4& color,
00053                 void (*commit_callback)(LLUICtrl* ctrl, void* userdata),
00054                 void* userdata )
00055 :       LLUICtrl(name, rect, TRUE, commit_callback, userdata, FOLLOWS_LEFT | FOLLOWS_TOP),
00056         mValid( TRUE ),
00057         mColor( color ),
00058         mBorderColor( gColors.getColor("DefaultHighlightLight") ),
00059         mCanApplyImmediately(FALSE),
00060         mOnCancelCallback(NULL),
00061         mOnSelectCallback(NULL)
00062 {
00063         mCaption = new LLTextBox( name,
00064                 LLRect( 0, BTN_HEIGHT_SMALL, mRect.getWidth(), 0 ),
00065                 LLString::null,
00066                 LLFontGL::sSansSerifSmall );
00067         mCaption->setFollows( FOLLOWS_LEFT | FOLLOWS_RIGHT | FOLLOWS_BOTTOM );
00068         addChild( mCaption );
00069 
00070         // Scalable UI made this off-by-one, I don't know why. JC
00071         LLRect border_rect(0, mRect.getHeight()-1, mRect.getWidth()-1, 0);
00072         border_rect.mBottom += BTN_HEIGHT_SMALL;
00073         mBorder = new LLViewBorder("border", border_rect, LLViewBorder::BEVEL_IN);
00074         addChild(mBorder);
00075 
00076         mAlphaGradientImage = gImageList.getImageFromUUID(LLUUID(gViewerArt.getString("color_swatch_alpha.tga")),
00077                                                                                                           MIPMAP_FALSE, TRUE, GL_ALPHA8, GL_ALPHA);
00078 }
00079 
00080 LLColorSwatchCtrl::LLColorSwatchCtrl(const std::string& name, const LLRect& rect, const std::string& label, const LLColor4& color,
00081                 void (*commit_callback)(LLUICtrl* ctrl, void* userdata),
00082                 void* userdata )
00083 :       LLUICtrl(name, rect, TRUE, commit_callback, userdata, FOLLOWS_LEFT | FOLLOWS_TOP),
00084         mValid( TRUE ),
00085         mColor( color ),
00086         mBorderColor( gColors.getColor("DefaultHighlightLight") ),
00087         mCanApplyImmediately(FALSE),
00088         mOnCancelCallback(NULL),
00089         mOnSelectCallback(NULL)
00090 {
00091         mCaption = new LLTextBox( label,
00092                 LLRect( 0, BTN_HEIGHT_SMALL, mRect.getWidth(), 0 ),
00093                 LLString::null,
00094                 LLFontGL::sSansSerifSmall );
00095         mCaption->setFollows( FOLLOWS_LEFT | FOLLOWS_RIGHT | FOLLOWS_BOTTOM );
00096         addChild( mCaption );
00097 
00098         // Scalable UI made this off-by-one, I don't know why. JC
00099         LLRect border_rect(0, mRect.getHeight()-1, mRect.getWidth()-1, 0);
00100         border_rect.mBottom += BTN_HEIGHT_SMALL;
00101         mBorder = new LLViewBorder("border", border_rect, LLViewBorder::BEVEL_IN);
00102         addChild(mBorder);
00103 
00104         mAlphaGradientImage = gImageList.getImageFromUUID(LLUUID(gViewerArt.getString("color_swatch_alpha.tga")),
00105                                                                                                           MIPMAP_FALSE, TRUE, GL_ALPHA8, GL_ALPHA);
00106 }
00107 
00108 LLColorSwatchCtrl::~LLColorSwatchCtrl ()
00109 {
00110         // parent dialog is destroyed so we are too and we need to cancel selection
00111         LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(mPickerHandle);
00112         if (pickerp)
00113         {
00114                 pickerp->cancelSelection();
00115                 pickerp->close();
00116         }
00117         mAlphaGradientImage = NULL;
00118 }
00119 
00120 BOOL LLColorSwatchCtrl::handleDoubleClick(S32 x, S32 y, MASK mask)
00121 {
00122         return handleMouseDown(x, y, mask);
00123 }
00124 
00125 BOOL LLColorSwatchCtrl::handleHover(S32 x, S32 y, MASK mask)
00126 {
00127         getWindow()->setCursor(UI_CURSOR_HAND);
00128         return TRUE;
00129 }
00130 
00131 BOOL LLColorSwatchCtrl::handleUnicodeCharHere(llwchar uni_char, BOOL called_from_parent)
00132 {
00133         if( getVisible() && mEnabled && !called_from_parent && ' ' == uni_char )
00134         {
00135                 showPicker(TRUE);
00136         }
00137         return LLUICtrl::handleUnicodeCharHere(uni_char, called_from_parent);
00138 }
00139 
00140 // forces color of this swatch and any associated floater to the input value, if currently invalid
00141 void LLColorSwatchCtrl::setOriginal(const LLColor4& color)
00142 {
00143         mColor = color;
00144         LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(mPickerHandle);
00145         if (pickerp)
00146         {
00147                 pickerp->setOrigRgb(mColor.mV[VRED], mColor.mV[VGREEN], mColor.mV[VBLUE]);
00148         }
00149 }
00150 
00151 void LLColorSwatchCtrl::set(const LLColor4& color, BOOL update_picker, BOOL from_event)
00152 {
00153         mColor = color; 
00154         LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(mPickerHandle);
00155         if (pickerp && update_picker)
00156         {
00157                 pickerp->setCurRgb(mColor.mV[VRED], mColor.mV[VGREEN], mColor.mV[VBLUE]);
00158         }
00159         if (!from_event)
00160         {
00161                 setControlValue(mColor.getValue());
00162         }
00163 }
00164 
00165 void LLColorSwatchCtrl::setLabel(const std::string& label)
00166 {
00167         mCaption->setText(label);
00168 }
00169 
00170 BOOL LLColorSwatchCtrl::handleMouseDown(S32 x, S32 y, MASK mask)
00171 {
00172         // Route future Mouse messages here preemptively.  (Release on mouse up.)
00173         // No handler is needed for capture lost since this object has no state that depends on it.
00174         gViewerWindow->setMouseCapture( this );
00175 
00176         return TRUE;
00177 }
00178 
00179 
00180 BOOL LLColorSwatchCtrl::handleMouseUp(S32 x, S32 y, MASK mask)
00181 {
00182         // We only handle the click if the click both started and ended within us
00183         if( hasMouseCapture() )
00184         {
00185                 // Release the mouse
00186                 gViewerWindow->setMouseCapture( NULL );
00187 
00188                 // If mouseup in the widget, it's been clicked
00189                 if ( pointInView(x, y) )
00190                 {
00191                         llassert(mEnabled);
00192                         llassert(getVisible());
00193 
00194                         showPicker(FALSE);
00195                 }
00196         }
00197 
00198         return TRUE;
00199 }
00200 
00201 
00202 // assumes GL state is set for 2D
00203 void LLColorSwatchCtrl::draw()
00204 {
00205         if( getVisible() )
00206         {
00207                 mBorder->setKeyboardFocusHighlight(hasFocus());
00208                 // Draw border
00209                 LLRect border( 0, mRect.getHeight(), mRect.getWidth(), BTN_HEIGHT_SMALL );
00210                 gl_rect_2d( border, mBorderColor, FALSE );
00211 
00212                 LLRect interior = border;
00213                 interior.stretch( -1 );
00214 
00215                 // Check state
00216                 if ( mValid )
00217                 {
00218                         LLGLSTexture gls_texture;
00219 
00220                         // Draw the color swatch
00221                         gl_rect_2d_checkerboard( interior );
00222                         gl_rect_2d(interior, mColor, TRUE);
00223                         LLColor4 opaque_color = mColor;
00224                         opaque_color.mV[VALPHA] = 1.f;
00225                         glColor4fv(opaque_color.mV);
00226                         if (mAlphaGradientImage.notNull())
00227                         {
00228                                 glPushMatrix();
00229                                 {
00230                                         glTranslatef((F32)interior.mLeft, (F32)interior.mBottom, 0.f);
00231                                         LLViewerImage::bindTexture(mAlphaGradientImage);
00232                                         gl_rect_2d_simple_tex(interior.getWidth(), interior.getHeight());
00233                                 }
00234                                 glPopMatrix();
00235                         }
00236                 }
00237                 else
00238                 {
00239                         // Draw grey and an X
00240                         gl_rect_2d(interior, LLColor4::grey, TRUE);
00241 
00242                         gl_draw_x(interior, LLColor4::black);
00243                 }
00244 
00245                 LLUICtrl::draw();
00246         }
00247 }
00248 
00249 void LLColorSwatchCtrl::setEnabled( BOOL enabled )
00250 {
00251         mCaption->setEnabled( enabled );
00252         LLView::setEnabled( enabled );
00253 
00254         if (!enabled)
00255         {
00256                 LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(mPickerHandle);
00257                 if (pickerp)
00258                 {
00259                         pickerp->cancelSelection();
00260                         pickerp->close();
00261                 }
00262         }
00263 }
00264 
00265 
00266 void LLColorSwatchCtrl::setValue(const LLSD& value)
00267 {
00268         set(LLColor4(value), TRUE, TRUE);
00269 }
00270 
00272 // called (infrequently) when the color changes so the subject of the swatch can be updated.
00273 void LLColorSwatchCtrl::onColorChanged ( void* data, EColorPickOp pick_op )
00274 {
00275         LLColorSwatchCtrl* subject = ( LLColorSwatchCtrl* )data;
00276         if ( subject )
00277         {
00278                 LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(subject->mPickerHandle);
00279                 if (pickerp)
00280                 {
00281                         // move color across from selector to internal widget storage
00282                         LLColor4 updatedColor ( pickerp->getCurR (), 
00283                                                                         pickerp->getCurG (), 
00284                                                                         pickerp->getCurB (), 
00285                                                                         subject->mColor.mV[VALPHA] ); // keep current alpha
00286                         subject->mColor = updatedColor;
00287                         subject->setControlValue(updatedColor.getValue());
00288 
00289                         if (pick_op == COLOR_CANCEL && subject->mOnCancelCallback)
00290                         {
00291                                 subject->mOnCancelCallback(subject, subject->mCallbackUserData);
00292                         }
00293                         else if (pick_op == COLOR_SELECT && subject->mOnSelectCallback)
00294                         {
00295                                 subject->mOnSelectCallback(subject, subject->mCallbackUserData);
00296                         }
00297                         else
00298                         {
00299                                 // just commit change
00300                                 subject->onCommit ();
00301                         }
00302                 }
00303         };
00304 }
00305 
00306 void LLColorSwatchCtrl::setValid(BOOL valid )
00307 {
00308         mValid = valid;
00309 
00310         LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(mPickerHandle);
00311         if (pickerp)
00312         {
00313                 pickerp->setActive(valid);
00314         }
00315 }
00316 
00317 void LLColorSwatchCtrl::showPicker(BOOL take_focus)
00318 {
00319         LLFloaterColorPicker* pickerp = (LLFloaterColorPicker*)LLFloater::getFloaterByHandle(mPickerHandle);
00320         if (!pickerp)
00321         {
00322                 pickerp = new LLFloaterColorPicker(this, mCanApplyImmediately);
00323                 gFloaterView->getParentFloater(this)->addDependentFloater(pickerp);
00324                 mPickerHandle = pickerp->getHandle();
00325         }
00326 
00327         // initialize picker singleton with current color
00328         pickerp->initUI ( mColor.mV [ VRED ], mColor.mV [ VGREEN ], mColor.mV [ VBLUE ] );
00329 
00330         // display it
00331         pickerp->showUI ();
00332 
00333         if (take_focus)
00334         {
00335                 pickerp->setFocus(TRUE);
00336         }
00337 }
00338 
00339 // virtual
00340 LLXMLNodePtr LLColorSwatchCtrl::getXML(bool save_children) const
00341 {
00342         LLXMLNodePtr node = LLUICtrl::getXML();
00343 
00344         node->createChild("color", TRUE)->setFloatValue(4, mColor.mV);
00345 
00346         node->createChild("border_color", TRUE)->setFloatValue(4, mBorderColor.mV);
00347 
00348         if (mCaption)
00349         {
00350                 node->createChild("label", TRUE)->setStringValue(mCaption->getText());
00351         }
00352 
00353         node->createChild("can_apply_immediately", TRUE)->setBoolValue(mCanApplyImmediately);
00354 
00355         return node;
00356 }
00357 
00358 LLView* LLColorSwatchCtrl::fromXML(LLXMLNodePtr node, LLView *parent, LLUICtrlFactory *factory)
00359 {
00360         LLString name("colorswatch");
00361         node->getAttributeString("name", name);
00362 
00363         LLString label;
00364         node->getAttributeString("label", label);
00365 
00366         LLColor4 color(1.f, 1.f, 1.f, 1.f);
00367         node->getAttributeColor("initial_color", color);
00368 
00369         LLRect rect;
00370         createRect(node, rect, parent, LLRect());
00371 
00372         BOOL can_apply_immediately = FALSE;
00373         node->getAttributeBOOL("can_apply_immediately", can_apply_immediately);
00374 
00375         LLUICtrlCallback callback = NULL;
00376 
00377         if (label.empty())
00378         {
00379                 label.assign(node->getValue());
00380         }
00381 
00382         LLColorSwatchCtrl* color_swatch = new LLColorSwatchCtrl(
00383                 name, 
00384                 rect,
00385                 label,
00386                 color,
00387                 callback,
00388                 NULL );
00389 
00390         color_swatch->setCanApplyImmediately(can_apply_immediately);
00391         color_swatch->initFromXML(node, parent);
00392 
00393         return color_swatch;
00394 }

Generated on Thu Jul 1 06:08:22 2010 for Second Life Viewer by  doxygen 1.4.7