guiGradientCtrl.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "console/console.h"
  23. #include "gfx/gfxDevice.h"
  24. #include "console/consoleTypes.h"
  25. #include "gui/core/guiCanvas.h"
  26. #include "gui/buttons/guiButtonCtrl.h"
  27. #include "gui/core/guiDefaultControlRender.h"
  28. #include "gui/controls/guiGradientCtrl.h"
  29. #include "gui/controls/guiColorPicker.h"
  30. #include "gfx/primBuilder.h"
  31. #include "gfx/gfxDrawUtil.h"
  32. #include "console/engineAPI.h"
  33. //-----------------------------------------------------------------------------
  34. // GuiGradientSwatchCtrl
  35. IMPLEMENT_CONOBJECT(GuiGradientSwatchCtrl);
  36. ConsoleDocClass( GuiGradientSwatchCtrl,
  37. "@brief Swatch selector that appears inside the GuiGradientCtrl object. These objects are automatically created by GuiGradientCtrl. \n\n"
  38. "Currently only appears to be editor specific\n\n"
  39. "@see GuiSwatchButtonCtrl\n"
  40. "@see GuiGradientCtrl\n\n"
  41. "@ingroup GuiCore\n"
  42. "@internal"
  43. );
  44. IMPLEMENT_CALLBACK( GuiGradientSwatchCtrl, onMouseDown, void, (),(),
  45. "@brief Called whenever the left mouse button has entered the down state while in this control.\n\n"
  46. "@tsexample\n"
  47. "// The left mouse button is down on the control, causing the callback to occur.\n"
  48. "GuiGradientSwatchCtrl::onMouseDown(%this)\n"
  49. " {\n"
  50. " // Code to run when the callback occurs\n"
  51. " }\n"
  52. "@endtsexample\n\n"
  53. "@see GuiControl\n"
  54. "@see GuiSwatchButtonCtrl\n\n"
  55. "@internal"
  56. );
  57. IMPLEMENT_CALLBACK( GuiGradientSwatchCtrl, onDoubleClick, void, (),(),
  58. "@brief Called whenever the left mouse button performs a double click while in this control.\n\n"
  59. "@tsexample\n"
  60. "// The left mouse button has performed a double click on the control, causing the callback to occur.\n"
  61. "GuiGradientSwatchCtrl::onDoubleClick(%this)\n"
  62. " {\n"
  63. " // Code to run when the callback occurs\n"
  64. " }\n"
  65. "@endtsexample\n\n"
  66. "@see GuiControl\n"
  67. "@see GuiSwatchButtonCtrl\n\n"
  68. "@internal"
  69. );
  70. GuiGradientSwatchCtrl::GuiGradientSwatchCtrl()
  71. {
  72. setPosition(0, 0);
  73. setExtent(14, 14);
  74. mMouseDownPosition = Point2I(0, 0);
  75. mSwatchColor = ColorI( 1, 1, 1, 1 );
  76. mColorFunction = StringTable->insert("getColorF");
  77. setDataField( StringTable->insert("Profile"), NULL, "GuiInspectorSwatchButtonProfile" );
  78. }
  79. bool GuiGradientSwatchCtrl::onWake()
  80. {
  81. if ( !Parent::onWake() )
  82. return false;
  83. static const U32 bufSize = 512;
  84. char* altCommand = Con::getReturnBuffer(bufSize);
  85. dSprintf( altCommand, bufSize, "%s(%i.color, \"%i.setColor\");", mColorFunction, getId(), getId() );
  86. setField( "altCommand", altCommand );
  87. return true;
  88. }
  89. void GuiGradientSwatchCtrl::onRender( Point2I offset, const RectI &updateRect )
  90. {
  91. bool highlight = mMouseOver;
  92. ColorI backColor = mSwatchColor;
  93. ColorI borderColor = mActive ? ( highlight ? mProfile->mBorderColorHL : mProfile->mBorderColor ) : mProfile->mBorderColorNA;
  94. RectI renderRect( offset, getExtent() );
  95. if ( !highlight )
  96. renderRect.inset( 1, 1 );
  97. GFXDrawUtil *drawer = GFX->getDrawUtil();
  98. drawer->clearBitmapModulation();
  99. // Draw background transparency grid texture...
  100. if ( mGrid.isValid() )
  101. drawer->drawBitmapStretch( mGrid, renderRect );
  102. // Draw swatch color as fill...
  103. drawer->drawRectFill( renderRect, mSwatchColor );
  104. // Draw any borders...
  105. drawer->drawRect( renderRect, borderColor );
  106. }
  107. void GuiGradientSwatchCtrl::onMouseDown(const GuiEvent &event)
  108. {
  109. if (! mActive)
  110. return;
  111. if (mProfile->mCanKeyFocus)
  112. setFirstResponder();
  113. //capture current bounds and mouse down position
  114. mOrigBounds = getBounds();
  115. mMouseDownPosition = event.mousePoint;
  116. if(mUseMouseEvents)
  117. onMouseDown_callback();
  118. //lock the mouse
  119. mouseLock();
  120. mDepressed = true;
  121. // If we have a double click then execute the alt command.
  122. if ( event.mouseClickCount == 2 )
  123. {
  124. onDoubleClick_callback();
  125. execAltConsoleCallback();
  126. }
  127. setUpdate();
  128. }
  129. void GuiGradientSwatchCtrl::onMouseDragged(const GuiEvent &event)
  130. {
  131. //gradientCtrl owns the y, x here however is regulated by the extent currently
  132. //dirty, please fix
  133. GuiGradientCtrl* parent = dynamic_cast<GuiGradientCtrl*>(getParent());
  134. if( !parent )
  135. return;
  136. //use bounds and delta to move the ctrl
  137. Point2I newPosition = mMouseDownPosition;
  138. Point2I deltaMousePosition = event.mousePoint - mMouseDownPosition;
  139. newPosition.x = mOrigBounds.point.x + deltaMousePosition.x;
  140. // default position but it needs to be standard; currently using this cops out a static y value
  141. newPosition.y = mOrigBounds.point.y;
  142. if( newPosition.x + parent->mSwatchFactor >= parent->mBlendRangeBox.point.x &&
  143. newPosition.x + parent->mSwatchFactor <= parent->mBlendRangeBox.extent.x )
  144. {
  145. setPosition(newPosition);
  146. if( parent )
  147. parent->sortColorRange();
  148. }
  149. }
  150. void GuiGradientSwatchCtrl::onRightMouseDown(const GuiEvent &event)
  151. {
  152. GuiGradientCtrl* parent = dynamic_cast<GuiGradientCtrl*>(getParent());
  153. if( parent )
  154. parent->removeColorRange( this );
  155. }
  156. //-----------------------------------------------------------------------------
  157. // GuiGradientCtrl
  158. static S32 QSORT_CALLBACK _numIncreasing( const void* a, const void* b )
  159. {
  160. GuiGradientCtrl::ColorRange *crA = (GuiGradientCtrl::ColorRange *) (a);
  161. GuiGradientCtrl::ColorRange *crB = (GuiGradientCtrl::ColorRange *) (b);
  162. S32 posA = crA->swatch->getPosition().x;
  163. S32 posB = crB->swatch->getPosition().x;
  164. return ( (posA < posB) ? -1 : ((posA > posB) ? 1 : 0) );
  165. }
  166. ImplementEnumType( GuiGradientPickMode,
  167. "\n\n"
  168. "@ingroup GuiCore"
  169. "@internal")
  170. { GuiGradientCtrl::pHorizColorRange, "HorizColor"},
  171. { GuiGradientCtrl::pHorizAlphaRange, "HorizAlpha"},
  172. EndImplementEnumType;
  173. IMPLEMENT_CONOBJECT(GuiGradientCtrl);
  174. ConsoleDocClass( GuiGradientCtrl,
  175. "@brief Visual representation of color box used with the GuiColorPickerCtrl\n\n"
  176. "Editor use only.\n\n"
  177. "@internal"
  178. );
  179. GuiGradientCtrl::GuiGradientCtrl()
  180. {
  181. setExtent(140, 30);
  182. mDisplayMode = pHorizColorRange;
  183. mSaveDisplayMode = pHorizColorRange;
  184. mBaseColor = ColorF(1.,.0,1.);
  185. mPickColor = ColorF(.0,.0,.0);
  186. mMouseDown = mMouseOver = false;
  187. mActive = true;
  188. mPositionChanged = false;
  189. mActionOnMove = false;
  190. mShowReticle = true;
  191. colorWhiteBlend = ColorF(1.,1.,1.,.75);
  192. mSwatchFactor = 7;
  193. }
  194. //--------------------------------------------------------------------------
  195. void GuiGradientCtrl::initPersistFields()
  196. {
  197. addGroup("ColorPicker");
  198. addField("baseColor", TypeColorF, Offset(mBaseColor, GuiGradientCtrl));
  199. addField("pickColor", TypeColorF, Offset(mPickColor, GuiGradientCtrl));
  200. addField("displayMode", TYPEID< PickMode >(), Offset(mDisplayMode, GuiGradientCtrl) );
  201. addField("actionOnMove", TypeBool,Offset(mActionOnMove, GuiGradientCtrl));
  202. addField("showReticle", TypeBool, Offset(mShowReticle, GuiGradientCtrl));
  203. addField("swatchFactor", TypeS32, Offset(mSwatchFactor, GuiGradientCtrl));
  204. endGroup("ColorPicker");
  205. Parent::initPersistFields();
  206. }
  207. bool GuiGradientCtrl::onAdd()
  208. {
  209. Parent::onAdd();
  210. S32 l = getBounds().point.x + mSwatchFactor, r = getBounds().point.x + getBounds().extent.x - mSwatchFactor;
  211. S32 t = getBounds().point.y, b = getBounds().point.y + getBounds().extent.y - mSwatchFactor;
  212. mBlendRangeBox = RectI( Point2I(l, t), Point2I(r, b) );
  213. setupDefaultRange();
  214. reInitSwatches( mDisplayMode );
  215. return true;
  216. }
  217. void GuiGradientCtrl::inspectPreApply()
  218. {
  219. mSaveDisplayMode = mDisplayMode;
  220. }
  221. void GuiGradientCtrl::inspectPostApply()
  222. {
  223. if((mSaveDisplayMode != mDisplayMode) )
  224. reInitSwatches( mDisplayMode );
  225. // Apply any transformations set in the editor
  226. Parent::inspectPostApply();
  227. }
  228. void GuiGradientCtrl::onRender(Point2I offset, const RectI& updateRect)
  229. {
  230. if (mStateBlock.isNull())
  231. {
  232. GFXStateBlockDesc desc;
  233. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  234. desc.setZReadWrite(false);
  235. desc.zWriteEnable = false;
  236. desc.setCullMode(GFXCullNone);
  237. mStateBlock = GFX->createStateBlock( desc );
  238. }
  239. RectI boundsRect(offset, getExtent());
  240. renderColorBox(boundsRect);
  241. if (mPositionChanged)
  242. {
  243. mPositionChanged = false;
  244. // Now do onAction() if we are allowed
  245. if (mActionOnMove)
  246. onAction();
  247. }
  248. //render the children
  249. renderChildControls( offset, updateRect);
  250. }
  251. /// Function to invoke calls to draw the picker box and swatch controls
  252. void GuiGradientCtrl::renderColorBox(RectI &bounds)
  253. {
  254. // Draw color box differently depending on mode
  255. if( mDisplayMode == pHorizColorRange )
  256. {
  257. drawBlendRangeBox( bounds, false, mColorRange);
  258. }
  259. else if( mDisplayMode == pHorizAlphaRange )
  260. {
  261. drawBlendRangeBox( bounds, false, mAlphaRange);
  262. }
  263. }
  264. /// Function to draw a set of boxes blending throughout an array of colors
  265. void GuiGradientCtrl::drawBlendRangeBox(RectI &bounds, bool vertical, Vector<ColorRange> colorRange)
  266. {
  267. GFX->setStateBlock(mStateBlock);
  268. // Create new global dimensions
  269. S32 l = bounds.point.x + mSwatchFactor, r = bounds.point.x + bounds.extent.x - mSwatchFactor;
  270. S32 t = bounds.point.y, b = bounds.point.y + bounds.extent.y - mSwatchFactor;
  271. // Draw border using new global dimensions
  272. if (mProfile->mBorder)
  273. GFX->getDrawUtil()->drawRect( RectI( Point2I(l,t),Point2I(r,b) ), mProfile->mBorderColor);
  274. // Update local dimensions
  275. mBlendRangeBox.point = globalToLocalCoord(Point2I(l, t));
  276. mBlendRangeBox.extent = globalToLocalCoord(Point2I(r, b));
  277. if(colorRange.size() == 1) // Only one color to draw
  278. {
  279. PrimBuild::begin( GFXTriangleFan, 4 );
  280. PrimBuild::color( colorRange.first().swatch->getColor() );
  281. PrimBuild::vertex2i( l, t );
  282. PrimBuild::vertex2i( l, b );
  283. PrimBuild::color( colorRange.first().swatch->getColor() );
  284. PrimBuild::vertex2i( r, b );
  285. PrimBuild::vertex2i( r, t );
  286. PrimBuild::end();
  287. }
  288. else
  289. {
  290. PrimBuild::begin( GFXTriangleFan, 4 );
  291. PrimBuild::color( colorRange.first().swatch->getColor() );
  292. PrimBuild::vertex2i( l, t );
  293. PrimBuild::vertex2i( l, b );
  294. PrimBuild::color( colorRange.first().swatch->getColor() );
  295. PrimBuild::vertex2i( l + colorRange.first().swatch->getPosition().x, b );
  296. PrimBuild::vertex2i( l + colorRange.first().swatch->getPosition().x, t );
  297. PrimBuild::end();
  298. for( U16 i = 0;i < colorRange.size() - 1; i++ )
  299. {
  300. PrimBuild::begin( GFXTriangleFan, 4 );
  301. if (!vertical) // Horizontal (+x)
  302. {
  303. // First color
  304. PrimBuild::color( colorRange[i].swatch->getColor() );
  305. PrimBuild::vertex2i( l + colorRange[i].swatch->getPosition().x, t );
  306. PrimBuild::vertex2i( l + colorRange[i].swatch->getPosition().x, b );
  307. // First color
  308. PrimBuild::color( colorRange[i+1].swatch->getColor() );
  309. PrimBuild::vertex2i( l + colorRange[i+1].swatch->getPosition().x, b );
  310. PrimBuild::vertex2i( l + colorRange[i+1].swatch->getPosition().x, t );
  311. }
  312. PrimBuild::end();
  313. }
  314. PrimBuild::begin( GFXTriangleFan, 4 );
  315. PrimBuild::color( colorRange.last().swatch->getColor() );
  316. PrimBuild::vertex2i( l + colorRange.last().swatch->getPosition().x, t );
  317. PrimBuild::vertex2i( l + colorRange.last().swatch->getPosition().x, b );
  318. PrimBuild::color( colorRange.last().swatch->getColor() );
  319. PrimBuild::vertex2i( r, b );
  320. PrimBuild::vertex2i( r, t );
  321. PrimBuild::end();
  322. }
  323. }
  324. void GuiGradientCtrl::onMouseDown(const GuiEvent &event)
  325. {
  326. if (!mActive)
  327. return;
  328. mouseLock(this);
  329. if (mProfile->mCanKeyFocus)
  330. setFirstResponder();
  331. if (mActive)
  332. onAction();
  333. Point2I extent = getRoot()->getExtent();
  334. Point2I resolution = getRoot()->getExtent();
  335. GFXTexHandle bb( resolution.x,
  336. resolution.y,
  337. GFXFormatR8G8B8A8, &GFXDefaultRenderTargetProfile, avar("%s() - bb (line %d)", __FUNCTION__, __LINE__) );
  338. Point2I tmpPt( event.mousePoint.x, event.mousePoint.y );
  339. GFXTarget *targ = GFX->getActiveRenderTarget();
  340. targ->resolveTo( bb );
  341. GBitmap bmp( bb.getWidth(), bb.getHeight() );
  342. bb.copyToBmp( &bmp );
  343. ColorI tmp;
  344. bmp.getColor( event.mousePoint.x, event.mousePoint.y, tmp );
  345. addColorRange( globalToLocalCoord(event.mousePoint), ColorF(tmp) );
  346. mMouseDown = true;
  347. }
  348. void GuiGradientCtrl::onMouseUp(const GuiEvent &)
  349. {
  350. //if we released the mouse within this control, perform the action
  351. if (mActive && mMouseDown )
  352. mMouseDown = false;
  353. mouseUnlock();
  354. }
  355. void GuiGradientCtrl::onMouseEnter(const GuiEvent &event)
  356. {
  357. mMouseOver = true;
  358. }
  359. void GuiGradientCtrl::onMouseLeave(const GuiEvent &)
  360. {
  361. // Reset state
  362. mMouseOver = false;
  363. }
  364. void GuiGradientCtrl::setupDefaultRange()
  365. {
  366. S32 l = mBlendRangeBox.point.x - mSwatchFactor;
  367. S32 r = mBlendRangeBox.extent.x - mSwatchFactor;
  368. //setup alpha range (white/black only)
  369. ColorRange crW;
  370. crW.pos = l;
  371. crW.color = ColorI(255,255,255);
  372. crW.swatch = NULL;
  373. mAlphaRange.push_back( crW );
  374. ColorRange crB;
  375. crB.pos = r;
  376. crB.color = ColorI(0,0,0);
  377. crB.swatch = NULL;
  378. mAlphaRange.push_back( crB );
  379. //setup color range (only 1 color necessary)
  380. ColorRange crD;
  381. crD.pos = l;
  382. crD.color = ColorI(255,0,0);
  383. crD.swatch = NULL;
  384. mColorRange.push_back( crD );
  385. }
  386. void GuiGradientCtrl::reInitSwatches( GuiGradientCtrl::PickMode )
  387. {
  388. //liable to crash in the guiEditor, needs fix
  389. for( S32 i = 0;i < mColorRange.size(); i++ )
  390. {
  391. if(mColorRange[i].swatch != NULL)
  392. {
  393. mColorRange[i].pos = mColorRange[i].swatch->getPosition().x;
  394. mColorRange[i].color = mColorRange[i].swatch->getColor();
  395. mColorRange[i].swatch->deleteObject();
  396. mColorRange[i].swatch = NULL;
  397. }
  398. }
  399. for( S32 i = 0;i < mAlphaRange.size(); i++ )
  400. {
  401. if(mAlphaRange[i].swatch != NULL)
  402. {
  403. mAlphaRange[i].pos = mAlphaRange[i].swatch->getPosition().x;
  404. mAlphaRange[i].color = mAlphaRange[i].swatch->getColor();
  405. mAlphaRange[i].swatch->deleteObject();
  406. mAlphaRange[i].swatch = NULL;
  407. }
  408. }
  409. S32 b = mBlendRangeBox.extent.y - mSwatchFactor;
  410. if( mDisplayMode == pHorizColorRange )
  411. {
  412. for( S32 i = 0;i < mColorRange.size(); i++ )
  413. {
  414. mColorRange[i].swatch = new GuiGradientSwatchCtrl();
  415. mColorRange[i].swatch->registerObject();
  416. addObject(mColorRange[i].swatch);
  417. mColorRange[i].swatch->setPosition( Point2I( mColorRange[i].pos, b ) );// needs to be adjusted
  418. mColorRange[i].swatch->setColor(ColorF(mColorRange[i].color));
  419. }
  420. }
  421. else if( mDisplayMode == pHorizAlphaRange )
  422. {
  423. for( S32 i = 0;i < mAlphaRange.size(); i++ )
  424. {
  425. mAlphaRange[i].swatch = new GuiGradientSwatchCtrl();
  426. mAlphaRange[i].swatch->registerObject();
  427. addObject(mAlphaRange[i].swatch);
  428. mAlphaRange[i].swatch->setPosition( Point2I( mAlphaRange[i].pos, b ) );// needs to be adjusted
  429. mAlphaRange[i].swatch->setColor(ColorF(mAlphaRange[i].color));
  430. }
  431. }
  432. }
  433. void GuiGradientCtrl::addColorRange( Point2I pos, ColorF color )
  434. {
  435. if( pos.x + mSwatchFactor < mBlendRangeBox.point.x &&
  436. pos.x + mSwatchFactor > mBlendRangeBox.extent.x )
  437. {
  438. return;
  439. }
  440. ColorRange range;
  441. range.pos = pos.x - mSwatchFactor;
  442. range.color = color;
  443. S32 b = mBlendRangeBox.extent.y - mSwatchFactor;
  444. range.swatch = new GuiGradientSwatchCtrl();
  445. range.swatch->registerObject();
  446. addObject( range.swatch );
  447. range.swatch->setPosition( pos.x - mSwatchFactor, b );//swatch factor and default location is going to have to be placed
  448. range.swatch->setColor( color );
  449. if( mDisplayMode == pHorizColorRange )
  450. {
  451. mColorRange.push_back( range );
  452. S32 size = mColorRange.size();
  453. if( size > 0 )
  454. dQsort( mColorRange.address(), size, sizeof(ColorRange), _numIncreasing);
  455. }
  456. else if( mDisplayMode == pHorizAlphaRange )
  457. {
  458. mAlphaRange.push_back( range );
  459. S32 size = mAlphaRange.size();
  460. if( size > 0 )
  461. dQsort( mAlphaRange.address(), size, sizeof(ColorRange), _numIncreasing);
  462. }
  463. }
  464. void GuiGradientCtrl::removeColorRange( GuiGradientSwatchCtrl* swatch )
  465. {
  466. if( mDisplayMode == pHorizColorRange )
  467. {
  468. if( mColorRange.size() <= 1 )
  469. return;
  470. for( S32 i = 0;i < mColorRange.size(); i++ )
  471. {
  472. if( mColorRange[i].swatch == swatch )
  473. {
  474. mColorRange.erase( U32(i) );
  475. swatch->safeDeleteObject();
  476. break;
  477. }
  478. }
  479. }
  480. else if( mDisplayMode == pHorizAlphaRange )
  481. {
  482. if( mAlphaRange.size() <= 1 )
  483. return;
  484. for( S32 i = 0;i < mAlphaRange.size(); i++ )
  485. {
  486. if( mAlphaRange[i].swatch == swatch )
  487. {
  488. mAlphaRange.erase( U32(i) );
  489. swatch->safeDeleteObject();
  490. break;
  491. }
  492. }
  493. }
  494. }
  495. void GuiGradientCtrl::sortColorRange()
  496. {
  497. if( mDisplayMode == pHorizColorRange )
  498. dQsort( mColorRange.address(), mColorRange.size(), sizeof(ColorRange), _numIncreasing);
  499. else if( mDisplayMode == pHorizAlphaRange )
  500. dQsort( mAlphaRange.address(), mAlphaRange.size(), sizeof(ColorRange), _numIncreasing);
  501. }
  502. ConsoleMethod(GuiGradientCtrl, getColorCount, S32, 2, 2, "Get color count")
  503. {
  504. if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  505. return object->mColorRange.size();
  506. else if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  507. return object->mColorRange.size();
  508. return 0;
  509. }
  510. ConsoleMethod(GuiGradientCtrl, getColor, const char*, 3, 3, "Get color value")
  511. {
  512. S32 idx = dAtoi(argv[2]);
  513. if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  514. {
  515. if ( idx >= 0 && idx < object->mColorRange.size() )
  516. {
  517. static const U32 bufSize = 256;
  518. char* rColor = Con::getReturnBuffer(bufSize);
  519. rColor[0] = 0;
  520. dSprintf(rColor, bufSize, "%f %f %f %f",
  521. object->mColorRange[idx].swatch->getColor().red,
  522. object->mColorRange[idx].swatch->getColor().green,
  523. object->mColorRange[idx].swatch->getColor().blue,
  524. object->mColorRange[idx].swatch->getColor().alpha);
  525. return rColor;
  526. }
  527. }
  528. else if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  529. {
  530. if ( idx >= 0 && idx < object->mAlphaRange.size() )
  531. {
  532. static const U32 bufSize = 256;
  533. char* rColor = Con::getReturnBuffer(bufSize);
  534. rColor[0] = 0;
  535. dSprintf(rColor, bufSize, "%f %f %f %f",
  536. object->mAlphaRange[idx].swatch->getColor().red,
  537. object->mAlphaRange[idx].swatch->getColor().green,
  538. object->mAlphaRange[idx].swatch->getColor().blue,
  539. object->mAlphaRange[idx].swatch->getColor().alpha);
  540. return rColor;
  541. }
  542. }
  543. return "1 1 1 1";
  544. }