guiBitmapButtonCtrl.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "console/console.h"
  23. #include "graphics/dgl.h"
  24. #include "console/consoleTypes.h"
  25. #include "platform/platformAudio.h"
  26. #include "gui/guiCanvas.h"
  27. #include "gui/guiDefaultControlRender.h"
  28. #include "gui/buttons/guiBitmapButtonCtrl.h"
  29. IMPLEMENT_CONOBJECT(GuiBitmapButtonCtrl);
  30. GuiBitmapButtonCtrl::GuiBitmapButtonCtrl()
  31. {
  32. mBitmapName = StringTable->EmptyString;
  33. mBitmapNormal = StringTable->EmptyString;
  34. mBitmapHilight = StringTable->EmptyString;
  35. mBitmapDepressed = StringTable->EmptyString;
  36. mBitmapInactive = StringTable->EmptyString;
  37. mIsLegacyVersion = true;
  38. mBounds.extent.set(140, 30);
  39. }
  40. void GuiBitmapButtonCtrl::initPersistFields()
  41. {
  42. Parent::initPersistFields();
  43. addField("isLegacyVersion", TypeBool, Offset(mIsLegacyVersion, GuiBitmapButtonCtrl), "Determines if this is a legacy version of the control (only uses bitmap field)");
  44. addField("bitmap", TypeFilename, Offset(mBitmapName, GuiBitmapButtonCtrl), "Base name for the bitmaps used in the button states.\n For example, you would only put \"button\""
  45. " to load button_n.png, button_d.png, button_h.png and button_i.png");
  46. addField("bitmapNormal", TypeFilename, Offset(mBitmapNormal, GuiBitmapButtonCtrl), "Name of texture used for the normal button state");
  47. addField("bitmapHilight", TypeFilename, Offset(mBitmapHilight, GuiBitmapButtonCtrl), "Name of texture used for the hilight/hover button state");
  48. addField("bitmapDepressed", TypeFilename, Offset(mBitmapDepressed, GuiBitmapButtonCtrl), "Name of texture used for the depressed button state");
  49. addField("bitmapInactive", TypeFilename, Offset(mBitmapInactive, GuiBitmapButtonCtrl), "Name of texture used for the inactive button state");
  50. }
  51. bool GuiBitmapButtonCtrl::onWake()
  52. {
  53. if (! Parent::onWake())
  54. return false;
  55. if(mIsLegacyVersion)
  56. {
  57. setBitmap(mBitmapName);
  58. }
  59. else
  60. {
  61. setBitmap(mBitmapNormal, NORMAL);
  62. setBitmap(mBitmapHilight, HILIGHT);
  63. setBitmap(mBitmapDepressed, DEPRESSED);
  64. setBitmap(mBitmapInactive, INACTIVE);
  65. }
  66. return true;
  67. }
  68. void GuiBitmapButtonCtrl::onSleep()
  69. {
  70. mTextureNormal = NULL;
  71. mTextureHilight = NULL;
  72. mTextureDepressed = NULL;
  73. mTextureInactive = NULL;
  74. Parent::onSleep();
  75. }
  76. ConsoleMethod( GuiBitmapButtonCtrl, setBitmap, void, 3, 3, "(filepath name) Loads a bitmap from a given file\n"
  77. "@return No return value.")
  78. {
  79. object->setBitmap(argv[2]);
  80. }
  81. ConsoleMethod( GuiBitmapButtonCtrl, setBitmapNormal, void, 3, 3, "(filepath name) Loads a bitmap from a given file for the \"up\" state\n"
  82. "@return No return value.")
  83. {
  84. object->setBitmap(argv[2], NORMAL);
  85. }
  86. ConsoleMethod( GuiBitmapButtonCtrl, setBitmapHilight, void, 3, 3, "(filepath name) Loads a bitmap from a given file for the \"down\" state\n"
  87. "@return No return value.")
  88. {
  89. object->setBitmap(argv[2], HILIGHT);
  90. }
  91. ConsoleMethod( GuiBitmapButtonCtrl, setBitmapDepressed, void, 3, 3, "(filepath name) Loads a bitmap from a given file for the \"hover\" state\n"
  92. "@return No return value.")
  93. {
  94. object->setBitmap(argv[2], DEPRESSED);
  95. }
  96. ConsoleMethod( GuiBitmapButtonCtrl, setBitmapInactive, void, 3, 3, "(filepath name) Loads a bitmap from a given file for the \"inactive\" state\n"
  97. "@return No return value.")
  98. {
  99. object->setBitmap(argv[2], INACTIVE);
  100. }
  101. void GuiBitmapButtonCtrl::inspectPostApply()
  102. {
  103. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  104. // set it's extent to be exactly the size of the normal bitmap (if present)
  105. Parent::inspectPostApply();
  106. if ((mBounds.extent.x == 0) && (mBounds.extent.y == 0) && mTextureNormal)
  107. {
  108. TextureObject *texture = (TextureObject *) mTextureNormal;
  109. mBounds.extent.x = texture->getBitmapWidth();
  110. mBounds.extent.y = texture->getBitmapHeight();
  111. }
  112. }
  113. void GuiBitmapButtonCtrl::setBitmap(const char *name)
  114. {
  115. mBitmapName = StringTable->insert(name);
  116. if(!isAwake())
  117. return;
  118. if (*mBitmapName)
  119. {
  120. char buffer[1024];
  121. char *p;
  122. dStrcpy(buffer, name);
  123. p = buffer + dStrlen(buffer);
  124. mTextureNormal = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  125. if (!mTextureNormal)
  126. {
  127. dStrcpy(p, "_n");
  128. mTextureNormal = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  129. }
  130. dStrcpy(p, "_h");
  131. mTextureHilight = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  132. if (!mTextureHilight)
  133. mTextureHilight = mTextureNormal;
  134. dStrcpy(p, "_d");
  135. mTextureDepressed = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  136. if (!mTextureDepressed)
  137. mTextureDepressed = mTextureHilight;
  138. dStrcpy(p, "_i");
  139. mTextureInactive = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  140. if (!mTextureInactive)
  141. mTextureInactive = mTextureNormal;
  142. }
  143. else
  144. {
  145. mTextureNormal = NULL;
  146. mTextureHilight = NULL;
  147. mTextureDepressed = NULL;
  148. mTextureInactive = NULL;
  149. }
  150. setUpdate();
  151. }
  152. void GuiBitmapButtonCtrl::setBitmap(const char *name, ButtonState state)
  153. {
  154. if(!isAwake() && *name)
  155. return;
  156. StringTableEntry temporaryName = StringTable->insert(name);
  157. if (*temporaryName)
  158. {
  159. char buffer[1024];
  160. char *p;
  161. dStrcpy(buffer, name);
  162. p = buffer + dStrlen(buffer);
  163. switch (state)
  164. {
  165. case NORMAL:
  166. mBitmapNormal = temporaryName;
  167. mTextureNormal = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  168. break;
  169. case HILIGHT:
  170. mBitmapHilight = temporaryName;
  171. mTextureHilight = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  172. break;
  173. case DEPRESSED:
  174. mBitmapDepressed = temporaryName;
  175. mTextureDepressed = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  176. break;
  177. case INACTIVE:
  178. mBitmapInactive = temporaryName;
  179. mTextureInactive = TextureHandle(buffer, TextureHandle::BitmapTexture, true);
  180. break;
  181. }
  182. }
  183. else
  184. {
  185. switch (state)
  186. {
  187. case NORMAL:
  188. mTextureNormal = NULL;
  189. break;
  190. case HILIGHT:
  191. mTextureHilight = NULL;
  192. break;
  193. case DEPRESSED:
  194. mTextureDepressed = NULL;
  195. break;
  196. case INACTIVE:
  197. mTextureInactive = NULL;
  198. break;
  199. }
  200. }
  201. setUpdate();
  202. }
  203. //-------------------------------------
  204. void GuiBitmapButtonCtrl::onRender(Point2I offset, const RectI& updateRect)
  205. {
  206. ButtonState state = NORMAL;
  207. if (mActive)
  208. {
  209. if (mMouseOver)
  210. state = HILIGHT;
  211. if (mDepressed || mStateOn)
  212. state = DEPRESSED;
  213. }
  214. else
  215. state = INACTIVE;
  216. switch (state)
  217. {
  218. case NORMAL:
  219. renderButton(mTextureNormal, offset, updateRect);
  220. break;
  221. case HILIGHT:
  222. renderButton(mTextureHilight ? mTextureHilight : mTextureNormal, offset, updateRect);
  223. break;
  224. case DEPRESSED:
  225. renderButton(mTextureDepressed, offset, updateRect);
  226. break;
  227. case INACTIVE:
  228. renderButton(mTextureInactive ? mTextureInactive : mTextureNormal, offset, updateRect);
  229. break;
  230. }
  231. }
  232. //------------------------------------------------------------------------------
  233. void GuiBitmapButtonCtrl::renderButton(TextureHandle &texture, Point2I &offset, const RectI& updateRect)
  234. {
  235. if (texture)
  236. {
  237. RectI rect(offset, mBounds.extent);
  238. dglClearBitmapModulation();
  239. dglDrawBitmapStretch(texture, rect);
  240. renderChildControls( offset, updateRect);
  241. }
  242. else
  243. Parent::onRender(offset, updateRect);
  244. }
  245. //------------------------------------------------------------------------------
  246. IMPLEMENT_CONOBJECT(GuiBitmapButtonTextCtrl);
  247. void GuiBitmapButtonTextCtrl::onRender(Point2I offset, const RectI& updateRect)
  248. {
  249. enum {
  250. NORMAL,
  251. HILIGHT,
  252. DEPRESSED,
  253. INACTIVE
  254. } state = NORMAL;
  255. if (mActive)
  256. {
  257. if (mMouseOver) state = HILIGHT;
  258. if (mDepressed || mStateOn) state = DEPRESSED;
  259. }
  260. else
  261. state = INACTIVE;
  262. TextureHandle texture;
  263. switch (state)
  264. {
  265. case NORMAL:
  266. texture = mTextureNormal;
  267. break;
  268. case HILIGHT:
  269. texture = mTextureHilight;
  270. break;
  271. case DEPRESSED:
  272. texture = mTextureDepressed;
  273. break;
  274. case INACTIVE:
  275. texture = mTextureInactive;
  276. if(!texture)
  277. texture = mTextureNormal;
  278. break;
  279. }
  280. if (texture)
  281. {
  282. RectI rect(offset, mBounds.extent);
  283. dglClearBitmapModulation();
  284. dglDrawBitmapStretch(texture, rect);
  285. Point2I textPos = offset;
  286. if(mDepressed)
  287. textPos += Point2I(1,1);
  288. // Make sure we take the profile's textOffset into account.
  289. textPos += mProfile->mTextOffset;
  290. dglSetBitmapModulation( mProfile->mFontColor );
  291. renderJustifiedText(textPos, mBounds.extent, mButtonText);
  292. renderChildControls( offset, updateRect);
  293. }
  294. else
  295. Parent::onRender(offset, updateRect);
  296. }