CardItem.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * CardItem.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 "CardItem.h"
  12. #include <QObject>
  13. #include "../../../lib/constants/EntityIdentifiers.h"
  14. #include "../../../lib/constants/StringConstants.h"
  15. #include "../../../lib/rmg/CRmgTemplate.h"
  16. QDomElement CardItem::getElementById(const QDomDocument& doc, const QString& id)
  17. {
  18. QDomElement root = doc.documentElement();
  19. std::function<QDomElement(const QDomElement&)> findById = [&](const QDomElement& elem) -> QDomElement {
  20. if (elem.attribute("id") == id)
  21. return elem;
  22. QDomElement child = elem.firstChildElement();
  23. while (!child.isNull())
  24. {
  25. QDomElement found = findById(child);
  26. if (!found.isNull())
  27. return found;
  28. child = child.nextSiblingElement();
  29. }
  30. return QDomElement();
  31. };
  32. return findById(root);
  33. }
  34. bool isBlackTextNeeded(const QColor& bg)
  35. {
  36. int r = bg.red();
  37. int g = bg.green();
  38. int b = bg.blue();
  39. double luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
  40. return luminance > 0.5;
  41. }
  42. CardItem::CardItem():
  43. selectCallback(nullptr),
  44. posChangeCallback(nullptr),
  45. useBlackText(false),
  46. mousePressed(false)
  47. {
  48. QFile file(":/icons/templateSquare.svg");
  49. file.open(QIODevice::ReadOnly);
  50. QByteArray data = file.readAll();
  51. doc.setContent(data);
  52. updateContent();
  53. }
  54. void CardItem::setSelectCallback(std::function<void(bool)> func)
  55. {
  56. selectCallback = func;
  57. }
  58. void CardItem::setPosChangeCallback(std::function<void(QPointF)> func)
  59. {
  60. posChangeCallback = func;
  61. }
  62. void CardItem::updateContent()
  63. {
  64. setSharedRenderer(new QSvgRenderer(doc.toByteArray()));
  65. }
  66. void CardItem::setFillColor(QColor color)
  67. {
  68. auto squareElem = getElementById(doc, "rect");
  69. squareElem.setAttribute("style", squareElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:" + color.name() + ";"));
  70. useBlackText = isBlackTextNeeded(color);
  71. }
  72. void CardItem::setMultiFillColor(QColor color1, QColor color2)
  73. {
  74. auto squareElem = getElementById(doc, "rect");
  75. squareElem.setAttribute("style", squareElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:url(#gradientExtra);"));
  76. auto gradientStopElem1 = getElementById(doc, "gradientExtraColorStop1");
  77. auto gradientStopElem2 = getElementById(doc, "gradientExtraColorStop2");
  78. auto gradientStopElem3 = getElementById(doc, "gradientExtraColorStop3");
  79. auto gradientStopElem4 = getElementById(doc, "gradientExtraColorStop4");
  80. auto gradientStopElem5 = getElementById(doc, "gradientExtraColorStop5");
  81. gradientStopElem1.setAttribute("style", gradientStopElem1.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + color1.name() + ";"));
  82. gradientStopElem2.setAttribute("style", gradientStopElem2.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + color2.name() + ";"));
  83. gradientStopElem3.setAttribute("style", gradientStopElem3.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + color1.name() + ";"));
  84. gradientStopElem4.setAttribute("style", gradientStopElem4.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + color2.name() + ";"));
  85. gradientStopElem5.setAttribute("style", gradientStopElem5.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + color1.name() + ";"));
  86. useBlackText = isBlackTextNeeded(color1);
  87. }
  88. void CardItem::setPlayerColor(PlayerColor color)
  89. {
  90. std::map<PlayerColor, std::pair<QString, QString>> colors =
  91. {
  92. { PlayerColor(0), { "#F80000", "#920000" } }, //red
  93. { PlayerColor(1), { "#0000F8", "#000092" } }, //blue
  94. { PlayerColor(2), { "#9B7251", "#35271C" } }, //tan
  95. { PlayerColor(3), { "#00FC00", "#009600" } }, //green
  96. { PlayerColor(4), { "#F88000", "#924B00" } }, //orange
  97. { PlayerColor(5), { "#F800F8", "#920092" } }, //purple
  98. { PlayerColor(6), { "#00FCF8", "#009694" } }, //teal
  99. { PlayerColor(7), { "#C07888", "#5A3840" } }, //pink
  100. };
  101. auto squareElem = getElementById(doc, "rect");
  102. squareElem.setAttribute("style", squareElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:url(#gradientPlayer);"));
  103. auto gradientStopElem1 = getElementById(doc, "gradientPlayerColorStop1");
  104. auto gradientStopElem2 = getElementById(doc, "gradientPlayerColorStop2");
  105. gradientStopElem1.setAttribute("style", gradientStopElem1.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + colors[color].first + ";"));
  106. gradientStopElem2.setAttribute("style", gradientStopElem2.attribute("style").replace(QRegularExpression("stop-color:.*?;"), "stop-color:" + colors[color].second + ";"));
  107. useBlackText = isBlackTextNeeded(QColor(colors[color].first));
  108. }
  109. void CardItem::setJunction(bool val)
  110. {
  111. auto squareElem = getElementById(doc, "rectJunction");
  112. squareElem.setAttribute("style", squareElem.attribute("style").replace(QRegularExpression("stroke-opacity:.*?;"), "stroke-opacity:" + QString::fromStdString(val ? "0.3" : "0.0") + ";"));
  113. }
  114. void CardItem::setId(int val)
  115. {
  116. auto textIdElem = getElementById(doc, "textId");
  117. textIdElem.setAttribute("style", textIdElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:" + QColor(useBlackText ? Qt::black : Qt::white).name() + ";"));
  118. textIdElem.firstChild().setNodeValue(QString::number(val));
  119. id = val;
  120. }
  121. int CardItem::getId()
  122. {
  123. return id;
  124. }
  125. void CardItem::setResAmount(GameResID res, int val)
  126. {
  127. auto textElem = getElementById(doc, "text" + QString::fromStdString(GameConstants::RESOURCE_NAMES[res]));
  128. textElem.setAttribute("style", textElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:" + QColor(useBlackText ? Qt::black : Qt::white).name() + ";"));
  129. textElem.firstChild().setNodeValue(val ? QString::number(val) : "");
  130. auto iconElem = getElementById(doc, "icon" + QString::fromStdString(GameConstants::RESOURCE_NAMES[res]));
  131. iconElem.setAttribute("opacity", val ? "1.0" : "0.1");
  132. }
  133. void CardItem::setChestValue(int val)
  134. {
  135. auto textElem = getElementById(doc, "textChest");
  136. textElem.setAttribute("style", textElem.attribute("style").replace(QRegularExpression("fill:.*?;"), "fill:" + QColor(useBlackText ? Qt::black : Qt::white).name() + ";"));
  137. textElem.firstChild().setNodeValue(val ? QString::number(val) : "");
  138. auto iconElem = getElementById(doc, "iconChest");
  139. iconElem.setAttribute("opacity", val ? "1.0" : "0.1");
  140. }
  141. void CardItem::setMonsterStrength(EMonsterStrength::EMonsterStrength val)
  142. {
  143. int level = 0;
  144. if(val == EMonsterStrength::ZONE_WEAK || val == EMonsterStrength::GLOBAL_WEAK)
  145. level = 1;
  146. else if(val == EMonsterStrength::ZONE_NORMAL || val == EMonsterStrength::GLOBAL_NORMAL)
  147. level = 2;
  148. else if(val == EMonsterStrength::ZONE_STRONG || val == EMonsterStrength::GLOBAL_STRONG)
  149. level = 3;
  150. getElementById(doc, "iconSword1").setAttribute("opacity", level > 0 ? "1.0" : "0.1");
  151. getElementById(doc, "iconSword2").setAttribute("opacity", level > 1 ? "1.0" : "0.1");
  152. getElementById(doc, "iconSword3").setAttribute("opacity", level > 2 ? "1.0" : "0.1");
  153. }
  154. void CardItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
  155. {
  156. if(event->button() == Qt::LeftButton)
  157. {
  158. // set element in grid
  159. double xx = x() + (boundingRect().width() / 2);
  160. double yy = y() + (boundingRect().height() / 2);
  161. xx = GRID_SIZE * round(xx / GRID_SIZE);
  162. yy = GRID_SIZE * round(yy / GRID_SIZE);
  163. setPos(xx - (boundingRect().width() / 2), yy - (boundingRect().height() / 2));
  164. }
  165. QGraphicsSvgItem::mouseReleaseEvent(event);
  166. if(posChangeCallback)
  167. posChangeCallback(pos());
  168. mousePressed = false;
  169. }
  170. void CardItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
  171. {
  172. QGraphicsSvgItem::mousePressEvent(event);
  173. mousePressed = true;
  174. }
  175. QVariant CardItem::itemChange(GraphicsItemChange change, const QVariant &value)
  176. {
  177. if(change == ItemSelectedHasChanged && selectCallback)
  178. selectCallback(isSelected());
  179. else if(change == ItemPositionHasChanged && posChangeCallback && mousePressed)
  180. posChangeCallback(pos());
  181. return QGraphicsSvgItem::itemChange(change, value);
  182. }