guiBitmapButtonCtrl.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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. }
  112. //-----------------------------------------------------------------------------
  113. void GuiBitmapButtonCtrl::initPersistFields()
  114. {
  115. docsURL;
  116. addGroup( "Bitmap" );
  117. INITPERSISTFIELD_IMAGEASSET(Bitmap, GuiBitmapButtonCtrl,"Texture file to display on this button.\n"
  118. "If useStates is false, this will be the file that renders on the control. Otherwise, this will "
  119. "specify the default texture name to which the various state and modifier suffixes are appended "
  120. "to find the per-state and per-modifier (if enabled) textures.")
  121. addField("color", TypeColorI, Offset(mColor, GuiBitmapButtonCtrl), "color mul");
  122. addField( "bitmapMode", TYPEID< BitmapMode >(), Offset( mBitmapMode, GuiBitmapButtonCtrl ),
  123. "Behavior for fitting the bitmap to the control extents.\n"
  124. "If set to 'Stretched', the bitmap will be stretched both verticall and horizontally to fit inside "
  125. "the control's extents.\n\n"
  126. "If set to 'Centered', the bitmap will stay at its original resolution centered in the control's "
  127. "rectangle (getting clipped if the control is smaller than the texture)." );
  128. addProtectedField( "autoFitExtents", TypeBool, Offset( mAutoFitExtents, GuiBitmapButtonCtrl ),
  129. &_setAutoFitExtents, &defaultProtectedGetFn,
  130. "If true, the control's extents will be set to match the bitmap's extents when setting the bitmap.\n"
  131. "The bitmap extents will always be taken from the default/normal bitmap (in case the extents of the various "
  132. "bitmaps do not match up.)" );
  133. addField( "useModifiers", TypeBool, Offset( mUseModifiers, GuiBitmapButtonCtrl ),
  134. "If true, per-modifier button functionality is enabled.\n"
  135. "@ref guibitmapbutton_modifiers" );
  136. addField( "useStates", TypeBool, Offset( mUseStates, GuiBitmapButtonCtrl ),
  137. "If true, per-mouse state button functionality is enabled.\n"
  138. "Defaults to true.\n\n"
  139. "If you do not use per-state images on this button set this to false to speed up the loading process "
  140. "by inhibiting searches for the individual images." );
  141. addField("masked", TypeBool, Offset(mMasked, GuiBitmapButtonCtrl),"Use alpha masking for interaction.");
  142. endGroup( "Bitmap" );
  143. Parent::initPersistFields();
  144. }
  145. //-----------------------------------------------------------------------------
  146. bool GuiBitmapButtonCtrl::onWake()
  147. {
  148. if (! Parent::onWake())
  149. return false;
  150. setActive( true );
  151. setBitmap( mBitmapName );
  152. return true;
  153. }
  154. //-----------------------------------------------------------------------------
  155. void GuiBitmapButtonCtrl::onSleep()
  156. {
  157. if( dStricmp(mBitmapName, "texhandle") != 0 )
  158. for( U32 i = 0; i < NumModifiers; ++ i )
  159. {
  160. mTextures[ i ].mTextureNormal = NULL;
  161. mTextures[ i ].mTextureHilight = NULL;
  162. mTextures[ i ].mTextureDepressed = NULL;
  163. mTextures[ i ].mTextureInactive = NULL;
  164. }
  165. if (mBitmapAsset.notNull())
  166. mBitmap = NULL;
  167. Parent::onSleep();
  168. }
  169. //-----------------------------------------------------------------------------
  170. bool GuiBitmapButtonCtrl::_setAutoFitExtents( void *object, const char *index, const char *data )
  171. {
  172. GuiBitmapButtonCtrl* ctrl = reinterpret_cast< GuiBitmapButtonCtrl* >( object );
  173. ctrl->setAutoFitExtents( dAtob( data ) );
  174. return false;
  175. }
  176. //-----------------------------------------------------------------------------
  177. /*bool GuiBitmapButtonCtrl::_setBitmap(void* object, const char* index, const char* data)
  178. {
  179. GuiBitmapButtonCtrl* ctrl = reinterpret_cast< GuiBitmapButtonCtrl* >( object );
  180. ctrl->setBitmap( StringTable->insert(data) );
  181. return false;
  182. }*/
  183. //-----------------------------------------------------------------------------
  184. // Legacy method. Can just assign to bitmap field.
  185. /*DefineEngineMethod(GuiBitmapButtonCtrl, setBitmap, void, (const char* path), ,
  186. "Set the bitmap to show on the button.\n"
  187. "@param path Path to the texture file in any of the supported formats.\n" )
  188. {
  189. object->setBitmap( StringTable->insert(path) );
  190. }*/
  191. //-----------------------------------------------------------------------------
  192. void GuiBitmapButtonCtrl::inspectPostApply()
  193. {
  194. Parent::inspectPostApply();
  195. setBitmap(mBitmapName);
  196. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  197. // set it's extent to be exactly the size of the normal bitmap (if present)
  198. if ((getWidth() == 0) && (getHeight() == 0) && mTextures[ 0 ].mTextureNormal)
  199. {
  200. setExtent( mTextures[ 0 ].mTextureNormal->getWidth(), mTextures[ 0 ].mTextureNormal->getHeight());
  201. }
  202. }
  203. //-----------------------------------------------------------------------------
  204. void GuiBitmapButtonCtrl::setAutoFitExtents( bool state )
  205. {
  206. mAutoFitExtents = state;
  207. if( mAutoFitExtents )
  208. setBitmap( mBitmapName );
  209. }
  210. //-----------------------------------------------------------------------------
  211. void GuiBitmapButtonCtrl::setBitmap( StringTableEntry name )
  212. {
  213. PROFILE_SCOPE( GuiBitmapButtonCtrl_setBitmap );
  214. _setBitmap(name);
  215. if( !isAwake() )
  216. return;
  217. if( mBitmapAsset.notNull())
  218. {
  219. if( dStricmp( mBitmapName, "texhandle" ) != 0 )
  220. {
  221. const U32 count = mUseModifiers ? NumModifiers : 1;
  222. for( U32 i = 0; i < count; ++ i )
  223. {
  224. static String modifiers[] =
  225. {
  226. "",
  227. "_ctrl",
  228. "_alt",
  229. "_shift"
  230. };
  231. static String s_n[2] = { "_n", "_n_image" };
  232. static String s_d[2] = { "_d", "_d_image" };
  233. static String s_h[2] = { "_h", "_h_image" };
  234. static String s_i[2] = { "_i", "_i_image" };
  235. String baseName = mBitmapAsset.getAssetId();
  236. //strip any pre-assigned suffix, just in case
  237. baseName = baseName.replace("_n_image", "");
  238. baseName = baseName.replace("_n", "");
  239. if( mUseModifiers )
  240. baseName += modifiers[ i ];
  241. mTextures[i].mTextureNormal = getBitmap();
  242. if( mUseStates )
  243. {
  244. //normal lookup
  245. StringTableEntry lookupName;
  246. for (U32 s = 0; s < 2; s++)
  247. {
  248. if (!mTextures[i].mTextureNormal)
  249. {
  250. lookupName = StringTable->insert(String(baseName + s_n[s]).c_str());
  251. if (AssetDatabase.isDeclaredAsset(lookupName))
  252. {
  253. mTextures[i].mTextureNormalAssetId = lookupName;
  254. mTextures[i].mTextureNormalAsset = mTextures[i].mTextureNormalAssetId;
  255. }
  256. if (mTextures[i].mTextureNormalAsset.notNull())
  257. {
  258. mTextures[i].mTextureNormalAsset->load();
  259. if (mTextures[i].mTextureNormalAsset->getStatus() == AssetBase::Ok)
  260. {
  261. mTextures[i].mTextureNormal = mTextures[i].mTextureNormalAsset->getTexture(&GFXDefaultGUIProfile);
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. //Hilight lookup
  268. for (U32 s = 0; s < 2; s++)
  269. {
  270. lookupName = StringTable->insert(String(baseName + s_h[s]).c_str());
  271. if (AssetDatabase.isDeclaredAsset(lookupName))
  272. {
  273. mTextures[i].mTextureHilightAssetId = lookupName;
  274. mTextures[i].mTextureHilightAsset = mTextures[i].mTextureHilightAssetId;
  275. }
  276. if (mTextures[i].mTextureHilightAsset.notNull())
  277. {
  278. mTextures[i].mTextureHilightAsset->load();
  279. if (mTextures[i].mTextureHilightAsset->getStatus() == AssetBase::Ok)
  280. {
  281. mTextures[i].mTextureHilight = mTextures[i].mTextureHilightAsset->getTexture(&GFXDefaultGUIProfile);
  282. break;
  283. }
  284. }
  285. }
  286. if( !mTextures[ i ].mTextureHilight )
  287. mTextures[ i ].mTextureHilight = mTextures[ i ].mTextureNormal;
  288. //Depressed lookup
  289. for (U32 s = 0; s < 2; s++)
  290. {
  291. lookupName = StringTable->insert(String(baseName + s_d[s]).c_str());
  292. if (AssetDatabase.isDeclaredAsset(lookupName))
  293. {
  294. mTextures[i].mTextureDepressedAssetId = lookupName;
  295. mTextures[i].mTextureDepressedAsset = mTextures[i].mTextureDepressedAssetId;
  296. }
  297. if (mTextures[i].mTextureDepressedAsset.notNull())
  298. {
  299. mTextures[i].mTextureDepressedAsset->load();
  300. if (mTextures[i].mTextureDepressedAsset->getStatus() == AssetBase::Ok)
  301. {
  302. mTextures[i].mTextureDepressed = mTextures[i].mTextureDepressedAsset->getTexture(&GFXDefaultGUIProfile);
  303. break;
  304. }
  305. }
  306. }
  307. if( !mTextures[ i ].mTextureDepressed )
  308. mTextures[ i ].mTextureDepressed = mTextures[ i ].mTextureHilight;
  309. //Depressed lookup
  310. for (U32 s = 0; s < 2; s++)
  311. {
  312. lookupName = StringTable->insert(String(baseName + s_i[s]).c_str());
  313. if (AssetDatabase.isDeclaredAsset(lookupName))
  314. {
  315. mTextures[i].mTextureInactiveAssetId = lookupName;
  316. mTextures[i].mTextureInactiveAsset = mTextures[i].mTextureInactiveAssetId;
  317. }
  318. if (mTextures[i].mTextureInactiveAsset.notNull())
  319. {
  320. mTextures[i].mTextureInactiveAsset->load();
  321. if (mTextures[i].mTextureInactiveAsset->getStatus() == AssetBase::Ok)
  322. {
  323. mTextures[i].mTextureInactive = mTextures[i].mTextureInactiveAsset->getTexture(&GFXDefaultGUIProfile);
  324. break;
  325. }
  326. }
  327. }
  328. if( !mTextures[ i ].mTextureInactive )
  329. mTextures[ i ].mTextureInactive = mTextures[ i ].mTextureNormal;
  330. }
  331. if( i == 0 && mTextures[ i ].mTextureNormal.isNull() && mTextures[ i ].mTextureHilight.isNull() && mTextures[ i ].mTextureDepressed.isNull() && mTextures[ i ].mTextureInactive.isNull() )
  332. {
  333. Con::warnf( "GuiBitmapButtonCtrl::setBitmap - Unable to load texture: %s", mBitmapName );
  334. this->setBitmap( StringTable->insert(GFXTextureManager::getUnavailableTexturePath().c_str()) );
  335. return;
  336. }
  337. }
  338. }
  339. if( mAutoFitExtents && !mTextures[ 0 ].mTextureNormal.isNull() )
  340. setExtent( mTextures[ 0 ].mTextureNormal.getWidth(), mTextures[ 0 ].mTextureNormal.getHeight() );
  341. }
  342. else
  343. {
  344. for( U32 i = 0; i < NumModifiers; ++ i )
  345. {
  346. mTextures[ i ].mTextureNormal = NULL;
  347. mTextures[ i ].mTextureHilight = NULL;
  348. mTextures[ i ].mTextureDepressed = NULL;
  349. mTextures[ i ].mTextureInactive = NULL;
  350. }
  351. }
  352. setUpdate();
  353. }
  354. //-----------------------------------------------------------------------------
  355. void GuiBitmapButtonCtrl::setBitmapHandles(GFXTexHandle normal, GFXTexHandle highlighted, GFXTexHandle depressed, GFXTexHandle inactive)
  356. {
  357. const U32 count = mUseModifiers ? NumModifiers : 1;
  358. for( U32 i = 0; i < count; ++ i )
  359. {
  360. mTextures[ i ].mTextureNormal = normal;
  361. mTextures[ i ].mTextureHilight = highlighted;
  362. mTextures[ i ].mTextureDepressed = depressed;
  363. mTextures[ i ].mTextureInactive = inactive;
  364. if (!mTextures[ i ].mTextureHilight)
  365. mTextures[ i ].mTextureHilight = mTextures[ i ].mTextureNormal;
  366. if (!mTextures[ i ].mTextureDepressed)
  367. mTextures[ i ].mTextureDepressed = mTextures[ i ].mTextureHilight;
  368. if (!mTextures[ i ].mTextureInactive)
  369. mTextures[ i ].mTextureInactive = mTextures[ i ].mTextureNormal;
  370. if (mTextures[ i ].mTextureNormal.isNull() && mTextures[ i ].mTextureHilight.isNull() && mTextures[ i ].mTextureDepressed.isNull() && mTextures[ i ].mTextureInactive.isNull())
  371. {
  372. Con::warnf("GuiBitmapButtonCtrl::setBitmapHandles() - Invalid texture handles");
  373. setBitmap( StringTable->insert(GFXTextureManager::getUnavailableTexturePath().c_str()) );
  374. return;
  375. }
  376. }
  377. mBitmapName = "texhandle";
  378. }
  379. //------------------------------------------------------------------------------
  380. GuiBitmapButtonCtrl::Modifier GuiBitmapButtonCtrl::getCurrentModifier()
  381. {
  382. U8 modifierKeys = Input::getModifierKeys();
  383. if( modifierKeys & SI_PRIMARY_CTRL )
  384. return ModifierCtrl;
  385. else if( modifierKeys & SI_PRIMARY_ALT )
  386. return ModifierAlt;
  387. else if( modifierKeys & SI_SHIFT )
  388. return ModifierShift;
  389. return ModifierNone;
  390. }
  391. //------------------------------------------------------------------------------
  392. GFXTexHandle& GuiBitmapButtonCtrl::getTextureForCurrentState()
  393. {
  394. U32 index = ModifierNone;
  395. if( mUseModifiers )
  396. index = getCurrentModifier();
  397. if( !mUseStates )
  398. {
  399. if( mTextures[ index ].mTextureNormal )
  400. return mTextures[ 0 ].mTextureNormal;
  401. else
  402. return mTextures[ index ].mTextureNormal;
  403. }
  404. switch( getState() )
  405. {
  406. case NORMAL:
  407. if( !mTextures[ index ].mTextureNormal )
  408. return mTextures[ 0 ].mTextureNormal;
  409. else
  410. return mTextures[ index ].mTextureNormal;
  411. case HILIGHT:
  412. if( !mTextures[ index ].mTextureHilight )
  413. return mTextures[ 0 ].mTextureHilight;
  414. else
  415. return mTextures[ index ].mTextureHilight;
  416. case DEPRESSED:
  417. if( !mTextures[ index ].mTextureDepressed )
  418. return mTextures[ 0 ].mTextureDepressed;
  419. else
  420. return mTextures[ index ].mTextureDepressed;
  421. default:
  422. if( !mTextures[ index ].mTextureInactive )
  423. return mTextures[ 0 ].mTextureInactive;
  424. else
  425. return mTextures[ index ].mTextureInactive;
  426. }
  427. }
  428. //------------------------------------------------------------------------------
  429. void GuiBitmapButtonCtrl::onAction()
  430. {
  431. Parent::onAction();
  432. if( mUseModifiers )
  433. {
  434. switch( getCurrentModifier() )
  435. {
  436. case ModifierNone:
  437. onDefaultClick_callback();
  438. break;
  439. case ModifierCtrl:
  440. onCtrlClick_callback();
  441. break;
  442. case ModifierAlt:
  443. onAltClick_callback();
  444. break;
  445. case ModifierShift:
  446. onShiftClick_callback();
  447. break;
  448. default:
  449. break;
  450. }
  451. }
  452. }
  453. //------------------------------------------------------------------------------
  454. void GuiBitmapButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  455. {
  456. GFXTexHandle& texture = getTextureForCurrentState();
  457. if( texture )
  458. {
  459. renderButton( texture, offset, updateRect );
  460. renderChildControls( offset, updateRect );
  461. }
  462. else
  463. Parent::onRender(offset, updateRect);
  464. }
  465. //------------------------------------------------------------------------------
  466. void GuiBitmapButtonCtrl::renderButton( GFXTexHandle &texture, const Point2I &offset, const RectI& updateRect )
  467. {
  468. GFX->getDrawUtil()->clearBitmapModulation();
  469. GFX->getDrawUtil()->setBitmapModulation(mColor);
  470. switch( mBitmapMode )
  471. {
  472. case BitmapStretched:
  473. {
  474. RectI rect( offset, getExtent() );
  475. GFX->getDrawUtil()->drawBitmapStretch( texture, rect );
  476. break;
  477. }
  478. case BitmapCentered:
  479. {
  480. Point2I p = offset;
  481. p.x += getExtent().x / 2 - texture.getWidth() / 2;
  482. p.y += getExtent().y / 2 - texture.getHeight() / 2;
  483. GFX->getDrawUtil()->drawBitmap( texture, p );
  484. break;
  485. }
  486. }
  487. }
  488. //=============================================================================
  489. // GuiBitmapButtonTextCtrl.
  490. //=============================================================================
  491. IMPLEMENT_CONOBJECT( GuiBitmapButtonTextCtrl);
  492. ConsoleDocClass( GuiBitmapButtonTextCtrl,
  493. "@brief An extension of GuiBitmapButtonCtrl that additionally renders a text label on the bitmapped button.\n\n"
  494. "The text for the label is taken from the GuiButtonBaseCtrl::text property.\n\n"
  495. "For rendering, the label is placed, relative to the control's upper left corner, at the text offset specified in the "
  496. "control's profile (GuiControlProfile::textOffset) and justified according to the profile's setting (GuiControlProfile::justify).\n\n"
  497. "@see GuiControlProfile::textOffset\n"
  498. "@see GuiControlProfile::justify\n"
  499. "@ingroup GuiButtons"
  500. );
  501. //-----------------------------------------------------------------------------
  502. void GuiBitmapButtonTextCtrl::renderButton( GFXTexHandle &texture, const Point2I &offset, const RectI& updateRect )
  503. {
  504. Parent::renderButton( texture, offset, updateRect );
  505. Point2I textPos = offset;
  506. if(mDepressed)
  507. textPos += Point2I(1,1);
  508. // Make sure we take the profile's textOffset into account.
  509. textPos += mProfile->mTextOffset;
  510. GFX->getDrawUtil()->setBitmapModulation( mProfile->mFontColor );
  511. renderJustifiedText(textPos, getExtent(), mButtonText);
  512. }
  513. bool GuiBitmapButtonCtrl::pointInControl(const Point2I& parentCoordPoint)
  514. {
  515. if (mMasked && getTextureForCurrentState())
  516. {
  517. ColorI rColor(0, 0, 0, 0);
  518. GBitmap* bmp;
  519. const RectI &bounds = getBounds();
  520. S32 xt = parentCoordPoint.x - bounds.point.x;
  521. S32 yt = parentCoordPoint.y - bounds.point.y;
  522. bmp = getTextureForCurrentState().getBitmap();
  523. if (!bmp)
  524. {
  525. setBitmap(mBitmapName);
  526. bmp = getTextureForCurrentState().getBitmap();
  527. }
  528. S32 relativeXRange = this->getExtent().x;
  529. S32 relativeYRange = this->getExtent().y;
  530. S32 fileXRange = bmp->getHeight(0);
  531. S32 fileYRange = bmp->getWidth(0);
  532. //Con::errorf("xRange:[%i -- %i], Range:[%i -- %i] pos:(%i,%i)",relativeXRange,fileXRange,relativeYRange,fileYRange,xt,yt);
  533. S32 fileX = (xt*fileXRange) / relativeXRange;
  534. S32 fileY = (yt*fileYRange) / relativeYRange;
  535. //Con::errorf("Checking %s @ (%i,%i)",this->getName(),fileX,fileY);
  536. bmp->getColor(fileX, fileY, rColor);
  537. if (rColor.alpha)
  538. return true;
  539. else
  540. return false;
  541. }
  542. else
  543. return Parent::pointInControl(parentCoordPoint);
  544. }
  545. DEF_ASSET_BINDS_REFACTOR(GuiBitmapButtonCtrl, Bitmap)