From 6c2ad3efaf9078933cb0a2f47e0baec7fc425ddc Mon Sep 17 00:00:00 2001
From: biwa <6475593+biwa@users.noreply.github.com>
Date: Tue, 8 Feb 2022 21:30:54 +0100
Subject: [PATCH] Linedefs Mode: further speed improvements related to sector
labels
---
Source/Core/Rendering/TextLabel.cs | 15 ++++++++++++
.../BuilderModes/ClassicModes/LinedefsMode.cs | 24 +++++++++++++------
2 files changed, 32 insertions(+), 7 deletions(-)
diff --git a/Source/Core/Rendering/TextLabel.cs b/Source/Core/Rendering/TextLabel.cs
index 125ae7f0..0fd59691 100755
--- a/Source/Core/Rendering/TextLabel.cs
+++ b/Source/Core/Rendering/TextLabel.cs
@@ -467,6 +467,21 @@ namespace CodeImp.DoomBuilder.Rendering
// This (re)loads the resources
public void ReloadResource() { }
+
+ ///
+ /// Checks if the whole label is in the viewport.
+ ///
+ /// true if the label in in the viewport, false if it isn't
+ public bool IsInViewport()
+ {
+ (double width, double height) = texturesize.IsEmpty ? ( 0, 0 ) : (texturesize.Width, texturesize.Height);
+
+ return
+ location.x >= (General.Map.CRenderer2D.Viewport.X - width) &&
+ location.x < (General.Map.CRenderer2D.Viewport.X + General.Map.CRenderer2D.Viewport.Width + width) &&
+ location.y <= (General.Map.CRenderer2D.Viewport.Y - height) &&
+ location.y > (General.Map.CRenderer2D.Viewport.Y + General.Map.CRenderer2D.Viewport.Height + height);
+ }
#endregion
}
diff --git a/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs b/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs
index 84bac16e..86b338e4 100755
--- a/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs
+++ b/Source/Plugins/BuilderModes/ClassicModes/LinedefsMode.cs
@@ -366,8 +366,14 @@ namespace CodeImp.DoomBuilder.BuilderModes
//mxd. This sets up new labels
private void SetupSectorLabels()
{
+ // Dummy label we need for the font
+ TextLabel dummylabel = new TextLabel();
+
+ // The "+" is always shown if the space for the label isn't big enough to show the full text
+ textlabelsizecache["+"] = General.Interface.MeasureString("+", dummylabel.Font).Width;
+
// Dispose old labels
- if(sectorlabels != null)
+ if (sectorlabels != null)
{
foreach(TextLabel[] larr in sectorlabels.Values)
foreach(TextLabel l in larr) l.Dispose();
@@ -396,6 +402,12 @@ namespace CodeImp.DoomBuilder.BuilderModes
tagdescarr[1] = "T" + s.Tag;
}
+ // Add string lengths to the label size cache
+ if (!textlabelsizecache.ContainsKey(tagdescarr[0]))
+ textlabelsizecache[tagdescarr[0]] = General.Interface.MeasureString(tagdescarr[0], dummylabel.Font).Width;
+ if (!textlabelsizecache.ContainsKey(tagdescarr[1]))
+ textlabelsizecache[tagdescarr[1]] = General.Interface.MeasureString(tagdescarr[1], dummylabel.Font).Width;
+
// Add to collection
sectortexts.Add(s, tagdescarr);
@@ -501,19 +513,17 @@ namespace CodeImp.DoomBuilder.BuilderModes
TextLabel[] labelarray = sectorlabels[group.Key];
for (int i = 0; i < group.Key.Labels.Count; i++)
{
+ // Only process this label if it's actually in view
+ if (!labelarray[i].IsInViewport())
+ continue;
+
TextLabel l = labelarray[i];
// Render only when enough space for the label to see
- if (!textlabelsizecache.ContainsKey(group.Value[0]))
- textlabelsizecache[group.Value[0]] = General.Interface.MeasureString(group.Value[0], l.Font).Width;
-
float requiredsize = textlabelsizecache[group.Value[0]] / 2 / renderer.Scale;
if (requiredsize > group.Key.Labels[i].radius)
{
- if (!textlabelsizecache.ContainsKey(group.Value[1]))
- textlabelsizecache[group.Value[1]] = General.Interface.MeasureString(group.Value[1], l.Font).Width;
-
requiredsize = textlabelsizecache[group.Value[1]] / 2 / renderer.Scale;
string newtext;