2
0

ImageLocator.cpp 3.1 KB

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