CardItem.cpp 8.0 KB

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