#include "selected_units_model.h" #include "../core/game_engine.h" #include #include #include #include #include #include #include #include #include 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(m_ids.size()); } auto SelectedUnitsModel::data(const QModelIndex &index, int role) const -> QVariant { if (!index.isValid() || index.row() < 0 || index.row() >= static_cast(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(static_cast(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(std::clamp(hp, 0, max_hp)) / static_cast(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(stamina_ratio); } if (role == IsRunningRole) { return is_running; } if (role == CanRunRole) { return can_run; } } return {}; } auto SelectedUnitsModel::roleNames() const -> QHash { 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 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(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(); }