Skip to content

Commit

Permalink
Added Icon Drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
Lachee committed Aug 10, 2022
1 parent fd43673 commit dd9c2b3
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 4 deletions.
29 changes: 29 additions & 0 deletions Editor/Icons/Icon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,34 @@ public static Texture Load(string name, bool recache = false)
_cache[name] = texture;
return texture;
}

/// <summary>
/// Draws a preview of a sprite
/// </summary>
/// <param name="position"></param>
/// <param name="sprite"></param>
public static void DrawSpritePreview(Rect position, Sprite sprite)
{
Vector2 fullSize = new Vector2(sprite.texture.width, sprite.texture.height);
Vector2 size = new Vector2(sprite.textureRect.width, sprite.textureRect.height);

Rect coords = sprite.textureRect;
coords.x /= fullSize.x;
coords.width /= fullSize.x;
coords.y /= fullSize.y;
coords.height /= fullSize.y;

Vector2 ratio;
ratio.x = position.width / size.x;
ratio.y = position.height / size.y;
float minRatio = Mathf.Min(ratio.x, ratio.y);

Vector2 center = position.center;
position.width = size.x * minRatio;
position.height = size.y * minRatio;
position.center = center;

GUI.DrawTextureWithTexCoords(position, sprite.texture, coords);
}
}
}
57 changes: 54 additions & 3 deletions Editor/Utilities/RandomListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class RandomListDrawerStyle
{
{ typeof(void), new BasicRistDrawer() },
{ typeof(Color), new ColorRistDrawer() },
{ typeof(AnimationCurve), new AnimationCurveRistDrawer() }
{ typeof(AnimationCurve), new AnimationCurveRistDrawer() },
{ typeof(Sprite), new SpriteRistDrawer() },
};

public readonly Color[] Colors =
Expand Down Expand Up @@ -436,9 +437,10 @@ private static List<WeightSlider> CreateSliders(SerializedProperty property, Rec
}
}

internal sealed class BasicRistDrawer : IRistBoxLabelDrawer
/// <summary>Basic extendable drawer for the Random List</summary>
public class BasicRistDrawer : IRistBoxLabelDrawer
{
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
public virtual void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
{
var name = property.hasVisibleChildren ? property.displayName : property.GetValueName();
var label = isMini
Expand All @@ -449,6 +451,7 @@ public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProp
}
}

/// <summary>Makes the box the same colour as the property</summary>
internal sealed class ColorRistDrawer : IRistBoxLabelDrawer
{
public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
Expand All @@ -468,6 +471,7 @@ public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProp
}
}

/// <summary>Draws the fancy animation curves</summary>
internal sealed class AnimationCurveRistDrawer : IRistBoxLabelDrawer
{
const float PADDING = 2;
Expand All @@ -492,4 +496,51 @@ public void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProp
EditorGUI.indentLevel++;
}
}

/// <summary>Draws little sprites</summary>
internal sealed class SpriteRistDrawer : BasicRistDrawer
{
const float PADDING = 2;

public bool WithBackground { get; set; } = false;

public override void DrawLabel(Rect position, RandomListDrawerStyle style, SerializedProperty property, float weight, bool isMini)
{
if (isMini)
{
base.DrawLabel(position, style, property, weight, isMini);
}
else
{
var name = property.GetValueName();
var label = string.Format("{0}\n{1:0}%", name, weight * 100);

float size = Mathf.Min(position.width - PADDING * 2, position.height - PADDING * 2);

Rect iconRect = new Rect(position.x + PADDING, position.y + PADDING, size, size);
if (iconRect.width > 2 || iconRect.height > 2)
{
if (WithBackground)
EditorGUI.DrawRect(iconRect, Color.gray);

if (property.objectReferenceValue is Texture texture)
{
EditorGUI.DrawPreviewTexture(iconRect, texture);
}
else if (property.objectReferenceValue is Sprite sprite)
{
Icon.DrawSpritePreview(iconRect, sprite);
}
}
else
{
// Reset so the label can get the most space
iconRect = new Rect();
}

Rect labelRect = new Rect(iconRect.xMax + 5, position.y, position.width - iconRect.width - 10, position.height);
style.sliderText.Draw(labelRect, label, false, false, false, false);
}
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.lachee.utilities",
"version": "1.3.4",
"version": "1.3.5",
"displayName": "Lachee's Utilities",
"description": "Bunch of utility functionality",
"unity": "2019.1",
Expand Down

0 comments on commit dd9c2b3

Please sign in to comment.