CCursorHandler.cpp 5.0 KB

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