afxSpellButton.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. //-------------------------------------
  25. //
  26. // Bitmap Button Contrl
  27. // Set 'bitmap' comsole field to base name of bitmaps to use. This control will
  28. // append '_n' for normal
  29. // append '_h' for hilighted
  30. // append '_d' for depressed
  31. //
  32. // if bitmap cannot be found it will use the default bitmap to render.
  33. //
  34. // if the extent is set to (0,0) in the gui editor and appy hit, this control will
  35. // set it's extent to be exactly the size of the normal bitmap (if present)
  36. //
  37. #include "afx/arcaneFX.h"
  38. #include "console/engineAPI.h"
  39. #include "gfx/gfxDrawUtil.h"
  40. #include "afx/ui/afxSpellButton.h"
  41. #include "afx/afxSpellBook.h"
  42. #include "afx/afxMagicSpell.h"
  43. #include "afx/rpg/afxRPGMagicSpell.h"
  44. IMPLEMENT_CONOBJECT(afxSpellButton);
  45. ConsoleDocClass( afxSpellButton,
  46. "@brief A GUI button with some special features that are useful for casting AFX spells.\n\n"
  47. "@ingroup afxGUI\n"
  48. "@ingroup AFX\n"
  49. );
  50. #define COOLDOWN_PROFILE &GFXDefaultGUIProfile, avar("%s() - Cooldown Texture (line %d)", __FUNCTION__, __LINE__)
  51. StringTableEntry afxSpellButton::sUnknownSpellBitmap = "";
  52. StringTableEntry afxSpellButton::sSpellCooldownBitmaps = "";
  53. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  54. afxSpellButton::afxSpellButton()
  55. {
  56. if (sUnknownSpellBitmap == NULL)
  57. sUnknownSpellBitmap = ST_NULLSTRING;
  58. if (sSpellCooldownBitmaps == NULL)
  59. sSpellCooldownBitmaps = ST_NULLSTRING;
  60. mBitmapName = ST_NULLSTRING;
  61. setExtent(140, 30);
  62. spellbook = NULL;
  63. book_slot.set(0, 0);
  64. }
  65. afxSpellButton::~afxSpellButton()
  66. {
  67. }
  68. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  69. void afxSpellButton::initPersistFields()
  70. {
  71. docsURL;
  72. addField("bitmap", TypeFilename, Offset(mBitmapName, afxSpellButton),
  73. "...");
  74. addField("book_slot", TypePoint2I, Offset(book_slot, afxSpellButton),
  75. "...");
  76. Parent::initPersistFields();
  77. Con::addVariable("pref::afxSpellButton::unknownSpellBitmap", TypeFilename, &sUnknownSpellBitmap);
  78. Con::addVariable("pref::afxSpellButton::spellCooldownBitmaps", TypeFilename, &sSpellCooldownBitmaps);
  79. }
  80. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  81. bool afxSpellButton::onAdd()
  82. {
  83. if (!Parent::onAdd())
  84. return false;
  85. if (sSpellCooldownBitmaps != NULL)
  86. {
  87. char buffer[256];
  88. for (int i = 0; i < NUM_COOLDOWN_FRAMES; i++)
  89. {
  90. dSprintf(buffer, 256, "%s_%.2d", sSpellCooldownBitmaps, i);
  91. cooldown_txrs[i].set(buffer, COOLDOWN_PROFILE);
  92. }
  93. }
  94. return true;
  95. }
  96. bool afxSpellButton::onWake()
  97. {
  98. if (! Parent::onWake())
  99. return false;
  100. setActive(true);
  101. update_bitmap();
  102. return true;
  103. }
  104. void afxSpellButton::onSleep()
  105. {
  106. mTextureNormal = NULL;
  107. mTextureHilight = NULL;
  108. mTextureDepressed = NULL;
  109. Parent::onSleep();
  110. }
  111. void afxSpellButton::onMouseEnter(const GuiEvent &event)
  112. {
  113. Parent::onMouseEnter(event);
  114. Con::executef(this, "onMouseEnter");
  115. }
  116. void afxSpellButton::onMouseLeave(const GuiEvent &event)
  117. {
  118. Parent::onMouseLeave(event);
  119. Con::executef(this, "onMouseLeave");
  120. }
  121. void afxSpellButton::inspectPostApply()
  122. {
  123. // if the extent is set to (0,0) in the gui editor and apply hit, this control will
  124. // set it's extent to be exactly the size of the normal bitmap (if present)
  125. Parent::inspectPostApply();
  126. if ((getWidth() == 0) && (getHeight() == 0) && mTextureNormal)
  127. {
  128. setExtent(mTextureNormal->getWidth(), mTextureNormal->getHeight());
  129. }
  130. }
  131. void afxSpellButton::setBitmap(const char *name, bool placeholder)
  132. {
  133. mBitmapName = (name) ? StringTable->insert(name) : ST_NULLSTRING;
  134. if (!isAwake())
  135. return;
  136. if (mBitmapName != ST_NULLSTRING)
  137. {
  138. char buffer[1024];
  139. char *p;
  140. if (placeholder)
  141. {
  142. dStrcpy(buffer, name, 1024);
  143. S32 pLen = 1024 - dStrlen(buffer);
  144. p = buffer + dStrlen(buffer);
  145. dStrcpy(p, "_i", pLen);
  146. mTextureInactive.set(buffer, COOLDOWN_PROFILE);
  147. mTextureNormal = mTextureInactive;
  148. mTextureHilight = mTextureInactive;
  149. mTextureDepressed = mTextureInactive;
  150. setActive(false);
  151. }
  152. else
  153. {
  154. dStrcpy(buffer, name, 1024);
  155. S32 pLen = 1024 - dStrlen(buffer);
  156. p = buffer + dStrlen(buffer);
  157. dStrcpy(p, "_n", pLen);
  158. mTextureNormal.set(buffer, COOLDOWN_PROFILE);
  159. dStrcpy(p, "_h", pLen);
  160. mTextureHilight.set(buffer, COOLDOWN_PROFILE);
  161. if (!mTextureHilight)
  162. mTextureHilight = mTextureNormal;
  163. dStrcpy(p, "_d", pLen);
  164. mTextureDepressed.set(buffer, COOLDOWN_PROFILE);
  165. if (!mTextureDepressed)
  166. mTextureDepressed = mTextureHilight;
  167. dStrcpy(p, "_i", pLen);
  168. mTextureInactive.set(buffer, COOLDOWN_PROFILE);
  169. if (!mTextureInactive)
  170. mTextureInactive = mTextureNormal;
  171. setActive(true);
  172. }
  173. }
  174. else
  175. {
  176. mTextureNormal = NULL;
  177. mTextureHilight = NULL;
  178. mTextureDepressed = NULL;
  179. mTextureInactive = NULL;
  180. }
  181. setUpdate();
  182. }
  183. void afxSpellButton::onRender(Point2I offset, const RectI& updateRect)
  184. {
  185. enum { NORMAL, HILIGHT, DEPRESSED, INACTIVE } state = NORMAL;
  186. if (mActive)
  187. {
  188. if (mHighlighted) state = HILIGHT;
  189. if (mDepressed || mStateOn) state = DEPRESSED;
  190. }
  191. else
  192. state = INACTIVE;
  193. switch (state)
  194. {
  195. case NORMAL: renderButton(mTextureNormal, offset, updateRect); break;
  196. case HILIGHT: renderButton(mTextureHilight ? mTextureHilight : mTextureNormal, offset, updateRect); break;
  197. case DEPRESSED: renderButton(mTextureDepressed, offset, updateRect); break;
  198. case INACTIVE: renderButton(mTextureInactive ? mTextureInactive : mTextureNormal, offset, updateRect); break;
  199. }
  200. }
  201. void afxSpellButton::onDeleteNotify(SimObject* obj)
  202. {
  203. // Handle Shape Deletion
  204. afxSpellBook* book = dynamic_cast<afxSpellBook*>(obj);
  205. if (book != NULL)
  206. {
  207. if (book == spellbook)
  208. {
  209. spellbook = NULL;
  210. setBitmap("");
  211. setVisible(false);
  212. return;
  213. }
  214. }
  215. Parent::onDeleteNotify(obj);
  216. }
  217. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  218. // protected:
  219. void afxSpellButton::renderButton(GFXTexHandle &texture, Point2I &offset,
  220. const RectI& updateRect)
  221. {
  222. if (texture)
  223. {
  224. RectI rect(offset, getExtent());
  225. GFX->getDrawUtil()->clearBitmapModulation();
  226. GFX->getDrawUtil()->drawBitmapStretch(texture, rect);
  227. if (spellbook)
  228. {
  229. F32 cooldown = spellbook->getCooldownFactor(book_slot.x, book_slot.y);
  230. if (cooldown < 1.0f)
  231. {
  232. if (cooldown_txrs[(int)(36.0f*cooldown)])
  233. GFX->getDrawUtil()->drawBitmapStretch(cooldown_txrs[(int)(36.0f*cooldown)], rect);
  234. }
  235. }
  236. renderChildControls( offset, updateRect);
  237. }
  238. else
  239. Parent::onRender(offset, updateRect);
  240. }
  241. void afxSpellButton::update_bitmap()
  242. {
  243. const char* icon_name = 0;
  244. bool is_placeholder = false;
  245. if (spellbook)
  246. {
  247. icon_name = spellbook->getSpellIcon(book_slot.x, book_slot.y);
  248. is_placeholder = spellbook->isPlaceholder(book_slot.x, book_slot.y);
  249. if (icon_name && icon_name[0] == '\0')
  250. icon_name = sUnknownSpellBitmap;
  251. }
  252. if (icon_name)
  253. {
  254. setBitmap(icon_name, is_placeholder);
  255. setVisible(true);
  256. }
  257. else
  258. {
  259. setBitmap("");
  260. setVisible(false);
  261. }
  262. }
  263. void afxSpellButton::setSpellBook(afxSpellBook* book, U8 page)
  264. {
  265. book_slot.x = page;
  266. if (spellbook)
  267. clearNotify(spellbook);
  268. spellbook = book;
  269. update_bitmap();
  270. if (spellbook)
  271. deleteNotify(spellbook);
  272. }
  273. void afxSpellButton::setPage(U8 page)
  274. {
  275. book_slot.x = page;
  276. update_bitmap();
  277. }
  278. const char* afxSpellButton::formatDesc(char* buffer, int len) const
  279. {
  280. return (spellbook) ? spellbook->formatDesc(buffer, len, book_slot.x, book_slot.y) : "";
  281. }
  282. afxMagicSpellData* afxSpellButton::getSpellDataBlock() const
  283. {
  284. return (spellbook) ? spellbook->getSpellData(book_slot.x, book_slot.y) : 0;
  285. }
  286. afxRPGMagicSpellData* afxSpellButton::getSpellRPGDataBlock() const
  287. {
  288. return (spellbook) ? spellbook->getSpellRPGData(book_slot.x, book_slot.y) : 0;
  289. }
  290. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  291. DefineEngineMethod( afxSpellButton, onSpellbookChange, void, ( afxSpellBook* spellbook, U32 page ),,
  292. "Notify an afxSpellButton when its associated spellbook has changed.\n" )
  293. {
  294. object->setSpellBook(spellbook, (U8)page);
  295. }
  296. DefineEngineMethod( afxSpellButton, onTurnPage, void, (U32 page ),,
  297. "Notify an afxSpellButton when the spellbook turns to a new page.\n" )
  298. {
  299. object->setPage((U8)page);
  300. }
  301. DefineEngineMethod( afxSpellButton, getSpellDescription, const char*, (),,
  302. "Get the text description of a spell.\n" )
  303. {
  304. char buf[1024];
  305. return object->formatDesc(buf, 1024);
  306. }
  307. DefineEngineMethod( afxSpellButton, getSpellDataBlock, S32, (),,
  308. "Get the spell's datablock.\n" )
  309. {
  310. afxMagicSpellData* spell_data = object->getSpellDataBlock();
  311. return (spell_data) ? spell_data->getId() : -1;
  312. }
  313. DefineEngineMethod( afxSpellButton, getSpellRPGDataBlock, S32, (),,
  314. "Get the spell's RPG datablock.\n" )
  315. {
  316. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  317. return (spell_rpg_data) ? spell_rpg_data->getId() : -1;
  318. }
  319. DefineEngineMethod( afxSpellButton, useFreeTargeting, bool, (),,
  320. "Test if spell uses free targeting.\n" )
  321. {
  322. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  323. return (spell_rpg_data) ? (spell_rpg_data->spell_target == afxRPGMagicSpellData::TARGET_FREE) : false;
  324. }
  325. DefineEngineMethod( afxSpellButton, getFreeTargetStyle, S32, (),,
  326. "Get the free targeting style used by the spell.\n" )
  327. {
  328. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  329. return (spell_rpg_data) ? spell_rpg_data->free_target_style : 0;
  330. }
  331. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//