Files
c-evo-evo/game/localplayer/Enhance.cpp
T

367 lines
14 KiB
C++

// 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 "Enhance.h"
#include "ui_Enhance.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/CevoTexture.h"
#include "ClientTools.h"
#include "Help.h"
#include "KeyBindings.h"
#include "Term.h"
#include <QKeyEvent>
#include <QPainter>
#include <array>
#include <cstring>
#include <set>
namespace Cevo {
namespace Enhance {
namespace {
// TODO: needs Phrases[] (StringTables.pas not ported) - plain English
// literals until localization is ported.
QString phrase(const QString &key)
{
static const QHash<QString, QString> table = {
{QStringLiteral("TITLE_ENHANCE"), QStringLiteral("Enhance terrain")},
{QStringLiteral("BTN_SELECT"), QStringLiteral("Select terrain")},
{QStringLiteral("TWOTERRAINS"), QStringLiteral("%1/%2")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? *it : key;
}
QString terrainName(int terrType)
{
return QStringLiteral("Terrain #%1").arg(terrType);
}
QString enhanceLabel(const QString &terrainLabel, const QString &workTime)
{
return QStringLiteral("%1 (%2)").arg(terrainLabel, workTime);
}
} // namespace
EnhanceDlg::EnhanceDlg(QWidget *parent) : FramedDlg(parent), ui(new Ui::EnhanceDlg)
{
ui->setupUi(this);
noMap = std::make_unique<IsoEngine::IsoMap>();
captionRight = ui->closeBtn->x();
captionLeft = ui->toggleBtn->x() + ui->toggleBtn->width();
initButtons();
setWindowTitle(phrase(QStringLiteral("TITLE_ENHANCE")));
ui->toggleBtn->setToolTip(phrase(QStringLiteral("BTN_SELECT")));
popup = new QMenu(this);
for (int terrType = fGrass; terrType <= fMountains; ++terrType) {
if (terrType == Term::fJungle)
continue;
QString caption;
if (terrType == fGrass)
caption = phrase(QStringLiteral("TWOTERRAINS")).arg(terrainName(fGrass)).arg(terrainName(fGrass + 12));
else if (terrType == fForest)
caption = phrase(QStringLiteral("TWOTERRAINS")).arg(terrainName(fForest)).arg(terrainName(Term::fJungle));
else
caption = terrainName(terrType);
QAction *action = popup->addAction(caption);
action->setCheckable(true);
action->setData(terrType);
connect(action, &QAction::triggered, this, [this, terrType] {
page = terrType;
smartUpdateContent();
});
}
ui->closeBtn->onClick = [this] { close(); };
ui->toggleBtn->onClick = [this] {
for (QAction *action : popup->actions())
action->setChecked(action->data().toInt() == page);
popup->popup(mapToGlobal(QPoint(ui->toggleBtn->x(), ui->toggleBtn->y() + ui->toggleBtn->height())));
};
auto jobClickHandler = [this](Cevo::ButtonC *button) {
button->onClick = [this, button] { jobClicked(button->property("jobTag").toInt()); };
};
ui->job1->setProperty("jobTag", jRoad);
ui->job2->setProperty("jobTag", jRR);
ui->job4->setProperty("jobTag", jIrr);
ui->job5->setProperty("jobTag", jFarm);
ui->job7->setProperty("jobTag", jMine);
ui->job3->setProperty("jobTag", jClear);
ui->job6->setProperty("jobTag", jAfforest);
ui->job9->setProperty("jobTag", jTrans);
jobClickHandler(ui->job1);
jobClickHandler(ui->job2);
jobClickHandler(ui->job4);
jobClickHandler(ui->job5);
jobClickHandler(ui->job7);
jobClickHandler(ui->job3);
jobClickHandler(ui->job6);
jobClickHandler(ui->job9);
onShow = [this] { offscreenPaint(); };
}
EnhanceDlg::~EnhanceDlg()
{
delete ui;
}
void EnhanceDlg::showNewContent(WindowMode newMode, int terrType)
{
page = (terrType < fGrass || terrType > fMountains) ? fGrass : terrType;
FramedDlg::showNewContent(newMode);
}
void EnhanceDlg::offscreenPaint()
{
FramedDlg::offscreenPaint();
if (!Term::myData || !HGrSystem || !noMap->hGrTerrain)
return;
Dpi::Canvas &canvas = offscreen.canvas();
canvas.begin();
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Small)]);
fillOffscreen(0, 0, innerWidth, innerHeight);
int endStage = 0;
while (endStage < 5 && Term::myData->enhancementJobs[page][endStage] != jNone)
++endStage;
int x = innerWidth / 2 - noMap->xxt - (noMap->xxt + 3) * endStage;
int terrType = page;
int tileImp = 0;
std::set<int> done;
int cost = 0;
for (int stage = 0; stage <= endStage; ++stage) {
if (stage > 0) {
sprite(canvas, *HGrSystem, x - 10, 66, 14, 14, 80, 1);
const int currentJob = Term::myData->enhancementJobs[page][stage - 1];
switch (currentJob) {
case jRoad:
cost += Terrain[terrType].MoveCost * RoadWork;
tileImp |= fRoad;
break;
case jRR:
cost += Terrain[terrType].MoveCost * RRWork;
tileImp |= fRR;
break;
case jIrr:
cost += Terrain[terrType].IrrClearWork;
tileImp = (tileImp & ~fTerImp) | tiIrrigation;
break;
case jFarm:
cost += Terrain[terrType].IrrClearWork * FarmWork;
tileImp = (tileImp & ~fTerImp) | tiFarm;
break;
case jMine:
cost += Terrain[terrType].MineAfforestWork;
tileImp = (tileImp & ~fTerImp) | tiMine;
break;
case jClear:
cost += Terrain[terrType].IrrClearWork;
terrType = Terrain[terrType].ClearTerrain;
break;
case jAfforest:
cost += Terrain[terrType].MineAfforestWork;
terrType = Terrain[terrType].AfforestTerrain;
break;
case jTrans:
cost += Terrain[terrType].TransWork;
terrType = Terrain[terrType].TransTerrain;
break;
default:
break;
}
done.insert(currentJob);
}
const int xxt = noMap->xxt;
const int yyt = noMap->yyt;
if (terrType < fForest) {
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + terrType * (xxt * 2 + 1), 1 + yyt);
} else {
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 2 * (yyt * 3 + 1));
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 7 * (xxt * 2 + 1),
1 + yyt + 2 * (2 + terrType - fForest) * (yyt * 3 + 1));
}
if ((tileImp & fTerImp) == tiFarm)
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + (xxt * 2 + 1),
1 + yyt + 12 * (yyt * 3 + 1));
else if ((tileImp & fTerImp) == tiIrrigation)
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1, 1 + yyt + 12 * (yyt * 3 + 1));
if (tileImp & fRR) {
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 6 * (xxt * 2 + 1),
1 + yyt + 10 * (yyt * 3 + 1));
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 10 * (yyt * 3 + 1));
} else if (tileImp & fRoad) {
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 6 * (xxt * 2 + 1),
1 + yyt + 9 * (yyt * 3 + 1));
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 9 * (yyt * 3 + 1));
}
if ((tileImp & fTerImp) == tiMine)
sprite(canvas, *noMap->hGrTerrain, x, 64 - yyt, xxt * 2, yyt * 2, 1 + 2 * (xxt * 2 + 1),
1 + yyt + 12 * (yyt * 3 + 1));
x += xxt * 2 + 6;
}
QString s;
for (QAction *action : popup->actions())
if (action->data().toInt() == page)
s = action->text();
if (cost > 0)
s = enhanceLabel(s, movementToString(cost));
loweredTextOut(canvas, -1, *MainTexture, (innerWidth - biColorTextWidth(canvas, s)) / 2, 12, s);
const int lastJob = endStage > 0 ? Term::myData->enhancementJobs[page][endStage - 1] : jNone;
ui->job1->setButtonIndex(done.count(jRoad) ? 3 : 2);
ui->job2->setButtonIndex(done.count(jRR) ? 3 : 2);
ui->job4->setButtonIndex(done.count(jIrr) ? 3 : 2);
ui->job5->setButtonIndex(done.count(jFarm) ? 3 : 2);
ui->job7->setButtonIndex(done.count(jMine) ? 3 : 2);
ui->job3->setButtonIndex(lastJob == jClear ? 3 : 2);
ui->job6->setButtonIndex(lastJob == jAfforest ? 3 : 2);
ui->job9->setButtonIndex(lastJob == jTrans ? 3 : 2);
const bool typeChanged = lastJob == jClear || lastJob == jAfforest || lastJob == jTrans;
ui->job1->setVisible(done.count(jRoad) || !typeChanged);
ui->job2->setVisible(done.count(jRR) || !typeChanged);
ui->job4->setVisible(done.count(jIrr) || (!typeChanged && Terrain[terrType].IrrEff > 0));
ui->job5->setVisible(done.count(jFarm) || (!typeChanged && Terrain[terrType].IrrEff > 0));
ui->job7->setVisible(done.count(jMine) || (!typeChanged && Terrain[terrType].MineEff > 0));
ui->job3->setVisible((!typeChanged && Terrain[terrType].ClearTerrain >= 0 &&
(terrType != fDesert || ClientTools::myRO->Wonder[woGardens].EffectiveOwner == ClientTools::me)) ||
lastJob == jClear);
ui->job6->setVisible((!typeChanged && Terrain[terrType].AfforestTerrain >= 0) || lastJob == jAfforest);
ui->job9->setVisible((!typeChanged && Terrain[terrType].TransTerrain >= 0) || lastJob == jTrans);
canvas.end();
markUsedOffscreen(innerWidth, innerHeight);
}
void EnhanceDlg::jobClicked(int newJob)
{
if (!Term::myData)
return;
auto &jobs = Term::myData->enhancementJobs[page];
auto removeJob = [&](int j) {
int stage = 0;
while (stage < 5 && jobs[stage] != jNone) {
if (jobs[stage] == j || (j == jRoad && jobs[stage] == jRR) || (j == jIrr && jobs[stage] == jFarm)) {
if (stage < 4)
std::memmove(&jobs[stage], &jobs[stage + 1], 4 - stage);
jobs[4] = jNone;
} else {
++stage;
}
}
};
std::set<int> done;
int stage = 0;
while (stage < 5 && jobs[stage] != jNone) {
done.insert(jobs[stage]);
++stage;
}
if (done.count(newJob)) {
removeJob(newJob);
} else {
if (newJob == jMine || newJob == jAfforest)
removeJob(jIrr);
if (newJob == jIrr || newJob == jFarm || newJob == jTrans)
removeJob(jMine);
if (newJob == jRR && !done.count(jRoad)) {
jobs[stage] = jRoad;
++stage;
}
if (newJob == jFarm && !done.count(jIrr)) {
jobs[stage] = jIrr;
++stage;
}
jobs[stage] = static_cast<std::uint8_t>(newJob);
}
smartUpdateContent();
}
void EnhanceDlg::paintEvent(QPaintEvent *event)
{
FramedDlg::paintEvent(event);
if (!MainTexture)
return;
QPainter painter(this);
if (!painter.isActive())
return;
Dpi::Canvas canvas;
canvas.bind(&painter);
btnFrame(canvas, QRect(ui->job1->x(), ui->job1->y(), ui->job7->x() + ui->job7->width() - ui->job1->x(),
ui->job1->height()),
*MainTexture);
btnFrame(canvas, QRect(ui->job3->x(), ui->job3->y(), ui->job9->x() + ui->job9->width() - ui->job3->x(),
ui->job3->height()),
*MainTexture);
if (HGrSystem) {
const auto children = findChildren<Cevo::ButtonC *>();
for (Cevo::ButtonC *child : children) {
const int tag = child->property("jobTag").toInt();
canvas.copyRect(QRect(child->x() + 2, child->y() - 11, 8, 8), HGrSystem->data.canvas(),
QRect(121 + tag % 7 * 9, 1 + tag / 7 * 9, 8, 8));
}
}
canvas.unbind();
}
void EnhanceDlg::resizeEvent(QResizeEvent *event)
{
FramedDlg::resizeEvent(event);
ui->closeBtn->move(width() - 38, ui->closeBtn->y());
}
void EnhanceDlg::keyPressEvent(QKeyEvent *event)
{
const QKeySequence pressed(event->key() | event->modifiers());
if (KeyBindings::bHelp && pressed.matches(KeyBindings::bHelp->shortCut) == QKeySequence::ExactMatch) {
// TODO: needs a live HelpDlg instance's textIndex() (Term.pas's
// MainScreen.HelpDlg isn't ported yet) - placeholder text index.
if (auto *dlg = Term::mainScreenHelpDlg())
dlg->showNewContent(windowModeMakePersistent(windowMode()), Help::LinkCategory::Text, 0);
} else
FramedDlg::keyPressEvent(event);
}
} // namespace Enhance
} // namespace Cevo