CursorHardware.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * CursorHardware.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 "CursorHardware.h"
  12. #include "SDL_Extensions.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../render/IScreenHandler.h"
  15. #include "../render/Colors.h"
  16. #include "../render/IImage.h"
  17. #include "../../lib/CConfigHandler.h"
  18. #include <SDL_render.h>
  19. #include <SDL_events.h>
  20. CursorHardware::CursorHardware():
  21. cursor(nullptr)
  22. {
  23. SDL_ShowCursor(SDL_DISABLE);
  24. }
  25. CursorHardware::~CursorHardware()
  26. {
  27. if(cursor)
  28. SDL_FreeCursor(cursor);
  29. }
  30. void CursorHardware::setVisible(bool on)
  31. {
  32. GH.dispatchMainThread([on]()
  33. {
  34. if (on)
  35. SDL_ShowCursor(SDL_ENABLE);
  36. else
  37. SDL_ShowCursor(SDL_DISABLE);
  38. });
  39. }
  40. void CursorHardware::setImage(std::shared_ptr<IImage> image, const Point & pivotOffset)
  41. {
  42. int videoScalingSettings = GH.screenHandler().getInterfaceScalingPercentage();
  43. float cursorScalingSettings = settings["video"]["cursorScalingFactor"].Float();
  44. int cursorScalingPercent = videoScalingSettings * cursorScalingSettings;
  45. Point cursorDimensions = image->dimensions() * GH.screenHandler().getScalingFactor();
  46. Point cursorDimensionsScaled = image->dimensions() * cursorScalingPercent / 100;
  47. Point pivotOffsetScaled = pivotOffset * cursorScalingPercent / 100 / GH.screenHandler().getScalingFactor();
  48. auto cursorSurface = CSDL_Ext::newSurface(cursorDimensions);
  49. CSDL_Ext::fillSurface(cursorSurface, CSDL_Ext::toSDL(Colors::TRANSPARENCY));
  50. image->draw(cursorSurface, Point(0,0), nullptr, GH.screenHandler().getScalingFactor());
  51. auto cursorSurfaceScaled = CSDL_Ext::scaleSurface(cursorSurface, cursorDimensionsScaled.x, cursorDimensionsScaled.y, EScalingAlgorithm::BILINEAR );
  52. auto oldCursor = cursor;
  53. cursor = SDL_CreateColorCursor(cursorSurfaceScaled, pivotOffsetScaled.x, pivotOffsetScaled.y);
  54. if (!cursor)
  55. logGlobal->error("Failed to set cursor! SDL says %s", SDL_GetError());
  56. SDL_FreeSurface(cursorSurface);
  57. SDL_FreeSurface(cursorSurfaceScaled);
  58. GH.dispatchMainThread([this, oldCursor](){
  59. SDL_SetCursor(cursor);
  60. if (oldCursor)
  61. SDL_FreeCursor(oldCursor);
  62. });
  63. }
  64. void CursorHardware::setCursorPosition( const Point & newPos )
  65. {
  66. //no-op
  67. }
  68. void CursorHardware::render()
  69. {
  70. //no-op
  71. }