From 48af52afcf194b853da640ad121fa2bc085c938e Mon Sep 17 00:00:00 2001
From: biwa <foracc@d00m.info>
Date: Fri, 31 May 2019 00:20:12 +0200
Subject: [PATCH] - Improved performance of the texture browser. This should
 significantly reduce the times it takes to open the texture browser when
 using large texture sets

---
 Source/Core/Controls/ImageBrowserItem.cs |  8 +-------
 Source/Core/Data/CameraTextureImage.cs   |  2 ++
 Source/Core/Data/FileImage.cs            |  2 ++
 Source/Core/Data/HiResImage.cs           |  2 ++
 Source/Core/Data/ImageData.cs            | 15 +++++++++++++++
 Source/Core/Data/PK3FileImage.cs         |  2 ++
 Source/Core/Data/TEXTURESImage.cs        |  2 ++
 7 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/Source/Core/Controls/ImageBrowserItem.cs b/Source/Core/Controls/ImageBrowserItem.cs
index 2bed6729f..aa7108cbe 100755
--- a/Source/Core/Controls/ImageBrowserItem.cs
+++ b/Source/Core/Controls/ImageBrowserItem.cs
@@ -30,8 +30,6 @@ namespace CodeImp.DoomBuilder.Controls
 		private bool showfullname;
 		protected ImageBrowserItemType itemtype;
 		private string tooltip;
-		private int namewidth;
-		private int shortnamewidth;
 
         private static Brush bgbrush, fgbrush_used, fgbrush_unused, selectedbgbrush, selectionbrush, selectiontextbrush, bgbrush_alpha;
         private static Color bgcolor;
@@ -48,7 +46,7 @@ namespace CodeImp.DoomBuilder.Controls
 		public virtual bool IsPreviewLoaded { get { return icon.IsPreviewLoaded; } }
 		public bool ShowFullName { set { showfullname = value; } get { return showfullname && (!(icon is PK3FileImage) || !((PK3FileImage)icon).IsBadForLongTextureNames); } }
 		public virtual string TextureName { get { return (ShowFullName ? icon.Name : icon.ShortName); } }
-		public virtual int TextureNameWidth { get { return (ShowFullName ? namewidth : shortnamewidth); } }
+		public virtual int TextureNameWidth { get { return ShowFullName ? icon.NameWidth : icon.ShortNameWidth; } } // biwa
 		public string ToolTip { get { return tooltip; } }
 
 		#endregion
@@ -65,10 +63,6 @@ namespace CodeImp.DoomBuilder.Controls
 			this.showfullname = showfullname; //mxd
 			this.imageloaded = icon.IsPreviewLoaded; //mxd
 			this.tooltip = tooltip; //mxd
-            
-			//mxd. Calculate names width
-			this.namewidth = (int)Math.Ceiling(General.Interface.MeasureString(icon.Name, SystemFonts.MessageBoxFont, 10000, StringFormat.GenericTypographic).Width)+6;
-			this.shortnamewidth = (int)Math.Ceiling(General.Interface.MeasureString(icon.ShortName, SystemFonts.MessageBoxFont, 10000, StringFormat.GenericTypographic).Width)+6;
 		}
 
 		#endregion
diff --git a/Source/Core/Data/CameraTextureImage.cs b/Source/Core/Data/CameraTextureImage.cs
index 2008276e3..5704fac44 100755
--- a/Source/Core/Data/CameraTextureImage.cs
+++ b/Source/Core/Data/CameraTextureImage.cs
@@ -113,6 +113,8 @@ namespace CodeImp.DoomBuilder.Data
 
 			base.SetName(name);
 			this.virtualname = "[CAMERA TEXTURES]" + Path.AltDirectorySeparatorChar + name;
+
+			ComputeNamesWidth(); // biwa
 		}
 
 		#endregion
diff --git a/Source/Core/Data/FileImage.cs b/Source/Core/Data/FileImage.cs
index d6dde33b6..b02f3f305 100755
--- a/Source/Core/Data/FileImage.cs
+++ b/Source/Core/Data/FileImage.cs
@@ -145,6 +145,8 @@ namespace CodeImp.DoomBuilder.Data
 
             this.longname = Lump.MakeLongName(this.name, uselongtexturenames);
             this.filepathname = filepathname;
