afxSpellButton.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. addField("bitmap", TypeFilename, Offset(mBitmapName, afxSpellButton),
  72. "...");
  73. addField("book_slot", TypePoint2I, Offset(book_slot, afxSpellButton),
  74. "...");
  75. Parent::initPersistFields();
  76. Con::addVariable("pref::afxSpellButton::unknownSpellBitmap", TypeFilename, &sUnknownSpellBitmap);
  77. Con::addVariable("pref::afxSpellButton::spellCooldownBitmaps", TypeFilename, &sSpellCooldownBitmaps);
  78. }
  79. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  80. bool afxSpellButton::onAdd()
  81. {
  82. if (!Parent::onAdd())
  83. return false;
  84. if (sSpellCooldownBitmaps != NULL)
  85. {
  86. char buffer[256];
  87. for (int i = 0; i < NUM_COOLDOWN_FRAMES; i++)
  88. {
  89. dSprintf(buffer, 256, "%s_%.2d", sSpellCooldownBitmaps, i);
  90. cooldown_txrs[i].set(buffer, COOLDOWN_PROFILE);
  91. }
  92. }
  93. return true;
  94. }
  95. bool afxSpellButton::onWake()
  96. {
  97. if (! Parent::onWake())
  98. return false;
  99. setActive(true);
  100. update_bitmap();
  101. return true;
  102. }
  103. void afxSpellButton::onSleep()
  104. {
  105. mTextureNormal = NULL;
  106. mTextureHilight = NULL;
  107. mTextureDepressed = NULL;
  108. Parent::onSleep();
  109. }
  110. void afxSpellButton::onMouseEnter(const GuiEvent &event)
  111. {
  112. Parent::onMouseEnter(event);
  113. Con::executef(this, "onMouseEnter");
  114. }
  115. void afxSpellButton::onMouseLeave(const GuiEvent &event)
  116. {
  117. Parent::onMouseLeave(event);
  118. Con::executef(this, "onMouseLeave");
  119. }
  120. void afxSpellButton::inspectPostApply()
  121. {
  122. // if the extent is set to (0,0) in the gui editor and apply hit, this control will
  123. // set it's extent to be exactly the size of the normal bitmap (if present)
  124. Parent::inspectPostApply();
  125. if ((getWidth() == 0) && (getHeight() == 0) && mTextureNormal)
  126. {
  127. setExtent(mTextureNormal->getWidth(), mTextureNormal->getHeight());
  128. }
  129. }
  130. void afxSpellButton::setBitmap(const char *name, bool placeholder)
  131. {
  132. mBitmapName = (name) ? StringTable->insert(name) : ST_NULLSTRING;
  133. if (!isAwake())
  134. return;
  135. if (mBitmapName != ST_NULLSTRING)
  136. {
  137. char buffer[1024];
  138. char *p;
  139. if (placeholder)
  140. {
  141. dStrcpy(buffer, name, 1024);
  142. S32 pLen = 1024 - dStrlen(buffer);
  143. p = buffer + dStrlen(buffer);
  144. dStrcpy(p, "_i", pLen);
  145. mTextureInactive.set(buffer, COOLDOWN_PROFILE);
  146. mTextureNormal = mTextureInactive;
  147. mTextureHilight = mTextureInactive;
  148. mTextureDepressed = mTextureInactive;
  149. setActive(false);
  150. }
  151. else
  152. {
  153. dStrcpy(buffer, name, 1024);
  154. S32 pLen = 1024 - dStrlen(buffer);
  155. p = buffer + dStrlen(buffer);
  156. dStrcpy(p, "_n", pLen);
  157. mTextureNormal.set(buffer, COOLDOWN_PROFILE);
  158. dStrcpy(p, "_h", pLen);
  159. mTextureHilight.set(buffer, COOLDOWN_PROFILE);
  160. if (!mTextureHilight)
  161. mTextureHilight = mTextureNormal;
  162. dStrcpy(p, "_d", pLen);
  163. mTextureDepressed.set(buffer, COOLDOWN_PROFILE);
  164. if (!mTextureDepressed)
  165. mTextureDepressed = mTextureHilight;
  166. dStrcpy(p, "_i", pLen);
  167. mTextureInactive.set(buffer, COOLDOWN_PROFILE);
  168. if (!mTextureInactive)
  169. mTextureInactive = mTextureNormal;
  170. setActive(true);
  171. }
  172. }
  173. else
  174. {
  175. mTextureNormal = NULL;
  176. mTextureHilight = NULL;
  177. mTextureDepressed = NULL;
  178. mTextureInactive = NULL;
  179. }
  180. setUpdate();
  181. }
  182. void afxSpellButton::onRender(Point2I offset, const RectI& updateRect)
  183. {
  184. enum { NORMAL, HILIGHT, DEPRESSED, INACTIVE } state = NORMAL;
  185. if (mActive)
  186. {
  187. if (mHighlighted) state = HILIGHT;
  188. if (mDepressed || mStateOn) state = DEPRESSED;
  189. }
  190. else
  191. state = INACTIVE;
  192. switch (state)
  193. {
  194. case NORMAL: renderButton(mTextureNormal, offset, updateRect); break;
  195. case HILIGHT: renderButton(mTextureHilight ? mTextureHilight : mTextureNormal, offset, updateRect); break;
  196. case DEPRESSED: renderButton(mTextureDepressed, offset, updateRect); break;
  197. case INACTIVE: renderButton(mTextureInactive ? mTextureInactive : mTextureNormal, offset, updateRect); break;
  198. }
  199. }
  200. void afxSpellButton::onDeleteNotify(SimObject* obj)
  201. {
  202. // Handle Shape Deletion
  203. afxSpellBook* book = dynamic_cast<afxSpellBook*>(obj);
  204. if (book != NULL)
  205. {
  206. if (book == spellbook)
  207. {
  208. spellbook = NULL;
  209. setBitmap("");
  210. setVisible(false);
  211. return;
  212. }
  213. }
  214. Parent::onDeleteNotify(obj);
  215. }
  216. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  217. // protected:
  218. void afxSpellButton::renderButton(GFXTexHandle &texture, Point2I &offset,
  219. const RectI& updateRect)
  220. {
  221. if (texture)
  222. {
  223. RectI rect(offset, getExtent());
  224. GFX->getDrawUtil()->clearBitmapModulation();
  225. GFX->getDrawUtil()->drawBitmapStretch(texture, rect);
  226. if (spellbook)
  227. {
  228. F32 cooldown = spellbook->getCooldownFactor(book_slot.x, book_slot.y);
  229. if (cooldown < 1.0f)
  230. {
  231. if (cooldown_txrs[(int)(36.0f*cooldown)])
  232. GFX->getDrawUtil()->drawBitmapStretch(cooldown_txrs[(int)(36.0f*cooldown)], rect);
  233. }
  234. }
  235. renderChildControls( offset, updateRect);
  236. }
  237. else
  238. Parent::onRender(offset, updateRect);
  239. }
  240. void afxSpellButton::update_bitmap()
  241. {
  242. const char* icon_name = 0;
  243. bool is_placeholder = false;
  244. if (spellbook)
  245. {
  246. icon_name = spellbook->getSpellIcon(book_slot.x, book_slot.y);
  247. is_placeholder = spellbook->isPlaceholder(book_slot.x, book_slot.y);
  248. if (icon_name && icon_name[0] == '\0')
  249. icon_name = sUnknownSpellBitmap;
  250. }
  251. if (icon_name)
  252. {
  253. setBitmap(icon_name, is_placeholder);
  254. setVisible(true);
  255. }
  256. else
  257. {
  258. setBitmap("");
  259. setVisible(false);
  260. }
  261. }
  262. void afxSpellButton::setSpellBook(afxSpellBook* book, U8 page)
  263. {
  264. book_slot.x = page;
  265. if (spellbook)
  266. clearNotify(spellbook);
  267. spellbook = book;
  268. update_bitmap();
  269. if (spellbook)
  270. deleteNotify(spellbook);
  271. }
  272. void afxSpellButton::setPage(U8 page)
  273. {
  274. book_slot.x = page;
  275. update_bitmap();
  276. }
  277. const char* afxSpellButton::formatDesc(char* buffer, int len) const
  278. {
  279. return (spellbook) ? spellbook->formatDesc(buffer, len, book_slot.x, book_slot.y) : "";
  280. }
  281. afxMagicSpellData* afxSpellButton::getSpellDataBlock() const
  282. {
  283. return (spellbook) ? spellbook->getSpellData(book_slot.x, book_slot.y) : 0;
  284. }
  285. afxRPGMagicSpellData* afxSpellButton::getSpellRPGDataBlock() const
  286. {
  287. return (spellbook) ? spellbook->getSpellRPGData(book_slot.x, book_slot.y) : 0;
  288. }
  289. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  290. DefineEngineMethod( afxSpellButton, onSpellbookChange, void, ( afxSpellBook* spellbook, U32 page ),,
  291. "Notify an afxSpellButton when its associated spellbook has changed.\n" )
  292. {
  293. object->setSpellBook(spellbook, (U8)page);
  294. }
  295. DefineEngineMethod( afxSpellButton, onTurnPage, void, (U32 page ),,
  296. "Notify an afxSpellButton when the spellbook turns to a new page.\n" )
  297. {
  298. object->setPage((U8)page);
  299. }
  300. DefineEngineMethod( afxSpellButton, getSpellDescription, const char*, (),,
  301. "Get the text description of a spell.\n" )
  302. {
  303. char buf[1024];
  304. return object->formatDesc(buf, 1024);
  305. }
  306. DefineEngineMethod( afxSpellButton, getSpellDataBlock, S32, (),,
  307. "Get the spell's datablock.\n" )
  308. {
  309. afxMagicSpellData* spell_data = object->getSpellDataBlock();
  310. return (spell_data) ? spell_data->getId() : -1;
  311. }
  312. DefineEngineMethod( afxSpellButton, getSpellRPGDataBlock, S32, (),,
  313. "Get the spell's RPG datablock.\n" )
  314. {
  315. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  316. return (spell_rpg_data) ? spell_rpg_data->getId() : -1;
  317. }
  318. DefineEngineMethod( afxSpellButton, useFreeTargeting, bool, (),,
  319. "Test if spell uses free targeting.\n" )
  320. {
  321. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  322. return (spell_rpg_data) ? (spell_rpg_data->spell_target == afxRPGMagicSpellData::TARGET_FREE) : false;
  323. }
  324. DefineEngineMethod( afxSpellButton, getFreeTargetStyle, S32, (),,
  325. "Get the free targeting style used by the spell.\n" )
  326. {
  327. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  328. return (spell_rpg_data) ? spell_rpg_data->free_target_style : 0;
  329. }
  330. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//