ImageLocator.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.image = image;
  65. result.defFile = defFile;
  66. result.defFrame = defFrame;
  67. result.defGroup = defGroup;
  68. return result;
  69. }
  70. ImageLocator ImageLocator::copyFileTransform() const
  71. {
  72. ImageLocator result = copyFile();
  73. result.horizontalFlip = horizontalFlip;
  74. result.verticalFlip = verticalFlip;
  75. return result;
  76. }
  77. ImageLocator ImageLocator::copyFileTransformScale() const
  78. {
  79. return *this; // full copy
  80. }
  81. std::string ImageLocator::toString() const
  82. {
  83. std::string result;
  84. if (empty())
  85. return "invalid";
  86. if (image)
  87. result += image->getOriginalName();
  88. if (defFile)
  89. {
  90. result += defFile->getOriginalName();
  91. result += "-" + std::to_string(defGroup);
  92. result += "-" + std::to_string(defFrame);
  93. }
  94. if (verticalFlip)
  95. result += "-vflip";
  96. if (horizontalFlip)
  97. result += "-hflip";
  98. if (scalingFactor > 1)
  99. result += "-scale" + std::to_string(scalingFactor);
  100. if (playerColored.isValidPlayer())
  101. result += "-player" + playerColored.toString();
  102. if (layer != EImageLayer::ALL)
  103. result =+ "-layer" + std::to_string(static_cast<int>(layer));
  104. return result;
  105. }