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

221 lines
9.1 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.
#pragma once
#include "Cevo/CevoBaseWin.h"
#include "Cevo/CevoPVScrollBar.h"
#include "Cevo/CevoStringTables.h"
#include "IsoEngine.h"
#include <QColor>
#include <QString>
#include <array>
#include <functional>
#include <memory>
#include <vector>
QT_BEGIN_NAMESPACE
namespace Ui {
class HelpDlg;
}
QT_END_NAMESPACE
namespace Dpi {
class Bitmap;
}
namespace Cevo {
namespace Help {
// Help.pas's `TLinkCategory` (which help-content section to jump to).
enum class LinkCategory { NoLink, Adv, Imp, Ter, Feature, Internet, Model, Misc, Text };
// Mirrors `MainScreen.HelpDlg.ShowNewContent(NewMode, Category, Index)` -
// wired up to a real HelpDlg instance's showNewContent() once one exists
// (Term.pas, which owns the one true instance, isn't ported yet).
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;
};
// 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) is loaded and rendered with real per-TextFormat icons
// (see Help.Paint.cpp); a handful of cosmetic pixel-level effects (the
// WaterSign watermark blend, isometric terrain-tile edge blending) are
// simplified - see the PORT STATUS comment at the top of Help.cpp.
class HelpDlg : public FramedDlg {
Q_OBJECT
public:
explicit HelpDlg(QWidget *parent = nullptr);
~HelpDlg() override;
void clearHistory() { histItems.clear(); }
// Help.pas's `Difficulty` (the game's difficulty level, 0 for none).
int difficulty = 0;
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);
// Builds mainText's body for a single LinkCategory::Adv page (Help.pas's
// THelpDlg.Prepare's hkAdv branch): prerequisites, and what the advance
// newly allows (governments/improvements/features/further research).
void prepareAdvance(int advance);
// Same idea as prepareAdvance(), for LinkCategory::Imp pages.
void prepareImprovement(int imp);
// Same idea, for LinkCategory::Ter (single terrain type) pages.
void prepareTerrain(int no);
// Same idea, for LinkCategory::Feature (unit feature) pages.
void prepareFeature(int no);
// Same idea, for LinkCategory::Model (special unit) pages.
void prepareModel(int no);
// Misc front page (navigation hub to every other page kind).
void prepareMiscMain();
void prepareMiscGovList();
void prepareMiscJobList();
void prepareMiscCredits();
// Help.pas's DecodeItem(): decodes a ":KEY"/":Xnn" link token (used
// inside addTextual()'s markup) into a category + index.
void decodeItem(const QString &item, LinkCategory &category, int &index) const;
// Help.pas's AddTextual(): parses Help.txt's inline markup (links,
// section/caption headers, bullet lists, line breaks) and word-wraps
// the result into mainText lines. Used by LinkCategory::Text pages and
// a few AddItem()-style single paragraphs elsewhere.
void addTextual(const QString &text);
void search(const QString &searchString);
void line(Dpi::Canvas &canvas, int i, bool lit);
// Draws the leading link-indicator dot and, per `mainText.info(idx)`'s
// TextFormat, the line's icon (if any) - advancing x0[i+2] so line()
// then draws the text starting right after it.
void paintIcon(Dpi::Canvas &canvas, int i);
// The pkTer/pkBigTer inline terrain-tile icon (edge-blended sprite
// pair) - a free-standing helper since it's also reused for the
// "complete terrain list" page's many icons.
void paintTerIcon(Dpi::Canvas &canvas, int &x0Ref, int y, int picpix);
void paintTerrIcon(int x, int y, int xSrc, int ySrc);
// Help.pas's WaterSign(): draws a big improvement/wonder illustration.
// Simplified from the original's per-pixel sky-detecting watermark blend
// (2x upscaled, faded into the paper background) to a plain icon blit -
// still shows the real illustration, just without the blend effect.
void waterSign(int x0, int y0, int 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;
// Help.pas's ExtPic: the single external illustration bitmap (Help/*.png)
// referenced by the currently displayed page's "@filename\" markup.
std::unique_ptr<Dpi::Bitmap> extPic;
// Help.pas's NoMap: a TIsoMap instance used only for its terrain-tile
// geometry (xxt/yyt) and sprite sheet (hGrTerrain) - never shown as a
// real game map.
std::unique_ptr<IsoEngine::IsoMap> noMap;
std::vector<HistItem> histItems;
PVScrollBar *scrollBar;
std::array<int, 183> x0{}; // x0[-2..180], offset by +2
Ui::HelpDlg *ui;
};
// Help.txt phrases carry raw C printf-style specifiers (copied from the
// original's Format() call sites) - NOT Qt's %1/%2 .arg() syntax. Scans left
// to right, filling each %d/%s in order from args (supports formats with
// more than one placeholder, e.g. "#STRENGTH Strength: %d/%d"). Shared by
// Help.cpp and Help.Prepare.cpp.
QString formatArgs(const QString &fmt, const QStringList &args);
QString formatD(const QString &fmt, int value);
QString formatS(const QString &fmt, const QString &value);
} // namespace Help
} // namespace Cevo