2
0

guiBitmapButtonCtrl.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 "platform/platform.h"
  23. #include "gui/buttons/guiBitmapButtonCtrl.h"
  24. #include "core/util/path.h"
  25. #include "console/console.h"
  26. #include "console/consoleTypes.h"
  27. #include "console/engineAPI.h"
  28. #include "gui/core/guiCanvas.h"
  29. #include "gui/core/guiDefaultControlRender.h"
  30. #include "gfx/gfxDrawUtil.h"
  31. #include "gfx/gfxTextureManager.h"
  32. #include "gui/editor/inspector/group.h"
  33. #include "gui/editor/inspector/field.h"
  34. #include "gui/editor/guiInspector.h"
  35. ImplementEnumType( GuiBitmapMode,
  36. "Rendering behavior when placing bitmaps in controls.\n\n"
  37. "@ingroup GuiImages" )
  38. { GuiBitmapButtonCtrl::BitmapStretched, "Stretched", "Stretch bitmap to fit control extents." },
  39. { GuiBitmapButtonCtrl::BitmapCentered, "Centered", "Center bitmap in control." },
  40. EndImplementEnumType;
  41. //=============================================================================
  42. // GuiBitmapButtonCtrl
  43. //=============================================================================
  44. IMPLEMENT_CONOBJECT(GuiBitmapButtonCtrl);
  45. ConsoleDocClass( GuiBitmapButtonCtrl,
  46. "@brief A button that renders its various states (mouse over, pushed, etc.) from separate bitmaps.\n\n"
  47. "A bitmapped button is a push button that uses one or more texture images for rendering its individual states.\n\n"
  48. "To find the individual textures associated with the button, a naming scheme is used. For each state "
  49. "a suffix is appended to the texture file name given in the GuiBitmapButtonCtrl::bitmap field:\n"
  50. "- \"_n\": Normal state. This one will be active when no other state applies.\n"
  51. "- \"_h\": Highlighted state. This applies when the mouse is hovering over the button.\n"
  52. "- \"_d\": Depressed state. This applies when the left mouse button has been clicked on the button but not yet released.\n"
  53. "- \"_i\": Inactive state. This applies when the button control has been deactivated (GuiControl::setActive())\n\n"
  54. "If a bitmap for a particular state cannot be found, the default bitmap will be used. To disable all state-based "
  55. "bitmap functionality, set useStates to false which will make the control solely render from the bitmap specified "
  56. "in the bitmap field.\n\n"
  57. "@section guibitmapbutton_modifiers Per-Modifier Button Actions\n"
  58. "If GuiBitmapButtonCtrl::useModifiers is set to true, per-modifier button actions and textures are enabled. This functionality "
  59. "allows to associate different images and different actions with a button depending on which modifiers are pressed "
  60. "on the keyboard by the user.\n\n"
  61. "When enabled, this functionality alters the texture lookup above by prepending the following strings to the "
  62. "suffixes listed above:\n"
  63. "- \"\": Default. No modifier is pressed.\n"
  64. "- \"_ctrl\": Image to use when CTRL/CMD is down.\n"
  65. "- \"_alt\": Image to use when ALT is down.\n"
  66. "- \"_shift\": Image to use when SHIFT is down\n\n"
  67. "When this functionality is enabled, a new set of callbacks is used:\n"
  68. "- onDefaultClick: Button was clicked without a modifier being presssed.\n"
  69. "- onCtrlClick: Button was clicked with the CTRL/CMD key down.\n"
  70. "- onAltClick: Button was clicked with the ALT key down.\n"
  71. "- onShiftClick: Button was clicked with the SHIFT key down.\n\n"
  72. "GuiControl::command or GuiControl::onAction() still work as before when per-modifier functionality is enabled.\n\n"
  73. "Note that modifiers cannot be mixed. If two or more modifiers are pressed, a single one will take precedence over "
  74. "the remaining modifiers. The order of precedence corresponds to the order listed above.\n\n"
  75. "@tsexample\n"
  76. "// Create an OK button that will trigger an onOk() call on its parent when clicked:\n"
  77. "%okButton = new GuiBitmapButtonCtrl()\n"
  78. "{\n"
  79. " bitmap = \"art/gui/okButton\";\n"
  80. " autoFitExtents = true;\n"
  81. " command = \"$ThisControl.getParent().onOk();\";\n"
  82. "};\n"
  83. "@endtsexample\n\n"
  84. "@ingroup GuiButtons"
  85. );
  86. IMPLEMENT_CALLBACK( GuiBitmapButtonCtrl, onDefaultClick, void, (), (),
  87. "Called when per-modifier functionality is enabled and the user clicks on the button without any modifier pressed.\n"
  88. "@ref guibitmapbutton_modifiers" );
  89. IMPLEMENT_CALLBACK( GuiBitmapButtonCtrl, onCtrlClick, void, (), (),
  90. "Called when per-modifier functionality is enabled and the user clicks on the button with the CTRL key pressed.\n"
  91. "@ref guibitmapbutton_modifiers" );
  92. IMPLEMENT_CALLBACK( GuiBitmapButtonCtrl, onAltClick, void, (), (),
  93. "Called when per-modifier functionality is enabled and the user clicks on the button with the ALT key pressed.\n"
  94. "@ref guibitmapbutton_modifiers" );
  95. IMPLEMENT_CALLBACK( GuiBitmapButtonCtrl, onShiftClick, void, (), (),
  96. "Called when per-modifier functionality is enabled and the user clicks on the button with the SHIFT key pressed.\n"
  97. "@ref guibitmapbutton_modifiers" );
  98. //-----------------------------------------------------------------------------
  99. GuiBitmapButtonCtrl::GuiBitmapButtonCtrl()
  100. {
  101. mBitmapMode = BitmapStretched;
  102. mAutoFitExtents = false;
  103. mUseModifiers = false;
  104. mUseStates = true;
  105. setExtent( 140, 30 );
  106. mMasked = false;
  107. mColor = ColorI::WHITE;
  108. mBitmapName = StringTable->EmptyString();
  109. mBitmap = NULL;
  110. mBitmapAsset.registerRefreshNotify(this);
  111. mBitmapFile = String::EmptyString;
  112. }
  113. //-----------------------------------------------------------------------------
  114. void GuiBitmapButtonCtrl::initPersistFields()
  115. {
  116. docsURL;
  117. addGroup( "Bitmap" );
  118. INITPERSISTFIELD_IMAGEASSET(Bitmap, GuiBitmapButtonCtrl,"Texture file to display on this button.\n"
  119. "If useStates is false, this will be the file that renders on the control. Otherwise, this will "
  120. "specify the default texture name to which the various state and modifier suffixes are appended "
  121. "to find the per-state and per-modifier (if enabled) textures.")
  122. addField("color", TypeColorI, Offset(mColor, GuiBitmapButtonCtrl), "color mul");
  123. addField( "bitmapMode", TYPEID< BitmapMode >(), Offset( mBitmapMode, GuiBitmapButtonCtrl ),
  124. "Behavior for fitting the bitmap to the control extents.\n"
  125. "If set to 'Stretched', the bitmap will be stretched both verticall and horizontally to fit inside "
  126. "the control's extents.\n\n"
  127. "If set to 'Centered', the bitmap will stay at its original resolution centered in the control's "
  128. "rectangle (getting clipped if the control is smaller than the texture)." );
  129. addProtectedField( "autoFitExtents", TypeBool, Offset( mAutoFitExtents, GuiBitmapButtonCtrl ),
  130. &_setAutoFitExtents, &defaultProtectedGetFn,
  131. "If true, the control's extents will be set to match the bitmap's extents when setting the bitmap.\n"
  132. "The bitmap extents will always be taken from the default/normal bitmap (in case the extents of the various "
  133. "bitmaps do not match up.)" );
  134. addField( "useModifiers", TypeBool, Offset( mUseModifiers, GuiBitmapButtonCtrl ),
  135. "If true, per-modifier button functionality is enabled.\n"
  136. "@ref guibitmapbutton_modifiers" );
  137. addField( "useStates", TypeBool, Offset( mUseStates, GuiBitmapButtonCtrl ),
  138. "If true, per-mouse state button functionality is enabled.\n"
  139. "Defaults to true.\n\n"
  140. "If you do not use per-state images on this button set this to false to speed up the loading process "
  141. "by inhibiting searches for the individual images." );
  142. addField("masked", TypeBool, Offset(mMasked, GuiBitmapButtonCtrl),"Use alpha masking for interaction.");
  143. endGroup( "Bitmap" );
  144. Parent::initPersistFields();
  145. }
  146. //-----------------------------------------------------------------------------
  147. bool GuiBitmapButtonCtrl::onWake()
  148. {
  149. if (! Parent::onWake())
  150. return false;
  151. setActive( true );
  152. setBitmap( mBitmapName );
  153. return true;
  154. }
  155. //-----------------------------------------------------------------------------
  156. void GuiBitmapButtonCtrl::onSleep()
  157. {
  158. if( dStricmp(mBitmapName, "texhandle") != 0 )
  159. for( U32 i = 0; i < NumModifiers; ++ i )
  160. {
  161. mTextures[ i ].mTextureNormal = NULL;
  162. mTextures[ i ].mTextureHilight = NULL;
  163. mTextures[ i ].mTextureDepressed = NULL;
  164. mTextures[ i ].mTextureInactive = NULL;
  165. }
  166. if (mBitmapAsset.notNull())
  167. mBitmap = NULL;
  168. Parent::onSleep();
  169. }
  170. //-----------------------------------------------------------------------------
  171. bool GuiBitmapButtonCtrl::_setAutoFitExtents( void *object, const char *index, const char *data )
  172. {
  173. GuiBitmapButtonCtrl* ctrl = reinterpret_cast< GuiBitmapButtonCtrl* >( object );
  174. ctrl->setAutoFitExtents( dAtob( data ) );
  175. return false;
  176. }
  177. //-----------------------------------------------------------------------------
  178. /*bool GuiBitmapButtonCtrl::_setBitmap(void* object, const char* index, const char* data)
  179. {
  180. GuiBitmapButtonCtrl* ctrl = reinterpret_cast< GuiBitmapButtonCtrl* >( object );
  181. ctrl->setBitmap( StringTable->insert(data) );
  182. return false;
  183. }*/
  184. //-----------------------------------------------------------------------------
  185. // Legacy method. Can just assign to bitmap field.
  186. /*DefineEngineMethod(GuiBitmapButtonCtrl, setBitmap, void, (const char* path), ,
  187. "Set the bitmap to show on the button.\n"
  188. "@param path Path to the texture file in any of the supported formats.\n" )
  189. {
  190. object->setBitmap( StringTable->insert(path) );
  191. }*/
  192. //-----------------------------------------------------------------------------
  193. void GuiBitmapButtonCtrl::inspectPostApply()
  194. {
  195. Parent::inspectPostApply();
  196. setBitmap(mBitmapName);
  197. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  198. // set it's extent to be exactly the size of the normal bitmap (if present)
  199. if ((getWidth() == 0) && (getHeight() == 0) && mTextures[ 0 ].mTextureNormal)
  200. {
  201. setExtent( mTextures[ 0 ].mTextureNormal->getWidth(), mTextures[ 0 ].mTextureNormal->getHeight());
  202. }
  203. }
  204. //-----------------------------------------------------------------------------
  205. void GuiBitmapButtonCtrl::setAutoFitExtents( bool state )
  206. {
  207. mAutoFitExtents = state;
  208. if( mAutoFitExtents )
  209. setBitmap( mBitmapName );
  210. }
  211. //-----------------------------------------------------------------------------
  212. void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
  213. {
  214. PROFILE_SCOPE( GuiBitmapButtonCtrl_setBitmap );
  215. _setBitmap(name);
  216. if( !isAwake() )
  217. return;
  218. if( mBitmapAsset.notNull())
  219. {
  220. if( dStricmp( mBitmapName, "texhandle" ) != 0 )
  221. {
  222. const U32 count = mUseModifiers ? NumModifiers : 1;
  223. for( U32 i = 0; i < count; ++ i )
  224. {
  225. static String modifiers[] =
  226. {
  227. "",
  228. "_ctrl",
  229. "_alt",
  230. "_shift"
  231. };
  232. static String s_n[2] = { "_n", "_n_image" };
  233. static String s_d[2] = { "_d", "_d_image" };
  234. static String s_h[2] = { "_h", "_h_image" };
  235. static String s_i[2] = { "_i", "_i_image" };
  236. String baseName = mBitmapAsset.getAssetId();
  237. //strip any pre-assigned suffix, just in case
  238. baseName = baseName.replace("_n_image", "");
  239. baseName = baseName.replace("_n", "");
  240. if( mUseModifiers )
  241. baseName += modifiers[ i ];
  242. mTextures[i].mTextureNormal = getBitmap();
  243. if( mUseStates )
  244. {
  245. //normal lookup
  246. StringTableEntry lookupName;
  247. for (U32 s = 0; s < 2; s++)
  248. {
  249. if (!mTextures[i].mTextureNormal)
  250. {
  251. lookupName = StringTable->insert(String(baseName + s_n[s]).c_str());
  252. if (AssetDatabase.isDeclaredAsset(lookupName))
  253. {
  254. mTextures[i].mTextureNormalAssetId = lookupName;
  255. mTextures[i].mTextureNormalAsset = mTextures[i].mTextureNormalAssetId;
  256. }
  257. if (mTextures[i].mTextureNormalAsset.notNull())
  258. {
  259. mTextures[i].mTextureNormalAsset->load();
  260. if (mTextures[i].mTextureNormalAsset->getStatus() == AssetBase::Ok)
  261. {
  262. mTextures[i].mTextureNormal = mTextures[i].mTextureNormalAsset->getTexture(&GFXDefaultGUIProfile);
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. //Hilight lookup
  269. for (U32 s = 0; s < 2; s++)
  270. {
  271. lookupName = StringTable->insert(String(baseName + s_h[s]).c_str());
  272. if (AssetDatabase.isDeclaredAsset(lookupName))
  273. {
  274. mTextures[i].mTextureHilightAssetId = lookupName;
  275. mTextures[i].mTextureHilightAsset = mTextures[i].mTextureHilightAssetId;
  276. }
  277. if (mTextures[i].mTextureHilightAsset.notNull())
  278. {
  279. mTextures[i].mTextureHilightAsset->load();
  280. if (mTextures[i].mTextureHilightAsset->getStatus() == AssetBase::Ok)
  281. {
  282. mTextures[i].mTextureHilight = mTextures[i].mTextureHilightAsset->getTexture(&GFXDefaultGUIProfile);
  283. break;
  284. }
  285. }
  286. }
  287. if( !mTextures[ i ].mTextureHilight )
  288. mTextures[ i ].mTextureHilight = mTextures[ i ].mTextureNormal;
  289. //Depressed lookup
  290. for (U32 s = 0; s < 2; s++)
  291. {
  292. lookupName = StringTable->insert(String(baseName + s_d[s]).c_str());
  293. if (AssetDatabase.isDeclaredAsset(lookupName))
  294. {
  295. mTextures[i].mTextureDepressedAssetId = lookupName;
  296. mTextures[i].mTextureDepressedAsset = mTextures[i].mTextureDepressedAssetId;
  297. }
  298. if (mTextures[i].mTextureDepressedAsset.notNull())
  299. {
  300. mTextures[i].mTextureDepressedAsset->load();
  301. if (mTextures[i].mTextureDepressedAsset->getStatus() == AssetBase::Ok)
  302. {
  303. mTextures[i].mTextureDepressed = mTextures[i].mTextureDepressedAsset->getTexture(&GFXDefaultGUIProfile);
  304. break;
  305. }
  306. }
  307. }
  308. if( !mTextures[ i ].mTextureDepressed )
  309. mTextures[ i ].mTextureDepressed = mTextures[ i ].mTextureHilight;
  310. //Depressed lookup
  311. for (U32 s = 0; s < 2; s++)
  312. {
  313. lookupName = StringTable->insert(String(baseName + s_i[s]).c_str());
  314. if (AssetDatabase.isDeclaredAsset(lookupName))
  315. {
  316. mTextures[i].mTextureInactiveAssetId = lookupName;
  317. mTextures[i].mTextureInactiveAsset = mTextures[i].mTextureInactiveAssetId;
  318. }
  319. if (mTextures[i].mTextureInactiveAsset.notNull())
  320. {
  321. mTextures[i].mTextureInactiveAsset->load();
  322. if (mTextures[i].mTextureInactiveAsset->getStatus() == AssetBase::Ok)
  323. {
  324. mTextures[i].mTextureInactive = mTextures[i].mTextureInactiveAsset->getTexture(&GFXDefaultGUIProfile);
  325. break;
  326. }
  327. }
  328. }
  329. if( !mTextures[ i ].mTextureInactive )
  330. mTextures[ i ].mTextureInactive = mTextures[ i ].mTextureNormal;
  331. }
  332. if( i == 0 && mTextures[ i ].mTextureNormal.isNull() && mTextures[ i ].mTextureHilight.isNull() && mTextures[ i ].mTextureDepressed.isNull() && mTextures[ i ].mTextureInactive.isNull() )
  333. {
  334. Con::warnf( "GuiBitmapButtonCtrl::setBitmap - Unable to load texture: %s", mBitmapName );
  335. this->setBitmap( StringTable->insert(GFXTextureManager::getUnavailableTexturePath().c_str()) );
  336. return;
  337. }
  338. }
  339. }
  340. if( mAutoFitExtents && !mTextures[ 0 ].mTextureNormal.isNull() )
  341. setExtent( mTextures[ 0 ].mTextureNormal.getWidth(), mTextures[ 0 ].mTextureNormal.getHeight() );
  342. }
  343. else
  344. {
  345. for( U32 i = 0; i < NumModifiers; ++ i )
  346. {
  347. mTextures[ i ].mTextureNormal = NULL;
  348. mTextures[ i ].mTextureHilight = NULL;
  349. mTextures[ i ].mTextureDepressed = NULL;
  350. mTextures[ i ].mTextureInactive = NULL;
  351. }
  352. }
  353. setUpdate();
  354. }
  355. //-----------------------------------------------------------------------------
  356. void GuiBitmapButtonCtrl::setBitmapHandles(GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive)
  357. {
  358. const U32 count = mUseModifiers ? NumModifiers : 1;
  359. for( U32 i = 0; i < count; ++ i )
  360. {
  361. mTextures[ i ].mTextureNormal = normal;
  362. mTextures[ i ].mTextureHilight = highlighted;
  363. mTextures[ i ].mTextureDepressed = depressed;
  364. mTextures[ i ].mTextureInactive = inactive;
  365. if (!mTextures[ i ].mTextureHilight)
  366. mTextures[ i ].mTextureHilight = mTextures[ i ].mTextureNormal;
  367. if (!mTextures[ i ].mTextureDepressed)
  368. mTextures[ i ].mTextureDepressed = mTextures[ i ].mTextureHilight;
  369. if (!mTextures[ i ].mTextureInactive)
  370. mTextures[ i ].mTextureInactive = mTextures[ i ].mTextureNormal;
  371. if (mTextures[ i ].mTextureNormal.isNull() && mTextures[ i ].mTextureHilight.isNull() && mTextures[ i ].mTextureDepressed.isNull() && mTextures[ i ].mTextureInactive.isNull())
  372. {
  373. Con::warnf("GuiBitmapButtonCtrl::setBitmapHandles() - Invalid texture handles");
  374. setBitmap( StringTable->insert(GFXTextureManager::getUnavailableTexturePath().c_str()) );
  375. return;
  376. }
  377. }
  378. mBitmapName = "texhandle";
  379. }
  380. //------------------------------------------------------------------------------
  381. GuiBitmapButtonCtrl::Modifier GuiBitmapButtonCtrl::getCurrentModifier()
  382. {
  383. U8 modifierKeys = Input::getModifierKeys();
  384. if( modifierKeys & SI_PRIMARY_CTRL )
  385. return ModifierCtrl;
  386. else if( modifierKeys & SI_PRIMARY_ALT )
  387. return ModifierAlt;
  388. else if( modifierKeys & SI_SHIFT )
  389. return ModifierShift;
  390. return ModifierNone;
  391. }
  392. //------------------------------------------------------------------------------
  393. GFXTexHandle& GuiBitmapButtonCtrl::getTextureForCurrentState()
  394. {
  395. U32 index = ModifierNone;
  396. if( mUseModifiers )
  397. index = getCurrentModifier();
  398. if( !mUseStates )
  399. {
  400. if( mTextures[ index ].mTextureNormal )
  401. return mTextures[ 0 ].mTextureNormal;
  402. else
  403. return mTextures[ index ].mTextureNormal;
  404. }
  405. switch( getState() )
  406. {
  407. case NORMAL:
  408. if( !mTextures[ index ].mTextureNormal )
  409. return mTextures[ 0 ].mTextureNormal;
  410. else
  411. return mTextures[ index ].mTextureNormal;
  412. case HILIGHT:
  413. if( !mTextures[ index ].mTextureHilight )
  414. return mTextures[ 0 ].mTextureHilight;
  415. else
  416. return mTextures[ index ].mTextureHilight;
  417. case DEPRESSED:
  418. if( !mTextures[ index ].mTextureDepressed )
  419. return mTextures[ 0 ].mTextureDepressed;
  420. else
  421. return mTextures[ index ].mTextureDepressed;
  422. default:
  423. if( !mTextures[ index ].mTextureInactive )
  424. return mTextures[ 0 ].mTextureInactive;
  425. else
  426. return mTextures[ index ].mTextureInactive;
  427. }
  428. }
  429. //------------------------------------------------------------------------------
  430. void GuiBitmapButtonCtrl::onAction()
  431. {
  432. Parent::onAction();
  433. if( mUseModifiers )
  434. {
  435. switch( getCurrentModifier() )
  436. {
  437. case ModifierNone:
  438. onDefaultClick_callback();
  439. break;
  440. case ModifierCtrl:
  441. onCtrlClick_callback();
  442. break;
  443. case ModifierAlt:
  444. onAltClick_callback();
  445. break;
  446. case ModifierShift:
  447. onShiftClick_callback();
  448. break;
  449. default:
  450. break;
  451. }
  452. }
  453. }
  454. //------------------------------------------------------------------------------
  455. void GuiBitmapButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  456. {
  457. GFXTexHandle& texture = getTextureForCurrentState();
  458. if( texture )
  459. {
  460. renderButton( texture, offset, updateRect );
  461. renderChildControls( offset, updateRect );
  462. }
  463. else
  464. Parent::onRender(offset, updateRect);
  465. }
  466. //------------------------------------------------------------------------------
  467. void GuiBitmapButtonCtrl::renderButton( GFXTexHandle &texture, const Point2I &offset, const RectI& updateRect )
  468. {
  469. GFX->getDrawUtil()->clearBitmapModulation();
  470. GFX->getDrawUtil()->setBitmapModulation(mColor);
  471. switch( mBitmapMode )
  472. {
  473. case BitmapStretched:
  474. {
  475. RectI rect( offset, getExtent() );
  476. GFX->getDrawUtil()->drawBitmapStretch( texture, rect );
  477. break;
  478. }
  479. case BitmapCentered:
  480. {
  481. Point2I p = offset;
  482. p.x += getExtent().x / 2 - texture.getWidth() / 2;
  483. p.y += getExtent().y / 2 - texture.getHeight() / 2;
  484. GFX->getDrawUtil()->drawBitmap( texture, p );
  485. break;
  486. }
  487. }
  488. }
  489. //=============================================================================
  490. // GuiBitmapButtonTextCtrl.
  491. //=============================================================================
  492. IMPLEMENT_CONOBJECT( GuiBitmapButtonTextCtrl);
  493. ConsoleDocClass( GuiBitmapButtonTextCtrl,
  494. "@brief An extension of GuiBitmapButtonCtrl that additionally renders a text label on the bitmapped button.\n\n"
  495. "The text for the label is taken from the GuiButtonBaseCtrl::text property.\n\n"
  496. "For rendering, the label is placed, relative to the control's upper left corner, at the text offset specified in the "
  497. "control's profile (GuiControlProfile::textOffset) and justified according to the profile's setting (GuiControlProfile::justify).\n\n"
  498. "@see GuiControlProfile::textOffset\n"
  499. "@see GuiControlProfile::justify\n"
  500. "@ingroup GuiButtons"
  501. );
  502. //-----------------------------------------------------------------------------
  503. void GuiBitmapButtonTextCtrl::renderButton( GFXTexHandle &texture, const Point2I &offset, const RectI& updateRect )
  504. {
  505. Parent::renderButton( texture, offset, updateRect );
  506. Point2I textPos = offset;
  507. if(mDepressed)
  508. textPos += Point2I(1,1);
  509. // Make sure we take the profile's textOffset into account.
  510. textPos += mProfile->mTextOffset;
  511. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  512. renderJustifiedText(textPos, getExtent(), mButtonText);
  513. }
  514. bool GuiBitmapButtonCtrl::pointInControl(const Point2I& parentCoordPoint)
  515. {
  516. if (mMasked && getTextureForCurrentState())
  517. {
  518. ColorI rColor(0, 0, 0, 0);
  519. GBitmap* bmp;
  520. const RectI &bounds = getBounds();
  521. S32 xt = parentCoordPoint.x - bounds.point.x;
  522. S32 yt = parentCoordPoint.y - bounds.point.y;
  523. bmp = getTextureForCurrentState().getBitmap();
  524. if (!bmp)
  525. {
  526. setBitmap(mBitmapName);
  527. bmp = getTextureForCurrentState().getBitmap();
  528. }
  529. S32 relativeXRange = this->getExtent().x;
  530. S32 relativeYRange = this->getExtent().y;
  531. S32 fileXRange = bmp->getHeight(0);
  532. S32 fileYRange = bmp->getWidth(0);
  533. //Con::errorf("xRange:[%i -- %i], Range:[%i -- %i] pos:(%i,%i)",relativeXRange,fileXRange,relativeYRange,fileYRange,xt,yt);
  534. S32 fileX = (xt*fileXRange) / relativeXRange;
  535. S32 fileY = (yt*fileYRange) / relativeYRange;
  536. //Con::errorf("Checking %s @ (%i,%i)",this->getName(),fileX,fileY);
  537. bmp->getColor(fileX, fileY, rColor);
  538. if (rColor.alpha)
  539. return true;
  540. else
  541. return false;
  542. }
  543. else
  544. return Parent::pointInControl(parentCoordPoint);
  545. }
  546. DEF_ASSET_BINDS_REFACTOR(GuiBitmapButtonCtrl, Bitmap)