summaryrefslogtreecommitdiff
path: root/src/textfile_gui.h
diff options
context:
space:
mode:
authorPeterN <peter1138@openttd.org>2021-04-29 18:58:26 +0100
committerGitHub <noreply@github.com>2021-04-29 18:58:26 +0100
commit0b460bf4a17e12ba479e0822b8f9b7de29ac5816 (patch)
treec331e1f4cb2aebb83c7c44a96f7de509d0c3e22f /src/textfile_gui.h
parent72a05921b06b02b04af6e86d981f55de4960ca61 (diff)
downloadopenttd-0b460bf4a17e12ba479e0822b8f9b7de29ac5816.tar.xz
Fix: 'Cache' top and bottom lines of textfile viewer to avoid overdraw. (#9131)
* Fix: 'Cache' top and bottom lines of textfile viewer to avoid overdraw. The text file viewer calculated the number of lines required to set the scrollbar, but did not retain this information, so this was recalculated on every draw operation. This includes overdrawing text outside the bounds of the current scroll position. With this change the top and bottom lines for each line of text are remembered, and reflowing is avoided where possible. Text outside the current scroll bounds is not drawn. Additionally the scroll interval is now based on text lines instead of pixel lines, which increases the text capacity depending on the font size. * Fix: Limit text viewer to showing 64k lines. Text files with more than 64k wrapped lines would exceed the scrollbar capacity and cause an assert. This is harder to reach now that the scrollbar counts lines instead of pixels.
Diffstat (limited to 'src/textfile_gui.h')
-rw-r--r--src/textfile_gui.h16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/textfile_gui.h b/src/textfile_gui.h
index d67435c01..5f1db14ca 100644
--- a/src/textfile_gui.h
+++ b/src/textfile_gui.h
@@ -19,13 +19,23 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
/** Window for displaying a textfile */
struct TextfileWindow : public Window, MissingGlyphSearcher {
+ struct Line {
+ int top; ///< Top scroll position.
+ int bottom; ///< Bottom scroll position.
+ const char *text; ///< Pointer to text buffer.
+
+ Line(int top, const char *text) : top(top), bottom(top + 1), text(text) {}
+ };
+
TextfileType file_type; ///< Type of textfile to view.
Scrollbar *vscroll; ///< Vertical scrollbar.
Scrollbar *hscroll; ///< Horizontal scrollbar.
char *text; ///< Lines of text from the NewGRF's textfile.
- std::vector<const char *> lines; ///< #text, split into lines in a table with lines.
+ std::vector<Line> lines; ///< #text, split into lines in a table with lines.
uint search_iterator; ///< Iterator for the font check search.
+ uint max_length; ///< Maximum length of unwrapped text line.
+
static const int TOP_SPACING = WD_FRAMETEXT_TOP; ///< Additional spacing at the top of the #WID_TF_BACKGROUND widget.
static const int BOTTOM_SPACING = WD_FRAMETEXT_BOTTOM; ///< Additional spacing at the bottom of the #WID_TF_BACKGROUND widget.
@@ -36,6 +46,7 @@ struct TextfileWindow : public Window, MissingGlyphSearcher {
void OnClick(Point pt, int widget, int click_count) override;
void DrawWidget(const Rect &r, int widget) const override;
void OnResize() override;
+ void OnInvalidateData(int data = 0, bool gui_scope = true) override;
void Reset() override;
FontSize DefaultSize() override;
@@ -46,8 +57,9 @@ struct TextfileWindow : public Window, MissingGlyphSearcher {
virtual void LoadTextfile(const char *textfile, Subdirectory dir);
private:
+ uint ReflowContent();
uint GetContentHeight();
- void SetupScrollbars();
+ void SetupScrollbars(bool force_reflow);
};
#endif /* TEXTFILE_GUI_H */