| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- #include "selected_units_model.h"
- #include "../core/game_engine.h"
- #include <algorithm>
- #include <qabstractitemmodel.h>
- #include <qhash.h>
- #include <qhashfunctions.h>
- #include <qobject.h>
- #include <qstringview.h>
- #include <qtmetamacros.h>
- #include <qvariant.h>
- #include <vector>
- SelectedUnitsModel::SelectedUnitsModel(GameEngine *engine, QObject *parent)
- : QAbstractListModel(parent), m_engine(engine) {}
- auto SelectedUnitsModel::rowCount(const QModelIndex &parent) const -> int {
- if (parent.isValid()) {
- return 0;
- }
- return static_cast<int>(m_ids.size());
- }
- auto SelectedUnitsModel::data(const QModelIndex &index,
- int role) const -> QVariant {
- if (!index.isValid() || index.row() < 0 ||
- index.row() >= static_cast<int>(m_ids.size())) {
- return {};
- }
- auto id = m_ids[index.row()];
- if (m_engine == nullptr) {
- return {};
- }
- QString name;
- QString nation;
- int hp = 0;
- int max_hp = 0;
- bool is_b = false;
- bool alive = false;
- if (role == UnitIdRole) {
- return QVariant::fromValue<int>(static_cast<int>(id));
- }
- if (role == UnitTypeRole) {
- QString type_key;
- if (m_engine->get_unit_type_key(id, type_key)) {
- return type_key;
- }
- return {};
- }
- if (!m_engine->get_unit_info(id, name, hp, max_hp, is_b, alive, nation)) {
- return {};
- }
- if (role == NameRole) {
- return name;
- }
- if (role == HealthRole) {
- return hp;
- }
- if (role == max_healthRole) {
- return max_hp;
- }
- if (role == HealthRatioRole) {
- return (max_hp > 0 ? static_cast<double>(std::clamp(hp, 0, max_hp)) /
- static_cast<double>(max_hp)
- : 0.0);
- }
- if (role == NationRole) {
- return nation;
- }
- if (role == StaminaRatioRole || role == IsRunningRole || role == CanRunRole) {
- float stamina_ratio = 1.0F;
- bool is_running = false;
- bool can_run = false;
- m_engine->get_unit_stamina_info(id, stamina_ratio, is_running, can_run);
- if (role == StaminaRatioRole) {
- return static_cast<double>(stamina_ratio);
- }
- if (role == IsRunningRole) {
- return is_running;
- }
- if (role == CanRunRole) {
- return can_run;
- }
- }
- return {};
- }
- auto SelectedUnitsModel::roleNames() const -> QHash<int, QByteArray> {
- return {{UnitIdRole, "unit_id"},
- {UnitTypeRole, "unit_type"},
- {NameRole, "name"},
- {HealthRole, "health"},
- {max_healthRole, "max_health"},
- {HealthRatioRole, "health_ratio"},
- {NationRole, "nation"},
- {StaminaRatioRole, "stamina_ratio"},
- {IsRunningRole, "is_running"},
- {CanRunRole, "can_run"}};
- }
- void SelectedUnitsModel::refresh() {
- if (m_engine == nullptr) {
- return;
- }
- std::vector<Engine::Core::EntityID> ids;
- m_engine->get_selected_unit_ids(ids);
- if (ids.size() == m_ids.size() &&
- std::equal(ids.begin(), ids.end(), m_ids.begin())) {
- if (!m_ids.empty()) {
- QModelIndex const first = index(0, 0);
- QModelIndex const last = index(static_cast<int>(m_ids.size()) - 1, 0);
- emit dataChanged(first, last,
- {HealthRole, max_healthRole, HealthRatioRole,
- StaminaRatioRole, IsRunningRole});
- }
- return;
- }
- beginResetModel();
- m_ids.clear();
- for (auto id : ids) {
- QString nm;
- QString nation;
- int hp = 0;
- int max_hp = 0;
- bool is_b = false;
- bool alive = false;
- if (!m_engine->get_unit_info(id, nm, hp, max_hp, is_b, alive, nation)) {
- continue;
- }
- if (is_b) {
- continue;
- }
- if (!alive) {
- continue;
- }
- m_ids.push_back(id);
- }
- endResetModel();
- }
|