CCursorHandler.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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::clearBuffer()
  18. {
  19. Uint32 fillColor = SDL_MapRGBA(buffer->format, 0, 0, 0, 0);
  20. CSDL_Ext::fillRect(buffer, nullptr, fillColor);
  21. }
  22. void CCursorHandler::updateBuffer(CIntObject * payload)
  23. {
  24. payload->moveTo(Point(0,0));
  25. payload->showAll(buffer);
  26. needUpdate = true;
  27. }
  28. void CCursorHandler::replaceBuffer(CIntObject * payload)
  29. {
  30. clearBuffer();
  31. updateBuffer(payload);
  32. }
  33. void CCursorHandler::initCursor()
  34. {
  35. cursorLayer = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 40, 40);
  36. SDL_SetTextureBlendMode(cursorLayer, SDL_BLENDMODE_BLEND);
  37. xpos = ypos = 0;
  38. type = ECursor::DEFAULT;
  39. dndObject = nullptr;
  40. cursors =
  41. {
  42. std::make_unique<CAnimImage>("CRADVNTR", 0),
  43. std::make_unique<CAnimImage>("CRCOMBAT", 0),
  44. std::make_unique<CAnimImage>("CRDEFLT", 0),
  45. std::make_unique<CAnimImage>("CRSPELL", 0)
  46. };
  47. currentCursor = cursors.at(int(ECursor::DEFAULT)).get();
  48. buffer = CSDL_Ext::newSurface(40,40);
  49. SDL_SetSurfaceBlendMode(buffer, SDL_BLENDMODE_NONE);
  50. SDL_ShowCursor(SDL_DISABLE);
  51. changeGraphic(ECursor::ADVENTURE, 0);
  52. }
  53. void CCursorHandler::changeGraphic(ECursor::ECursorTypes type, int index)
  54. {
  55. if(type != this->type)
  56. {
  57. this->type = type;
  58. this->frame = index;
  59. currentCursor = cursors.at(int(type)).get();
  60. currentCursor->setFrame(index);
  61. }
  62. else if(index != this->frame)
  63. {
  64. this->frame = index;
  65. currentCursor->setFrame(index);
  66. }
  67. replaceBuffer(currentCursor);
  68. }
  69. void CCursorHandler::dragAndDropCursor(std::unique_ptr<CAnimImage> object)
  70. {
  71. dndObject = std::move(object);
  72. if(dndObject)
  73. replaceBuffer(dndObject.get());
  74. else
  75. replaceBuffer(currentCursor);
  76. }
  77. void CCursorHandler::cursorMove(const int & x, const int & y)
  78. {
  79. xpos = x;
  80. ypos = y;
  81. }
  82. void CCursorHandler::shiftPos( int &x, int &y )
  83. {
  84. if(( type == ECursor::COMBAT && frame != ECursor::COMBAT_POINTER) || type == ECursor::SPELLBOOK)
  85. {
  86. x-=16;
  87. y-=16;
  88. // Properly align the melee attack cursors.
  89. if (type == ECursor::COMBAT)
  90. {
  91. switch (frame)
  92. {
  93. case 7: // Bottom left
  94. x -= 6;
  95. y += 16;
  96. break;
  97. case 8: // Left
  98. x -= 16;
  99. y += 10;
  100. break;
  101. case 9: // Top left
  102. x -= 6;
  103. y -= 6;
  104. break;
  105. case 10: // Top right
  106. x += 16;
  107. y -= 6;
  108. break;
  109. case 11: // Right
  110. x += 16;
  111. y += 11;
  112. break;
  113. case 12: // Bottom right
  114. x += 16;
  115. y += 16;
  116. break;
  117. case 13: // Below
  118. x += 9;
  119. y += 16;
  120. break;
  121. case 14: // Above
  122. x += 9;
  123. y -= 15;
  124. break;
  125. }
  126. }
  127. }
  128. else if(type == ECursor::ADVENTURE)
  129. {
  130. if (frame == 0); //to exclude
  131. else if(frame == 2)
  132. {
  133. x -= 12;
  134. y -= 10;
  135. }
  136. else if(frame == 3)
  137. {
  138. x -= 12;
  139. y -= 12;
  140. }
  141. else if(frame < 27)
  142. {
  143. int hlpNum = (frame - 4)%6;
  144. if(hlpNum == 0)
  145. {
  146. x -= 15;
  147. y -= 13;
  148. }
  149. else if(hlpNum == 1)
  150. {
  151. x -= 13;
  152. y -= 13;
  153. }
  154. else if(hlpNum == 2)
  155. {
  156. x -= 20;
  157. y -= 20;
  158. }
  159. else if(hlpNum == 3)
  160. {
  161. x -= 13;
  162. y -= 16;
  163. }
  164. else if(hlpNum == 4)
  165. {
  166. x -= 8;
  167. y -= 9;
  168. }
  169. else if(hlpNum == 5)
  170. {
  171. x -= 14;
  172. y -= 16;
  173. }
  174. }
  175. else if(frame == 41)
  176. {
  177. x -= 14;
  178. y -= 16;
  179. }
  180. else if(frame < 31 || frame == 42)
  181. {
  182. x -= 20;
  183. y -= 20;
  184. }
  185. }
  186. }
  187. void CCursorHandler::centerCursor()
  188. {
  189. this->xpos = static_cast<int>((screen->w / 2.) - (currentCursor->pos.w / 2.));
  190. this->ypos = static_cast<int>((screen->h / 2.) - (currentCursor->pos.h / 2.));
  191. SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
  192. SDL_WarpMouse(this->xpos, this->ypos);
  193. SDL_EventState(SDL_MOUSEMOTION, SDL_ENABLE);
  194. }
  195. void CCursorHandler::render()
  196. {
  197. if(!showing)
  198. return;
  199. //the must update texture in the main (renderer) thread, but changes to cursor type may come from other threads
  200. updateTexture();
  201. int x = xpos;
  202. int y = ypos;
  203. shiftPos(x, y);
  204. if(dndObject)
  205. {
  206. x -= dndObject->pos.w/2;
  207. y -= dndObject->pos.h/2;
  208. }
  209. SDL_Rect destRect;
  210. destRect.x = x;
  211. destRect.y = y;
  212. destRect.w = 40;
  213. destRect.h = 40;
  214. SDL_RenderCopy(mainRenderer, cursorLayer, nullptr, &destRect);
  215. }
  216. void CCursorHandler::updateTexture()
  217. {
  218. if(needUpdate)
  219. {
  220. SDL_UpdateTexture(cursorLayer, nullptr, buffer->pixels, buffer->pitch);
  221. needUpdate = false;
  222. }
  223. }
  224. CCursorHandler::CCursorHandler()
  225. : needUpdate(true),
  226. buffer(nullptr),
  227. cursorLayer(nullptr),
  228. showing(false)
  229. {
  230. }
  231. CCursorHandler::~CCursorHandler()
  232. {
  233. if(buffer)
  234. SDL_FreeSurface(buffer);
  235. if(cursorLayer)
  236. SDL_DestroyTexture(cursorLayer);
  237. }