guiIconButtonCtrl.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. " autoSize = \"0\";\n"
  64. " text = \"Lag Icon\";\n"
  65. " textID = \"\"STR_LAG\"\";\n"
  66. " buttonType = \"PushButton\";\n"
  67. " profile = \"GuiIconButtonProfile\";\n"
  68. "};\n"
  69. "@endtsexample\n\n"
  70. "@see GuiControl\n"
  71. "@see GuiButtonCtrl\n\n"
  72. "@ingroup GuiCore\n"
  73. );
  74. GuiIconButtonCtrl::GuiIconButtonCtrl()
  75. {
  76. INIT_ASSET(Bitmap);
  77. mTextLocation = TextLocLeft;
  78. mIconLocation = IconLocLeft;
  79. mTextMargin = 4;
  80. mButtonMargin.set(4,4);
  81. mFitBitmapToButton = false;
  82. mMakeIconSquare = false;
  83. mAutoSize = false;
  84. setExtent(140, 30);
  85. }
  86. ImplementEnumType( GuiIconButtonTextLocation,
  87. "\n\n"
  88. "@ingroup GuiImages" )
  89. { GuiIconButtonCtrl::TextLocNone, "None" },
  90. { GuiIconButtonCtrl::TextLocBottom, "Bottom" },
  91. { GuiIconButtonCtrl::TextLocRight, "Right" },
  92. { GuiIconButtonCtrl::TextLocTop, "Top" },
  93. { GuiIconButtonCtrl::TextLocLeft, "Left" },
  94. { GuiIconButtonCtrl::TextLocCenter, "Center" },
  95. EndImplementEnumType;
  96. ImplementEnumType( GuiIconButtonIconLocation,
  97. "\n\n"
  98. "@ingroup GuiImages" )
  99. { GuiIconButtonCtrl::IconLocNone, "None" },
  100. { GuiIconButtonCtrl::IconLocLeft, "Left" },
  101. { GuiIconButtonCtrl::IconLocRight, "Right" },
  102. { GuiIconButtonCtrl::IconLocCenter, "Center" }
  103. EndImplementEnumType;
  104. void GuiIconButtonCtrl::initPersistFields()
  105. {
  106. docsURL;
  107. addField( "buttonMargin", TypePoint2I, Offset( mButtonMargin, GuiIconButtonCtrl ),"Margin area around the button.\n");
  108. addProtectedField( "iconBitmap", TypeImageFilename, Offset( mBitmapName, GuiIconButtonCtrl ), &_setBitmapData, &defaultProtectedGetFn, "Bitmap file for the icon to display on the button.\n", AbstractClassRep::FIELD_HideInInspectors);
  109. INITPERSISTFIELD_IMAGEASSET(Bitmap, GuiIconButtonCtrl, "Bitmap file for the icon to display on the button.\n");
  110. 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");
  111. addField( "sizeIconToButton", TypeBool, Offset( mFitBitmapToButton, GuiIconButtonCtrl ),"If true, the icon will be scaled to be the same size as the button.\n");
  112. addField( "makeIconSquare", TypeBool, Offset( mMakeIconSquare, GuiIconButtonCtrl ),"If true, will make sure the icon is square.\n");
  113. addField( "textLocation", TYPEID< TextLocation >(), Offset( mTextLocation, GuiIconButtonCtrl ),"Where to place the text on the control.\n"
  114. "Options are 0 (None), 1 (Bottom), 2 (Right), 3 (Top), 4 (Left), 5 (Center).\n");
  115. addField( "textMargin", TypeS32, Offset( mTextMargin, GuiIconButtonCtrl ),"Margin between the icon and the text.\n");
  116. addField( "autoSize", TypeBool, Offset( mAutoSize, GuiIconButtonCtrl ),"If true, the text and icon will be automatically sized to the size of the control.\n");
  117. Parent::initPersistFields();
  118. }
  119. bool GuiIconButtonCtrl::onWake()
  120. {
  121. if (! Parent::onWake())
  122. return false;
  123. setActive(true);
  124. setBitmap(mBitmapName);
  125. if( mProfile )
  126. mProfile->constructBitmapArray();
  127. return true;
  128. }
  129. void GuiIconButtonCtrl::onSleep()
  130. {
  131. Parent::onSleep();
  132. }
  133. void GuiIconButtonCtrl::inspectPostApply()
  134. {
  135. Parent::inspectPostApply();
  136. }
  137. void GuiIconButtonCtrl::onStaticModified(const char* slotName, const char* newValue)
  138. {
  139. if ( isProperlyAdded() && !dStricmp(slotName, "autoSize") )
  140. resize( getPosition(), getExtent() );
  141. }
  142. bool GuiIconButtonCtrl::resize(const Point2I &newPosition, const Point2I &newExtent)
  143. {
  144. if ( !mAutoSize || !mProfile->mFont )
  145. return Parent::resize( newPosition, newExtent );
  146. Point2I autoExtent( mMinExtent );
  147. if ( mIconLocation != IconLocNone )
  148. {
  149. autoExtent.y = mBitmap.getHeight() + mButtonMargin.y * 2;
  150. autoExtent.x = mBitmap.getWidth() + mButtonMargin.x * 2;
  151. }
  152. if ( mTextLocation != TextLocNone && mButtonText && mButtonText[0] )
  153. {
  154. U32 strWidth = mProfile->mFont->getStrWidthPrecise( mButtonText );
  155. if ( mTextLocation == TextLocLeft || mTextLocation == TextLocRight )
  156. {
  157. autoExtent.x += strWidth + mTextMargin * 2;
  158. }
  159. else // Top, Bottom, Center
  160. {
  161. strWidth += mTextMargin * 2;
  162. if ( strWidth > autoExtent.x )
  163. autoExtent.x = strWidth;
  164. }
  165. }
  166. return Parent::resize( newPosition, autoExtent );
  167. }
  168. void GuiIconButtonCtrl::setBitmap(const char *name)
  169. {
  170. if(!isAwake())
  171. return;
  172. _setBitmap(getBitmap());
  173. // So that extent is recalculated if autoSize is set.
  174. resize( getPosition(), getExtent() );
  175. setUpdate();
  176. }
  177. void GuiIconButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  178. {
  179. renderButton( offset, updateRect);
  180. }
  181. void GuiIconButtonCtrl::renderButton( Point2I &offset, const RectI& updateRect )
  182. {
  183. bool highlight = mHighlighted;
  184. bool depressed = mDepressed;
  185. ColorI fontColor = mActive ? (highlight ? mProfile->mFontColorHL : mProfile->mFontColor) : mProfile->mFontColorNA;
  186. RectI boundsRect(offset, getExtent());
  187. GFXDrawUtil *drawer = GFX->getDrawUtil();
  188. if (mDepressed || mStateOn)
  189. {
  190. // If there is a bitmap array then render using it.
  191. // Otherwise use a standard fill.
  192. if(mProfile->mUseBitmapArray && !mProfile->mBitmapArrayRects.empty())
  193. renderBitmapArray(boundsRect, statePressed);
  194. else
  195. renderSlightlyLoweredBox(boundsRect, mProfile);
  196. }
  197. else if(mHighlighted && mActive)
  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. {
  203. renderBitmapArray(boundsRect, stateMouseOver);
  204. }
  205. else
  206. {
  207. if (mProfile->mBorder != 0)
  208. renderFilledBorder(boundsRect, mProfile->mBorderColorHL, mProfile->mFillColorHL, mProfile->mBorderThickness);
  209. else
  210. GFX->getDrawUtil()->drawRectFill(boundsRect, mProfile->mFillColorHL);
  211. }
  212. }
  213. else
  214. {
  215. // If there is a bitmap array then render using it.
  216. // Otherwise use a standard fill.
  217. if(mProfile->mUseBitmapArray && !mProfile->mBitmapArrayRects.empty())
  218. {
  219. if(mActive)
  220. renderBitmapArray(boundsRect, stateNormal);
  221. else
  222. renderBitmapArray(boundsRect, stateDisabled);
  223. }
  224. else
  225. {
  226. if (mActive)
  227. {
  228. if (mProfile->mBorder != 0)
  229. renderFilledBorder(boundsRect, mProfile->mBorderColor, mProfile->mFillColor, mProfile->mBorderThickness);
  230. else
  231. GFX->getDrawUtil()->drawRectFill(boundsRect, mProfile->mFillColor);
  232. }
  233. else
  234. {
  235. if (mProfile->mBorder != 0)
  236. renderFilledBorder(boundsRect, mProfile->mBorderColorNA, mProfile->mFillColorNA, mProfile->mBorderThickness);
  237. else
  238. GFX->getDrawUtil()->drawRectFill(boundsRect, mProfile->mFillColor);
  239. }
  240. }
  241. }
  242. Point2I textPos = offset;
  243. if(depressed)
  244. textPos += Point2I(1,1);
  245. RectI iconRect( 0, 0, 0, 0 );
  246. // Render the icon
  247. if ( mBitmap && mIconLocation != GuiIconButtonCtrl::IconLocNone )
  248. {
  249. // Render the normal bitmap
  250. drawer->clearBitmapModulation();
  251. // Maintain the bitmap size or fill the button?
  252. if ( !mFitBitmapToButton )
  253. {
  254. Point2I textureSize(mBitmap->getWidth(), mBitmap->getHeight() );
  255. iconRect.set( offset + mButtonMargin, textureSize );
  256. if ( mIconLocation == IconLocRight )
  257. {
  258. iconRect.point.x = ( offset.x + getWidth() ) - ( mButtonMargin.x + textureSize.x );
  259. iconRect.point.y = offset.y + ( getHeight() - textureSize.y ) / 2;
  260. }
  261. else if ( mIconLocation == IconLocLeft )
  262. {
  263. iconRect.point.x = offset.x + mButtonMargin.x;
  264. iconRect.point.y = offset.y + ( getHeight() - textureSize.y ) / 2;
  265. }
  266. else if ( mIconLocation == IconLocCenter )
  267. {
  268. iconRect.point.x = offset.x + ( getWidth() - textureSize.x ) / 2;
  269. iconRect.point.y = offset.y + ( getHeight() - textureSize.y ) / 2;
  270. }
  271. drawer->drawBitmapStretch(mBitmap, iconRect );
  272. }
  273. else
  274. {
  275. iconRect.set( offset + mButtonMargin, getExtent() - (Point2I(mAbs(mButtonMargin.x), mAbs(mButtonMargin.y)) * 2) );
  276. if ( mMakeIconSquare )
  277. {
  278. // Square the icon to the smaller axis extent.
  279. if ( iconRect.extent.x < iconRect.extent.y )
  280. iconRect.extent.y = iconRect.extent.x;
  281. else
  282. iconRect.extent.x = iconRect.extent.y;
  283. }
  284. if (mIconLocation == IconLocRight)
  285. {
  286. iconRect.point.x = (offset.x + getWidth()) - iconRect.extent.x + mButtonMargin.x;
  287. }
  288. else if (mIconLocation == IconLocLeft)
  289. {
  290. //default state presumes left positioning
  291. }
  292. else if (mIconLocation == IconLocCenter)
  293. {
  294. iconRect.point.x = offset.x + (getWidth() / 2) - (iconRect.extent.x / 2) + mButtonMargin.x;
  295. iconRect.point.y = offset.y + (getHeight() / 2) - (iconRect.extent.y / 2) + mButtonMargin.y;
  296. }
  297. drawer->drawBitmapStretch( mBitmap, iconRect );
  298. }
  299. }
  300. // Render text
  301. if ( mTextLocation != TextLocNone )
  302. {
  303. // Clip text to fit (appends ...),
  304. // pad some space to keep it off our border
  305. String text( mButtonText );
  306. S32 textWidth = clipText( text, getWidth() - 4 - mTextMargin );
  307. drawer->setBitmapModulation( fontColor );
  308. if ( mTextLocation == TextLocRight )
  309. {
  310. Point2I start( mTextMargin, ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  311. if (mBitmap && mIconLocation != IconLocNone )
  312. {
  313. start.x = iconRect.extent.x + mButtonMargin.x + mTextMargin;
  314. }
  315. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  316. }
  317. if ( mTextLocation == TextLocLeft )
  318. {
  319. Point2I start( mTextMargin, ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  320. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  321. }
  322. if ( mTextLocation == TextLocCenter )
  323. {
  324. Point2I start;
  325. if (mBitmap && mIconLocation == IconLocLeft )
  326. {
  327. start.set( ( getWidth() - textWidth - iconRect.extent.x ) / 2 + iconRect.extent.x,
  328. ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  329. }
  330. else
  331. start.set( ( getWidth() - textWidth ) / 2, ( getHeight() - mProfile->mFont->getHeight() ) / 2 );
  332. drawer->setBitmapModulation( fontColor );
  333. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  334. }
  335. if ( mTextLocation == TextLocBottom )
  336. {
  337. Point2I start;
  338. start.set( ( getWidth() - textWidth ) / 2, getHeight() - mProfile->mFont->getHeight() - mTextMargin );
  339. // If the text is longer then the box size
  340. // it will get clipped, force Left Justify
  341. if( textWidth > getWidth() )
  342. start.x = 0;
  343. drawer->setBitmapModulation( fontColor );
  344. drawer->drawText( mProfile->mFont, start + offset, text, mProfile->mFontColors );
  345. }
  346. }
  347. renderChildControls( offset, updateRect);
  348. }
  349. // Draw the bitmap array's borders according to the button's state.
  350. void GuiIconButtonCtrl::renderBitmapArray(RectI &bounds, S32 state)
  351. {
  352. switch(state)
  353. {
  354. case stateNormal:
  355. if(mProfile->mBorder == -2)
  356. renderSizableBitmapBordersFilled(bounds, 1, mProfile);
  357. else
  358. renderFixedBitmapBordersFilled(bounds, 1, mProfile);
  359. break;
  360. case stateMouseOver:
  361. if(mProfile->mBorder == -2)
  362. renderSizableBitmapBordersFilled(bounds, 2, mProfile);
  363. else
  364. renderFixedBitmapBordersFilled(bounds, 2, mProfile);
  365. break;
  366. case statePressed:
  367. if(mProfile->mBorder == -2)
  368. renderSizableBitmapBordersFilled(bounds, 3, mProfile);
  369. else
  370. renderFixedBitmapBordersFilled(bounds, 3, mProfile);
  371. break;
  372. case stateDisabled:
  373. if(mProfile->mBorder == -2)
  374. renderSizableBitmapBordersFilled(bounds, 4, mProfile);
  375. else
  376. renderFixedBitmapBordersFilled(bounds, 4, mProfile);
  377. break;
  378. }
  379. }
  380. DEF_ASSET_BINDS(GuiIconButtonCtrl, Bitmap);