selected_units_model.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. QString nation;
  32. int hp = 0;
  33. int max_hp = 0;
  34. bool is_b = false;
  35. bool alive = false;
  36. if (role == UnitIdRole) {
  37. return QVariant::fromValue<int>(static_cast<int>(id));
  38. }
  39. if (role == UnitTypeRole) {
  40. QString type_key;
  41. if (m_engine->get_unit_type_key(id, type_key)) {
  42. return type_key;
  43. }
  44. return {};
  45. }
  46. if (!m_engine->get_unit_info(id, name, hp, max_hp, is_b, alive, nation)) {
  47. return {};
  48. }
  49. if (role == NameRole) {
  50. return name;
  51. }
  52. if (role == HealthRole) {
  53. return hp;
  54. }
  55. if (role == max_healthRole) {
  56. return max_hp;
  57. }
  58. if (role == HealthRatioRole) {
  59. return (max_hp > 0 ? static_cast<double>(std::clamp(hp, 0, max_hp)) /
  60. static_cast<double>(max_hp)
  61. : 0.0);
  62. }
  63. if (role == NationRole) {
  64. return nation;
  65. }
  66. if (role == StaminaRatioRole || role == IsRunningRole || role == CanRunRole) {
  67. float stamina_ratio = 1.0F;
  68. bool is_running = false;
  69. bool can_run = false;
  70. m_engine->get_unit_stamina_info(id, stamina_ratio, is_running, can_run);
  71. if (role == StaminaRatioRole) {
  72. return static_cast<double>(stamina_ratio);
  73. }
  74. if (role == IsRunningRole) {
  75. return is_running;
  76. }
  77. if (role == CanRunRole) {
  78. return can_run;
  79. }
  80. }
  81. return {};
  82. }
  83. auto SelectedUnitsModel::roleNames() const -> QHash<int, QByteArray> {
  84. return {{UnitIdRole, "unit_id"},
  85. {UnitTypeRole, "unit_type"},
  86. {NameRole, "name"},
  87. {HealthRole, "health"},
  88. {max_healthRole, "max_health"},
  89. {HealthRatioRole, "health_ratio"},
  90. {NationRole, "nation"},
  91. {StaminaRatioRole, "stamina_ratio"},
  92. {IsRunningRole, "is_running"},
  93. {CanRunRole, "can_run"}};
  94. }
  95. void SelectedUnitsModel::refresh() {
  96. if (m_engine == nullptr) {
  97. return;
  98. }
  99. std::vector<Engine::Core::EntityID> ids;
  100. m_engine->get_selected_unit_ids(ids);
  101. if (ids.size() == m_ids.size() &&
  102. std::equal(ids.begin(), ids.end(), m_ids.begin())) {
  103. if (!m_ids.empty()) {
  104. QModelIndex const first = index(0, 0);
  105. QModelIndex const last = index(static_cast<int>(m_ids.size()) - 1, 0);
  106. emit dataChanged(first, last,
  107. {HealthRole, max_healthRole, HealthRatioRole,
  108. StaminaRatioRole, IsRunningRole});
  109. }
  110. return;
  111. }
  112. beginResetModel();
  113. m_ids.clear();
  114. for (auto id : ids) {
  115. QString nm;
  116. QString nation;
  117. int hp = 0;
  118. int max_hp = 0;
  119. bool is_b = false;
  120. bool alive = false;
  121. if (!m_engine->get_unit_info(id, nm, hp, max_hp, is_b, alive, nation)) {
  122. continue;
  123. }
  124. if (is_b) {
  125. continue;
  126. }
  127. if (!alive) {
  128. continue;
  129. }
  130. m_ids.push_back(id);
  131. }
  132. endResetModel();
  133. }