CCursorHandler.cpp 4.2 KB

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