123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #include "ui/hotseat_recruitment/recruitmentscene.h"
- #include "gui/guiscenemanager.h"
- #include "gui/scene.h"
- #include "playermanager.h"
- #include "player.h"
- #include "racemanager.h"
- #include "race.h"
- #include "soundengine.h"
- #include <ui_recruitmentscene.h>
- #include <QString>
- #include <QStringList>
- #include <QMovie>
- #include <QDebug>
- #include <vector>
- RecruitmentScene::RecruitmentScene(QWidget *parent)
- : Scene(parent)
- , ui(new Ui::RecruitmentScene)
- {
- ui->setupUi(this);
- movie = new QMovie(":/assets/hotseat_recruit/image.gif");
- ui->image->setMovie(movie);
- movie->start();
- watcher = new ButtonEventListener(this);
- ui->complete_choice_button->installEventFilter(watcher);
- ui->back_button->installEventFilter(watcher);
- // Создаём обработчик событий нажатия иконок рас
- race_icon_watcher = new RaceIconEventListener(this, this);
- for (unsigned i = 0; i < RACES_NUMBER; i++) {
- QLabel* race_icon = findChild<QLabel*>("available_race_" + QString::number(i));
- if (race_icon)
- race_icon->installEventFilter(race_icon_watcher);
- }
- ui->chosen_race_image_->installEventFilter(race_icon_watcher);
- // Создаём обработчик событий нажатия иконок юнитов
- unit_icon_watcher = new UnitIconEventListener(this, this);
- // for (unsigned i = 0; i < AVAILABLE_UNIT_NUMBER; i++) {
- // QLabel* unit_icon = findChild<QLabel*>("available_unit_" + QString::number(i));
- // if (unit_icon)
- // unit_icon->installEventFilter(unit_icon_watcher);
- // }
- // for (unsigned i = 0; i < MAX_PARTY_SIZE; i++) {
- // QLabel* unit_icon = findChild<QLabel*>("chosen_unit_" + QString::number(i));
- // if (unit_icon)
- // unit_icon->installEventFilter(unit_icon_watcher);
- // }
- }
- RecruitmentScene::~RecruitmentScene()
- {
- }
- void RecruitmentScene::parseArgs(QString args) {
- QStringList list = args.split("|");
- if (list.size() < 2) {
- // TODO: ERROR MESSAGE - incorrect arguments number
- GuiSceneManager::getInstance().changeScene("main_menu");
- }
- current_player_id_ = list[0].toInt();
- current_player_ = PlayerManager::getInstance().getPlayer(current_player_id_);
- available_money_ = std::max(0, list[1].toInt());
- if (!current_player_) {
- // TODO: ERROR MESSAGE - incorrect argument - player_id;
- GuiSceneManager::getInstance().changeScene("main_menu");
- }
- }
- void RecruitmentScene::init() {
- current_money_ = available_money_;
- initAvailableRaces();
- initAvailableUnits();
- showChosenUnits();
- ui->title_->setText(
- "<p align=\"center\">Набор юнитов - " + current_player_->getPlayerName() + "</p>"
- );
- QString race_name = current_player_->getRace()->getRaceId();
- ui->chosen_race_image_->setText(
- "<img src=\":/assets/units/" + race_name + "/icon.png\" width=\"100\" height=\"100\"/>"
- );
- setMoney(current_money_);
- }
- void RecruitmentScene::initAvailableRaces() {
- auto races = RaceManager::getInstance().getAvailableRacesList();
- race_ids_.clear();
- for (unsigned i = 0; i < std::min(RACES_NUMBER, races.size()); i++) {
- QString race_name = races[i];
- race_ids_.push_back(race_name);
- QLabel* race_icon = findChild<QLabel*>("available_race_" + QString::number(i));
- if (race_icon)
- race_icon->setText("<img src=\":/assets/units/" + race_name + "/icon.png\" width=\"66\" height=\"66\"/>");
- }
- }
- void RecruitmentScene::initAvailableUnits() {
- auto units_list = current_player_->getRace()->getAllUnitsList();
- available_unit_ids.clear();
- for (unsigned i = 0; i < std::min(AVAILABLE_UNIT_NUMBER, units_list.size()); i++) {
- QString race_name = current_player_->getRace()->getRaceId();
- QString unit_name = units_list[i]->getUnitId();
- available_unit_ids.push_back(unit_name);
- QLabel* unit_icon = findChild<QLabel*>("available_unit_" + QString::number(i));
- qDebug() << "<img src=\":/assets/units/" + race_name + "/" + unit_name + "/icon.png\" width=\"58\" height=\"58\"/>";
- if (unit_icon)
- unit_icon->setText("<img src=\":/assets/units/" + race_name + "/" + unit_name + "/icon.png\" width=\"58\" height=\"58\"/>");
- }
- }
- void RecruitmentScene::showChosenUnits() {
- int counter = 0;
- chosen_unit_ids.clear();
- for (auto unit : current_player_->getUnits()) {
- if (counter >= MAX_PARTY_SIZE)
- break;
- QString unit_name = unit->getUnitId();
- chosen_unit_ids.push_back(unit_name);
- QString race_name = current_player_->getRace()->getRaceId();
- QLabel* unit_icon = findChild<QLabel*>("chosen_unit_" + QString::number(counter));
- if (unit_icon)
- unit_icon->setText("<img src=\":/assets/units/" + race_name + "/" + unit_name + "/icon.png\" width=\"58\" height=\"58\"/>");
- counter++;
- }
- for (int i = counter; i < MAX_PARTY_SIZE; i++) {
- QLabel* unit_icon = findChild<QLabel*>("chosen_unit_" + QString::number(i));
- if (unit_icon)
- unit_icon->setText("");
- }
- }
- void RecruitmentScene::changeRace(Race *race) {
- current_player_->clearUnits();
- current_player_->setRace(race);
- init();
- }
- void RecruitmentScene::setMoney(int money_) {
- ui->available_money_text_->setText("Монеты: " + QString::number(money_));
- }
- void RecruitmentScene::on_back_button_clicked() {
- GuiSceneManager::getInstance().changeScene("pvp_intro");
- }
- RaceIconEventListener::RaceIconEventListener(QObject *parent, RecruitmentScene* scene)
- : QObject(parent)
- , scene_(scene) {
- }
- bool RaceIconEventListener::eventFilter(QObject *watched, QEvent *event) {
- QLabel* icon = qobject_cast<QLabel*>(watched);
- if (!icon) {
- return false;
- }
- if (event->type() == QEvent::Enter) {
- IconHint *hint = new IconHint(scene_);
- unsigned race_id_number = icon->objectName().mid(icon->objectName().lastIndexOf('_') + 1).toInt();
- QString race_name = RaceManager::getInstance().getRace(scene_->race_ids_[race_id_number])->getRaceName();
- hint->setTitle("Раса \"" + race_name + "\"");
- if (icon->objectName() != "chosen_race_image_") {
- icon->setStyleSheet("border-image: url(:/assets/hotseat_recruit/unit_icon_hover.png);\nbackground-color: rgb(253, 234, 168);");
- SoundEngine::getInstance().playAsync("qrc:/assets/common/buttons/button_hover.mp3");
- hint->setText("ЛКМ - сменить расу\nПКМ - открыть окно информации");
- } else {
- hint->setText("ПКМ - открыть окно информации");
- }
- hint->move(icon->mapToGlobal(QPoint(0,0)).x() + icon->width() / 2 - hint->width() / 2, icon->mapToGlobal(QPoint(0,0)).y() + icon->height() + 2);
- return false;
- }
- if (event->type() == QEvent::Leave) {
- if (icon->objectName() != "chosen_race_image_") {
- icon->setStyleSheet("border-image: url(:/assets/hotseat_recruit/unit_icon_default.png);\nbackground-color: rgb(253, 234, 168);");
- SoundEngine::getInstance().playAsync("qrc:/assets/common/buttons/button_hover.mp3");
- if (!scene_)
- return false;
- }
- QList<IconHint*> hints = scene_->findChildren<IconHint*>("hint");
- for (auto &hint : hints) {
- hint->hide();
- hint->deleteLater();
- }
- return false;
- }
- if (event->type() == QEvent::MouseButtonDblClick) {
- }
- return false;
- }
- UnitIconEventListener::UnitIconEventListener(QObject *parent, RecruitmentScene *scene)
- : QObject(parent)
- , scene_(scene) {
- }
- bool UnitIconEventListener::eventFilter(QObject *watched, QEvent *event) {
- return false;
- }
|