CCursorHandler.cpp 4.4 KB

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