selected_units_model.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "selected_units_model.h"
  2. #include "../core/game_engine.h"
  3. #include <algorithm>
  4. #include <qabstractitemmodel.h>
  5. #include <qhash.h>
  6. #include <qhashfunctions.h>
  7. #include <qobject.h>
  8. #include <qstringview.h>
  9. #include <qtmetamacros.h>
  10. #include <qvariant.h>
  11. #include <vector>
  12. SelectedUnitsModel::SelectedUnitsModel(GameEngine *engine, QObject *parent)
  13. : QAbstractListModel(parent), m_engine(engine) {}
  14. auto SelectedUnitsModel::rowCount(const QModelIndex &parent) const -> int {
  15. if (parent.isValid()) {
  16. return 0;
  17. }
  18. return static_cast<int>(m_ids.size());
  19. }
  20. auto SelectedUnitsModel::data(const QModelIndex &index,
  21. int role) const -> QVariant {
  22. if (!index.isValid() || index.row() < 0 ||
  23. index.row() >= static_cast<int>(m_ids.size())) {
  24. return {};
  25. }
  26. auto id = m_ids[index.row()];
  27. if (m_engine == nullptr) {
  28. return {};
  29. }
  30. QString name;
  31. int hp = 0;
  32. int max_hp = 0;
  33. bool is_b = false;
  34. bool alive = false;
  35. if (role == UnitIdRole) {
  36. return QVariant::fromValue<int>(static_cast<int>(id));
  37. }
  38. if (!m_engine->getUnitInfo(id, name, hp, max_hp, is_b, alive)) {
  39. return {};
  40. }
  41. if (role == NameRole) {
  42. return name;
  43. }
  44. if (role == HealthRole) {
  45. return hp;
  46. }
  47. if (role == max_healthRole) {
  48. return max_hp;
  49. }
  50. if (role == HealthRatioRole) {
  51. return (max_hp > 0 ? static_cast<double>(std::clamp(hp, 0, max_hp)) /
  52. static_cast<double>(max_hp)
  53. : 0.0);
  54. }
  55. return {};
  56. }
  57. auto SelectedUnitsModel::roleNames() const -> QHash<int, QByteArray> {
  58. return {{UnitIdRole, "unit_id"},
  59. {NameRole, "name"},
  60. {HealthRole, "health"},
  61. {max_healthRole, "max_health"},
  62. {HealthRatioRole, "health_ratio"}};
  63. }
  64. void SelectedUnitsModel::refresh() {
  65. if (m_engine == nullptr) {
  66. return;
  67. }
  68. std::vector<Engine::Core::EntityID> ids;
  69. m_engine->getSelectedUnitIds(ids);
  70. if (ids.size() == m_ids.size() &&
  71. std::equal(ids.begin(), ids.end(), m_ids.begin())) {
  72. if (!m_ids.empty()) {
  73. QModelIndex const first = index(0, 0);
  74. QModelIndex const last = index(static_cast<int>(m_ids.size()) - 1, 0);
  75. emit dataChanged(first, last,
  76. {HealthRole, max_healthRole, HealthRatioRole});
  77. }
  78. return;
  79. }
  80. beginResetModel();
  81. m_ids.clear();
  82. for (auto id : ids) {
  83. QString nm;
  84. int hp = 0;
  85. int max_hp = 0;
  86. bool is_b = false;
  87. bool alive = false;
  88. if (!m_engine->getUnitInfo(id, nm, hp, max_hp, is_b, alive)) {
  89. continue;
  90. }
  91. if (is_b) {
  92. continue;
  93. }
  94. if (!alive) {
  95. continue;
  96. }
  97. m_ids.push_back(id);
  98. }
  99. endResetModel();
  100. }