guiIconButtonCtrl.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. //-------------------------------------
  23. //
  24. // Icon Button Control
  25. // Draws the bitmap within a special button control. Only a single bitmap is used and the
  26. // button will be drawn in a highlighted mode when the mouse hovers over it or when it
  27. // has been clicked.
  28. //
  29. // Use mTextLocation to choose where within the button the text will be drawn, if at all.
  30. // Use mTextMargin to set the text away from the button sides or from the bitmap.
  31. // Use mButtonMargin to set everything away from the button sides.
  32. // Use mErrorBitmapName to set the name of a bitmap to draw if the main bitmap cannot be found.
  33. // Use mFitBitmapToButton to force the bitmap to fill the entire button extent. Usually used
  34. // with no button text defined.
  35. //
  36. //
  37. #include "platform/platform.h"
  38. #include "gui/buttons/guiIconButtonCtrl.h"
  39. #include "console/console.h"
  40. #include "gfx/gfxDevice.h"
  41. #include "gfx/gfxDrawUtil.h"
  42. #include "console/consoleTypes.h"
  43. #include "gui/core/guiCanvas.h"
  44. #include "gui/core/guiDefaultControlRender.h"
  45. #include "console/engineAPI.h"
  46. static const ColorI colorWhite(255,255,255);
  47. static const ColorI colorBlack(0,0,0);
  48. IMPLEMENT_CONOBJECT(GuiIconButtonCtrl);
  49. ConsoleDocClass( GuiIconButtonCtrl,
  50. "@brief Draws the bitmap within a special button control. Only a single bitmap is used and the\n"
  51. "button will be drawn in a highlighted mode when the mouse hovers over it or when it\n"
  52. "has been clicked.\n\n"
  53. "@tsexample\n"
  54. "new GuiIconButtonCtrl(TestIconButton)\n"
  55. "{\n"
  56. " buttonMargin = \"4 4\";\n"
  57. " iconBitmap = \"art/gui/lagIcon.png\";\n"
  58. " iconLocation = \"Center\";\n"
  59. " sizeIconToButton = \"0\";\n"
  60. " makeIconSquare = \"1\";\n"
  61. " textLocation = \"Bottom\";\n"
  62. " textMargin = \"-2\";\n"
  63. " bitmapMargin = \"0\";\n"
  64. " autoSize = \"0\";\n"
  65. " text = \"Lag Icon\";\n"
  66. " textID = \"\"STR_LAG\"\";\n"
  67. " buttonType = \"PushButton\";\n"
  68. " profile = \"GuiIconButtonProfile\";\n"
  69. "};\n"
  70. "@endtsexample\n\n"
  71. "@see GuiControl\n"
  72. "@see GuiButtonCtrl\n\n"
  73. "@ingroup GuiCore\n"
  74. );
  75. GuiIconButtonCtrl::GuiIconButtonCtrl()
  76. {
  77. mTextLocation = TextLocLeft;
  78. mIconLocation = IconLocLeft;
  79. mTextMargin = 4;
  80. mButtonMargin.set(4,4);
  81. mFitBitmapToButton = false;
  82. mMakeIconSquare = false;
  83. mAutoSize = false;
  84. mBitmapMargin = 0;
  85. setExtent(140, 30);
  86. }
  87. ImplementEnumType( GuiIconButtonTextLocation,
  88. "\n\n"
  89. "@ingroup GuiImages" )
  90. { GuiIconButtonCtrl::TextLocNone, "None" },
  91. { GuiIconButtonCtrl::TextLocBottom, "Bottom" },
  92. { GuiIconButtonCtrl::TextLocRight, "Right" },
  93. { GuiIconButtonCtrl::TextLocTop, "Top" },
  94. { GuiIconButtonCtrl::TextLocLeft, "Left" },
  95. { GuiIconButtonCtrl::TextLocCenter, "Center" },
  96. EndImplementEnumType;
  97. ImplementEnumType( GuiIconButtonIconLocation,
  98. "\n\n"
  99. "@ingroup GuiImages" )
  100. { GuiIconButtonCtrl::IconLocNone, "None" },
  101. { GuiIconButtonCtrl::IconLocLeft, "Left" },
  102. { GuiIconButtonCtrl::IconLocRight, "Right" },
  103. { GuiIconButtonCtrl::IconLocCenter, "Center" }
  104. EndImplementEnumType;
  105. void GuiIconButtonCtrl::initPersistFields()
  106. {
  107. docsURL;
  108. addField( "buttonMargin", TypePoint2I, Offset( mButtonMargin, GuiIconButtonCtrl ),"Margin area around the button.\n");
  109. addProtectedField( "iconBitmap", TypeImageFilename, Offset( mBitmapAsset, GuiIconButtonCtrl ), &_setBitmapData, &defaultProtectedGetFn, "Bitmap file for the icon to display on the button.\n", AbstractClassRep::FIELD_HideInInspectors);
  110. INITPERSISTFIELD_IMAGEASSET(Bitmap, GuiIconButtonCtrl, "Bitmap file for the icon to display on the button.\n");
  111. addField( "iconLocation", TYPEID< IconLocation >(), Offset( mIconLocation, GuiIconButtonCtrl ),"Where to place the icon on the control. Options are 0 (None), 1 (Left), 2 (Right), 3 (Center).\n");
  112. addField( "sizeIconToButton", TypeBool, Offset( mFitBitmapToButton, GuiIconButtonCtrl ),"If true, the icon will be scaled to be the same size as the button.\n");
  113. addField( "makeIconSquare", TypeBool, Offset( mMakeIconSquare, GuiIconButtonCtrl ),"If true, will make sure the icon is square.\n");
  114. addField( "textLocation", TYPEID< TextLocation >(), Offset( mTextLocation, GuiIconButtonCtrl ),"Where to place the text on the control.\n"
  115. "Options are 0 (None), 1 (Bottom), 2 (Right), 3 (Top), 4 (Left), 5 (Center).\n");
  116. addFieldV( "textMargin", TypeRangedS32, Offset( mTextMargin, GuiIconButtonCtrl ),&CommonValidators::PositiveInt,"Margin between the icon and the text.\n");
  117. addField( "autoSize", TypeBool, Offset( mAutoSize, GuiIconButtonCtrl ),"If true, the text and icon will be automatically sized to the size of the control.\n");
  118. addFieldV( "bitmapMargin", TypeRangedS32, Offset( mBitmapMargin, GuiIconButtonCtrl), &CommonValidators::PositiveInt, "Margin between the icon and the border.\n");
  119. Parent::initPersistFields();
  120. }
  121. bool GuiIconButtonCtrl::onWake()
  122. {
  123. if (! Parent::onWake())
  124. return false;
  125. setActive(true);
  126. if( mProfile )
  127. mProfile->constructBitmapArray();
  128. return true;
  129. }
  130. void GuiIconButtonCtrl::onSleep()
  131. {
  132. Parent::onSleep();
  133. }
  134. void GuiIconButtonCtrl::inspectPostApply()
  135. {
  136. Parent::inspectPostApply();
  137. }
  138. void GuiIconButtonCtrl::onStaticModified(const char* slotName, const char* newValue)
  139. {
  140. if ( isProperlyAdded() && !dStricmp(slotName, "autoSize") )
  141. resize( getPosition(), getExtent() );
  142. }
  143. bool GuiIconButtonCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  144. {
  145. if ( !mAutoSize || !mProfile->mFont )
  146. return Parent::resize( newPosition, newExtent );
  147. Point2I autoExtent( mMinExtent );
  148. if ( mIconLocation != IconLocNone )
  149. {
  150. autoExtent.y = getBitmap().getHeight() + mButtonMargin.y * 2;
  151. autoExtent.x = getBitmap().getWidth() + mButtonMargin.x * 2;
  152. }
  153. if ( mTextLocation != TextLocNone && mButtonText && mButtonText[0] )
  154. {
  155. U32 strWidth = mProfile->mFont->getStrWidthPrecise( mButtonText );
  156. if ( mTextLocation == TextLocLeft || mTextLocation == TextLocRight )
  157. {
  158. autoExtent.x += strWidth + mTextMargin * 2;
  159. }
  160. else // Top, Bottom, Center
  161. {
  162. strWidth += mTextMargin * 2;
  163. if ( strWidth > autoExtent.x )
  164. autoExtent.x = strWidth;
  165. }
  166. }
  167. return Parent::resize( newPosition, autoExtent );
  168. }
  169. void GuiIconButtonCtrl::setBitmap(const char *name)
  170. {
  171. if(!isAwake())
  172. return;
  173. _setBitmap(name);
  174. // So that extent is recalculated if autoSize is set.
  175. resize( getPosition(), getExtent() );
  176. setUpdate();
  177. }
  178. void GuiIconButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  179. {
  180. renderButton( offset, updateRect);
  181. }
  182. void GuiIconButtonCtrl::renderButton( Point2I &offset, const RectI& updateRect )
  183. {
  184. bool highlight = mHighlighted;
  185. bool depressed = mDepressed;
  186. ColorI fontColor = mActive ? (highlight ? mProfile->mFontColorHL : mProfile->mFontColor) : mProfile->mFontColorNA;
  187. ColorI borderColor = mActive ? (highlight ? mProfile->mBorderColorHL : mProfile->mBorderColor) : mProfile->mBorderColorNA;
  188. ColorI fillColor = mActive ? (highlight ? mProfile->mFillColorHL : mProfile->mFillColor) : mProfile->mFillColorNA;
  189. if (mActive && (depressed || mStateOn))
  190. {
  191. fontColor = mProfile->mFontColorSEL;
  192. fillColor = mProfile->mFillColorSEL;
  193. borderColor = mProfile->mBorderColorSEL;
  194. }
  195. RectI boundsRect(offset, getExtent());
  196. GFXDrawUtil *drawer = GFX->getDrawUtil();
  197. if (mDepressed || mStateOn)
  198. {
  199. // If there is a bitmap array then render using it.
  200. // Otherwise use a standard fill.
  201. if(mProfile->mUseBitmapArray && !mProfile->mBitmapArrayRects.empty())
  202. renderBitmapArray(boundsRect, statePressed);
  203. else
  204. {
  205. if (mProfile->mBorder != 0)
  206. renderFilledBorder(boundsRect, borderColor, fillColor, mProfile->mBorderThickness);
  207. else
  208. GFX->getDrawUtil()->drawRectFill(boundsRect, fillColor);
  209. }
  210. }
  211. else if(mHighlighted && mActive)
  212. {
  213. // If there is a bitmap array then render using it.
  214. // Otherwise use a standard fill.
  215. if (mProfile->mUseBitmapArray && !mProfile->mBitmapArrayRects.empty())
  216. {
  217. renderBitmapArray(boundsRect, stateMouseOver);
  218. }
  219. else
  220. {
  221. if (mProfile->mBorder != 0)
  222. renderFilledBorder(boundsRect, borderColor, fillColor, mProfile->mBorderThickness);
  223. else
  224. GFX->getDrawUtil()->drawRectFill(boundsRect, fillColor);
  225. }
  226. }
  227. else
  228. {
  229. // If there is a bitmap array then render using it.
  230. // Otherwise use a standard fill.
  231. if(mProfile->mUseBitmapArray && !mProfile->mBitmapArrayRects.empty())
  232. {
  233. if(mActive)
  234. renderBitmapArray(boundsRect, stateNormal);
  235. else
  236. renderBitmapArray(boundsRect, stateDisabled);
  237. }
  238. else
  239. {
  240. if (mProfile->mBorder != 0)
  241. renderFilledBorder(boundsRect, borderColor, fillColor, mProfile->mBorderThickness);
  242. else
  243. GFX->getDrawUtil()->drawRectFill(boundsRect, mProfile->mFillColor);
  244. }
  245. }
  246. Point2I textPos = offset;
  247. if(depressed)
  248. textPos += Point2I(1,1);
  249. RectI iconRect( 0, 0, 0, 0 );
  250. // Render the icon
  251. if ( mBitmapAsset.notNull() && mIconLocation != GuiIconButtonCtrl::IconLocNone)
  252. {
  253. // Render the normal bitmap
  254. drawer->clearBitmapModulation();
  255. // Size of the bitmap
  256. Point2I textureSize(getBitmap()->getWidth(), getBitmap()->getHeight());
  257. // Reduce the size with the margin (if set)
  258. textureSize.x = textureSize.x - (mBitmapMargin * 2);
  259. textureSize.y = textureSize.y - (mBitmapMargin * 2);
  260. // Maintain the bitmap size or fill the button?
  261. if ( !mFitBitmapToButton )
  262. {
  263. iconRect.set( offset + mButtonMargin, textureSize );
  264. if ( mIconLocation == IconLocRight )
  265. {
  266. iconRect.point.x = ( offset.x + getWidth() ) - ( mButtonMargin.x + textureSize.x );
  267. iconRect.point.y = offset.y + ( getHeight() - textureSize.y ) / 2;
  268. }
  269. else if ( mIconLocation == IconLocLeft )
  270. {
  271. iconRect.point.x = offset.x + mButtonMargin.x;
  272. iconRect.point.y = offset.y + ( getHeight() - textureSize.y ) / 2;
  273. }
  274. else if ( mIconLocation == IconLocCenter )
  275. {
  276. iconRect.point.x = offset.x + ( getWidth() - textureSize.x ) / 2;
  277. iconRect.point.y = offset.y + ( getHeight() - textureSize.y ) / 2;
  278. }
  279. drawer->drawBitmapStretch(getBitmap(), iconRect );
  280. }
  281. else
  282. {
  283. // adding offset with the bitmap margin next to the button margin
  284. Point2I bitMapOffset(mBitmapMargin, mBitmapMargin);
  285. // set the offset
  286. iconRect.set( offset + mButtonMargin + bitMapOffset, getExtent() - (Point2I(mAbs(mButtonMargin.x - (mBitmapMargin * 2)), mAbs(mButtonMargin.y - (mBitmapMargin * 2))) * 2) );
  287. if ( mMakeIconSquare )
  288. {
  289. // Square the icon to the smaller axis extent.
  290. if ( iconRect.extent.x < iconRect.extent.y )
  291. iconRect.extent.y = iconRect.extent.x;
  292. else
  293. iconRect.extent.x = iconRect.extent.y;
  294. }
  295. if (mIconLocation == IconLocRight)
  296. {
  297. iconRect.point.x = (offset.x + getWidth()) - iconRect.extent.x + mButtonMargin.x;
  298. }
  299. else if (mIconLocation == IconLocLeft)
  300. {
  301. //default state presumes left positioning
  302. }
  303. else if (mIconLocation == IconLocCenter)
  304. {
  305. iconRect.point.x = offset.x + (getWidth() / 2) - (iconRect.extent.x / 2) + mButtonMargin.x;
  306. iconRect.point.y = offset.y + (getHeight() / 2) - (iconRect.extent.y / 2) + mButtonMargin.y;
  307. }
  308. drawer->drawBitmapStretch(getBitmap(), iconRect );
  309. }
  310. }
  311. // Render text
  312. if ( mTextLocation != TextLocNone )
  313. {
  314. // Clip text to fit (appends ...),
  315. // pad some space to keep it off our border
  316. String text( mButtonText );
  317. S32 textWidth = clipText( text, getWidth() - 4 - mTextMargin );
  318. drawer->setBitmapModulation( fontColor );
  319. if ( mTextLocation == TextLocRight )
  320. {
  321. Point2I start( mTextMargin, ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  322. if (mBitmapAsset.notNull() && mIconLocation != IconLocNone)
  323. {
  324. start.x = getWidth() - (iconRect.extent.x + mButtonMargin.x + textWidth);
  325. }
  326. drawer->setBitmapModulation(fontColor);
  327. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  328. }
  329. if ( mTextLocation == TextLocLeft )
  330. {
  331. Point2I start( mTextMargin, ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  332. drawer->setBitmapModulation(fontColor);
  333. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  334. }
  335. if ( mTextLocation == TextLocCenter )
  336. {
  337. Point2I start;
  338. if (mBitmapAsset.notNull() && mIconLocation == IconLocLeft )
  339. {
  340. start.set( ( getWidth() - textWidth - iconRect.extent.x ) / 2 + iconRect.extent.x,
  341. ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  342. }
  343. else
  344. start.set( ( getWidth() - textWidth ) / 2, ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  345. drawer->setBitmapModulation( fontColor );
  346. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  347. }
  348. if ( mTextLocation == TextLocBottom )
  349. {
  350. Point2I start;
  351. start.set( ( getWidth() - textWidth ) / 2, getHeight() - mProfile->mFont->getHeight() - mTextMargin );
  352. // If the text is longer then the box size
  353. // it will get clipped, force Left Justify
  354. if( textWidth > getWidth() )
  355. start.x = 0;
  356. drawer->setBitmapModulation( fontColor );
  357. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  358. }
  359. }
  360. renderChildControls( offset, updateRect);
  361. }
  362. // Draw the bitmap array's borders according to the button's state.
  363. void GuiIconButtonCtrl::renderBitmapArray(RectI &bounds, S32 state)
  364. {
  365. switch(state)
  366. {
  367. case stateNormal:
  368. if(mProfile->mBorder == -2)
  369. renderSizableBitmapBordersFilled(bounds, 1, mProfile);
  370. else
  371. renderFixedBitmapBordersFilled(bounds, 1, mProfile);
  372. break;
  373. case stateMouseOver:
  374. if(mProfile->mBorder == -2)
  375. renderSizableBitmapBordersFilled(bounds, 2, mProfile);
  376. else
  377. renderFixedBitmapBordersFilled(bounds, 2, mProfile);
  378. break;
  379. case statePressed:
  380. if(mProfile->mBorder == -2)
  381. renderSizableBitmapBordersFilled(bounds, 3, mProfile);
  382. else
  383. renderFixedBitmapBordersFilled(bounds, 3, mProfile);
  384. break;
  385. case stateDisabled:
  386. if(mProfile->mBorder == -2)
  387. renderSizableBitmapBordersFilled(bounds, 4, mProfile);
  388. else
  389. renderFixedBitmapBordersFilled(bounds, 4, mProfile);
  390. break;
  391. }
  392. }
  393. DEF_ASSET_BINDS_REFACTOR(GuiIconButtonCtrl, Bitmap)