Skip to content

Commit

Permalink
Tint icon textures upon loading
Browse files Browse the repository at this point in the history
All downstream mods will now receive tinted icons, instead of only those
that permit KSP's DialogGUIBase.tint or Unity's Image.color
  • Loading branch information
cake-pie committed Feb 14, 2018
1 parent ceedd62 commit f46b643
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions Source/KerbalTraitSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,38 @@ public class KerbalTraitSetting
public KerbalTraitSetting(string name, Texture2D icon, Color color)
{
Name = name;
Icon = icon;
Color = color;

if (icon == null) return;

CTIAddon.log("Tinting icon for {0}.", name);
Icon = new Texture2D(icon.width, icon.height, TextureFormat.ARGB32, false); // disable mipmap!
Color32[] tex;
try
{
// readable textures
tex = icon.GetPixels32();
}
catch (UnityException e)
{
// workaround for unreadable textures
RenderTexture bak = RenderTexture.active;
RenderTexture tmp = RenderTexture.GetTemporary(icon.width, icon.height, 0);
Graphics.Blit(icon, tmp);
RenderTexture.active = tmp;
Icon.ReadPixels(new Rect(0, 0, icon.width, icon.height), 0, 0);
RenderTexture.active = bak;
RenderTexture.ReleaseTemporary(tmp);
tex = Icon.GetPixels32();
}
for (int i = 0; i < tex.Length; i++)
tex[i] = tex[i] * Color;
Icon.SetPixels32(tex);
Icon.Apply(false,true);
}

#region Icon Generation
// Generate a Sprite of the icon for this trait. Note: not tinted with color.
// Generate a Sprite of the icon for this trait.
public Sprite makeSprite()
{
if (Icon == null) return null;
Expand All @@ -31,14 +57,14 @@ public Sprite makeSprite()
public DialogGUIImage makeDialogGUIImage(Vector2 s, Vector2 p)
{
if (Icon == null) return null;
return new DialogGUIImage(s,p,Color,Icon);
return new DialogGUIImage(s,p,Color.white,Icon);
}

// Generate a KSP DialogGUISprite of the icon for this trait.
public DialogGUISprite makeDialogGUISprite(Vector2 s, Vector2 p)
{
if (Icon == null) return null;
return new DialogGUISprite(s,p,Color,makeSprite());
return new DialogGUISprite(s,p,Color.white,makeSprite());
}

// Generate a GameObject with an Image component of the icon for this trait.
Expand All @@ -62,7 +88,6 @@ public bool attachImage(GameObject go)
if (Icon == null) return false;
Image i = go.AddComponent<Image>();
i.sprite = makeSprite();
i.color = Color;
return true;
}
#endregion Icon Generation
Expand Down

0 comments on commit f46b643

Please sign in to comment.