A game about forced loneliness, made by TACStudios
at master 201 lines 7.5 kB view raw
1using System.Collections.Generic; 2using UnityEngine; 3 4namespace UnityEditor.UI 5{ 6 /// <summary> 7 /// PropertyDrawer for [[SpriteState]]. 8 /// This is a PropertyDrawer for SpriteState it is implemented using the standard unity PropertyDrawer framework. 9 /// </summary> 10 internal class SpriteDrawUtility 11 { 12 static Texture2D s_ContrastTex; 13 14 // Returns a usable texture that looks like a high-contrast checker board. 15 static Texture2D contrastTexture 16 { 17 get 18 { 19 if (s_ContrastTex == null) 20 s_ContrastTex = CreateCheckerTex( 21 new Color(0f, 0.0f, 0f, 0.5f), 22 new Color(1f, 1f, 1f, 0.5f)); 23 return s_ContrastTex; 24 } 25 } 26 27 // Create a checker-background texture. 28 static Texture2D CreateCheckerTex(Color c0, Color c1) 29 { 30 Texture2D tex = new Texture2D(16, 16); 31 tex.name = "[Generated] Checker Texture"; 32 tex.hideFlags = HideFlags.DontSave; 33 34 for (int y = 0; y < 8; ++y) for (int x = 0; x < 8; ++x) tex.SetPixel(x, y, c1); 35 for (int y = 8; y < 16; ++y) for (int x = 0; x < 8; ++x) tex.SetPixel(x, y, c0); 36 for (int y = 0; y < 8; ++y) for (int x = 8; x < 16; ++x) tex.SetPixel(x, y, c0); 37 for (int y = 8; y < 16; ++y) for (int x = 8; x < 16; ++x) tex.SetPixel(x, y, c1); 38 39 tex.Apply(); 40 tex.filterMode = FilterMode.Point; 41 return tex; 42 } 43 44 // Create a gradient texture. 45 static Texture2D CreateGradientTex() 46 { 47 Texture2D tex = new Texture2D(1, 16); 48 tex.name = "[Generated] Gradient Texture"; 49 tex.hideFlags = HideFlags.DontSave; 50 51 Color c0 = new Color(1f, 1f, 1f, 0f); 52 Color c1 = new Color(1f, 1f, 1f, 0.4f); 53 54 for (int i = 0; i < 16; ++i) 55 { 56 float f = Mathf.Abs((i / 15f) * 2f - 1f); 57 f *= f; 58 tex.SetPixel(0, i, Color.Lerp(c0, c1, f)); 59 } 60 61 tex.Apply(); 62 tex.filterMode = FilterMode.Bilinear; 63 return tex; 64 } 65 66 // Draws the tiled texture. Like GUI.DrawTexture() but tiled instead of stretched. 67 static void DrawTiledTexture(Rect rect, Texture tex) 68 { 69 float u = rect.width / tex.width; 70 float v = rect.height / tex.height; 71 72 Rect texCoords = new Rect(0, 0, u, v); 73 TextureWrapMode originalMode = tex.wrapMode; 74 tex.wrapMode = TextureWrapMode.Repeat; 75 GUI.DrawTextureWithTexCoords(rect, tex, texCoords); 76 tex.wrapMode = originalMode; 77 } 78 79 // Draw the specified Image. 80 public static void DrawSprite(Sprite sprite, Rect drawArea, Color color) 81 { 82 if (sprite == null) 83 return; 84 85 Texture2D tex = sprite.texture; 86 if (tex == null) 87 return; 88 89 Rect outer = sprite.rect; 90 Rect inner = outer; 91 inner.xMin += sprite.border.x; 92 inner.yMin += sprite.border.y; 93 inner.xMax -= sprite.border.z; 94 inner.yMax -= sprite.border.w; 95 96 Vector4 uv4 = UnityEngine.Sprites.DataUtility.GetOuterUV(sprite); 97 Rect uv = new Rect(uv4.x, uv4.y, uv4.z - uv4.x, uv4.w - uv4.y); 98 Vector4 padding = UnityEngine.Sprites.DataUtility.GetPadding(sprite); 99 padding.x /= outer.width; 100 padding.y /= outer.height; 101 padding.z /= outer.width; 102 padding.w /= outer.height; 103 104 DrawSprite(tex, drawArea, padding, outer, inner, uv, color, null); 105 } 106 107 // Draw the specified Image. 108 public static void DrawSprite(Texture tex, Rect drawArea, Rect outer, Rect uv, Color color) 109 { 110 DrawSprite(tex, drawArea, Vector4.zero, outer, outer, uv, color, null); 111 } 112 113 // Draw the specified Image. 114 private static void DrawSprite(Texture tex, Rect drawArea, Vector4 padding, Rect outer, Rect inner, Rect uv, Color color, Material mat) 115 { 116 // Create the texture rectangle that is centered inside rect. 117 Rect outerRect = drawArea; 118 outerRect.width = Mathf.Abs(outer.width); 119 outerRect.height = Mathf.Abs(outer.height); 120 121 if (outerRect.width > 0f) 122 { 123 float f = drawArea.width / outerRect.width; 124 outerRect.width *= f; 125 outerRect.height *= f; 126 } 127 128 if (drawArea.height > outerRect.height) 129 { 130 outerRect.y += (drawArea.height - outerRect.height) * 0.5f; 131 } 132 else if (outerRect.height > drawArea.height) 133 { 134 float f = drawArea.height / outerRect.height; 135 outerRect.width *= f; 136 outerRect.height *= f; 137 } 138 139 if (drawArea.width > outerRect.width) 140 outerRect.x += (drawArea.width - outerRect.width) * 0.5f; 141 142 // Draw the background 143 EditorGUI.DrawTextureTransparent(outerRect, null, ScaleMode.ScaleToFit, outer.width / outer.height); 144 145 // Draw the Image 146 GUI.color = color; 147 148 Rect paddedTexArea = new Rect( 149 outerRect.x + outerRect.width * padding.x, 150 outerRect.y + outerRect.height * padding.w, 151 outerRect.width - (outerRect.width * (padding.z + padding.x)), 152 outerRect.height - (outerRect.height * (padding.w + padding.y)) 153 ); 154 155 if (mat == null) 156 { 157 GUI.DrawTextureWithTexCoords(paddedTexArea, tex, uv, true); 158 } 159 else 160 { 161 // NOTE: There is an issue in Unity that prevents it from clipping the drawn preview 162 // using BeginGroup/EndGroup, and there is no way to specify a UV rect... 163 EditorGUI.DrawPreviewTexture(paddedTexArea, tex, mat); 164 } 165 166 // Draw the border indicator lines 167 GUI.BeginGroup(outerRect); 168 { 169 tex = contrastTexture; 170 GUI.color = Color.white; 171 172 if (inner.xMin != outer.xMin) 173 { 174 float x = (inner.xMin - outer.xMin) / outer.width * outerRect.width - 1; 175 DrawTiledTexture(new Rect(x, 0f, 1f, outerRect.height), tex); 176 } 177 178 if (inner.xMax != outer.xMax) 179 { 180 float x = (inner.xMax - outer.xMin) / outer.width * outerRect.width - 1; 181 DrawTiledTexture(new Rect(x, 0f, 1f, outerRect.height), tex); 182 } 183 184 if (inner.yMin != outer.yMin) 185 { 186 // GUI.DrawTexture is top-left based rather than bottom-left 187 float y = (inner.yMin - outer.yMin) / outer.height * outerRect.height - 1; 188 DrawTiledTexture(new Rect(0f, outerRect.height - y, outerRect.width, 1f), tex); 189 } 190 191 if (inner.yMax != outer.yMax) 192 { 193 float y = (inner.yMax - outer.yMin) / outer.height * outerRect.height - 1; 194 DrawTiledTexture(new Rect(0f, outerRect.height - y, outerRect.width, 1f), tex); 195 } 196 } 197 198 GUI.EndGroup(); 199 } 200 } 201}