CCursorHandler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "StdInc.h"
  2. #include "CCursorHandler.h"
  3. #include <SDL.h>
  4. #include "SDL_Extensions.h"
  5. #include "CGuiHandler.h"
  6. #include "../CMT.h"
  7. #include "../widgets/CAnimation.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. #ifndef VCMI_SDL1
  25. //No blending. Ensure, that we are copying pixels during "screen restore draw"
  26. SDL_SetSurfaceBlendMode(help,SDL_BLENDMODE_NONE);
  27. #endif // VCMI_SDL1
  28. SDL_ShowCursor(SDL_DISABLE);
  29. changeGraphic(ECursor::ADVENTURE, 0);
  30. }
  31. void CCursorHandler::changeGraphic(ECursor::ECursorTypes type, int index)
  32. {
  33. std::string cursorDefs[4] = { "CRADVNTR.DEF", "CRCOMBAT.DEF", "CRDEFLT.DEF", "CRSPELL.DEF" };
  34. if (type != this->type)
  35. {
  36. BLOCK_CAPTURING; // not used here
  37. this->type = type;
  38. this->frame = index;
  39. delete currentCursor;
  40. currentCursor = new CAnimImage(cursorDefs[int(type)], index);
  41. }
  42. if (frame != index)
  43. {
  44. frame = index;
  45. currentCursor->setFrame(index);
  46. }
  47. }
  48. void CCursorHandler::dragAndDropCursor(CAnimImage * object)
  49. {
  50. if (dndObject)
  51. delete dndObject;
  52. dndObject = object;
  53. }
  54. void CCursorHandler::cursorMove(const int & x, const int & y)
  55. {
  56. xpos = x;
  57. ypos = y;
  58. }
  59. void CCursorHandler::drawWithScreenRestore()
  60. {
  61. if(!showing) return;
  62. int x = xpos, y = ypos;
  63. shiftPos(x, y);
  64. SDL_Rect temp_rect1 = genRect(40,40,x,y);
  65. SDL_Rect temp_rect2 = genRect(40,40,0,0);
  66. SDL_BlitSurface(screen, &temp_rect1, help, &temp_rect2);
  67. if (dndObject)
  68. {
  69. dndObject->moveTo(Point(x - dndObject->pos.w/2, y - dndObject->pos.h/2));
  70. dndObject->showAll(screen);
  71. }
  72. else
  73. {
  74. currentCursor->moveTo(Point(x,y));
  75. currentCursor->showAll(screen);
  76. }
  77. }
  78. void CCursorHandler::drawRestored()
  79. {
  80. if(!showing)
  81. return;
  82. int x = xpos, y = ypos;
  83. shiftPos(x, y);
  84. SDL_Rect temp_rect = genRect(40, 40, x, y);
  85. SDL_BlitSurface(help, nullptr, screen, &temp_rect);
  86. //blitAt(help,x,y);
  87. }
  88. void CCursorHandler::draw(SDL_Surface *to)
  89. {
  90. currentCursor->moveTo(Point(xpos, ypos));
  91. currentCursor->showAll(screen);
  92. }
  93. void CCursorHandler::shiftPos( int &x, int &y )
  94. {
  95. if(( type == ECursor::COMBAT && frame != ECursor::COMBAT_POINTER) || type == ECursor::SPELLBOOK)
  96. {
  97. x-=16;
  98. y-=16;
  99. // Properly align the melee attack cursors.
  100. if (type == ECursor::COMBAT)
  101. {
  102. switch (frame)
  103. {
  104. case 7: // Bottom left
  105. x -= 6;
  106. y += 16;
  107. break;
  108. case 8: // Left
  109. x -= 16;
  110. y += 10;
  111. break;
  112. case 9: // Top left
  113. x -= 6;
  114. y -= 6;
  115. break;
  116. case 10: // Top right
  117. x += 16;
  118. y -= 6;
  119. break;
  120. case 11: // Right
  121. x += 16;
  122. y += 11;
  123. break;
  124. case 12: // Bottom right
  125. x += 16;
  126. y += 16;
  127. break;
  128. case 13: // Below
  129. x += 9;
  130. y += 16;
  131. break;
  132. case 14: // Above
  133. x += 9;
  134. y -= 15;
  135. break;
  136. }
  137. }
  138. }
  139. else if(type == ECursor::ADVENTURE)
  140. {
  141. if (frame == 0); //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. }