uniticon.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <hotseatgame/uniticon.h>
  2. #include <QDebug>
  3. #include <QTime>
  4. #include <QBitmap>
  5. UnitIcon::UnitIcon(QWidget* parent, int width, int height)
  6. : QLabel(parent)
  7. {
  8. state_ = 0;
  9. width_ = width;
  10. height_ = height;
  11. unit_ = nullptr;
  12. setAttribute(Qt::WA_Hover, true);
  13. installEventFilter(this);
  14. default_border_.load(":/assets/common/unit_icon_default.png");
  15. hover_border_.load(":/assets/common/unit_icon_hover.png");
  16. active_border_.load(":/assets/common/unit_icon_active.png");
  17. drawIcon();
  18. }
  19. UnitIcon::~UnitIcon()
  20. {
  21. }
  22. Unit* UnitIcon::getUnit() {
  23. if (!unit_)
  24. return nullptr;
  25. return unit_;
  26. }
  27. void UnitIcon::setUnit(Unit* unit) {
  28. unit_ = unit;
  29. if (unit_ != nullptr)
  30. setUnitIcon(unit_->getUnitIcon());
  31. return;
  32. }
  33. void UnitIcon::setState(int state) {
  34. state_ = state;
  35. }
  36. void UnitIcon::drawIcon() {
  37. QPixmap combined(width_, height_);
  38. combined.fill(QColor(253, 234, 168));
  39. QPainter p(&combined);
  40. if (state_ != 0)
  41. p.drawImage(QPoint(4, 4), icon_.scaled(width_ - 8, height_ - 8, Qt::KeepAspectRatio));
  42. if (state_ != 2 && state_ != 3)
  43. p.drawImage(QPoint(0, 0), default_border_.scaled(width_, height_, Qt::KeepAspectRatio));
  44. if (state_ == 2)
  45. p.drawImage(QPoint(0, 0), hover_border_.scaled(width_, height_, Qt::KeepAspectRatio));
  46. if (state_ == 3)
  47. p.drawImage(QPoint(0, 0), active_border_.scaled(width_, height_, Qt::KeepAspectRatio));
  48. p.end();
  49. setPixmap(combined);
  50. }
  51. void UnitIcon::setUnitIcon(QString racename, QString unitname) {
  52. icon_.load(":/assets/units/" + racename + "/" + unitname + "/icon.png");
  53. if (state_ == 0)
  54. state_ = 1;
  55. drawIcon();
  56. }
  57. void UnitIcon::setUnitIcon(QImage icon) {
  58. icon_ = icon;
  59. if (state_ == 0)
  60. state_ = 1;
  61. drawIcon();
  62. }
  63. void UnitIcon::unsetUnitIcon() {
  64. state_ = 0;
  65. drawIcon();
  66. }
  67. void UnitIcon::resize(int w, int h) {
  68. width_ = w;
  69. height_ = h;
  70. drawIcon();
  71. }
  72. void UnitIcon::deactivate() {
  73. if (state_ > 1)
  74. state_ = 1;
  75. drawIcon();
  76. }
  77. void UnitIcon::activate() {
  78. state_ = 3;
  79. drawIcon();
  80. }
  81. void UnitIcon::mousePressEvent(QMouseEvent*)
  82. {
  83. if (state_ == 0 || unit_ == nullptr || state_ >= 5)
  84. return;
  85. activate();
  86. QTime current_time = QTime::currentTime();
  87. if (current_time.msecsSinceStartOfDay() - previous_click_time_.msecsSinceStartOfDay() < 350) {
  88. deactivate();
  89. emit doubleclicked(this);
  90. } else {
  91. previous_click_time_ = current_time;
  92. emit clicked(this);
  93. }
  94. }
  95. void UnitIcon::enterEvent(QEvent*)
  96. {
  97. if (state_ == 0 || state_ == 3 || unit_ == nullptr || state_ >= 5)
  98. return;
  99. state_ = 2;
  100. drawIcon();
  101. emit hovered(this);
  102. }
  103. void UnitIcon::leaveEvent(QEvent*)
  104. {
  105. if (state_ == 0 || state_ == 3 || unit_ == nullptr || state_ >= 5)
  106. return;
  107. state_ = 1;
  108. drawIcon();
  109. emit unhovered(this);
  110. }