CCursorHandler.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "StdInc.h"
  2. #include "CCursorHandler.h"
  3. #include <SDL.h>
  4. #include "SDL_Extensions.h"
  5. #include "CGuiHandler.h"
  6. #include "widgets/Images.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. //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) return;
  60. int x = xpos, y = ypos;
  61. shiftPos(x, y);
  62. SDL_Rect temp_rect1 = genRect(40,40,x,y);
  63. SDL_Rect temp_rect2 = genRect(40,40,0,0);
  64. SDL_BlitSurface(screen, &temp_rect1, help, &temp_rect2);
  65. if (dndObject)
  66. {
  67. dndObject->moveTo(Point(x - dndObject->pos.w/2, y - dndObject->pos.h/2));
  68. dndObject->showAll(screen);
  69. }
  70. else
  71. {
  72. currentCursor->moveTo(Point(x,y));
  73. currentCursor->showAll(screen);
  74. }
  75. }
  76. void CCursorHandler::drawRestored()
  77. {
  78. if(!showing)
  79. return;
  80. int x = xpos, y = ypos;
  81. shiftPos(x, y);
  82. SDL_Rect temp_rect = genRect(40, 40, x, y);
  83. SDL_BlitSurface(help, nullptr, screen, &temp_rect);
  84. //blitAt(help,x,y);
  85. }
  86. void CCursorHandler::draw(SDL_Surface *to)
  87. {
  88. currentCursor->moveTo(Point(xpos, ypos));
  89. currentCursor->showAll(screen);
  90. }
  91. void CCursorHandler::shiftPos( int &x, int &y )
  92. {
  93. if(( type == ECursor::COMBAT && frame != ECursor::COMBAT_POINTER) || type == ECursor::SPELLBOOK)
  94. {
  95. x-=16;
  96. y-=16;
  97. // Properly align the melee attack cursors.
  98. if (type == ECursor::COMBAT)
  99. {
  100. switch (frame)
  101. {
  102. case 7: // Bottom left
  103. x -= 6;
  104. y += 16;
  105. break;
  106. case 8: // Left
  107. x -= 16;
  108. y += 10;
  109. break;
  110. case 9: // Top left
  111. x -= 6;
  112. y -= 6;
  113. break;
  114. case 10: // Top right
  115. x += 16;
  116. y -= 6;
  117. break;
  118. case 11: // Right
  119. x += 16;
  120. y += 11;
  121. break;
  122. case 12: // Bottom right
  123. x += 16;
  124. y += 16;
  125. break;
  126. case 13: // Below
  127. x += 9;
  128. y += 16;
  129. break;
  130. case 14: // Above
  131. x += 9;
  132. y -= 15;
  133. break;
  134. }
  135. }
  136. }
  137. else if(type == ECursor::ADVENTURE)
  138. {
  139. if (frame == 0); //to exclude
  140. else if(frame == 2)
  141. {
  142. x -= 12;
  143. y -= 10;
  144. }
  145. else if(frame == 3)
  146. {
  147. x -= 12;
  148. y -= 12;
  149. }
  150. else if(frame < 27)
  151. {
  152. int hlpNum = (frame - 4)%6;
  153. if(hlpNum == 0)
  154. {
  155. x -= 15;
  156. y -= 13;
  157. }
  158. else if(hlpNum == 1)
  159. {
  160. x -= 13;
  161. y -= 13;
  162. }
  163. else if(hlpNum == 2)
  164. {
  165. x -= 20;
  166. y -= 20;
  167. }
  168. else if(hlpNum == 3)
  169. {
  170. x -= 13;
  171. y -= 16;
  172. }
  173. else if(hlpNum == 4)
  174. {
  175. x -= 8;
  176. y -= 9;
  177. }
  178. else if(hlpNum == 5)
  179. {
  180. x -= 14;
  181. y -= 16;
  182. }
  183. }
  184. else if(frame == 41)
  185. {
  186. x -= 14;
  187. y -= 16;
  188. }
  189. else if(frame < 31 || frame == 42)
  190. {
  191. x -= 20;
  192. y -= 20;
  193. }
  194. }
  195. }
  196. void CCursorHandler::centerCursor()
  197. {
  198. this->xpos = (screen->w / 2.) - (currentCursor->pos.w / 2.);
  199. this->ypos = (screen->h / 2.) - (currentCursor->pos.h / 2.);
  200. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  201. SDL_WarpMouse(this->xpos, this->ypos);
  202. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  203. }
  204. void CCursorHandler::render()
  205. {
  206. drawWithScreenRestore();
  207. CSDL_Ext::update(screen);
  208. drawRestored();
  209. }
  210. CCursorHandler::~CCursorHandler()
  211. {
  212. if(help)
  213. SDL_FreeSurface(help);
  214. delete currentCursor;
  215. delete dndObject;
  216. }