// This file is part of c-evo-evo. // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // "http://www.cecill.info". // // As a counterpart to the access to the source code and rights to copy, // modify and redistribute granted by the license, users are provided only // with a limited warranty and the software's author, the holder of the // economic rights, and the successive licensors have only limited // liability. // // In this respect, the user's attention is drawn to the risks associated // with loading, using, modifying and/or developing or reproducing the // software by the user in light of its specific status of free software, // that may mean that it is complicated to manipulate, and that also // therefore means that it is reserved for developers and experienced // professionals having in-depth computer knowledge. Users are therefore // encouraged to load and test the software's suitability as regards their // requirements in conditions enabling the security of their systems and/or // data to be ensured and, more generally, to use and operate it in the // same conditions as regards security. // // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. #include "Sound.h" #include "MessgEx.h" #include "ui_MessgEx.h" #include "Cevo/CevoScreenTools.h" #include "Cevo/CevoTexture.h" #include "Cevo/Protocol.h" #include "ClientTools.h" #include "Diagram.h" #include "Global.h" #include "Term.h" #include "Tribes.h" #include "UnitStat.h" #include #include #include #include #include "Dpi/DpiPixelPointer.h" #include #include #include namespace Cevo { namespace MessgEx { namespace { constexpr int LostUnitsPerLine = 6; MessgExDlg *instance = nullptr; } // namespace MessgExDlg::MessgExDlg(QWidget *parent) : BaseMessgDlg(parent), ui(new Ui::MessgExDlg) { ui->setupUi(this); initButtons(); iconKind = MessageIconKind::None; centerTo = 0; openSound.clear(); ui->button1->onClick = [this] { setModalResult(Dpi::ModalResult::Ok); close(); }; ui->button2->onClick = [this] { if (kind == MessageKind::OkHelp) Help::showNewContent(WindowMode::Submodal, helpKind, helpNo); else if (kind == MessageKind::Model) UnitStat::showNewContentOwnModel(WindowMode::Submodal, iconIndex); else { setModalResult(Dpi::ModalResult::Ignore); close(); } }; ui->button3->onClick = [this] { setModalResult(Dpi::ModalResult::Cancel); close(); }; ui->removeBtn->onClick = [this] { setModalResult(Dpi::ModalResult::No); close(); }; } MessgExDlg::~MessgExDlg() { delete ui; } QString MessgExDlg::inputText() const { return ui->eInput->text(); } void MessgExDlg::setInputText(const QString &text) { ui->eInput->setText(text); } void MessgExDlg::closeEvent(QCloseEvent *event) { iconKind = MessageIconKind::None; centerTo = 0; BaseMessgDlg::closeEvent(event); } void MessgExDlg::keyPressEvent(QKeyEvent *event) { if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { setModalResult(Dpi::ModalResult::Ok); close(); } else if (event->key() == Qt::Key_Escape) { if (ui->button3->isVisible()) { setModalResult(Dpi::ModalResult::Cancel); close(); } else if (ui->button2->isVisible()) { setModalResult(Dpi::ModalResult::Ignore); close(); } } else { BaseMessgDlg::keyPressEvent(event); } } void MessgExDlg::showEvent(QShowEvent *event) { BaseMessgDlg::showEvent(event); prepareLayout(); } // Sizes and arranges the dialog for its content; also called before show() because some window systems // ignore geometry changes made once the window is mapped. void MessgExDlg::prepareLayout() { setWindowTitle(phrases.lookup(QStringLiteral("TITLE_MESSAGE"))); if (iconKind == MessageIconKind::EnemyArmy) Term::initAllEnemyModels(); const bool isMovie = Term::gameMode == cMovie; ui->button1->setVisible(!isMovie); ui->button2->setVisible(!isMovie && kind != MessageKind::Ok); ui->button3->setVisible(!isMovie && kind == MessageKind::YesNoCancel); ui->removeBtn->setVisible(!isMovie && kind == MessageKind::OkCancelRemove); ui->eInput->setVisible(!isMovie && kind == MessageKind::Model); if (ui->button3->isVisible()) { ui->button1->move(43, ui->button1->y()); ui->button2->move(159, ui->button2->y()); } else if (ui->button2->isVisible()) { ui->button1->move(101, ui->button1->y()); ui->button2->move(217, ui->button2->y()); } else { ui->button1->move(159, ui->button1->y()); } ui->removeBtn->move(width() - 38, ui->removeBtn->y()); switch (kind) { case MessageKind::YesNo: case MessageKind::YesNoCancel: ui->button1->setCaption(phrases.lookup(QStringLiteral("BTN_YES"))); ui->button2->setCaption(phrases.lookup(QStringLiteral("BTN_NO"))); break; case MessageKind::OkCancel: case MessageKind::OkCancelRemove: ui->button1->setCaption(phrases.lookup(QStringLiteral("BTN_OK"))); ui->button2->setCaption(phrases.lookup(QStringLiteral("BTN_CANCEL"))); break; default: ui->button1->setCaption(phrases.lookup(QStringLiteral("BTN_OK"))); ui->button2->setCaption(phrases.lookup(QStringLiteral("BTN_INFO"))); break; } ui->button3->setCaption(phrases.lookup(QStringLiteral("BTN_CANCEL"))); ui->removeBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_DELGAME"))); switch (iconKind) { case MessageIconKind::Imp: case MessageIconKind::Model: case MessageIconKind::Age: case MessageIconKind::PureIcon: topSpace = 56; break; case MessageIconKind::BigIcon: topSpace = 152; break; case MessageIconKind::EnemyShipComplete: topSpace = 136; break; case MessageIconKind::Book: topSpace = iconIndex >= 0 ? 84 : 47; break; case MessageIconKind::Tribe: Tribes::tribe[static_cast(iconIndex)]->initAge(ClientTools::getAge(iconIndex)); if (Tribes::tribe[static_cast(iconIndex)]->faceHGr) topSpace = 64; break; case MessageIconKind::FullControl: topSpace = 80; break; case MessageIconKind::Ship: topSpace = 240; break; default: topSpace = 0; break; } splitText(true); int newHeight = 72 + Border + topSpace + lines * MessageLineSpacing; if (isMovie) newHeight -= 32; if (kind == MessageKind::Model) newHeight += 36; if (iconKind == MessageIconKind::MyArmy || iconKind == MessageIconKind::EnemyArmy) { if (Term::nLostArmy > LostUnitsPerLine * 6) newHeight += 6 * 48; else newHeight += ((Term::nLostArmy - 1) / LostUnitsPerLine + 1) * 48; } const Dpi::Monitor monitor = Dpi::Screen::primaryMonitor(); int newLeft, newTop; switch (centerTo) { case 1: newLeft = monitor.left() + (monitor.width() - width()) / 4; newTop = monitor.top() + (monitor.height() - newHeight) * 2 / 3 - Term::MapCenterUp; break; case -1: newLeft = monitor.left() + (monitor.width() - width()) / 4; newTop = monitor.top() + (monitor.height() - newHeight) / 3 - Term::MapCenterUp; break; default: newLeft = monitor.left() + (monitor.width() - width()) / 2; newTop = monitor.top() + (monitor.height() - newHeight) / 2 - Term::MapCenterUp; break; } setGeometry(newLeft, newTop, width(), newHeight); const auto children = findChildren(QString(), Qt::FindDirectChildrenOnly); for (QWidget *child : children) child->move(child->x(), newHeight - (34 + Border)); if (kind == MessageKind::Model) ui->eInput->move(ui->eInput->x(), newHeight - (76 + Border)); } Dpi::ModalResult MessgExDlg::showModal() { prepareLayout(); if (Term::gameMode == cMovie) { movieCancelled = false; show(); QEventLoop loop; QTimer timer; timer.setSingleShot(true); QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit); // Poll for CancelMovie() every 10ms instead of the original's // Application.ProcessMessages/Sleep(1) busy loop. QTimer poll; QObject::connect(&poll, &QTimer::timeout, &loop, [&] { if (movieCancelled) loop.quit(); }); poll.start(10); timer.start(1500); loop.exec(); hide(); return Dpi::ModalResult::Ok; } return Dpi::Form::showModal(); } void MessgExDlg::cancelMovie() { movieCancelled = true; } void MessgExDlg::paintBook(Dpi::Canvas &canvas, int x, int y, QColor clPage, QColor clCover) { constexpr int xScrewed = 77, yScrewed = 10, wScrewed = 43, hScrewed = 27; const int width = xSizeBig; const int height = ySizeBig; std::vector> screwed; QRect bookRect; if (iconIndex >= 0 && bigImp) { const int xIcon = (iconIndex % 7) * xSizeBig; const int yIcon = (iconIndex + Term::SystemIconLines * 7) / 7 * ySizeBig; screwed.assign(static_cast(wScrewed * hScrewed), std::array{0, 0, 0, 0}); auto cellAt = [&](int ix, int iy) -> std::array & { return screwed[static_cast(iy * wScrewed + ix)]; }; Dpi::PixelPointer srcPtr(&bigImp->image(), xIcon, yIcon); for (int iy = 0; iy < height; ++iy) { for (int ix = 0; ix < width; ++ix) { double xR = ix * (37.0 + iy * 5.0 / height) / width; int xDst = static_cast(xR); xR -= xDst; const double x1 = (120 - ix) * (120 - ix) - 10000.0; double yR = iy * 18.0 / height + x1 * x1 / 4000000.0; int yDst = static_cast(yR); yR -= yDst; const QRgb srcColor = srcPtr.pixelRGB(); for (int dx = 0; dx <= 1; ++dx) { for (int dy = 0; dy <= 1; ++dy) { double share = dx == 0 ? 1 - xR : xR; share *= dy == 0 ? 1 - yR : yR; if (xDst + dx < wScrewed && yDst + dy < hScrewed) { auto &cell = cellAt(xDst + dx, yDst + dy); cell[0] += share * qBlue(srcColor); cell[1] += share * qGreen(srcColor); cell[2] += share * qRed(srcColor); cell[3] += share; } } } srcPtr.nextPixel(); } srcPtr.nextLine(); } GraphicSetItem *bigBook = Templates ? Templates->searchByName(QStringLiteral("BigBook")) : nullptr; bookRect = bigBook ? bigBook->boundsRect() : QRect(); } else { GraphicSetItem *smallBook = Templates ? Templates->searchByName(QStringLiteral("SmallBook")) : nullptr; bookRect = smallBook ? smallBook->boundsRect() : QRect(); } x -= bookRect.width() / 2; unshareBitmap(drawBuffer); drawBufferEnsureSize(bookRect.width(), bookRect.height()); drawBuffer.canvas().begin(); // Replicates the already-painted background rather than reading it // back from the live widget canvas (Qt can't read back mid-paintEvent // the way the original's GDI-backed BitBltCanvas could) - fill()'s // tiling phase doesn't depend on the destination offset, so this // produces an identical result. fill(drawBuffer.canvas(), 0, 0, bookRect.width(), bookRect.height()); drawBuffer.canvas().end(); if (iconIndex >= 0) { for (int iy = 0; iy < hScrewed; ++iy) { for (int ix = 0; ix < wScrewed; ++ix) { const auto &cell = screwed[static_cast(iy * wScrewed + ix)]; if (cell[3] > 0.01) { const int b = static_cast(cell[0] / cell[3]); const int g = static_cast(cell[1] / cell[3]); const int r = static_cast(cell[2] / cell[3]); drawBuffer.image().setPixel(xScrewed + ix, yScrewed + iy, qRgb(r, g, b)); } } } } if (Templates) imageOpBCC(drawBuffer, Templates->data, 0, 0, bookRect.left(), bookRect.top(), bookRect.width(), bookRect.height(), colorToDelphi(clCover), colorToDelphi(clPage)); canvas.copyRect(QRect(x, y, bookRect.width(), bookRect.height()), drawBuffer.canvas(), QRect(0, 0, bookRect.width(), bookRect.height())); } void MessgExDlg::paintMyArmy(Dpi::Canvas &canvas) { Q_UNUSED(canvas); // Empty in the original too. } void MessgExDlg::paintEnemyArmy(Dpi::Canvas &canvas) { int ix = 0; int iy = 0; int unitsInLine = std::min(Term::nLostArmy, LostUnitsPerLine); for (int emix = 0; emix < ClientTools::myRO->nEnemyModel; ++emix) { const TModelInfo &enemyModel = (*ClientTools::myRO->EnemyModel)[emix]; for (int count = 0; count < Term::lostArmy[static_cast(emix)]; ++count) { const int x = width() / 2 + ix * 64 - unitsInLine * 32; const int y = 26 + Border + topSpace + lines * MessageLineSpacing + iy * 48; const Tribes::ModelPicture &mp = Tribes::tribe[static_cast(enemyModel.Owner)]->modelPictureAt(enemyModel.mix); if (mp.hGr) sprite(canvas, *mp.hGr, x, y, 64, 48, mp.pix % 10 * 65 + 1, mp.pix / 10 * 49 + 1); ++ix; if (ix == LostUnitsPerLine) { ix = 0; ++iy; if (iy == 6) return; unitsInLine = std::min(Term::nLostArmy - LostUnitsPerLine * iy, LostUnitsPerLine); } } } } void MessgExDlg::paintEvent(QPaintEvent *event) { if (iconKind == MessageIconKind::Imp && iconIndex == 27 && MainTexture) { // "YOU WIN" message const QColor saveLight = MainTexture->colorTextLight; const QColor saveShade = MainTexture->colorTextShade; MainTexture->colorTextLight = Qt::black; MainTexture->colorTextShade = colorFromDelphi(0x0FDBFF); BaseMessgDlg::paintEvent(event); MainTexture->colorTextLight = saveLight; MainTexture->colorTextShade = saveShade; } else { BaseMessgDlg::paintEvent(event); } QPainter painter(this); Dpi::Canvas canvas; canvas.bind(&painter); switch (iconKind) { case MessageIconKind::Imp: if (Imp[iconIndex].Kind == ikWonder) { const int p1 = ClientTools::myRO->Wonder[iconIndex].EffectiveOwner; unshareBitmap(drawBuffer); drawBufferEnsureSize(xSizeBig + 16, ySizeBig + 16); fill(drawBuffer.canvas(), 0, 0, xSizeBig + 16, ySizeBig + 16); if (bigImp) bitBltBitmap(drawBuffer, 8, 8, xSizeBig, ySizeBig, *bigImp, (iconIndex % 7) * xSizeBig, (iconIndex + Term::SystemIconLines * 7) / 7 * ySizeBig); glowFrame(drawBuffer, 8, 8, xSizeBig, ySizeBig, p1 < 0 ? QColor(Qt::black) : Tribes::tribe[static_cast(p1)]->color); canvas.copyRect(QRect(width() / 2 - 36, 16, xSizeBig + 16, ySizeBig + 16), drawBuffer.canvas(), QRect(0, 0, xSizeBig + 16, ySizeBig + 16)); } else { Term::impImage(canvas, width() / 2 - 28, 24, iconIndex); } break; case MessageIconKind::Age: Term::impImage(canvas, width() / 2 - 28, 24, iconIndex == 0 ? -7 : 24 + iconIndex); break; case MessageIconKind::Model: { const Tribes::ModelPicture &mp = Tribes::tribe[static_cast(ClientTools::me)]->modelPictureAt(iconIndex); if (bigImp) frameImage(canvas, *bigImp, width() / 2 - 28, 24, xSizeBig, ySizeBig, 0, 0); if (mp.hGr) sprite(canvas, *mp.hGr, width() / 2 - 32, 20, 64, 44, mp.pix % 10 * 65 + 1, mp.pix / 10 * 49 + 1); break; } case MessageIconKind::Book: if (MainTexture) paintBook(canvas, width() / 2, 24, MainTexture->colorPage, MainTexture->colorCover); break; case MessageIconKind::Tribe: { Tribes::Tribe &tribe = *Tribes::tribe[static_cast(iconIndex)]; if (tribe.faceHGr) { frame(canvas, width() / 2 - 32 - 1, 24 - 1, width() / 2 + 32, 24 + 48, Qt::black, Qt::black); canvas.copyRect(QRect(width() / 2 - 32, 24, 64, 48), tribe.faceHGr->data.canvas(), QRect(1 + tribe.facePix % 10 * 65, 1 + tribe.facePix / 10 * 49, 64, 48)); } break; } case MessageIconKind::PureIcon: if (bigImp) frameImage(canvas, *bigImp, width() / 2 - 28, 24, xSizeBig, ySizeBig, (iconIndex % 7) * xSizeBig, (iconIndex / 7) * ySizeBig); break; case MessageIconKind::BigIcon: if (bigImp) frameImage(canvas, *bigImp, width() / 2 - 3 * 28, 32, xSizeBig * 3, ySizeBig * 3, (iconIndex % 2) * 3 * xSizeBig, (iconIndex / 2) * 3 * ySizeBig); break; case MessageIconKind::EnemyShipComplete: { unshareBitmap(drawBuffer); drawBufferEnsureSize(140, 120); fill(drawBuffer.canvas(), 0, 0, 140, 120); GraphicSetItem *departed = Templates ? Templates->searchByName(QStringLiteral("StarshipDeparted")) : nullptr; if (departed && Templates) imageOpBCC(drawBuffer, Templates->data, 0, 0, departed->boundsRect().left(), departed->boundsRect().top(), departed->boundsRect().width(), departed->boundsRect().height(), 0, colorToDelphi(Qt::white)); canvas.copyRect(QRect((width() - 140) / 2, 24, 140, 120), drawBuffer.canvas(), QRect(0, 0, 140, 120)); break; } case MessageIconKind::MyArmy: paintMyArmy(canvas); break; case MessageIconKind::EnemyArmy: paintEnemyArmy(canvas); break; case MessageIconKind::FullControl: if (HGrSystem2) sprite(canvas, *HGrSystem2, width() / 2 - 31, 24, 63, 63, 1, 281); break; case MessageIconKind::Ship: Diagram::paintColonyShip(canvas, iconIndex, 17, width() - 34, 38); break; default: break; } if (ui->eInput->isVisible() && MainTexture) editFrame(canvas, ui->eInput->geometry(), *MainTexture); canvas.unbind(); if (!openSound.isEmpty()) { QTimer::singleShot(0, this, [this] { Sound::play(openSound); openSound.clear(); }); } } void setInstance(MessgExDlg *dlg) { instance = dlg; } namespace { MessgExDlg *current() { return instance ? instance : Term::mainScreenMessgExDlg(); } } // namespace void soundMessageEx(const QString &simpleText, const QString &soundItem) { MessgExDlg *dlg = current(); if (!dlg) return; dlg->messageText = simpleText; dlg->openSound = soundItem; dlg->kind = MessageKind::Ok; dlg->showModal(); } void tribeMessage(int p, const QString &simpleText, const QString &soundItem) { MessgExDlg *dlg = current(); if (!dlg) return; dlg->openSound = soundItem; dlg->messageText = simpleText; dlg->kind = MessageKind::Ok; dlg->iconKind = MessageIconKind::Tribe; dlg->iconIndex = p; dlg->showModal(); } Dpi::ModalResult simpleQuery(MessageKind queryKind, const QString &simpleText, const QString &soundItem) { MessgExDlg *dlg = current(); if (!dlg) return Dpi::ModalResult::None; dlg->messageText = simpleText; dlg->openSound = soundItem; dlg->kind = queryKind; return dlg->showModal(); } void contextMessage(const QString &simpleText, const QString &soundItem, Help::LinkCategory contextKind, int contextNo) { MessgExDlg *dlg = current(); if (!dlg) return; dlg->messageText = simpleText; dlg->openSound = soundItem; dlg->kind = MessageKind::OkHelp; dlg->helpKind = contextKind; dlg->helpNo = contextNo; dlg->showModal(); } } // namespace MessgEx } // namespace Cevo