ImageLocator.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * ImageLocator.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 "ImageLocator.h"
  12. #include "../gui/CGuiHandler.h"
  13. #include "IScreenHandler.h"
  14. #include "../../lib/json/JsonNode.h"
  15. ImageLocator::ImageLocator(const JsonNode & config)
  16. : image(ImagePath::fromJson(config["file"]))
  17. , defFile(AnimationPath::fromJson(config["defFile"]))
  18. , defFrame(config["defFrame"].Integer())
  19. , defGroup(config["defGroup"].Integer())
  20. , verticalFlip(config["verticalFlip"].Bool())
  21. , horizontalFlip(config["horizontalFlip"].Bool())
  22. {
  23. }
  24. ImageLocator::ImageLocator(const ImagePath & path)
  25. : image(path)
  26. {
  27. }
  28. ImageLocator::ImageLocator(const AnimationPath & path, int frame, int group)
  29. : defFile(path)
  30. , defFrame(frame)
  31. , defGroup(group)
  32. {
  33. }
  34. bool ImageLocator::operator<(const ImageLocator & other) const
  35. {
  36. if(image != other.image)
  37. return image < other.image;
  38. if(defFile != other.defFile)
  39. return defFile < other.defFile;
  40. if(defGroup != other.defGroup)
  41. return defGroup < other.defGroup;
  42. if(defFrame != other.defFrame)
  43. return defFrame < other.defFrame;
  44. if(verticalFlip != other.verticalFlip)
  45. return verticalFlip < other.verticalFlip;
  46. if(horizontalFlip != other.horizontalFlip)
  47. return horizontalFlip < other.horizontalFlip;
  48. if(scalingFactor != other.scalingFactor)
  49. return scalingFactor < other.scalingFactor;
  50. if(playerColored != other.playerColored)
  51. return playerColored < other.playerColored;
  52. if(layer != other.layer)
  53. return layer < other.layer;
  54. return false;
  55. }
  56. bool ImageLocator::empty() const
  57. {
  58. return !image.has_value() && !defFile.has_value();
  59. }
  60. ImageLocator ImageLocator::copyFile() const
  61. {
  62. ImageLocator result;
  63. result.scalingFactor = 1;
  64. result.preScaledFactor = preScaledFactor;
  65. result.image = image;
  66. result.defFile = defFile;
  67. result.defFrame = defFrame;
  68. result.defGroup = defGroup;
  69. return result;
  70. }
  71. ImageLocator ImageLocator::copyFileTransform() const
  72. {
  73. ImageLocator result = copyFile();
  74. result.horizontalFlip = horizontalFlip;
  75. result.verticalFlip = verticalFlip;
  76. return result;
  77. }
  78. ImageLocator ImageLocator::copyFileTransformScale() const
  79. {
  80. return *this; // full copy
  81. }
  82. std::string ImageLocator::toString() const
  83. {
  84. std::string result;
  85. if (empty())
  86. return "invalid";
  87. if (image)
  88. {
  89. result += image->getOriginalName();
  90. assert(!result.empty());
  91. }
  92. if (defFile)
  93. {
  94. result += defFile->getOriginalName();
  95. assert(!result.empty());
  96. result += "-" + std::to_string(defGroup);
  97. result += "-" + std::to_string(defFrame);
  98. }
  99. if (verticalFlip)
  100. result += "-vflip";
  101. if (horizontalFlip)
  102. result += "-hflip";
  103. if (scalingFactor > 1)
  104. result += "-scale" + std::to_string(scalingFactor);
  105. if (playerColored.isValidPlayer())
  106. result += "-player" + playerColored.toString();
  107. if (layer != EImageLayer::ALL)
  108. result += "-layer" + std::to_string(static_cast<int>(layer));
  109. return result;
  110. }