guiBitmapButtonCtrl.cpp 25 KB

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