recruitmentscene.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "ui/hotseat_recruitment/recruitmentscene.h"
  2. #include "gui/guiscenemanager.h"
  3. #include "gui/scene.h"
  4. #include "playermanager.h"
  5. #include "player.h"
  6. #include "racemanager.h"
  7. #include "race.h"
  8. #include "soundengine.h"
  9. #include <ui_recruitmentscene.h>
  10. #include <QString>
  11. #include <QStringList>
  12. #include <QMovie>
  13. #include <QDebug>
  14. #include <vector>
  15. RecruitmentScene::RecruitmentScene(QWidget *parent)
  16. : Scene(parent)
  17. , ui(new Ui::RecruitmentScene)
  18. {
  19. ui->setupUi(this);
  20. movie = new QMovie(":/assets/hotseat_recruit/image.gif");
  21. ui->image->setMovie(movie);
  22. movie->start();
  23. watcher = new ButtonEventListener(this);
  24. ui->complete_choice_button->installEventFilter(watcher);
  25. ui->back_button->installEventFilter(watcher);
  26. // Создаём обработчик событий нажатия иконок рас
  27. race_icon_watcher = new RaceIconEventListener(this, this);
  28. for (unsigned i = 0; i < RACES_NUMBER; i++) {
  29. QLabel* race_icon = findChild<QLabel*>("available_race_" + QString::number(i));
  30. if (race_icon)
  31. race_icon->installEventFilter(race_icon_watcher);
  32. }
  33. ui->chosen_race_image_->installEventFilter(race_icon_watcher);
  34. // Создаём обработчик событий нажатия иконок юнитов
  35. unit_icon_watcher = new UnitIconEventListener(this, this);
  36. // for (unsigned i = 0; i < AVAILABLE_UNIT_NUMBER; i++) {
  37. // QLabel* unit_icon = findChild<QLabel*>("available_unit_" + QString::number(i));
  38. // if (unit_icon)
  39. // unit_icon->installEventFilter(unit_icon_watcher);
  40. // }
  41. // for (unsigned i = 0; i < MAX_PARTY_SIZE; i++) {
  42. // QLabel* unit_icon = findChild<QLabel*>("chosen_unit_" + QString::number(i));
  43. // if (unit_icon)
  44. // unit_icon->installEventFilter(unit_icon_watcher);
  45. // }
  46. }
  47. RecruitmentScene::~RecruitmentScene()
  48. {
  49. }
  50. void RecruitmentScene::parseArgs(QString args) {
  51. QStringList list = args.split("|");
  52. if (list.size() < 2) {
  53. // TODO: ERROR MESSAGE - incorrect arguments number
  54. GuiSceneManager::getInstance().changeScene("main_menu");
  55. }
  56. current_player_id_ = list[0].toInt();
  57. current_player_ = PlayerManager::getInstance().getPlayer(current_player_id_);
  58. available_money_ = std::max(0, list[1].toInt());
  59. if (!current_player_) {
  60. // TODO: ERROR MESSAGE - incorrect argument - player_id;
  61. GuiSceneManager::getInstance().changeScene("main_menu");
  62. }
  63. }
  64. void RecruitmentScene::init() {
  65. current_money_ = available_money_;
  66. initAvailableRaces();
  67. initAvailableUnits();
  68. showChosenUnits();
  69. ui->title_->setText(
  70. "<p align=\"center\">Набор юнитов - " + current_player_->getPlayerName() + "</p>"
  71. );
  72. QString race_name = current_player_->getRace()->getRaceId();
  73. ui->chosen_race_image_->setText(
  74. "<img src=\":/assets/units/" + race_name + "/icon.png\" width=\"100\" height=\"100\"/>"
  75. );
  76. setMoney(current_money_);
  77. }
  78. void RecruitmentScene::initAvailableRaces() {
  79. auto races = RaceManager::getInstance().getAvailableRacesList();
  80. race_ids_.clear();
  81. for (unsigned i = 0; i < std::min(RACES_NUMBER, races.size()); i++) {
  82. QString race_name = races[i];
  83. race_ids_.push_back(race_name);
  84. QLabel* race_icon = findChild<QLabel*>("available_race_" + QString::number(i));
  85. if (race_icon)
  86. race_icon->setText("<img src=\":/assets/units/" + race_name + "/icon.png\" width=\"66\" height=\"66\"/>");
  87. }
  88. }
  89. void RecruitmentScene::initAvailableUnits() {
  90. auto units_list = current_player_->getRace()->getAllUnitsList();
  91. available_unit_ids.clear();
  92. for (unsigned i = 0; i < std::min(AVAILABLE_UNIT_NUMBER, units_list.size()); i++) {
  93. QString race_name = current_player_->getRace()->getRaceId();
  94. QString unit_name = units_list[i]->getUnitId();
  95. available_unit_ids.push_back(unit_name);
  96. QLabel* unit_icon = findChild<QLabel*>("available_unit_" + QString::number(i));
  97. qDebug() << "<img src=\":/assets/units/" + race_name + "/" + unit_name + "/icon.png\" width=\"58\" height=\"58\"/>";
  98. if (unit_icon)
  99. unit_icon->setText("<img src=\":/assets/units/" + race_name + "/" + unit_name + "/icon.png\" width=\"58\" height=\"58\"/>");
  100. }
  101. }
  102. void RecruitmentScene::showChosenUnits() {
  103. int counter = 0;
  104. chosen_unit_ids.clear();
  105. for (auto unit : current_player_->getUnits()) {
  106. if (counter >= MAX_PARTY_SIZE)
  107. break;
  108. QString unit_name = unit->getUnitId();
  109. chosen_unit_ids.push_back(unit_name);
  110. QString race_name = current_player_->getRace()->getRaceId();
  111. QLabel* unit_icon = findChild<QLabel*>("chosen_unit_" + QString::number(counter));
  112. if (unit_icon)
  113. unit_icon->setText("<img src=\":/assets/units/" + race_name + "/" + unit_name + "/icon.png\" width=\"58\" height=\"58\"/>");
  114. counter++;
  115. }
  116. for (int i = counter; i < MAX_PARTY_SIZE; i++) {
  117. QLabel* unit_icon = findChild<QLabel*>("chosen_unit_" + QString::number(i));
  118. if (unit_icon)
  119. unit_icon->setText("");
  120. }
  121. }
  122. void RecruitmentScene::changeRace(Race *race) {
  123. current_player_->clearUnits();
  124. current_player_->setRace(race);
  125. init();
  126. }
  127. void RecruitmentScene::setMoney(int money_) {
  128. ui->available_money_text_->setText("Монеты: " + QString::number(money_));
  129. }
  130. void RecruitmentScene::on_back_button_clicked() {
  131. GuiSceneManager::getInstance().changeScene("pvp_intro");
  132. }
  133. RaceIconEventListener::RaceIconEventListener(QObject *parent, RecruitmentScene* scene)
  134. : QObject(parent)
  135. , scene_(scene) {
  136. }
  137. bool RaceIconEventListener::eventFilter(QObject *watched, QEvent *event) {
  138. QLabel* icon = qobject_cast<QLabel*>(watched);
  139. if (!icon) {
  140. return false;
  141. }
  142. if (event->type() == QEvent::Enter) {
  143. IconHint *hint = new IconHint(scene_);
  144. unsigned race_id_number = icon->objectName().mid(icon->objectName().lastIndexOf('_') + 1).toInt();
  145. QString race_name = RaceManager::getInstance().getRace(scene_->race_ids_[race_id_number])->getRaceName();
  146. hint->setTitle("Раса \"" + race_name + "\"");
  147. if (icon->objectName() != "chosen_race_image_") {
  148. icon->setStyleSheet("border-image: url(:/assets/hotseat_recruit/unit_icon_hover.png);\nbackground-color: rgb(253, 234, 168);");
  149. SoundEngine::getInstance().playAsync("qrc:/assets/common/buttons/button_hover.mp3");
  150. hint->setText("ЛКМ - сменить расу\nПКМ - открыть окно информации");
  151. } else {
  152. hint->setText("ПКМ - открыть окно информации");
  153. }
  154. hint->move(icon->mapToGlobal(QPoint(0,0)).x() + icon->width() / 2 - hint->width() / 2, icon->mapToGlobal(QPoint(0,0)).y() + icon->height() + 2);
  155. return false;
  156. }
  157. if (event->type() == QEvent::Leave) {
  158. if (icon->objectName() != "chosen_race_image_") {
  159. icon->setStyleSheet("border-image: url(:/assets/hotseat_recruit/unit_icon_default.png);\nbackground-color: rgb(253, 234, 168);");
  160. SoundEngine::getInstance().playAsync("qrc:/assets/common/buttons/button_hover.mp3");
  161. if (!scene_)
  162. return false;
  163. }
  164. QList<IconHint*> hints = scene_->findChildren<IconHint*>("hint");
  165. for (auto &hint : hints) {
  166. hint->hide();
  167. hint->deleteLater();
  168. }
  169. return false;
  170. }
  171. if (event->type() == QEvent::MouseButtonDblClick) {
  172. }
  173. return false;
  174. }
  175. UnitIconEventListener::UnitIconEventListener(QObject *parent, RecruitmentScene *scene)
  176. : QObject(parent)
  177. , scene_(scene) {
  178. }
  179. bool UnitIconEventListener::eventFilter(QObject *watched, QEvent *event) {
  180. return false;
  181. }