// 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 "TechTree.h" #include "ui_TechTree.h" #include "Cevo/CevoScreenTools.h" #include "Global.h" #include #include #include #include #include #include namespace Cevo { namespace TechTree { namespace { constexpr int BlackBorder = 4; constexpr int LeftBorder = 72; constexpr int RightBorder = 45; constexpr int TopBorder = 16; constexpr int BottomBorder = 48; constexpr int xStart = 0; constexpr int yStart = 40; constexpr int xPitch = 160; constexpr int yPitch = 90; constexpr int xLegend = 44; constexpr int yLegend = 79; constexpr int yLegendPitch = 32; // Phrases/Phrases2 (StringTables.pas) aren't ported; disclosed // plain-English placeholders for this dialog's own text. QString phrase(const QString &key) { static const QHash table = { {QStringLiteral("MENU_ADVTREE"), QStringLiteral("Advances")}, {QStringLiteral("ADVTREE_UP0"), QStringLiteral("Age I advance")}, {QStringLiteral("ADVTREE_UP1"), QStringLiteral("Age II advance")}, {QStringLiteral("ADVTREE_UP2"), QStringLiteral("Age III/IV advance")}, {QStringLiteral("ADVTREE_GOV"), QStringLiteral("Government advance")}, {QStringLiteral("ADVTREE_OTHER"), QStringLiteral("Other advance")}, }; const auto it = table.constFind(key); return it != table.constEnd() ? it.value() : key; } } // namespace TechTreeDlg::TechTreeDlg(QWidget *parent) : DrawDlg(parent), ui(new Ui::TechTreeDlg) { ui->setupUi(this); initButtons(); ui->closeBtn->onClick = [this] { close(); }; timerKeyPressed = new QTimer(this); timerKeyPressed->setInterval(250); connect(timerKeyPressed, &QTimer::timeout, this, [this] { onTimerKeyPressed(); }); } TechTreeDlg::~TechTreeDlg() { delete ui; } void TechTreeDlg::closeEvent(QCloseEvent *event) { timerKeyPressed->stop(); rightPressed = false; downPressed = false; leftPressed = false; upPressed = false; DrawDlg::closeEvent(event); } void TechTreeDlg::paintEvent(QPaintEvent *) { if (!image) return; QPainter painter(this); Dpi::Canvas canvas; canvas.bind(&painter); // black border canvas.brush().setColor(Qt::black); canvas.fillRect(QRect(0, 0, BlackBorder, height())); canvas.fillRect(QRect(BlackBorder, 0, width() - 2 * BlackBorder, BlackBorder)); canvas.fillRect(QRect(width() - BlackBorder, 0, BlackBorder, height())); canvas.fillRect(QRect(BlackBorder, height() - BlackBorder, width() - 2 * BlackBorder, BlackBorder)); // texturize empty space if (offset.x() > 0) fillRectSeamless(canvas, BlackBorder, BlackBorder, BlackBorder + offset.x(), height() - BlackBorder, -BlackBorder - offset.x(), -BlackBorder - offset.y(), paper); if (offset.x() + image->width() < width() - 2 * BlackBorder) fillRectSeamless(canvas, BlackBorder + offset.x() + image->width(), BlackBorder, width() - BlackBorder, height() - BlackBorder, -BlackBorder - offset.x(), -BlackBorder - offset.y(), paper); const int x = std::max(BlackBorder, BlackBorder + offset.x()); const int w = std::min(BlackBorder + offset.x() + image->width(), width() - BlackBorder); if (offset.y() > 0) fillRectSeamless(canvas, x, BlackBorder, w, BlackBorder + offset.y(), -BlackBorder - offset.x(), -BlackBorder - offset.y(), paper); if (offset.y() + image->height() < height() - 2 * BlackBorder) fillRectSeamless(canvas, x, BlackBorder + offset.y() + image->height(), w, height() - BlackBorder, -BlackBorder - offset.x(), -BlackBorder - offset.y(), paper); const int destX = std::max(BlackBorder, BlackBorder + offset.x()); const int destY = std::max(BlackBorder, BlackBorder + offset.y()); const int blitWidth = std::min({image->width(), image->width() + offset.x(), width() - 2 * BlackBorder, width() - 2 * BlackBorder - offset.x()}); const int blitHeight = std::min({image->height(), image->height() + offset.y(), height() - 2 * BlackBorder, height() - 2 * BlackBorder - offset.y()}); if (blitWidth > 0 && blitHeight > 0) canvas.copyRect(QRect(destX, destY, blitWidth, blitHeight), image->canvas(), QRect(std::max(0, -offset.x()), std::max(0, -offset.y()), blitWidth, blitHeight)); canvas.unbind(); } void TechTreeDlg::showEvent(QShowEvent *event) { DrawDlg::showEvent(event); setWindowTitle(phrase(QStringLiteral("MENU_ADVTREE"))); if (!image) { image = std::make_unique(); image->loadFromFile(localizedFilePath(QStringLiteral("Help/AdvTree") + PngExt)); if (paper.width() == 0) paper.loadFromFile(getGraphicsDir() + QStringLiteral("/Paper.jpg")); Dpi::Canvas &canvas = image->canvas(); canvas.begin(); canvas.font().assign(UniFont[static_cast(FontType::Small)]); canvas.font().setColor(Qt::black); canvas.brush().setStyle(Qt::NoBrush); for (int x = 0; x <= (image->width() - xStart) / xPitch; ++x) { for (int y = 0; y <= (image->height() - yStart) / yPitch; ++y) { const QRgb marker = image->image().pixel(xStart + x * xPitch + 10, yStart + y * yPitch - 1); if (qGreen(marker) == 0 && qBlue(marker) == 0) { const int ad = qRed(marker); QString s = QStringLiteral("Advance %1").arg(ad); while (canvas.textWidth(s) > 112) s.chop(1); canvas.textOut(xStart + x * xPitch + 2, yStart + y * yPitch, s); image->image().setPixel(xStart + x * xPitch + 10, yStart + y * yPitch - 1, TransparentColor2); } } } canvas.textOut(xLegend, yLegend, phrase(QStringLiteral("ADVTREE_UP0"))); canvas.textOut(xLegend, yLegend + yLegendPitch, phrase(QStringLiteral("ADVTREE_UP1"))); canvas.textOut(xLegend, yLegend + 2 * yLegendPitch, phrase(QStringLiteral("ADVTREE_UP2"))); canvas.textOut(xLegend, yLegend + 3 * yLegendPitch, phrase(QStringLiteral("ADVTREE_GOV"))); canvas.textOut(xLegend, yLegend + 4 * yLegendPitch, phrase(QStringLiteral("ADVTREE_OTHER"))); canvas.end(); texturize(*image, paper, QColor::fromRgb(TransparentColor2)); } const Dpi::Monitor monitor = Dpi::Screen::primaryMonitor(); const int newWidth = std::min(monitor.width() - 40, image->width() + LeftBorder + RightBorder + 2 * BlackBorder); const int newHeight = std::min(monitor.height() - 40, image->height() + TopBorder + BottomBorder + 2 * BlackBorder); setGeometry(monitor.left() + (monitor.width() - newWidth) / 2, monitor.top() + (monitor.height() - newHeight) / 2, newWidth, newHeight); ui->closeBtn->move(width() - ui->closeBtn->width() - BlackBorder - 8, BlackBorder + 8); offset.setX((width() - image->width() + LeftBorder - RightBorder) / 2 - BlackBorder); offset.setY(height() - 2 * BlackBorder - image->height() - BottomBorder); } void TechTreeDlg::resizeEvent(QResizeEvent *event) { DrawDlg::resizeEvent(event); ui->closeBtn->move(width() - 43, ui->closeBtn->y()); } void TechTreeDlg::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { dragging = true; down = event->pos(); } DrawDlg::mousePressEvent(event); } void TechTreeDlg::mouseReleaseEvent(QMouseEvent *event) { dragging = false; DrawDlg::mouseReleaseEvent(event); } void TechTreeDlg::mouseMoveEvent(QMouseEvent *event) { if (dragging) { move(event->pos() - down); down = event->pos(); } DrawDlg::mouseMoveEvent(event); } void TechTreeDlg::keyPressEvent(QKeyEvent *event) { // Matches the original's (own) inverted key->direction mapping verbatim. switch (event->key()) { case Qt::Key_Left: rightPressed = true; break; case Qt::Key_Up: downPressed = true; break; case Qt::Key_Right: leftPressed = true; break; case Qt::Key_Down: upPressed = true; break; default: DrawDlg::keyPressEvent(event); return; } onTimerKeyPressed(); if (rightPressed || downPressed || leftPressed || upPressed) timerKeyPressed->start(); else timerKeyPressed->stop(); } void TechTreeDlg::keyReleaseEvent(QKeyEvent *event) { switch (event->key()) { case Qt::Key_Left: rightPressed = false; break; case Qt::Key_Up: downPressed = false; break; case Qt::Key_Right: leftPressed = false; break; case Qt::Key_Down: upPressed = false; break; default: break; } if (rightPressed || downPressed || leftPressed || upPressed) timerKeyPressed->start(); else timerKeyPressed->stop(); } void TechTreeDlg::onTimerKeyPressed() { constexpr int Diff = 50; QPoint change(0, 0); if (rightPressed) change.setX(change.x() + Diff); if (leftPressed) change.setX(change.x() - Diff); if (downPressed) change.setY(change.y() + Diff); if (upPressed) change.setY(change.y() - Diff); if (!change.isNull()) move(change); } void TechTreeDlg::move(QPoint diff) { offset += diff; if (offset.x() > LeftBorder) offset.setX(LeftBorder); if (offset.x() < width() - 2 * BlackBorder - image->width() - RightBorder) offset.setX(width() - 2 * BlackBorder - image->width() - RightBorder); if (offset.y() > TopBorder) offset.setY(TopBorder); if (offset.y() < height() - 2 * BlackBorder - image->height() - BottomBorder) offset.setY(height() - 2 * BlackBorder - image->height() - BottomBorder); update(); } } // namespace TechTree } // namespace Cevo