- Timestamp:
- Dec 12, 2015, 4:08:53 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/Handlers/ItemIconHandler.cs
r251 r253 8 8 namespace AllocsFixes.NetConnections.Servers.Web.Handlers 9 9 { 10 public class ItemIconHandler : PathHandler 11 { 10 public class ItemIconHandler : PathHandler { 12 11 private string staticPart; 13 12 private bool logMissingFiles; 14 15 13 private Dictionary<string, byte[]> icons = new Dictionary<string, byte[]> (); 14 private bool loaded = false; 16 15 17 16 public ItemIconHandler (string staticPart, bool logMissingFiles, string moduleName = null) : base(moduleName) { … … 29 28 } 30 29 31 // BEGIN CHANGED BY PSOUZA432 30 string requestFileName = req.Url.AbsolutePath.Remove (0, staticPart.Length); 33 31 requestFileName = requestFileName.Remove (requestFileName.LastIndexOf ('.')); 34 32 35 string requestColorTintHex = string.Empty; 36 37 // Chose a split instead of using a querystring variable in the URI, but that may arguably be cleaner 38 if (requestFileName.Contains("@@")) { 39 try { 40 string[] tempTintingOptions = requestFileName.Split (new string[] { "@@" }, StringSplitOptions.RemoveEmptyEntries); 41 requestFileName = tempTintingOptions [0]; 42 requestColorTintHex = tempTintingOptions [1].Trim ().ToUpper (); 43 } 44 catch { } 45 } 46 47 if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.ToLower ().EndsWith(".png")) { 33 if (icons.ContainsKey (requestFileName) && req.Url.AbsolutePath.ToLower ().EndsWith (".png")) { 48 34 resp.ContentType = MimeType.GetMimeType (".png"); 49 35 50 byte[] itemIconData = icons [requestFileName]; 51 52 // Note: optionally split this code into a ImageMultiplyBlend method 53 if (!string.IsNullOrEmpty (requestColorTintHex) && (requestColorTintHex != "FFFFFF")) { 54 try { 55 System.Drawing.Color colorTint = System.Drawing.ColorTranslator.FromHtml ("#" + requestColorTintHex); 56 System.Drawing.Bitmap image = (System.Drawing.Bitmap)PetesUtils.GetImageFromBytes (itemIconData); 57 58 for (var x = 0; x < image.Width; x++) { 59 for (int y = 0; y < image.Height; y++) { 60 System.Drawing.Color originalColor = image.GetPixel (x, y); 61 System.Drawing.Color changedColor = originalColor; 62 63 // Only change the icon tinting if the alpha channel is fully opaque 64 if (originalColor.A == 255) { 65 // based on http://stackoverflow.com/questions/3837757/multiply-two-images-in-c-sharp-as-multiply-two-layers-in-photoshop 66 67 double component_R = (((double)originalColor.R) * ((double)colorTint.R)) / 255.0; 68 double component_G = (((double)originalColor.G) * ((double)colorTint.G)) / 255.0; 69 double component_B = (((double)originalColor.B) * ((double)colorTint.B)) / 255.0; 70 71 if (component_R > 255.0) component_R = 255.0; 72 if (component_G > 255.0) component_G = 255.0; 73 if (component_B > 255.0) component_B = 255.0; 74 75 // multiply blend shouldn't ever calculate below 0, but for completeness let's leave in this logic 76 if (component_R < 0.0) component_R = 0.0; 77 if (component_G < 0.0) component_G = 0.0; 78 if (component_B < 0.0) component_B = 0.0; 79 80 changedColor = System.Drawing.Color.FromArgb (originalColor.A, (int)component_R, (int)component_G, (int)component_B); 81 } 82 83 image.SetPixel (x, y, changedColor); 84 } 85 } 86 87 itemIconData = PetesUtils.SaveImage_ToBytes (image, true); 88 } 89 catch { } 90 } 36 byte[] itemIconData = icons [requestFileName]; 91 37 92 38 resp.ContentLength64 = itemIconData.Length; 93 39 resp.OutputStream.Write (itemIconData, 0, itemIconData.Length); 94 // END CHANGED BY PSOUZA495 40 } else { 96 41 resp.StatusCode = (int)HttpStatusCode.NotFound; 97 if (logMissingFiles) 42 if (logMissingFiles) { 98 43 Log.Out ("Web:IconHandler:FileNotFound: \"" + req.Url.AbsolutePath + "\" "); 44 } 99 45 return; 100 46 } … … 106 52 return true; 107 53 } 54 55 MicroStopwatch microStopwatch = new MicroStopwatch (); 108 56 109 57 GameObject atlasObj = GameObject.Find ("/NGUI Root (2D)/ItemIconAtlas"); … … 120 68 } 121 69 70 Dictionary<string, List<Color>> tintedIcons = new Dictionary<string, List<Color>> (); 71 foreach (ItemClass ic in ItemClass.list) { 72 if (ic != null) { 73 Color tintColor = ic.GetIconTint (); 74 if (tintColor != Color.white) { 75 string name = ic.GetIconName (); 76 if (!tintedIcons.ContainsKey (name)) { 77 tintedIcons.Add (name, new List<Color> ()); 78 } 79 List<Color> list = tintedIcons [name]; 80 list.Add (tintColor); 81 } 82 } 83 } 84 122 85 Texture2D atlasTex = atlas.texture as Texture2D; 123 86 … … 126 89 Texture2D tex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false); 127 90 tex.SetPixels (atlasTex.GetPixels (data.x, atlasTex.height - data.height - data.y, data.width, data.height)); 128 byte[] pixData = tex.EncodeToPNG ();129 91 130 icons.Add (name, pixData); 92 icons.Add (name + "__FFFFFF", tex.EncodeToPNG ()); 93 94 if (tintedIcons.ContainsKey (name)) { 95 foreach (Color c in tintedIcons [name]) { 96 Texture2D tintedTex = new Texture2D (data.width, data.height, TextureFormat.ARGB32, false); 97 98 for (int x = 0; x < data.width; x++) { 99 for (int y = 0; y < data.height; y++) { 100 tintedTex.SetPixel (x, y, tex.GetPixel (x, y) * c); 101 } 102 } 103 104 icons.Add (name + "__" + AllocsUtils.ColorToHex (c), tintedTex.EncodeToPNG ()); 105 106 UnityEngine.Object.Destroy (tintedTex); 107 } 108 } 109 131 110 UnityEngine.Object.Destroy (tex); 132 111 } 133 112 134 113 loaded = true; 135 Log.Out ("Web:IconHandler: Icons loaded ");114 Log.Out ("Web:IconHandler: Icons loaded - {0} ms", microStopwatch.ElapsedMilliseconds); 136 115 137 116 return true;
Note:
See TracChangeset
for help on using the changeset viewer.