CCursorHandler.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include "StdInc.h"
  2. #include <SDL.h>
  3. #include "CCursorHandler.h"
  4. #include "GL2D.h"
  5. #include "../Gfx/Animations.h"
  6. #include "../Gfx/Images.h"
  7. #include "../CAnimation.h"
  8. #include "CGuiHandler.h"
  9. /*
  10. * CCursorHandler.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. CCursorHandler::CCursorHandler()
  19. {
  20. xpos = ypos = 0;
  21. type = ECursor::DEFAULT;
  22. dndObject = nullptr;
  23. currentCursor = nullptr;
  24. //* help = CSDL_Ext::newSurface(40,40);
  25. SDL_ShowCursor(SDL_DISABLE);
  26. changeGraphic(ECursor::ADVENTURE, 0);
  27. }
  28. void CCursorHandler::changeGraphic(ECursor::ECursorTypes type, int index)
  29. {
  30. const std::string cursorDefs[4] = { "CRADVNTR", "CRCOMBAT", "CRDEFLT", "CRSPELL" };
  31. if (type != this->type)
  32. {
  33. BLOCK_CAPTURING; // not used here
  34. this->type = type;
  35. this->frame = index;
  36. currentCursor = Gfx::CManager::getAnimation(cursorDefs[type]);
  37. }
  38. frame = index;
  39. }
  40. void CCursorHandler::dragAndDropCursor(CAnimImage * object)
  41. {
  42. if (dndObject) delete dndObject;
  43. dndObject = object;
  44. }
  45. void CCursorHandler::cursorMove(int x, int y)
  46. {
  47. xpos = x;
  48. ypos = y;
  49. }
  50. void CCursorHandler::drawWithScreenRestore()
  51. {
  52. if(!showing) return;
  53. int x = xpos, y = ypos;
  54. shiftPos(x, y);
  55. if (dndObject)
  56. {
  57. dndObject->moveTo(Point(x - dndObject->pos.w/2, y - dndObject->pos.h/2));
  58. dndObject->showAll();
  59. }
  60. else
  61. {
  62. currentCursor->getFrame(frame)->putAt(Gfx::Point(x, y));
  63. }
  64. }
  65. void CCursorHandler::drawRestored()
  66. {
  67. if(!showing)
  68. return;
  69. int x = xpos, y = ypos;
  70. shiftPos(x, y);
  71. }
  72. void CCursorHandler::draw()
  73. {
  74. currentCursor->getFrame(frame)->putAt(Gfx::Point(xpos, ypos));
  75. }
  76. void CCursorHandler::shiftPos( int &x, int &y )
  77. {
  78. if(( type == ECursor::COMBAT && frame != ECursor::COMBAT_POINTER) || type == ECursor::SPELLBOOK)
  79. {
  80. x-=16;
  81. y-=16;
  82. // Properly align the melee attack cursors.
  83. if (type == ECursor::COMBAT)
  84. {
  85. switch (frame)
  86. {
  87. case 7: // Bottom left
  88. x -= 6;
  89. y += 16;
  90. break;
  91. case 8: // Left
  92. x -= 16;
  93. y += 10;
  94. break;
  95. case 9: // Top left
  96. x -= 6;
  97. y -= 6;
  98. break;
  99. case 10: // Top right
  100. x += 16;
  101. y -= 6;
  102. break;
  103. case 11: // Right
  104. x += 16;
  105. y += 11;
  106. break;
  107. case 12: // Bottom right
  108. x += 16;
  109. y += 16;
  110. break;
  111. case 13: // Below
  112. x += 9;
  113. y += 16;
  114. break;
  115. case 14: // Above
  116. x += 9;
  117. y -= 15;
  118. break;
  119. }
  120. }
  121. }
  122. else if(type == ECursor::ADVENTURE)
  123. {
  124. if (frame == 0); //to exclude
  125. else if(frame == 2)
  126. {
  127. x -= 12;
  128. y -= 10;
  129. }
  130. else if(frame == 3)
  131. {
  132. x -= 12;
  133. y -= 12;
  134. }
  135. else if(frame < 27)
  136. {
  137. int hlpNum = (frame - 4)%6;
  138. if(hlpNum == 0)
  139. {
  140. x -= 15;
  141. y -= 13;
  142. }
  143. else if(hlpNum == 1)
  144. {
  145. x -= 13;
  146. y -= 13;
  147. }
  148. else if(hlpNum == 2)
  149. {
  150. x -= 20;
  151. y -= 20;
  152. }
  153. else if(hlpNum == 3)
  154. {
  155. x -= 13;
  156. y -= 16;
  157. }
  158. else if(hlpNum == 4)
  159. {
  160. x -= 8;
  161. y -= 9;
  162. }
  163. else if(hlpNum == 5)
  164. {
  165. x -= 14;
  166. y -= 16;
  167. }
  168. }
  169. else if(frame == 41)
  170. {
  171. x -= 14;
  172. y -= 16;
  173. }
  174. else if(frame < 31 || frame == 42)
  175. {
  176. x -= 20;
  177. y -= 20;
  178. }
  179. }
  180. }
  181. void CCursorHandler::centerCursor()
  182. {
  183. this->xpos = (GL2D::getScreenWidth() - currentCursor->getWidth()) / 2;
  184. this->ypos = (GL2D::getScreenHeight() - currentCursor->getHeight()) / 2;
  185. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  186. SDL_WarpMouse(this->xpos, this->ypos);
  187. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  188. }
  189. CCursorHandler::~CCursorHandler()
  190. {
  191. delete dndObject;
  192. }