Pokered Save Editor 2
Pokemon Red & Blue save file editor - Qt 6 C++/QML
Loading...
Searching...
No Matches
fontpreviewprovider.h
Go to the documentation of this file.
1/*
2 * Copyright 2020 Twilight
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16#pragma once
17#include <QVector>
18#include <QPixmap>
19#include <QQuickImageProvider>
20#include <QString>
21#include <QSize>
22#include <QStringList>
23#include <QColor>
24#include <QCache>
25
26#include <pse-common/types.h>
27
29
43{
46 FontPreviewInstance(QStringList idParts,
48 QSize* size,
49 const QSize& requestedSize);
50
52 enum : int {
66 };
67
68 // Tiles to draw a box with arrow
69 // A box is 6 tiles tall and 20 tiles wide. It's contents are double spaced
70 // so it can only hold 2 lines of text but gives it nice line-height and
71 // it has no left margin but a right margin of 1 tile. This is because the
72 // text sits right in the box but keeps it from overlapping the right bottom
73 // arrow.
74 static constexpr int boxTiles[] = {
75 0x79,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7B,
76 0x7C,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7C,
77 0x7C,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7C,
78 0x7C,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7C,
79 0x7C,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0x7F,0xEE,0x7C,
80 0x7D,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7A,0x7E
81 };
82
83 // Tile size
84 static constexpr int tileSize = 8;
85
86 // Hard-coded in FontDB and in FrontPreviewProvider
87 static constexpr int maxLines = 2;
88
89 // Spacing between mutliple lines
90 static constexpr int lineHeightTiles = 1;
91 static constexpr int lineHeight = lineHeightTiles * tileSize;
92
93 // Width and Heights
94 static constexpr int drawWidthTiles = 20;
95 static constexpr int drawHeightLines1Tiles = 1;
96 static constexpr int drawHeightLines2Tiles = 2 + lineHeightTiles;
97 static constexpr int drawHeightBoxTiles = 6;
98
99 static constexpr int drawWidth = drawWidthTiles * tileSize;
102 static constexpr int drawHeightBox = drawHeightBoxTiles * tileSize;
103
104 // Max str length
105 static constexpr int maxStrLenTiles = drawWidthTiles * maxLines;
106
107 void setup(QStringList idParts);
108
109 void getInputStr();
110 void getTiles();
111 void getResultingText();
112 void getImageWidth();
113 void getImageHeight();
114 void getBaseImg();
115 void drawBox();
116 void drawFg();
117 void finishImg();
118 void postProcess();
119
120 void getPlayersName();
121 void getRivalsName();
122
123 // From ID String
124 QString tileset;
125 QString type;
126 int frame = 0;
127 int toWidth = 0;
128 int toHeight = 0;
129 bool box = false;
130 bool lines2 = false;
131 int chopLen = 0;
132 QColor bgColor;
133 QColor fgColor;
134 QString placeholder;
135 QString inputStr;
136
137 // Other vars
138 QStringList idParts;
139 QVector<QVector<var8>> resultingText;
140 QImage bgImg;
141 QImage baseImg;
142 QImage boxImg;
143 QImage fgImg;
144 QPixmap resultingImg;
145 QVector<QPixmap> tiles;
146 QString playersName;
147 QString rivalsName;
148 int imgWidth = 0;
150 int imgHeight = 0;
152
153 bool useFg = false;
154
155 // Provider Vars
156 QSize* size = nullptr;
157 const QSize& requestedSize;
158
159 // Reference vars
161};
162
165{
166 QString playersName;
167 QString rivalsName;
168 QPixmap resultingImg;
169};
170
180class FontPreviewProvider : public QQuickImageProvider
181{
182public:
184
185 // <tileset>/<type>/<frame>/<width>/<height>/<box>/<2-lines>/<max>/<bgColor>/<fgColor>/<placeholder>/<str>
186 // * <tileset> is the tileset, case-insensitive and spaces converted to
187 // underscores
188 // * <type> is the type, specifically "outdoor" or not is used here
189 // * <frame> can be any positive number, a full frame cycle completes in 8
190 // frames though so it's suggested to use multiple of 8 for smooth
191 // animation
192 // * <box> tells whether to render a textbox or not as a bg
193 // specifically "box" or not
194 // * <2-lines> tells whether to render on 2 lines or not
195 // specifically "2-lines" or not
196 // * <max> is maximum line length (Excluding placeholder), if not between
197 // 1 and 21 then it will be set to 21
198 // * <bgColor> tells what background color it should be in any QColor format
199 // * <fgColor> tells what text color it should be in any QColor format
200 // "none" however is special and means don't paint
201 // * <str> is the font str to render
202 // * <placeholder> is text to put str into, use %% in placeholder to insert
203 // the str
205 virtual QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override;
206 QPixmap getErrorImg(QSize* size, const QSize& requestedSize);
207
209};
virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override
Render the preview for id (format documented above).
FontPreviewProvider(SaveFileExpanded *expanded)
SaveFileExpanded * expanded
Live save (for name substitution).
QPixmap getErrorImg(QSize *size, const QSize &requestedSize)
Fallback image when an id is invalid.
Root of the editable object tree – the friendly mirror of a raw save.
Project-wide fixed-width integer aliases (var8, var16, ...).
A cached font-preview result, keyed alongside the names it depended on.
QString playersName
Player name at render time (cache validity).
QPixmap resultingImg
The cached pixmap.
QString rivalsName
Rival name at render time (cache validity).
int toWidth
Target width (id part).
QPixmap resultingImg
The finished pixmap.
QVector< QVector< var8 > > resultingText
Text as tile-code rows.
void getBaseImg()
Allocate the base image.
void getImageWidth()
Compute output width.
static constexpr int drawHeightLines2Tiles
QString tileset
Tileset name (id part).
SaveFileExpanded * expanded
Live save (for name substitution).
int imgWidthTiles
Output width (tiles).
FontPreviewInstance(QStringList idParts, SaveFileExpanded *expanded, QSize *size, const QSize &requestedSize)
static constexpr int drawWidth
void setup(QStringList idParts)
Parse the id parts into the fields below.
void getRivalsName()
Read the rival's name from the save (for substitution).
static constexpr int drawWidthTiles
const QSize & requestedSize
Size QML requested.
static constexpr int drawHeightLines1Tiles
QString type
Type, e.g. "outdoor" (id part).
static constexpr int drawHeightLines2
bool box
Render a dialogue box (id part).
QImage fgImg
Foreground layer.
void drawFg()
Draw the foreground text.
int toHeight
Target height (id part).
static constexpr int boxTiles[]
Tilemap for a 20x6 dialogue box with arrow (see note above).
QImage bgImg
Background layer.
QColor bgColor
Background colour (id part).
void postProcess()
Apply any post-processing.
QImage baseImg
Base canvas.
void getPlayersName()
Read the player's name from the save (for substitution).
static constexpr int drawHeightBox
QString placeholder
Template the str is inserted into (id part).
static constexpr int drawHeightBoxTiles
int imgWidth
Output width (px).
static constexpr int maxLines
Max text lines (must match FontsDB).
void getInputStr()
Resolve the final input string (placeholder + str + names).
int imgHeightTiles
Output height (tiles).
QSize * size
Out-param size handed back to QML.
void drawBox()
Draw the dialogue box background, if requested.
static constexpr int lineHeight
static constexpr int maxStrLenTiles
QString rivalsName
Resolved rival name.
int imgHeight
Output height (px).
QStringList idParts
The raw split id parts.
void getTiles()
Render the per-tile pixmaps for the text.
void getResultingText()
Convert the string to tile-code rows via FontsDB.
bool useFg
Whether the foreground is painted.
static constexpr int tileSize
Pixels per tile edge.
bool lines2
Render on two lines (id part).
int frame
Animation frame (id part).
void finishImg()
Composite into the final pixmap.
static constexpr int drawHeightLines1
int chopLen
Max line length before chopping (id part).
QColor fgColor
Foreground/text colour (id part; "none" = don't paint).
QString inputStr
The raw font string to render (id part).
QString playersName
Resolved player name.
static constexpr int lineHeightTiles
void getImageHeight()
Compute output height.
QVector< QPixmap > tiles
Per-character tile pixmaps.