public PositioningProcessor(AssetIconsCompiledStyle compiledStyleDefinition, AssetIconsStyle style) { widthProvider = new StaticValueProvider(new MathValue(1.0f, true)); heightProvider = new StaticValueProvider(new MathValue(1.0f, true)); var horizontalContext = new MathContextBuilder() .WithTerm("width", widthProvider) .WithTerm("height", heightProvider) .ImplicitlyReferences(widthProvider) .Build(); var verticalContext = new MathContextBuilder() .WithTerm("width", widthProvider) .WithTerm("height", heightProvider) .ImplicitlyReferences(heightProvider) .Build(); compiledWidth = CompiledExpression.Compile(style.Width, horizontalContext); compiledHeight = CompiledExpression.Compile(style.Height, verticalContext); compiledX = CompiledExpression.Compile(style.X, horizontalContext); compiledY = CompiledExpression.Compile(style.Y, verticalContext); compiledDisplay = CompiledExpression.Compile(style.Display, horizontalContext); styleAnchoring = compiledStyleDefinition.Anchor; }
/// <summary> /// <para>Draws a sprite graphic at a specified <see cref="Rect"/>.</para> /// </summary> /// <example> /// <para>Below is an example of drawing a sprite in an editor window.</para> /// <code> /// using AssetIcons.Editors; /// using UnityEditor; /// using UnityEngine; /// /// public class DemoWindow : EditorWindow /// { /// private Sprite spriteToDraw; /// /// [MenuItem("AssetIcons/Demo Window")] /// private static void Init() /// { /// var window = GetWindow(typeof(DemoWindow), false, "Demo Window"); /// window.Show(); /// } /// /// private void OnGUI() /// { /// spriteToDraw = EditorGUILayout.ObjectField(spriteToDraw, typeof(Sprite), false); /// /// var rect = GUILayoutUtility.GetRect(20.0f, 20.0f); /// /// AssetIconsGUI.DrawSprite(rect, spriteToDraw); /// } /// } /// </code> /// </example> /// <remarks> /// <para>This implementation draws a <see cref="Texture2D"/> and crops the boundaries. This may result in /// sprites that are tightly packed to draw artifacts.</para> /// </remarks> /// <param name="rect">The <see cref="Rect"/> in which the item is drawn.</param> /// <param name="sprite">The graphic to draw.</param> /// <param name="style">A style used to modify the appearance of a graphic.</param> /// <param name="selected">Whether the graphic should be rendered with a selection tint.</param> public static void DrawSprite(Rect rect, Sprite sprite, AssetIconsCompiledStyle style = null, bool selected = false) { if (sprite == null || sprite.Equals(null) || Event.current.type != EventType.Repaint) { return; } Texture2D textureIcon = null; Rect textureRect; if (sprite.packed) { textureIcon = SpriteUtility.GetSpriteTexture(sprite, false); } if (textureIcon == null) { textureIcon = sprite.texture; } textureRect = sprite.rect; DrawTexWithCoords(rect, textureIcon, textureRect, style, selected); }
/// <summary> /// <para>Draw a flat <see cref="Color"/>.</para> /// </summary> /// <example> /// <para>Below is an example of drawing a flat colour in an editor window.</para> /// <code> /// using AssetIcons.Editors; /// using UnityEditor; /// using UnityEngine; /// /// public class DemoWindow : EditorWindow /// { /// [MenuItem("AssetIcons/Demo Window")] /// private static void Init() /// { /// var window = GetWindow(typeof(DemoWindow), false, "Demo Window"); /// window.Show(); /// } /// /// private void OnGUI() /// { /// var rect = GUILayoutUtility.GetRect(20.0f, 20.0f); /// /// AssetIconsGUI.DrawColor(rect, Color.red); /// } /// } /// </code> /// </example> /// <param name="rect">The <see cref="Rect"/> in which the item is drawn.</param> /// <param name="color">A <see cref="Color"/> to draw in the provided <see cref="Rect"/>.</param> /// <param name="style">A style used to modify the appearance of a graphic.</param> /// <param name="selected">Whether the graphic should be rendered with a selection tint.</param> public static void DrawColor(Rect rect, Color color, AssetIconsCompiledStyle style = null, bool selected = false) { if (Event.current.type != EventType.Repaint) { return; } if (Mathf.Abs(color.a) < 0.005f) { return; } var info = ApplyGeneralModifiers(rect, style); if (!info.Display) { return; } // Take the edge off that color, we still want to resemble our original color. var multiplyColor = Color.Lerp(info.Tint, Color.white, 0.5f); multiplyColor.a = info.Tint.a; // Blend the tint color into our color color *= multiplyColor; if (selected) { color = AssetIconsPreferences.SelectionTint.Value.Apply(color, 0.5f); } EditorGUI.DrawRect(info.Position, color); }
/// <summary> /// <para>Draws a portion of a texture at a specified <see cref="Rect"/>.</para> /// </summary> /// <example> /// <para>Below is an example of drawing a sprite in an editor window.</para> /// <code> /// using AssetIcons.Editors; /// using UnityEditor; /// using UnityEngine; /// /// public class DemoWindow : EditorWindow /// { /// private Texture textureToDraw; /// /// [MenuItem("AssetIcons/Demo Window")] /// private static void Init() /// { /// var window = GetWindow(typeof(DemoWindow), false, "Demo Window"); /// window.Show(); /// } /// /// private void OnGUI() /// { /// textureToDraw = EditorGUILayout.ObjectField(spriteToDraw, typeof(Texture), false); /// /// var rect = GUILayoutUtility.GetRect(20.0f, 20.0f); /// /// AssetIconsGUI.DrawTexWithCoords(rect, textureToDraw, new Rect(8.0f, 8.0f, 32.0f, 32.0f)); /// } /// } /// </code> /// </example> /// <param name="rect">The <see cref="Rect"/> in which the item is drawn.</param> /// <param name="texture">The texture to draw.</param> /// <param name="textureRect">The rect (in pixels) of the texture to draw.</param> /// <param name="style">A style used to modify the appearance of a graphic.</param> /// <param name="selected">Whether the graphic should be rendered with a selection tint.</param> public static void DrawTexWithCoords(Rect rect, Texture texture, Rect textureRect, AssetIconsCompiledStyle style = null, bool selected = false) { if (texture == null || texture.Equals(null) || Event.current.type != EventType.Repaint) { return; } var normalisedTextureRect = RemapRect(textureRect, texture.width, texture.height); DrawTexWithUVCoords(rect, texture, normalisedTextureRect, style, selected); }
/// <summary> /// <para>Draws a texture graphic at a specified <see cref="Rect"/>.</para> /// </summary> /// <example> /// <para>Below is an example of drawing a sprite in an editor window.</para> /// <code> /// using AssetIcons.Editors; /// using UnityEditor; /// using UnityEngine; /// /// public class DemoWindow : EditorWindow /// { /// private Texture textureToDraw; /// /// [MenuItem("AssetIcons/Demo Window")] /// private static void Init() /// { /// var window = GetWindow(typeof(DemoWindow), false, "Demo Window"); /// window.Show(); /// } /// /// private void OnGUI() /// { /// textureToDraw = EditorGUILayout.ObjectField(spriteToDraw, typeof(Texture), false); /// /// var rect = GUILayoutUtility.GetRect(20.0f, 20.0f); /// /// AssetIconsGUI.DrawSprite(rect, textureToDraw); /// } /// } /// </code> /// </example> /// <param name="rect">The <see cref="Rect"/> in which the item is drawn.</param> /// <param name="texture">The texture to draw.</param> /// <param name="style">A style used to modify the appearance of a graphic.</param> /// <param name="selected">Whether the graphic should be rendered with a selection tint.</param> public static void DrawTexture(Rect rect, Texture texture, AssetIconsCompiledStyle style = null, bool selected = false) { if (texture == null || texture.Equals(null) || Event.current.type != EventType.Repaint) { return; } var textureRect = new Rect(0.0f, 0.0f, texture.width, texture.height); DrawTexWithCoords(rect, texture, textureRect, style, selected); }
/// <summary> /// <para>Draws text at a specified <see cref="Rect"/>.</para> /// </summary> /// <example> /// <para>Below is an example of drawing a string in an editor window.</para> /// <code> /// using AssetIcons.Editors; /// using UnityEditor; /// using UnityEngine; /// /// public class DemoWindow : EditorWindow /// { /// [MenuItem("AssetIcons/Demo Window")] /// private static void Init() /// { /// var window = GetWindow(typeof(DemoWindow), false, "Demo Window"); /// window.Show(); /// } /// /// private void OnGUI() /// { /// var rect = GUILayoutUtility.GetRect(20.0f, 20.0f); /// /// AssetIconsGUI.DrawText(rect, "Example"); /// } /// } /// </code> /// </example> /// <param name="rect">The <see cref="Rect"/> in which the item is drawn.</param> /// <param name="text">The text to draw for the asset icon.</param> /// <param name="style">A style used to modify the appearance of a graphic.</param> /// <param name="selected">Whether the graphic should be rendered with a selection tint.</param> public static void DrawText(Rect rect, string text, AssetIconsCompiledStyle style = null, bool selected = false) { if (Event.current.type != EventType.Repaint) { return; } var originalColor = GUI.color; var guiStyle = new GUIStyle(EditorStyles.label) { alignment = TextAnchor.MiddleCenter, fontStyle = FontStyle.Bold }; if (style != null) { bool display; rect = style.Filter(rect, out display); if (!display) { return; } GUI.color = style.Tint; TextStyle.normal.textColor = style.Tint; TextStyle.alignment = style.TextAnchor; TextStyle.fontStyle = style.FontStyle; } else { GUI.color = Color.black; TextStyle.alignment = TextAnchor.MiddleCenter; TextStyle.fontStyle = FontStyle.Normal; } TextStyle.fontSize = Mathf.FloorToInt(rect.height * 0.3f); if (selected) { GUI.color = AssetIconsPreferences.SelectionTint.Value.Apply(GUI.color); } EditorGUI.LabelField(rect, text, TextStyle); GUI.color = originalColor; }
/// <summary> /// <para>Draws a portion of a texture at a specified <see cref="Rect"/>.</para> /// </summary> /// <example> /// <para>Below is an example of drawing a sprite in an editor window.</para> /// <code> /// using AssetIcons.Editors; /// using UnityEditor; /// using UnityEngine; /// /// public class DemoWindow : EditorWindow /// { /// private Texture textureToDraw; /// /// [MenuItem("AssetIcons/Demo Window")] /// private static void Init() /// { /// var window = GetWindow(typeof(DemoWindow), false, "Demo Window"); /// window.Show(); /// } /// /// private void OnGUI() /// { /// textureToDraw = EditorGUILayout.ObjectField(spriteToDraw, typeof(Texture), false); /// /// var rect = GUILayoutUtility.GetRect(20.0f, 20.0f); /// /// AssetIconsGUI.DrawTexWithUVCoords(rect, textureToDraw, new Rect(0.25, 0.25f, 0.5f, 0.5f)); /// } /// } /// </code> /// </example> /// <param name="rect">The <see cref="Rect"/> in which the item is drawn.</param> /// <param name="texture">The texture to draw.</param> /// <param name="uvCoords">The <see cref="Rect"/> (in normalised texture coordinates) of the <see cref="Texture"/> to draw.</param> /// <param name="style">A style used to modify the appearance of a graphic.</param> /// <param name="selected">Whether the graphic should be rendered with a selection tint.</param> public static void DrawTexWithUVCoords(Rect rect, Texture texture, Rect uvCoords, AssetIconsCompiledStyle style = null, bool selected = false) { if (texture == null || texture.Equals(null) || Event.current.type != EventType.Repaint) { return; } var info = ApplyGeneralModifiers(rect, style); if (!info.Display) { return; } if (style != null) { // Get the targets aspect ratio float aspectRatio = uvCoords.width * texture.width / (uvCoords.height * texture.height); // Apply aspect handling if (style.Aspect != IconAspect.Stretch) { info.Position = ForceAspectRatio(info.Position, aspectRatio, style.Aspect == IconAspect.Envelop); } } else { info.Position = ForceAspectRatio(info.Position, texture.width * uvCoords.width / (texture.height / uvCoords.height)); } var originalColor = GUI.color; GUI.color = info.Tint; if (SELECTION_TINT && selected) { GUI.color = AssetIconsPreferences.SelectionTint.Value.Apply(GUI.color); } GUI.DrawTextureWithTexCoords(info.Position, texture, uvCoords, true); GUI.color = originalColor; }
private static GeneralDrawInfo ApplyGeneralModifiers(Rect rect, AssetIconsCompiledStyle style) { var info = new GeneralDrawInfo(); if (style != null) { info.Position = style.Filter(rect, out info.Display); info.Tint = style.Tint; } else { info.Position = rect; info.Tint = Color.white; info.Display = true; } return(info); }