first pass of help implementation

This commit is contained in:
Geoffray Levasseur committed 2026-11-03 15:32:58 +01:00
1 parent 1667e18dda
commit b5085fd11f
3 files changed
+617 -9

No files matched your search

+378
View File
@@ -26,12 +26,390 @@
// The fact that you are presently reading this means that you have had // The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms. // 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; this is a deliberately SIMPLIFIED port. The navigation/history/
// search-plumbing/scrolling/link-click machinery is faithful, but
// prepare()/offscreenPaint()/line() render a plain single-paragraph
// placeholder per page instead of the original's full icon-rich,
// per-TextFormat layout engine (pkAdvIcon/pkBigIcon/pkTer/pkModel/...),
// because that engine's actual CONTENT comes from Help/Help.txt via
// StringTables.pas, which isn't ported (StringTable below is a disclosed
// stub). waterSign()/paintTerrIcon() (cosmetic pixel effects) are no-ops.
// Extend this file (not a new one) if StringTables.pas is ever ported.
#include "Help.h" #include "Help.h"
#include "ui_Help.h"
#include "Cevo/CevoScreenTools.h"
#include "Global.h"
#include "Inp.h"
#include "KeyBindings.h"
#include "Messg.h"
#include <QHash>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QPainter>
#include <QWheelEvent>
#include <algorithm>
#include <memory>
namespace Cevo { namespace Cevo {
namespace Help { namespace Help {
std::function<void(WindowMode, LinkCategory, int)> showNewContent; std::function<void(WindowMode, LinkCategory, int)> showNewContent;
namespace {
constexpr int MaxHist = 16;
// Phrases/Phrases2 (StringTables.pas) aren't ported; disclosed
// plain-English placeholders for this dialog's own chrome text.
QString phrase(const QString &key)
{
static const QHash<QString, QString> table = {
{QStringLiteral("BTN_CONTENTS"), QStringLiteral("Contents")},
{QStringLiteral("BTN_BACK"), QStringLiteral("Back")},
{QStringLiteral("BTN_SEARCH"), QStringLiteral("Search")},
{QStringLiteral("SEARCH"), QStringLiteral("Search Help")},
};
const auto it = table.constFind(key);
return it != table.constEnd() ? it.value() : key;
}
} // 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);
});
helpText.loadFromFile(localizedFilePath(QStringLiteral("Help/Help.txt")));
helpContext = QStringLiteral("CONCEPTS");
ui->topBtn->setToolTip(phrase(QStringLiteral("BTN_CONTENTS")));
ui->backBtn->setToolTip(phrase(QStringLiteral("BTN_BACK")));
ui->searchBtn->setToolTip(phrase(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 = QStringLiteral("Advance #%1").arg(no); break;
case LinkCategory::Imp: title = QStringLiteral("Improvement #%1").arg(no); break;
case LinkCategory::Ter: title = QStringLiteral("Terrain #%1").arg(no); break;
case LinkCategory::Feature: title = QStringLiteral("Feature #%1").arg(no); break;
case LinkCategory::Model: title = QStringLiteral("Unit design #%1").arg(no); break;
case LinkCategory::Text: title = helpText.lookupByHandle(no); break;
case LinkCategory::Misc:
switch (static_cast<MiscLinkIndex>(no)) {
case MiscLinkIndex::Main: title = QStringLiteral("C-evo Help"); break;
case MiscLinkIndex::Credits: title = QStringLiteral("Credits"); break;
case MiscLinkIndex::GovList: title = QStringLiteral("Governments"); break;
case MiscLinkIndex::JobList: title = QStringLiteral("Terrain jobs"); break;
case MiscLinkIndex::SearchResult: title = QStringLiteral("Search results"); 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.appendList(searchResult);
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::line(Dpi::Canvas &canvas, int i, bool lit)
{
const int idx = scrollBar->position() + i;
if (idx < 0 || idx >= mainText.count())
return;
const QString &s = mainText.text(idx);
if (s.isEmpty())
return;
int x = x0[static_cast<std::size_t>(i + 2)];
int y = 2 + i * 24;
if (&canvas != &offscreen.canvas()) {
x += SideFrame;
y += WideFrame;
}
const HelpLineInfo &lineInfo = mainText.info(idx);
const bool isCaption = lineInfo.format == TextFormat::Caption;
if (isCaption)
canvas.font().setBold(true);
const QColor textColor = isCaption ? captionColor : colorFromDelphi(0x000000);
biColorTextOut(canvas, textColor, colorFromDelphi(0x7F007F), x, y, s);
if (lit) {
canvas.pen().setColor(textColor);
canvas.moveTo(x + 1, y + 18);
canvas.lineTo(x + biColorTextWidth(canvas, s), y + 18);
}
if (isCaption)
canvas.font().setBold(false);
}
void HelpDlg::paintTerrIcon(int x, int y, int xSrc, int ySrc)
{
// Cosmetic terrain-icon compositing (uses a TIsoMap instance in the
// original) - not ported, no caller in this simplified offscreenPaint.
Q_UNUSED(x);
Q_UNUSED(y);
Q_UNUSED(xSrc);
Q_UNUSED(ySrc);
}
void HelpDlg::offscreenPaint()
{
FramedDlg::offscreenPaint();
captionColor = colorFromDelphi(0xC08040);
Dpi::Canvas &canvas = offscreen.canvas();
canvas.begin();
fillSeamless(canvas, 0, 0, innerWidth, innerHeight, 0, scrollBar->position() * 24, paper);
canvas.font().assign(UniFont[static_cast<std::size_t>(FontType::Normal)]);
for (int i = -2; i <= innerHeight / 24; ++i) {
const int idx = scrollBar->position() + i;
if (idx < 0 || idx >= mainText.count())
continue;
x0[static_cast<std::size_t>(i + 2)] = 8;
line(canvas, i, i == sel);
}
canvas.end();
markUsedOffscreen(innerWidth, innerHeight);
}
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.Height := 0; -- the external-link preview bitmap isn't ported.
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(phrase(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));
}
}
}
void HelpDlg::search(const QString &searchString)
{
searchResult.clear();
// Real full-text search needs Help/Help.txt's content via
// StringTables.pas (not ported) - the StringTable stub's search()
// always returns false, so this never finds anything yet.
int handle = -1;
int index = -1;
while (helpText.search(searchString, handle, index)) {
// unreachable with the current StringTable stub
}
}
} // namespace Help } // namespace Help
} // namespace Cevo } // namespace Cevo
+148 -9
View File
@@ -29,26 +29,165 @@
#pragma once #pragma once
#include "Cevo/CevoBaseWin.h" #include "Cevo/CevoBaseWin.h"
#include "Cevo/CevoPVScrollBar.h"
#include <QColor>
#include <QString>
#include <array>
#include <functional> #include <functional>
#include <vector>
QT_BEGIN_NAMESPACE
namespace Ui {
class HelpDlg;
}
QT_END_NAMESPACE
namespace Cevo { namespace Cevo {
namespace Help { namespace Help {
// PORT STATUS: Help.pas itself (~2300 lines, the in-game encyclopedia) is
// deliberately deferred - it needs StringTables.pas (not ported) for its
// actual content. This file only pre-seeds the handful of symbols other
// leaf dialogs need to request a help page; extend this same file in
// place when Help.pas's real turn comes, do not create a second
// Help-ish file.
// Help.pas's `TLinkCategory` (which help-content section to jump to). // Help.pas's `TLinkCategory` (which help-content section to jump to).
enum class LinkCategory { NoLink, Adv, Imp, Ter, Feature, Internet, Model, Misc, Text }; enum class LinkCategory { NoLink, Adv, Imp, Ter, Feature, Internet, Model, Misc, Text };
// Mirrors `MainScreen.HelpDlg.ShowNewContent(NewMode, Category, Index)` - // Mirrors `MainScreen.HelpDlg.ShowNewContent(NewMode, Category, Index)` -
// null (no-op) until Term.pas is ported and wires this up to a real // wired up to a real HelpDlg instance's showNewContent() once one exists
// THelpDlg instance. // (Term.pas, which owns the one true instance, isn't ported yet).
extern std::function<void(WindowMode, LinkCategory, int)> showNewContent; extern std::function<void(WindowMode, LinkCategory, int)> showNewContent;
// Help.pas's `TMiscLinkIndex` (link indices for category Misc).
enum class MiscLinkIndex { Main, Credits, GovList, JobList, SearchResult };
// Help.pas's `TTextFormat` (how a hypertext line's icon/layout is drawn).
enum class TextFormat {
Normal, Caption, SmallIcon, BigIcon, AdvIcon, Ter, BigTer, Feature, Dot, NormalDot, Domain, Section, BigFeature,
Exp, AITStat, External, Model, Normal64, Illu, Logo, TerImp, RightIcon, AdvIconAsPreq, SmallIconAsPreq,
SpecialIcon, Gov,
};
// THelpLineInfo equivalent: per-line metadata stored alongside each
// QString in a HyperText list.
struct HelpLineInfo {
TextFormat format = TextFormat::Normal;
int picPix = 0;
LinkCategory category = LinkCategory::NoLink;
int index = 0;
bool crossLink = false;
};
// THyperText equivalent: a list of displayable lines, each with its own
// HelpLineInfo (TStringList+Objects in the original).
class HyperText {
public:
void addLine(const QString &s = QString(), TextFormat format = TextFormat::Normal, int picPix = 0,
LinkCategory linkCategory = LinkCategory::NoLink, int linkIndex = 0, bool crossLink = false);
void lineFeed() { addLine(); }
void appendList(const HyperText &source);
void clear();
int count() const { return static_cast<int>(m_lines.size()); }
const QString &text(int i) const { return m_lines[static_cast<std::size_t>(i)]; }
QString &text(int i) { return m_lines[static_cast<std::size_t>(i)]; }
const HelpLineInfo &info(int i) const { return m_info[static_cast<std::size_t>(i)]; }
private:
std::vector<QString> m_lines;
std::vector<HelpLineInfo> m_info;
};
// THistItem equivalent: one navigation history entry.
struct HistItem {
LinkCategory kind = LinkCategory::NoLink;
int no = 0;
int pos = 0;
QString searchContent;
};
// Minimal stand-in for StringTables.pas's TStringTable (not ported) - a
// key->text lookup with handle-based indirection and full-text search.
// Since Help/Help.txt's real content isn't available without a
// StringTables.pas port, every lookup returns a disclosed placeholder and
// search() never matches anything.
class StringTable {
public:
void loadFromFile(const QString &fileName) { Q_UNUSED(fileName); }
QString operator[](const QString &key) const { return key; }
int getHandle(const QString &key) const
{
Q_UNUSED(key);
return -1;
}
QString lookupByHandle(int handle) const
{
Q_UNUSED(handle);
return QString();
}
bool search(const QString &searchString, int &handle, int &index) const
{
Q_UNUSED(searchString);
Q_UNUSED(handle);
Q_UNUSED(index);
return false;
}
};
// THelpDlg equivalent: the in-game encyclopedia/hypertext browser. UI built
// from Help.ui (adapted from the original's Help.lfm). The real content
// (Help/Help.txt via StringTables.pas) isn't available, so prepare()/
// offscreenPaint() render a simplified, disclosed-placeholder version of
// each page rather than the original's full icon-rich layout - the
// history/search/scrolling/link-click machinery itself is faithful.
class HelpDlg : public FramedDlg {
Q_OBJECT
public:
explicit HelpDlg(QWidget *parent = nullptr);
~HelpDlg() override;
void clearHistory() { histItems.clear(); }
void showNewContent(WindowMode newMode, LinkCategory category, int index);
int textIndex(const QString &item) const { return helpText.getHandle(item); }
protected:
void offscreenPaint() override;
void resizeEvent(QResizeEvent *event) override;
void closeEvent(QCloseEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private:
void prepare(int sbPos = 0);
void search(const QString &searchString);
void line(Dpi::Canvas &canvas, int i, bool lit);
void paintTerrIcon(int x, int y, int xSrc, int ySrc);
// Placeholder for the original's pixel-level water-reflection effect
// on wonder/illustration icons - purely cosmetic, not yet ported.
void waterSign(int x0, int y0, int iix)
{
Q_UNUSED(x0);
Q_UNUSED(y0);
Q_UNUSED(iix);
}
void backBtnClicked();
void topBtnClicked();
void searchBtnClicked();
LinkCategory kind = LinkCategory::Misc;
int no = static_cast<int>(MiscLinkIndex::Main);
int sel = -1;
QColor captionColor;
QString searchContent;
QString newSearchContent;
HyperText mainText;
HyperText searchResult;
StringTable helpText;
std::vector<HistItem> histItems;
PVScrollBar *scrollBar;
std::array<int, 183> x0{}; // x0[-2..180], offset by +2
Ui::HelpDlg *ui;
};
} // namespace Help } // namespace Help
} // namespace Cevo } // namespace Cevo
+91
View File
@@ -0,0 +1,91 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<ui version="4.0">
<class>HelpDlg</class>
<widget class="QWidget" name="HelpDlg">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>560</width>
<height>479</height>
</rect>
</property>
<property name="windowTitle">
<string>C-evo</string>
</property>
<widget class="Cevo::ButtonB" name="topBtn">
<property name="geometry">
<rect>
<x>13</x>
<y>6</y>
<width>25</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonB" name="backBtn">
<property name="geometry">
<rect>
<x>42</x>
<y>6</y>
<width>25</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonB" name="searchBtn">
<property name="geometry">
<rect>
<x>493</x>
<y>6</y>
<width>25</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="Cevo::ButtonB" name="closeBtn">
<property name="geometry">
<rect>
<x>522</x>
<y>6</y>
<width>25</width>
<height>25</height>
</rect>
</property>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>Cevo::ButtonB</class>
<extends>QWidget</extends>
<header>Cevo/CevoButtonB.h</header>
</customwidget>
</customwidgets>
<resources/>
</ui>