+
+			ComputeNamesWidth(); // biwa
         }
 
         // This loads the image
diff --git a/Source/Core/Data/HiResImage.cs b/Source/Core/Data/HiResImage.cs
index 84a037afc..8e885b663 100755
--- a/Source/Core/Data/HiResImage.cs
+++ b/Source/Core/Data/HiResImage.cs
@@ -83,6 +83,8 @@ namespace CodeImp.DoomBuilder.Data
 			this.displayname = this.name;
 			this.shortname = this.name;
 			this.longname = Lump.MakeLongName(this.name);
+
+			ComputeNamesWidth(); // biwa
 		}
 
 		internal void ApplySettings(ImageData overridden)
diff --git a/Source/Core/Data/ImageData.cs b/Source/Core/Data/ImageData.cs
index 34b472341..4dea7b137 100755
--- a/Source/Core/Data/ImageData.cs
+++ b/Source/Core/Data/ImageData.cs
@@ -58,6 +58,8 @@ namespace CodeImp.DoomBuilder.Data
 		protected bool ismasked; //mxd. If true, has pixels with zero alpha
 		protected bool hasLongName; //mxd. Texture name is longer than DataManager.CLASIC_IMAGE_NAME_LENGTH
 		protected bool hasPatchWithSameName; //mxd
+		protected int namewidth; // biwa
+		protected int shortnamewidth; // biwa
 
 		//mxd. Hashing
 		private static int hashcounter;
@@ -123,6 +125,8 @@ namespace CodeImp.DoomBuilder.Data
 		public virtual float ScaledHeight { get { return (float)Math.Round(height * scale.y); } }
 		public virtual Vector2D Scale { get { return scale; } }
 		public bool WorldPanning { get { return worldpanning; } }
+		public int NameWidth {  get { return namewidth; } } // biwa
+		public int ShortNameWidth { get { return shortnamewidth; } } // biwa
 
 		#endregion
 
@@ -206,6 +210,8 @@ namespace CodeImp.DoomBuilder.Data
 			this.virtualname = name; //mxd
 			this.displayname = name; //mxd
 			this.longname = Lump.MakeLongName(name); //mxd
+
+			ComputeNamesWidth(); // biwa
 		}
 		
 		// This unloads the image
@@ -219,6 +225,15 @@ namespace CodeImp.DoomBuilder.Data
 			}
 		}
 
+		// biwa. Computing the widths in the constructor of ImageBrowserItem accumulates to taking forever when loading many images,
+		// like when showing the texture browser of huge texture sets like OTEX
+		internal void ComputeNamesWidth()
+		{
+			//mxd. Calculate names width
+			namewidth = (int)Math.Ceiling(General.Interface.MeasureString(name, SystemFonts.MessageBoxFont, 10000, StringFormat.GenericTypographic).Width) + 6;
+			shortnamewidth = (int)Math.Ceiling(General.Interface.MeasureString(shortname, SystemFonts.MessageBoxFont, 10000, StringFormat.GenericTypographic).Width) + 6;
+		}
+
 		// This returns the bitmap image
 		public Bitmap GetBitmap()
 		{
diff --git a/Source/Core/Data/PK3FileImage.cs b/Source/Core/Data/PK3FileImage.cs
index 313962cbc..79906eb35 100755
--- a/Source/Core/Data/PK3FileImage.cs
+++ b/Source/Core/Data/PK3FileImage.cs
@@ -104,6 +104,8 @@ namespace CodeImp.DoomBuilder.Data
 			this.longname = Lump.MakeLongName(this.name);
 			this.virtualname = filepathname.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
 			this.filepathname = filepathname;
+
+			ComputeNamesWidth(); // biwa
 		}
 
 		// This loads the image
diff --git a/Source/Core/Data/TEXTURESImage.cs b/Source/Core/Data/TEXTURESImage.cs
index 218779140..65b243674 100755
--- a/Source/Core/Data/TEXTURESImage.cs
+++ b/Source/Core/Data/TEXTURESImage.cs
@@ -85,6 +85,8 @@ namespace CodeImp.DoomBuilder.Data
 			{
 				this.shortname = this.shortname.Substring(0, DataManager.CLASIC_IMAGE_NAME_LENGTH);
 			}
+
+			ComputeNamesWidth(); // biwa
 		}
 
 		// This adds a patch to the texture
-- 
GitLab