From 3b90a571c08a001b5bc196710aa36215896922f6 Mon Sep 17 00:00:00 2001 From: Geoffray Levasseur Date: Tue, 3 Nov 2026 11:40:52 +0100 Subject: [PATCH] added diagram dialog --- CMakeLists.txt | 3 + game/localplayer/Diagram.cpp | 366 +++++++++++++++++++++++++++++++++++ game/localplayer/Diagram.h | 80 ++++++++ game/localplayer/Diagram.ui | 71 +++++++ 4 files changed, 520 insertions(+) create mode 100644 game/localplayer/Diagram.cpp create mode 100644 game/localplayer/Diagram.h create mode 100644 game/localplayer/Diagram.ui diff --git a/CMakeLists.txt b/CMakeLists.txt index 46b779b..58ba3e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,6 +119,9 @@ set(PROJECT_SOURCES game/localplayer/KeyBindings.h game/localplayer/ClientTools.cpp game/localplayer/ClientTools.h + game/localplayer/Diagram.cpp + game/localplayer/Diagram.h + game/localplayer/Diagram.ui game/localplayer/Diplomacy.cpp game/localplayer/Diplomacy.h game/localplayer/Tribes.cpp diff --git a/game/localplayer/Diagram.cpp b/game/localplayer/Diagram.cpp new file mode 100644 index 0000000..f94b100 --- /dev/null +++ b/game/localplayer/Diagram.cpp @@ -0,0 +1,366 @@ +// 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 "Diagram.h" +#include "ui_Diagram.h" + +#include "Cevo/CevoScreenTools.h" +#include "Cevo/CevoTexture.h" +#include "Cevo/Database.h" +#include "ClientTools.h" +#include "Tribes.h" + +#include +#include +#include + +#include + +namespace Cevo { +namespace Diagram { + +namespace { + +// 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("BTN_PAGE"), QStringLiteral("Next chart")}, + {QStringLiteral("BTN_SELECT"), QStringLiteral("Select nation")}, + {QStringLiteral("TITLE_DIAGRAMS"), QStringLiteral("Charts")}, + {QStringLiteral("TITLE_SHIPS"), QStringLiteral("Colonization Ships")}, + {QStringLiteral("SHARE"), QStringLiteral("%1/%2")}, + {QStringLiteral("SHIPHAB"), QStringLiteral("Habitation")}, + {QStringLiteral("SHIPPOW"), QStringLiteral("Power")}, + {QStringLiteral("SHIPCOMP"), QStringLiteral("Components")}, + }; + const auto it = table.constFind(key); + return it != table.constEnd() ? it.value() : key; +} + +QString phraseDiagram(int mode) +{ + static const QHash table = { + {stPop, QStringLiteral("Population")}, {stTerritory, QStringLiteral("Territory")}, + {stMil, QStringLiteral("Military")}, {stScience, QStringLiteral("Science")}, + {stExplore, QStringLiteral("Explored")}, {stWork, QStringLiteral("Workers")}, + }; + const auto it = table.constFind(mode); + return it != table.constEnd() ? it.value() : QString(); +} + +constexpr int Border = 24; +constexpr int RoundPixels[nStat] = { 0, 0, 0, 5, 5, 5 }; + +constexpr int yArea = 48; +constexpr int xComp[6] = { -60, -28, 4, 4, 36, 68 }; +constexpr int yComp[6] = { -40, -40, -79, -1, -40, -40 }; +constexpr int xPow[4] = { -116, -116, -116, -116 }; +constexpr int yPow[4] = { -28, 0, -44, 16 }; +constexpr int xHab[2] = { 23, 23 }; +constexpr int yHab[2] = { -81, 1 }; + +void shareBar(Dpi::Canvas &canvas, int x, int y, const QString &cap, int val0, int val1) +{ + loweredTextOut(canvas, -1, *MainTexture, x - 2, y, cap); + dLine(canvas, x - 2, x + 169, y + 16, MainTexture->colorTextShade, MainTexture->colorTextLight); + const QString s = val0 > 0 ? phrase(QStringLiteral("SHARE")).arg(val0).arg(val1) : QStringLiteral("0"); + risedTextOut(canvas, x + 170 - biColorTextWidth(canvas, s), y, s); +} + +} // namespace + +void paintColonyShip(Dpi::Canvas &canvas, int player, int left, int width, int top) +{ + canvas.brush().setColor(Qt::black); + canvas.fillRect(QRect(left, top, width, 200)); + canvas.brush().setStyle(Qt::NoBrush); + frame(canvas, left - 1, top - 1, left + width, top + 200, MainTexture->colorBevelShade, + MainTexture->colorBevelLight); + rframe(canvas, left - 2, top - 2, left + width + 1, top + 200 + 1, MainTexture->colorBevelShade, + MainTexture->colorBevelLight); + + // stars + Database::delphiRandSeed = player * 11111; + for (int i = 1; i <= width - 16; ++i) { + const int x = Database::delphiRandom((width - 16) * 200); + const int r = Database::delphiRandom(13) + 28; + const int grey = std::min(255, (r * r * r * r) / 10001); + canvas.setPixel(x / 200 + 8, x % 200 + top, QColor(grey, grey, grey)); + } + + int nComp = static_cast(ClientTools::myRO->Ship[player].Parts[spComp]); + int nPow = static_cast(ClientTools::myRO->Ship[player].Parts[spPow]); + int nHab = static_cast(ClientTools::myRO->Ship[player].Parts[spHab]); + if (nComp > 6) nComp = 6; + if (nPow > 4) nPow = 4; + if (nHab > 2) nHab = 2; + + for (int i = 0; i < nHab; ++i) + sprite(canvas, *HGrSystem2, left + width / 2 + xHab[i], top + 100 + yHab[i], 80, 80, 34, 1); + for (int i = 0; i < nComp; ++i) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[i], top + 100 + yComp[i], 32, 80, 1, 1); + if (nComp > 0) + for (int i = 3; i >= nPow; --i) + sprite(canvas, *HGrSystem2, left + width / 2 + xPow[i] + 40, top + 100 + yPow[i], 16, 27, 1, 82); + for (int i = nPow - 1; i >= 0; --i) + sprite(canvas, *HGrSystem2, left + width / 2 + xPow[i], top + 100 + yPow[i], 56, 28, 58, 82); + if (nComp < 3 && nHab >= 1) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[2] + 32 - 16, top + 100 + 7 + yComp[2], 16, 27, 1, 82); + if (nComp >= 3 && nHab < 1) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[2] + 32, top + 100 + 7 + yComp[2], 16, 27, 18, 82); + if (nComp < 4 && nHab >= 2) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[3] + 32 - 16, top + 100 + 46 + yComp[3], 16, 27, 1, 82); + if (nComp >= 4 && nHab < 2) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[3] + 32, top + 100 + 46 + yComp[3], 16, 27, 18, 82); + if (nComp != 6 && nComp != 2 && !(nComp == 0 && nPow < 1)) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[nComp], top + 100 + 7 + yComp[nComp], 16, 27, 18, 82); + if (nComp != 6 && nComp != 3 && !(nComp == 0 && nPow < 2)) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[nComp], top + 100 + 46 + yComp[nComp], 16, 27, 18, 82); + if (nComp == 2) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[3], top + 100 + 7 + yComp[3], 16, 27, 18, 82); + if (nComp == 3) + sprite(canvas, *HGrSystem2, left + width / 2 + xComp[4], top + 100 + 7 + yComp[4], 16, 27, 18, 82); +} + +DiaDlg::DiaDlg(QWidget *parent) : FramedDlg(parent), ui(new Ui::DiaDlg) +{ + ui->setupUi(this); + ui->closeBtn->setButtonIndex(0); + ui->toggleBtn->setButtonIndex(15); + popup = new QMenu(this); + + titleHeight = WideFrame + 20; + innerHeight = height() - titleHeight - NarrowFrame; + captionRight = ui->closeBtn->x(); + captionLeft = ui->toggleBtn->x() + ui->toggleBtn->width(); + initButtons(); + + ui->closeBtn->onClick = [this] { close(); }; + ui->toggleBtn->onClick = [this] { toggleBtnClicked(); }; +} + +DiaDlg::~DiaDlg() +{ + delete ui; +} + +void DiaDlg::offscreenPaint() +{ + FramedDlg::offscreenPaint(); + Dpi::Canvas &canvas = offscreen.canvas(); + canvas.begin(); + + if (kind == DiagramKind::Chart) { + canvas.font().assign(UniFont[static_cast(FontType::Tiny)]); + canvas.font().setColor(QColor(0x80, 0x80, 0x80)); + + const int roundRange = RoundPixels[mode] * (ClientTools::myRO->Turn - 1) / (innerWidth - 2 * Border); + std::vector list(static_cast(ClientTools::myRO->Turn + 2)); + auto roundValue = [&](int t) { + const int n = std::min(t, roundRange); + int sum = 0; + for (int i = t - n; i <= t; ++i) + sum += list[static_cast(i)]; + return sum / (n + 1); + }; + + int max = 0; + if (mode == stExplore) { + max = ClientTools::g.lx * ClientTools::g.ly; + } else { + max = -1; + for (int p = 0; p < nPl; ++p) { + if (ClientTools::g.Difficulty[p] > 0 && + ClientTools::server(sGetChart + (mode << 4), ClientTools::me, p, list.data()) >= rExecuted) { + for (int t = 0; t < ClientTools::myRO->Turn; ++t) { + const int r = roundValue(t); + if (r > max) + max = r; + } + } + } + } + + canvas.brush().setColor(Qt::black); + canvas.fillRect(QRect(0, 0, innerWidth, innerHeight)); + canvas.brush().setStyle(Qt::NoBrush); + canvas.pen().setColor(QColor(0x60, 0x60, 0x60)); + canvas.moveTo(Border, innerHeight - Border); + canvas.lineTo(innerWidth - Border, innerHeight - Border); + + const int lineStep = ClientTools::myRO->Turn >= 800 ? 200 : ClientTools::myRO->Turn >= 400 ? 100 : 50; + for (int t = 0; t <= (ClientTools::myRO->Turn - 1) / lineStep; ++t) { + const int x = Border + (innerWidth - 2 * Border) * t * lineStep / (ClientTools::myRO->Turn - 1); + canvas.moveTo(x, Border); + canvas.lineTo(x, innerHeight - Border); + const QString s = QString::number(std::abs(turnToYear(t * lineStep))); + canvas.textOut(x - canvas.textWidth(s) / 2, Border - 16, s); + } + + int y0 = 0; + if (max > 0) { + for (int p = 0; p < nPl; ++p) { + if (ClientTools::g.Difficulty[p] > 0 && + ClientTools::server(sGetChart + (mode << 4), ClientTools::me, p, list.data()) >= rExecuted) { + canvas.pen().setColor(Tribes::tribe[static_cast(p)]->color); + int stop = ClientTools::myRO->Turn - 1; + while (stop > 0 && list[static_cast(stop)] == 0) + --stop; + for (int t = 0; t <= stop; ++t) { + const int r = roundValue(t); + const int x = Border + (innerWidth - 2 * Border) * t / (ClientTools::myRO->Turn - 1); + const int y = innerHeight - Border - (innerHeight - 2 * Border) * r / max; + if (t == 0) + canvas.moveTo(x, y); + else if (RoundPixels[mode] == 0) { + if (y != y0 || t == stop) + canvas.lineTo(x, y); + } else { + canvas.lineTo(x, y); + } + y0 = y; + } + } + } + } + } else { + canvas.font().assign(UniFont[static_cast(FontType::Small)]); + fillOffscreen(0, 0, innerWidth, innerHeight); + + paintColonyShip(canvas, player, 8, innerWidth - 16, yArea); + + shareBar(canvas, innerWidth / 2 - 85, innerHeight - 62, phrase(QStringLiteral("SHIPHAB")), + ClientTools::myRO->Ship[player].Parts[spHab], 2); + shareBar(canvas, innerWidth / 2 - 85, innerHeight - 43, phrase(QStringLiteral("SHIPPOW")), + ClientTools::myRO->Ship[player].Parts[spPow], 4); + shareBar(canvas, innerWidth / 2 - 85, innerHeight - 24, phrase(QStringLiteral("SHIPCOMP")), + ClientTools::myRO->Ship[player].Parts[spComp], 6); + } + + canvas.end(); + markUsedOffscreen(innerWidth, innerHeight); +} + +void DiaDlg::vpaint(Dpi::Canvas &canvas) +{ + FramedDlg::vpaint(canvas); + if (!MainTexture) + return; + + canvas.font().assign(UniFont[static_cast(FontType::Normal)]); + const QString s = kind == DiagramKind::Chart + ? phraseDiagram(mode) + : Tribes::tribe[static_cast(player)]->tPhrase(QStringLiteral("SHORTNAME")); + loweredTextOut(canvas, -1, *MainTexture, (width() - biColorTextWidth(canvas, s)) / 2, 31, s); +} + +void DiaDlg::showEvent(QShowEvent *event) +{ + FramedDlg::showEvent(event); + if (windowMode() == WindowMode::Modal) + centerToScreen(); + offscreenPaint(); +} + +void DiaDlg::resizeEvent(QResizeEvent *event) +{ + FramedDlg::resizeEvent(event); + ui->closeBtn->move(width() - 38, ui->closeBtn->y()); +} + +void DiaDlg::keyPressEvent(QKeyEvent *event) +{ + if (event->key() == Qt::Key_F6 && kind == DiagramKind::Chart) { + toggleBtnClicked(); + } else if (event->key() == Qt::Key_F8 && kind == DiagramKind::Ship) { + // Swallow (matches the original's empty branch). + } else { + FramedDlg::keyPressEvent(event); + } +} + +void DiaDlg::showNewContentCharts(WindowMode newMode) +{ + kind = DiagramKind::Chart; + mode = stPop; + ui->toggleBtn->setButtonIndex(15); + ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_PAGE"))); + setWindowTitle(phrase(QStringLiteral("TITLE_DIAGRAMS"))); + showNewContent(newMode); +} + +void DiaDlg::showNewContentShip(WindowMode newMode, int p) +{ + kind = DiagramKind::Ship; + if (p < 0) { + player = ClientTools::me; + while (ClientTools::myRO->Ship[player].Parts[spComp] + ClientTools::myRO->Ship[player].Parts[spPow] + + ClientTools::myRO->Ship[player].Parts[spHab] == + 0) + player = (player + 1) % nPl; + } else { + player = p; + } + ui->toggleBtn->setButtonIndex(28); + ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_SELECT"))); + setWindowTitle(phrase(QStringLiteral("TITLE_SHIPS"))); + showNewContent(newMode); +} + +void DiaDlg::toggleBtnClicked() +{ + if (kind == DiagramKind::Chart) { + mode = (mode + 1) % nStat; + offscreenPaint(); + update(); + } else { + popup->clear(); + for (int p1 = 0; p1 < nPl; ++p1) { + if (ClientTools::myRO->Ship[p1].Parts[spComp] + ClientTools::myRO->Ship[p1].Parts[spPow] + + ClientTools::myRO->Ship[p1].Parts[spHab] > + 0) { + QAction *action = + popup->addAction(Tribes::tribe[static_cast(p1)]->tPhrase(QStringLiteral("SHORTNAME"))); + action->setCheckable(true); + action->setChecked(p1 == player); + connect(action, &QAction::triggered, this, [this, p1] { playerClicked(p1); }); + } + } + popup->popup(mapToGlobal(QPoint(ui->toggleBtn->x(), ui->toggleBtn->y() + ui->toggleBtn->height()))); + } +} + +void DiaDlg::playerClicked(int p) +{ + showNewContentShip(windowMode(), p); +} + +} // namespace Diagram +} // namespace Cevo diff --git a/game/localplayer/Diagram.h b/game/localplayer/Diagram.h new file mode 100644 index 0000000..96ad342 --- /dev/null +++ b/game/localplayer/Diagram.h @@ -0,0 +1,80 @@ +// 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. + +#pragma once + +#include "Cevo/CevoBaseWin.h" + +class QMenu; + +QT_BEGIN_NAMESPACE +namespace Ui { +class DiaDlg; +} +QT_END_NAMESPACE + +namespace Cevo { +namespace Diagram { + +enum class DiagramKind { Chart, Ship }; + +// TDiaDlg equivalent: the charts/colony-ship status dialog. UI built from +// Diagram.ui (adapted from the original's Diagram.lfm). +class DiaDlg : public FramedDlg { + Q_OBJECT +public: + explicit DiaDlg(QWidget *parent = nullptr); + ~DiaDlg() override; + + void showNewContentCharts(WindowMode newMode); + void showNewContentShip(WindowMode newMode, int p = -1); + +protected: + void offscreenPaint() override; + void vpaint(Dpi::Canvas &canvas) override; + void showEvent(QShowEvent *event) override; + void resizeEvent(QResizeEvent *event) override; + void keyPressEvent(QKeyEvent *event) override; + +private: + void toggleBtnClicked(); + void playerClicked(int p); + + DiagramKind kind = DiagramKind::Chart; + int player = 0; + int mode = 0; + + Ui::DiaDlg *ui; + QMenu *popup; +}; + +// Paints a colony ship's assembled parts (used by DiaDlg's ship view). +void paintColonyShip(Dpi::Canvas &canvas, int player, int left, int width, int top); + +} // namespace Diagram +} // namespace Cevo diff --git a/game/localplayer/Diagram.ui b/game/localplayer/Diagram.ui new file mode 100644 index 0000000..3ace0e5 --- /dev/null +++ b/game/localplayer/Diagram.ui @@ -0,0 +1,71 @@ + + + + DiaDlg + + + + 0 + 0 + 418 + 387 + + + + C-evo + + + + + 13 + 6 + 25 + 25 + + + + + + + 380 + 6 + 25 + 25 + + + + + + + Cevo::ButtonB + QWidget +
Cevo/CevoButtonB.h
+
+
+ +