afxSpellButton.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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);
  142. p = buffer + dStrlen(buffer);
  143. dStrcpy(p, "_i");
  144. mTextureInactive.set(buffer, COOLDOWN_PROFILE);
  145. mTextureNormal = mTextureInactive;
  146. mTextureHilight = mTextureInactive;
  147. mTextureDepressed = mTextureInactive;
  148. setActive(false);
  149. }
  150. else
  151. {
  152. dStrcpy(buffer, name);
  153. p = buffer + dStrlen(buffer);
  154. dStrcpy(p, "_n");
  155. mTextureNormal.set(buffer, COOLDOWN_PROFILE);
  156. dStrcpy(p, "_h");
  157. mTextureHilight.set(buffer, COOLDOWN_PROFILE);
  158. if (!mTextureHilight)
  159. mTextureHilight = mTextureNormal;
  160. dStrcpy(p, "_d");
  161. mTextureDepressed.set(buffer, COOLDOWN_PROFILE);
  162. if (!mTextureDepressed)
  163. mTextureDepressed = mTextureHilight;
  164. dStrcpy(p, "_i");
  165. mTextureInactive.set(buffer, COOLDOWN_PROFILE);
  166. if (!mTextureInactive)
  167. mTextureInactive = mTextureNormal;
  168. setActive(true);
  169. }
  170. }
  171. else
  172. {
  173. mTextureNormal = NULL;
  174. mTextureHilight = NULL;
  175. mTextureDepressed = NULL;
  176. mTextureInactive = NULL;
  177. }
  178. setUpdate();
  179. }
  180. void afxSpellButton::onRender(Point2I offset, const RectI& updateRect)
  181. {
  182. enum { NORMAL, HILIGHT, DEPRESSED, INACTIVE } state = NORMAL;
  183. if (mActive)
  184. {
  185. if (mMouseOver) state = HILIGHT;
  186. if (mDepressed || mStateOn) state = DEPRESSED;
  187. }
  188. else
  189. state = INACTIVE;
  190. switch (state)
  191. {
  192. case NORMAL: renderButton(mTextureNormal, offset, updateRect); break;
  193. case HILIGHT: renderButton(mTextureHilight ? mTextureHilight : mTextureNormal, offset, updateRect); break;
  194. case DEPRESSED: renderButton(mTextureDepressed, offset, updateRect); break;
  195. case INACTIVE: renderButton(mTextureInactive ? mTextureInactive : mTextureNormal, offset, updateRect); break;
  196. }
  197. }
  198. void afxSpellButton::onDeleteNotify(SimObject* obj)
  199. {
  200. // Handle Shape Deletion
  201. afxSpellBook* book = dynamic_cast<afxSpellBook*>(obj);
  202. if (book != NULL)
  203. {
  204. if (book == spellbook)
  205. {
  206. spellbook = NULL;
  207. setBitmap("");
  208. setVisible(false);
  209. return;
  210. }
  211. }
  212. Parent::onDeleteNotify(obj);
  213. }
  214. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  215. // protected:
  216. void afxSpellButton::renderButton(GFXTexHandle &texture, Point2I &offset,
  217. const RectI& updateRect)
  218. {
  219. if (texture)
  220. {
  221. RectI rect(offset, getExtent());
  222. GFX->getDrawUtil()->clearBitmapModulation();
  223. GFX->getDrawUtil()->drawBitmapStretch(texture, rect);
  224. if (spellbook)
  225. {
  226. F32 cooldown = spellbook->getCooldownFactor(book_slot.x, book_slot.y);
  227. if (cooldown < 1.0f)
  228. {
  229. if (cooldown_txrs[(int)(36.0f*cooldown)])
  230. GFX->getDrawUtil()->drawBitmapStretch(cooldown_txrs[(int)(36.0f*cooldown)], rect);
  231. }
  232. }
  233. renderChildControls( offset, updateRect);
  234. }
  235. else
  236. Parent::onRender(offset, updateRect);
  237. }
  238. void afxSpellButton::update_bitmap()
  239. {
  240. const char* icon_name = 0;
  241. bool is_placeholder = false;
  242. if (spellbook)
  243. {
  244. icon_name = spellbook->getSpellIcon(book_slot.x, book_slot.y);
  245. is_placeholder = spellbook->isPlaceholder(book_slot.x, book_slot.y);
  246. if (icon_name && icon_name[0] == '\0')
  247. icon_name = sUnknownSpellBitmap;
  248. }
  249. if (icon_name)
  250. {
  251. setBitmap(icon_name, is_placeholder);
  252. setVisible(true);
  253. }
  254. else
  255. {
  256. setBitmap("");
  257. setVisible(false);
  258. }
  259. }
  260. void afxSpellButton::setSpellBook(afxSpellBook* book, U8 page)
  261. {
  262. book_slot.x = page;
  263. if (spellbook)
  264. clearNotify(spellbook);
  265. spellbook = book;
  266. update_bitmap();
  267. if (spellbook)
  268. deleteNotify(spellbook);
  269. }
  270. void afxSpellButton::setPage(U8 page)
  271. {
  272. book_slot.x = page;
  273. update_bitmap();
  274. }
  275. char* afxSpellButton::formatDesc(char* buffer, int len) const
  276. {
  277. return (spellbook) ? spellbook->formatDesc(buffer, len, book_slot.x, book_slot.y) : (char*)"";
  278. }
  279. afxMagicSpellData* afxSpellButton::getSpellDataBlock() const
  280. {
  281. return (spellbook) ? spellbook->getSpellData(book_slot.x, book_slot.y) : 0;
  282. }
  283. afxRPGMagicSpellData* afxSpellButton::getSpellRPGDataBlock() const
  284. {
  285. return (spellbook) ? spellbook->getSpellRPGData(book_slot.x, book_slot.y) : 0;
  286. }
  287. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  288. DefineEngineMethod( afxSpellButton, onSpellbookChange, void, ( afxSpellBook* spellbook, U32 page ),,
  289. "Notify an afxSpellButton when its associated spellbook has changed.\n" )
  290. {
  291. object->setSpellBook(spellbook, (U8)page);
  292. }
  293. DefineEngineMethod( afxSpellButton, onTurnPage, void, (U32 page ),,
  294. "Notify an afxSpellButton when the spellbook turns to a new page.\n" )
  295. {
  296. object->setPage((U8)page);
  297. }
  298. DefineEngineMethod( afxSpellButton, getSpellDescription, const char*, (),,
  299. "Get the text description of a spell.\n" )
  300. {
  301. char buf[1024];
  302. return object->formatDesc(buf, 1024);
  303. }
  304. DefineEngineMethod( afxSpellButton, getSpellDataBlock, S32, (),,
  305. "Get the spell's datablock.\n" )
  306. {
  307. afxMagicSpellData* spell_data = object->getSpellDataBlock();
  308. return (spell_data) ? spell_data->getId() : -1;
  309. }
  310. DefineEngineMethod( afxSpellButton, getSpellRPGDataBlock, S32, (),,
  311. "Get the spell's RPG datablock.\n" )
  312. {
  313. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  314. return (spell_rpg_data) ? spell_rpg_data->getId() : -1;
  315. }
  316. DefineEngineMethod( afxSpellButton, useFreeTargeting, bool, (),,
  317. "Test if spell uses free targeting.\n" )
  318. {
  319. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  320. return (spell_rpg_data) ? (spell_rpg_data->spell_target == afxRPGMagicSpellData::TARGET_FREE) : false;
  321. }
  322. DefineEngineMethod( afxSpellButton, getFreeTargetStyle, S32, (),,
  323. "Get the free targeting style used by the spell.\n" )
  324. {
  325. afxRPGMagicSpellData* spell_rpg_data = object->getSpellRPGDataBlock();
  326. return (spell_rpg_data) ? spell_rpg_data->free_target_style : 0;
  327. }
  328. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//