Files
c-evo-evo/game/localplayer/Help.cpp
T
2026-11-17 12:28:13 +01:00

364 lines
13 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.
// PORT STATUS (see /memories/repo/cevo-evo-game.md): Help.pas is 2297
// lines, split across 3 files here (matching the GameServer.cpp/
// Term.MainScreen.cpp precedent for large files):
// - Help.cpp (this file): the dialog shell - construction, navigation/
// history, event handlers, the prepare() dispatcher.
// - Help.Prepare.cpp: per-LinkCategory content generation (prepareAdvance/
// Improvement/Terrain/Feature/Model/Misc*), addTextual()'s markup
// parser, and search().
// - Help.Paint.cpp: offscreenPaint()/line() and the real per-TextFormat
// icon rendering (small/big advance+improvement icons, terrain tiles,
// feature/domain/government icons, link-indicator dots).
// Remaining disclosed simplifications: waterSign() draws the real
// improvement/wonder illustration but skips the original's per-pixel
// sky-detecting watermark blend (plain blit instead); the isometric
// terrain-tile icon (TextFormat::Ter) draws a single representative sprite
// rather than the original's 2-layer edge-blended compositing (same
// simplification Enhance.cpp's own terrain preview already uses); the
// Credits page doesn't scan Graphics/Sounds/ for per-asset *.credits.txt
// filesystem credit rolls.
#include "Help.h"
#include "ui_Help.h"
#include "Cevo/CevoScreenTools.h"
#include "Cevo/Protocol.h"
#include "ClientTools.h"
#include "Global.h"
#include "Inp.h"
#include "KeyBindings.h"
#include "Messg.h"
#include "Term.h"
#include <QKeyEvent>
#include <QMouseEvent>
#include <QWheelEvent>
namespace Cevo {
namespace Help {
std::function<void(WindowMode, LinkCategory, int)> showNewContent;
namespace {
constexpr int MaxHist = 16;
} // namespace
void HyperText::addLine(const QString &s, TextFormat format, int picPix, LinkCategory linkCategory, int linkIndex,
bool crossLink)
{
m_lines.push_back(s);
HelpLineInfo info;
info.format = format;
info.picPix = picPix;
info.category = linkCategory;
info.index = linkIndex;
info.crossLink = crossLink;
m_info.push_back(info);
}
void HyperText::appendList(const HyperText &source)
{
for (int i = 0; i < source.count(); ++i) {
m_lines.push_back(source.text(i));
m_info.push_back(source.info(i));
}
}
void HyperText::clear()
{
m_lines.clear();
m_info.clear();
}
HelpDlg::HelpDlg(QWidget *parent) : FramedDlg(parent), ui(new Ui::HelpDlg)
{
ui->setupUi(this);
initButtons();
ui->closeBtn->setButtonIndex(0);
ui->backBtn->setButtonIndex(6);
ui->topBtn->setButtonIndex(7);
ui->searchBtn->setButtonIndex(18);
captionLeft = ui->backBtn->x() + ui->backBtn->width();
captionRight = ui->searchBtn->x();
modalFrameIndent += 29;
scrollBar = new PVScrollBar(this);
scrollBar->setBorderSpacing(36, 9, 11);
connect(scrollBar, &PVScrollBar::updated, this, [this] {
sel = -1;
smartUpdateContent(true);
});
noMap = std::make_unique<IsoEngine::IsoMap>();
helpText.loadFromFile(localizedFilePath(QStringLiteral("Help/Help.txt")));
helpContext = QStringLiteral("CONCEPTS");
ui->topBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_CONTENTS")));
ui->backBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_BACK")));
ui->searchBtn->setToolTip(phrases.lookup(QStringLiteral("BTN_SEARCH")));
ui->closeBtn->onClick = [this] { close(); };
ui->backBtn->onClick = [this] { backBtnClicked(); };
ui->topBtn->onClick = [this] { topBtnClicked(); };
ui->searchBtn->onClick = [this] { searchBtnClicked(); };
showNewContentHook = [this](WindowMode mode, const QString &context) {
showNewContent(mode, LinkCategory::Text, textIndex(context));
};
Cevo::Help::showNewContent = [this](WindowMode mode, LinkCategory category, int index) {
showNewContent(mode, category, index);
};
}
HelpDlg::~HelpDlg()
{
showNewContentHook = nullptr;
Cevo::Help::showNewContent = nullptr;
delete ui;
}
void HelpDlg::showNewContent(WindowMode newMode, LinkCategory category, int index)
{
if (category != kind || index != no ||
(category == LinkCategory::Misc && index == static_cast<int>(MiscLinkIndex::SearchResult))) {
if (static_cast<int>(histItems.size()) == MaxHist)
histItems.erase(histItems.begin());
HistItem item;
if (histItems.empty()) {
item.kind = category;
item.no = index;
item.searchContent = newSearchContent;
} else {
item.kind = kind;
item.no = no;
item.searchContent = searchContent;
}
item.pos = scrollBar->position();
histItems.push_back(item);
}
kind = category;
no = index;
searchContent = newSearchContent;
prepare();
offscreenPaint();
FramedDlg::showNewContent(newMode);
}
void HelpDlg::prepare(int sbPos)
{
mainText.clear();
QString title;
switch (kind) {
case LinkCategory::Adv: title = phrases.lookup(QStringLiteral("ADVANCES"), no); break;
case LinkCategory::Imp: title = phrases.lookup(QStringLiteral("IMPROVEMENTS"), no); break;
case LinkCategory::Ter:
title = no == 200 ? helpText.lookup(QStringLiteral("HELPTITLE_TERLIST"))
: phrases.lookup(QStringLiteral("TERRAIN"), no);
break;
case LinkCategory::Feature:
title = no == 200 ? helpText.lookup(QStringLiteral("HELPTITLE_FEATURELIST"))
: phrases.lookup(QStringLiteral("FEATURES"), no);
break;
case LinkCategory::Model: title = helpText.lookup(QStringLiteral("HELPTITLE_MODELLIST")); break;
case LinkCategory::Text: title = helpText.lookupByHandle(no); break;
case LinkCategory::Misc:
switch (static_cast<MiscLinkIndex>(no)) {
case MiscLinkIndex::Main: title = helpText.lookup(QStringLiteral("HELPTITLE_MAIN")); break;
case MiscLinkIndex::Credits: title = helpText.lookup(QStringLiteral("HELPTITLE_CREDITS")); break;
case MiscLinkIndex::GovList: title = helpText.lookup(QStringLiteral("HELPTITLE_GOVLIST")); break;
case MiscLinkIndex::JobList: title = helpText.lookup(QStringLiteral("HELPTITLE_JOBLIST")); break;
case MiscLinkIndex::SearchResult: title = helpText.lookup(QStringLiteral("HELPTITLE_SEARCHRESULTS")); break;
}
break;
default: title = QStringLiteral("Help"); break;
}
if (title.isEmpty())
title = QStringLiteral("Help");
mainText.addLine(title, TextFormat::Caption);
mainText.lineFeed();
if (kind == LinkCategory::Misc && static_cast<MiscLinkIndex>(no) == MiscLinkIndex::SearchResult) {
mainText.addLine(formatS(helpText.lookup(QStringLiteral("MATCHES")), searchContent));
mainText.appendList(searchResult);
} else if (kind == LinkCategory::Adv && no >= 0 && no < nAdv) {
prepareAdvance(no);
} else if (kind == LinkCategory::Imp && no >= 0 && no < nImp) {
prepareImprovement(no);
} else if (kind == LinkCategory::Ter && (no == 200 || (no >= 0 && no < 48))) {
prepareTerrain(no);
} else if (kind == LinkCategory::Feature && (no == 200 || (no >= 0 && no < nFeature))) {
prepareFeature(no);
} else if (kind == LinkCategory::Model) {
prepareModel(no);
} else if (kind == LinkCategory::Misc && static_cast<MiscLinkIndex>(no) == MiscLinkIndex::Main) {
prepareMiscMain();
} else if (kind == LinkCategory::Misc && static_cast<MiscLinkIndex>(no) == MiscLinkIndex::GovList) {
prepareMiscGovList();
} else if (kind == LinkCategory::Misc && static_cast<MiscLinkIndex>(no) == MiscLinkIndex::JobList) {
prepareMiscJobList();
} else if (kind == LinkCategory::Misc && static_cast<MiscLinkIndex>(no) == MiscLinkIndex::Credits) {
prepareMiscCredits();
} else if (kind == LinkCategory::Text) {
addTextual(helpText.lookupByHandle(no));
} else {
mainText.addLine(QStringLiteral("Detailed help content isn't available in this build."));
}
mainText.lineFeed();
scrollBar->init(mainText.count() - 1, innerHeight / 24);
scrollBar->setPos(sbPos);
ui->backBtn->setVisible(histItems.size() > 1);
ui->topBtn->setVisible(histItems.size() > 1 || kind != LinkCategory::Misc ||
no != static_cast<int>(MiscLinkIndex::Main));
sel = -1;
}
void HelpDlg::resizeEvent(QResizeEvent *event)
{
FramedDlg::resizeEvent(event);
ui->searchBtn->move(width() - 67, ui->searchBtn->y());
ui->closeBtn->move(width() - 38, ui->closeBtn->y());
}
void HelpDlg::closeEvent(QCloseEvent *event)
{
extPic.reset(); // Help.pas's ExtPic.Height := 0 - free the illustration bitmap.
FramedDlg::closeEvent(event);
}
void HelpDlg::keyPressEvent(QKeyEvent *event)
{
const QKeySequence pressed(event->keyCombination());
if (KeyBindings::bHelp && pressed.matches(KeyBindings::bHelp->shortCut) == QKeySequence::ExactMatch) {
// Swallow (matches the original's empty branch - already viewing help).
} else {
FramedDlg::keyPressEvent(event);
}
}
void HelpDlg::wheelEvent(QWheelEvent *event)
{
scrollBar->setPosition(scrollBar->position() - event->angleDelta().y() / 120);
sel = -1;
offscreenPaint();
update();
}
void HelpDlg::mouseMoveEvent(QMouseEvent *event)
{
const int y = event->position().toPoint().y() - WideFrame;
const int x = event->position().toPoint().x();
const int i0 = scrollBar->position();
const int sel0 = sel;
if (x >= SideFrame && x < SideFrame + innerWidth && y >= 0 && y < innerHeight && y % 24 >= 8)
sel = y / 24;
else
sel = -1;
if (sel >= 0) {
const int idx = sel + i0;
if (idx >= mainText.count() ||
(mainText.info(idx).category == LinkCategory::NoLink && mainText.info(idx).index == 0))
sel = -1;
}
if (sel != sel0) {
offscreenPaint();
update();
}
FramedDlg::mouseMoveEvent(event);
}
void HelpDlg::mousePressEvent(QMouseEvent *event)
{
if (sel >= 0) {
const int idx = sel + scrollBar->position();
if (idx < mainText.count()) {
const HelpLineInfo &info = mainText.info(idx);
if (info.category != LinkCategory::Internet && info.index >= 0)
showNewContent(windowMode(), info.category, info.index);
}
}
FramedDlg::mousePressEvent(event);
}
void HelpDlg::backBtnClicked()
{
if (histItems.size() > 1) {
const HistItem item = histItems.back();
histItems.pop_back();
if (item.kind == LinkCategory::Misc && item.no == static_cast<int>(MiscLinkIndex::SearchResult) &&
item.searchContent != searchContent) {
searchContent = item.searchContent;
search(searchContent);
}
kind = item.kind;
no = item.no;
prepare(item.pos);
offscreenPaint();
update();
}
}
void HelpDlg::topBtnClicked()
{
while (histItems.size() > 1)
histItems.pop_back();
kind = LinkCategory::Misc;
no = static_cast<int>(MiscLinkIndex::Main);
prepare();
offscreenPaint();
update();
}
void HelpDlg::searchBtnClicked()
{
auto inputDlg = std::make_unique<InputDlg>(nullptr);
inputDlg->setWindowTitle(phrases.lookup(QStringLiteral("SEARCH")));
inputDlg->editInput()->setText(searchContent);
inputDlg->centerToRect(geometry());
if (inputDlg->showModal() == Dpi::ModalResult::Ok && inputDlg->editInput()->text().length() >= 2) {
search(inputDlg->editInput()->text());
if (searchResult.count() == 0) {
simpleMessage(QStringLiteral("No matches found."));
} else if (searchResult.count() == 1) {
const HelpLineInfo &info = searchResult.info(0);
showNewContent(windowMode(), info.category, info.index);
} else {
newSearchContent = inputDlg->editInput()->text();
showNewContent(windowMode(), LinkCategory::Misc, static_cast<int>(MiscLinkIndex::SearchResult));
}
}
}
} // namespace Help
} // namespace Cevo