guiGradientCtrl.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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. char* altCommand = Con::getReturnBuffer(512);
  84. dSprintf( altCommand, 512, "%s(%i.color, \"%i.setColor\");", mColorFunction, getId(), getId() );
  85. setField( "altCommand", altCommand );
  86. return true;
  87. }
  88. void GuiGradientSwatchCtrl::onRender( Point2I offset, const RectI &updateRect )
  89. {
  90. bool highlight = mMouseOver;
  91. ColorI backColor = mSwatchColor;
  92. ColorI borderColor = mActive ? ( highlight ? mProfile->mBorderColorHL : mProfile->mBorderColor ) : mProfile->mBorderColorNA;
  93. RectI renderRect( offset, getExtent() );
  94. if ( !highlight )
  95. renderRect.inset( 1, 1 );
  96. GFXDrawUtil *drawer = GFX->getDrawUtil();
  97. drawer->clearBitmapModulation();
  98. // Draw background transparency grid texture...
  99. if ( mGrid.isValid() )
  100. drawer->drawBitmapStretch( mGrid, renderRect );
  101. // Draw swatch color as fill...
  102. drawer->drawRectFill( renderRect, mSwatchColor );
  103. // Draw any borders...
  104. drawer->drawRect( renderRect, borderColor );
  105. }
  106. void GuiGradientSwatchCtrl::onMouseDown(const GuiEvent &event)
  107. {
  108. if (! mActive)
  109. return;
  110. if (mProfile->mCanKeyFocus)
  111. setFirstResponder();
  112. //capture current bounds and mouse down position
  113. mOrigBounds = getBounds();
  114. mMouseDownPosition = event.mousePoint;
  115. if(mUseMouseEvents)
  116. onMouseDown_callback();
  117. //lock the mouse
  118. mouseLock();
  119. mDepressed = true;
  120. // If we have a double click then execute the alt command.
  121. if ( event.mouseClickCount == 2 )
  122. {
  123. onDoubleClick_callback();
  124. execAltConsoleCallback();
  125. }
  126. setUpdate();
  127. }
  128. void GuiGradientSwatchCtrl::onMouseDragged(const GuiEvent &event)
  129. {
  130. //gradientCtrl owns the y, x here however is regulated by the extent currently
  131. //dirty, please fix
  132. GuiGradientCtrl* parent = dynamic_cast<GuiGradientCtrl*>(getParent());
  133. if( !parent )
  134. return;
  135. //use bounds and delta to move the ctrl
  136. Point2I newPosition = mMouseDownPosition;
  137. Point2I deltaMousePosition = event.mousePoint - mMouseDownPosition;
  138. newPosition.x = mOrigBounds.point.x + deltaMousePosition.x;
  139. // default position but it needs to be standard; currently using this cops out a static y value
  140. newPosition.y = mOrigBounds.point.y;
  141. if( newPosition.x + parent->mSwatchFactor >= parent->mBlendRangeBox.point.x &&
  142. newPosition.x + parent->mSwatchFactor <= parent->mBlendRangeBox.extent.x )
  143. {
  144. setPosition(newPosition);
  145. if( parent )
  146. parent->sortColorRange();
  147. }
  148. }
  149. void GuiGradientSwatchCtrl::onRightMouseDown(const GuiEvent &event)
  150. {
  151. GuiGradientCtrl* parent = dynamic_cast<GuiGradientCtrl*>(getParent());
  152. if( parent )
  153. parent->removeColorRange( this );
  154. }
  155. //-----------------------------------------------------------------------------
  156. // GuiGradientCtrl
  157. static S32 QSORT_CALLBACK _numIncreasing( const void* a, const void* b )
  158. {
  159. GuiGradientCtrl::ColorRange *crA = (GuiGradientCtrl::ColorRange *) (a);
  160. GuiGradientCtrl::ColorRange *crB = (GuiGradientCtrl::ColorRange *) (b);
  161. S32 posA = crA->swatch->getPosition().x;
  162. S32 posB = crB->swatch->getPosition().x;
  163. return ( (posA < posB) ? -1 : ((posA > posB) ? 1 : 0) );
  164. }
  165. ImplementEnumType( GuiGradientPickMode,
  166. "\n\n"
  167. "@ingroup GuiCore"
  168. "@internal")
  169. { GuiGradientCtrl::pHorizColorRange, "HorizColor"},
  170. { GuiGradientCtrl::pHorizAlphaRange, "HorizAlpha"},
  171. EndImplementEnumType;
  172. IMPLEMENT_CONOBJECT(GuiGradientCtrl);
  173. ConsoleDocClass( GuiGradientCtrl,
  174. "@brief Visual representation of color box used with the GuiColorPickerCtrl\n\n"
  175. "Editor use only.\n\n"
  176. "@internal"
  177. );
  178. GuiGradientCtrl::GuiGradientCtrl()
  179. {
  180. setExtent(140, 30);
  181. mDisplayMode = pHorizColorRange;
  182. mSaveDisplayMode = pHorizColorRange;
  183. mBaseColor = ColorF(1.,.0,1.);
  184. mPickColor = ColorF(.0,.0,.0);
  185. mMouseDown = mMouseOver = false;
  186. mActive = true;
  187. mPositionChanged = false;
  188. mActionOnMove = false;
  189. mShowReticle = true;
  190. colorWhiteBlend = ColorF(1.,1.,1.,.75);
  191. mSwatchFactor = 7;
  192. }
  193. //--------------------------------------------------------------------------
  194. void GuiGradientCtrl::initPersistFields()
  195. {
  196. addGroup("ColorPicker");
  197. addField("baseColor", TypeColorF, Offset(mBaseColor, GuiGradientCtrl));
  198. addField("pickColor", TypeColorF, Offset(mPickColor, GuiGradientCtrl));
  199. addField("displayMode", TYPEID< PickMode >(), Offset(mDisplayMode, GuiGradientCtrl) );
  200. addField("actionOnMove", TypeBool,Offset(mActionOnMove, GuiGradientCtrl));
  201. addField("showReticle", TypeBool, Offset(mShowReticle, GuiGradientCtrl));
  202. addField("swatchFactor", TypeS32, Offset(mSwatchFactor, GuiGradientCtrl));
  203. endGroup("ColorPicker");
  204. Parent::initPersistFields();
  205. }
  206. bool GuiGradientCtrl::onAdd()
  207. {
  208. Parent::onAdd();
  209. S32 l = getBounds().point.x + mSwatchFactor, r = getBounds().point.x + getBounds().extent.x - mSwatchFactor;
  210. S32 t = getBounds().point.y, b = getBounds().point.y + getBounds().extent.y - mSwatchFactor;
  211. mBlendRangeBox = RectI( Point2I(l, t), Point2I(r, b) );
  212. setupDefaultRange();
  213. reInitSwatches( mDisplayMode );
  214. return true;
  215. }
  216. void GuiGradientCtrl::inspectPreApply()
  217. {
  218. mSaveDisplayMode = mDisplayMode;
  219. }
  220. void GuiGradientCtrl::inspectPostApply()
  221. {
  222. if((mSaveDisplayMode != mDisplayMode) )
  223. reInitSwatches( mDisplayMode );
  224. // Apply any transformations set in the editor
  225. Parent::inspectPostApply();
  226. }
  227. void GuiGradientCtrl::onRender(Point2I offset, const RectI& updateRect)
  228. {
  229. if (mStateBlock.isNull())
  230. {
  231. GFXStateBlockDesc desc;
  232. desc.setBlend(true, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  233. desc.setZReadWrite(false);
  234. desc.zWriteEnable = false;
  235. desc.setCullMode(GFXCullNone);
  236. mStateBlock = GFX->createStateBlock( desc );
  237. }
  238. RectI boundsRect(offset, getExtent());
  239. renderColorBox(boundsRect);
  240. if (mPositionChanged)
  241. {
  242. mPositionChanged = false;
  243. // Now do onAction() if we are allowed
  244. if (mActionOnMove)
  245. onAction();
  246. }
  247. //render the children
  248. renderChildControls( offset, updateRect);
  249. }
  250. /// Function to invoke calls to draw the picker box and swatch controls
  251. void GuiGradientCtrl::renderColorBox(RectI &bounds)
  252. {
  253. // Draw color box differently depending on mode
  254. if( mDisplayMode == pHorizColorRange )
  255. {
  256. drawBlendRangeBox( bounds, false, mColorRange);
  257. }
  258. else if( mDisplayMode == pHorizAlphaRange )
  259. {
  260. drawBlendRangeBox( bounds, false, mAlphaRange);
  261. }
  262. }
  263. /// Function to draw a set of boxes blending throughout an array of colors
  264. void GuiGradientCtrl::drawBlendRangeBox(RectI &bounds, bool vertical, Vector<ColorRange> colorRange)
  265. {
  266. GFX->setStateBlock(mStateBlock);
  267. // Create new global dimensions
  268. S32 l = bounds.point.x + mSwatchFactor, r = bounds.point.x + bounds.extent.x - mSwatchFactor;
  269. S32 t = bounds.point.y, b = bounds.point.y + bounds.extent.y - mSwatchFactor;
  270. // Draw border using new global dimensions
  271. if (mProfile->mBorder)
  272. GFX->getDrawUtil()->drawRect( RectI( Point2I(l,t),Point2I(r,b) ), mProfile->mBorderColor);
  273. // Update local dimensions
  274. mBlendRangeBox.point = globalToLocalCoord(Point2I(l, t));
  275. mBlendRangeBox.extent = globalToLocalCoord(Point2I(r, b));
  276. if(colorRange.size() == 1) // Only one color to draw
  277. {
  278. PrimBuild::begin( GFXTriangleFan, 4 );
  279. PrimBuild::color( colorRange.first().swatch->getColor() );
  280. PrimBuild::vertex2i( l, t );
  281. PrimBuild::vertex2i( l, b );
  282. PrimBuild::color( colorRange.first().swatch->getColor() );
  283. PrimBuild::vertex2i( r, b );
  284. PrimBuild::vertex2i( r, t );
  285. PrimBuild::end();
  286. }
  287. else
  288. {
  289. PrimBuild::begin( GFXTriangleFan, 4 );
  290. PrimBuild::color( colorRange.first().swatch->getColor() );
  291. PrimBuild::vertex2i( l, t );
  292. PrimBuild::vertex2i( l, b );
  293. PrimBuild::color( colorRange.first().swatch->getColor() );
  294. PrimBuild::vertex2i( l + colorRange.first().swatch->getPosition().x, b );
  295. PrimBuild::vertex2i( l + colorRange.first().swatch->getPosition().x, t );
  296. PrimBuild::end();
  297. for( U16 i = 0;i < colorRange.size() - 1; i++ )
  298. {
  299. PrimBuild::begin( GFXTriangleFan, 4 );
  300. if (!vertical) // Horizontal (+x)
  301. {
  302. // First color
  303. PrimBuild::color( colorRange[i].swatch->getColor() );
  304. PrimBuild::vertex2i( l + colorRange[i].swatch->getPosition().x, t );
  305. PrimBuild::vertex2i( l + colorRange[i].swatch->getPosition().x, b );
  306. // First color
  307. PrimBuild::color( colorRange[i+1].swatch->getColor() );
  308. PrimBuild::vertex2i( l + colorRange[i+1].swatch->getPosition().x, b );
  309. PrimBuild::vertex2i( l + colorRange[i+1].swatch->getPosition().x, t );
  310. }
  311. PrimBuild::end();
  312. }
  313. PrimBuild::begin( GFXTriangleFan, 4 );
  314. PrimBuild::color( colorRange.last().swatch->getColor() );
  315. PrimBuild::vertex2i( l + colorRange.last().swatch->getPosition().x, t );
  316. PrimBuild::vertex2i( l + colorRange.last().swatch->getPosition().x, b );
  317. PrimBuild::color( colorRange.last().swatch->getColor() );
  318. PrimBuild::vertex2i( r, b );
  319. PrimBuild::vertex2i( r, t );
  320. PrimBuild::end();
  321. }
  322. }
  323. void GuiGradientCtrl::onMouseDown(const GuiEvent &event)
  324. {
  325. if (!mActive)
  326. return;
  327. mouseLock(this);
  328. if (mProfile->mCanKeyFocus)
  329. setFirstResponder();
  330. if (mActive)
  331. onAction();
  332. Point2I extent = getRoot()->getExtent();
  333. Point2I resolution = getRoot()->getExtent();
  334. GFXTexHandle bb( resolution.x,
  335. resolution.y,
  336. GFXFormatR8G8B8A8, &GFXDefaultRenderTargetProfile, avar("%s() - bb (line %d)", __FUNCTION__, __LINE__) );
  337. Point2I tmpPt( event.mousePoint.x, event.mousePoint.y );
  338. GFXTarget *targ = GFX->getActiveRenderTarget();
  339. targ->resolveTo( bb );
  340. GBitmap bmp( bb.getWidth(), bb.getHeight() );
  341. bb.copyToBmp( &bmp );
  342. ColorI tmp;
  343. bmp.getColor( event.mousePoint.x, event.mousePoint.y, tmp );
  344. addColorRange( globalToLocalCoord(event.mousePoint), ColorF(tmp) );
  345. mMouseDown = true;
  346. }
  347. void GuiGradientCtrl::onMouseUp(const GuiEvent &)
  348. {
  349. //if we released the mouse within this control, perform the action
  350. if (mActive && mMouseDown )
  351. mMouseDown = false;
  352. mouseUnlock();
  353. }
  354. void GuiGradientCtrl::onMouseEnter(const GuiEvent &event)
  355. {
  356. mMouseOver = true;
  357. }
  358. void GuiGradientCtrl::onMouseLeave(const GuiEvent &)
  359. {
  360. // Reset state
  361. mMouseOver = false;
  362. }
  363. void GuiGradientCtrl::setupDefaultRange()
  364. {
  365. S32 l = mBlendRangeBox.point.x - mSwatchFactor;
  366. S32 r = mBlendRangeBox.extent.x - mSwatchFactor;
  367. //setup alpha range (white/black only)
  368. ColorRange crW;
  369. crW.pos = l;
  370. crW.color = ColorI(255,255,255);
  371. crW.swatch = NULL;
  372. mAlphaRange.push_back( crW );
  373. ColorRange crB;
  374. crB.pos = r;
  375. crB.color = ColorI(0,0,0);
  376. crB.swatch = NULL;
  377. mAlphaRange.push_back( crB );
  378. //setup color range (only 1 color necessary)
  379. ColorRange crD;
  380. crD.pos = l;
  381. crD.color = ColorI(255,0,0);
  382. crD.swatch = NULL;
  383. mColorRange.push_back( crD );
  384. }
  385. void GuiGradientCtrl::reInitSwatches( GuiGradientCtrl::PickMode )
  386. {
  387. //liable to crash in the guiEditor, needs fix
  388. for( S32 i = 0;i < mColorRange.size(); i++ )
  389. {
  390. if(mColorRange[i].swatch != NULL)
  391. {
  392. mColorRange[i].pos = mColorRange[i].swatch->getPosition().x;
  393. mColorRange[i].color = mColorRange[i].swatch->getColor();
  394. mColorRange[i].swatch->deleteObject();
  395. mColorRange[i].swatch = NULL;
  396. }
  397. }
  398. for( S32 i = 0;i < mAlphaRange.size(); i++ )
  399. {
  400. if(mAlphaRange[i].swatch != NULL)
  401. {
  402. mAlphaRange[i].pos = mAlphaRange[i].swatch->getPosition().x;
  403. mAlphaRange[i].color = mAlphaRange[i].swatch->getColor();
  404. mAlphaRange[i].swatch->deleteObject();
  405. mAlphaRange[i].swatch = NULL;
  406. }
  407. }
  408. S32 b = mBlendRangeBox.extent.y - mSwatchFactor;
  409. if( mDisplayMode == pHorizColorRange )
  410. {
  411. for( S32 i = 0;i < mColorRange.size(); i++ )
  412. {
  413. mColorRange[i].swatch = new GuiGradientSwatchCtrl();
  414. mColorRange[i].swatch->registerObject();
  415. addObject(mColorRange[i].swatch);
  416. mColorRange[i].swatch->setPosition( Point2I( mColorRange[i].pos, b ) );// needs to be adjusted
  417. mColorRange[i].swatch->setColor(ColorF(mColorRange[i].color));
  418. }
  419. }
  420. else if( mDisplayMode == pHorizAlphaRange )
  421. {
  422. for( S32 i = 0;i < mAlphaRange.size(); i++ )
  423. {
  424. mAlphaRange[i].swatch = new GuiGradientSwatchCtrl();
  425. mAlphaRange[i].swatch->registerObject();
  426. addObject(mAlphaRange[i].swatch);
  427. mAlphaRange[i].swatch->setPosition( Point2I( mAlphaRange[i].pos, b ) );// needs to be adjusted
  428. mAlphaRange[i].swatch->setColor(ColorF(mAlphaRange[i].color));
  429. }
  430. }
  431. }
  432. void GuiGradientCtrl::addColorRange( Point2I pos, ColorF color )
  433. {
  434. if( pos.x + mSwatchFactor < mBlendRangeBox.point.x &&
  435. pos.x + mSwatchFactor > mBlendRangeBox.extent.x )
  436. {
  437. return;
  438. }
  439. ColorRange range;
  440. range.pos = pos.x - mSwatchFactor;
  441. range.color = color;
  442. S32 b = mBlendRangeBox.extent.y - mSwatchFactor;
  443. range.swatch = new GuiGradientSwatchCtrl();
  444. range.swatch->registerObject();
  445. addObject( range.swatch );
  446. range.swatch->setPosition( pos.x - mSwatchFactor, b );//swatch factor and default location is going to have to be placed
  447. range.swatch->setColor( color );
  448. if( mDisplayMode == pHorizColorRange )
  449. {
  450. mColorRange.push_back( range );
  451. S32 size = mColorRange.size();
  452. if( size > 0 )
  453. dQsort( mColorRange.address(), size, sizeof(ColorRange), _numIncreasing);
  454. }
  455. else if( mDisplayMode == pHorizAlphaRange )
  456. {
  457. mAlphaRange.push_back( range );
  458. S32 size = mAlphaRange.size();
  459. if( size > 0 )
  460. dQsort( mAlphaRange.address(), size, sizeof(ColorRange), _numIncreasing);
  461. }
  462. }
  463. void GuiGradientCtrl::removeColorRange( GuiGradientSwatchCtrl* swatch )
  464. {
  465. if( mDisplayMode == pHorizColorRange )
  466. {
  467. if( mColorRange.size() <= 1 )
  468. return;
  469. for( S32 i = 0;i < mColorRange.size(); i++ )
  470. {
  471. if( mColorRange[i].swatch == swatch )
  472. {
  473. mColorRange.erase( U32(i) );
  474. swatch->safeDeleteObject();
  475. break;
  476. }
  477. }
  478. }
  479. else if( mDisplayMode == pHorizAlphaRange )
  480. {
  481. if( mAlphaRange.size() <= 1 )
  482. return;
  483. for( S32 i = 0;i < mAlphaRange.size(); i++ )
  484. {
  485. if( mAlphaRange[i].swatch == swatch )
  486. {
  487. mAlphaRange.erase( U32(i) );
  488. swatch->safeDeleteObject();
  489. break;
  490. }
  491. }
  492. }
  493. }
  494. void GuiGradientCtrl::sortColorRange()
  495. {
  496. if( mDisplayMode == pHorizColorRange )
  497. dQsort( mColorRange.address(), mColorRange.size(), sizeof(ColorRange), _numIncreasing);
  498. else if( mDisplayMode == pHorizAlphaRange )
  499. dQsort( mAlphaRange.address(), mAlphaRange.size(), sizeof(ColorRange), _numIncreasing);
  500. }
  501. ConsoleMethod(GuiGradientCtrl, getColorCount, S32, 2, 2, "Get color count")
  502. {
  503. if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  504. return object->mColorRange.size();
  505. else if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  506. return object->mColorRange.size();
  507. return 0;
  508. }
  509. ConsoleMethod(GuiGradientCtrl, getColor, const char*, 3, 3, "Get color value")
  510. {
  511. S32 idx = dAtoi(argv[2]);
  512. if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  513. {
  514. if ( idx >= 0 && idx < object->mColorRange.size() )
  515. {
  516. char* rColor = Con::getReturnBuffer(256);
  517. rColor[0] = 0;
  518. dSprintf(rColor, 256, "%f %f %f %f",
  519. object->mColorRange[idx].swatch->getColor().red,
  520. object->mColorRange[idx].swatch->getColor().green,
  521. object->mColorRange[idx].swatch->getColor().blue,
  522. object->mColorRange[idx].swatch->getColor().alpha);
  523. return rColor;
  524. }
  525. }
  526. else if( object->getDisplayMode() == GuiGradientCtrl::pHorizColorRange )
  527. {
  528. if ( idx >= 0 && idx < object->mAlphaRange.size() )
  529. {
  530. char* rColor = Con::getReturnBuffer(256);
  531. rColor[0] = 0;
  532. dSprintf(rColor, 256, "%f %f %f %f",
  533. object->mAlphaRange[idx].swatch->getColor().red,
  534. object->mAlphaRange[idx].swatch->getColor().green,
  535. object->mAlphaRange[idx].swatch->getColor().blue,
  536. object->mAlphaRange[idx].swatch->getColor().alpha);
  537. return rColor;
  538. }
  539. }
  540. return "1 1 1 1";
  541. }