|
| 1 | +// Unity C# reference source |
| 2 | +// Copyright (c) Unity Technologies. For terms of use, see |
| 3 | +// https://unity3d.com/legal/licenses/Unity_Reference_Only_License |
| 4 | + |
| 5 | +using UnityEngine; |
| 6 | +using UnityEngine.UIElements; |
| 7 | +using Color = UnityEngine.Color; |
| 8 | + |
| 9 | +namespace UnityEditor.Audio.UIElements |
| 10 | +{ |
| 11 | + internal class Tickmarks : VisualElement |
| 12 | + { |
| 13 | + private interface Scale |
| 14 | + { |
| 15 | + int DivisionCount(); |
| 16 | + string[] Labels(); |
| 17 | + int[] SubDivisionCount(); |
| 18 | + float[] LabelOffsets(); |
| 19 | + } |
| 20 | + |
| 21 | + private struct LargeScale : Scale |
| 22 | + { |
| 23 | + public int DivisionCount() { return 15; } |
| 24 | + public string[] Labels() { return new string[] { "80", "70", "60", "50", "40", "30", "24", "21", "18", "15", "12", "9", "6", "3", "0" }; } |
| 25 | + public int[] SubDivisionCount() { return new int[] { 4, 4, 4, 4, 4, 5, 2, 2, 2, 2, 2, 2, 2, 2 }; } |
| 26 | + public float[] LabelOffsets() { return new float[] { -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -6, -3.5f, -3.5f, -3.5f, -3.5f }; } |
| 27 | + } |
| 28 | + |
| 29 | + private struct CompactScale : Scale |
| 30 | + { |
| 31 | + public int DivisionCount() { return 8; } |
| 32 | + public string[] Labels() { return new string[] { "80", "60", "40", "24", "18", "12", "6", "0" }; } |
| 33 | + public int[] SubDivisionCount() { return new int[] { 4, 4, 3, 2, 2, 2, 2 }; } |
| 34 | + public float[] LabelOffsets() { return new float[] { -6, -6, -6, -6, -6, -6, -3.5f, -3.5f }; } |
| 35 | + } |
| 36 | + |
| 37 | + private struct MiniScale : Scale |
| 38 | + { |
| 39 | + public int DivisionCount() { return 5; } |
| 40 | + public string[] Labels() { return new string[] { "80", "45", "21", "12", "0" }; } |
| 41 | + public int[] SubDivisionCount() { return new int[] { 6, 3, 2, 3 }; } |
| 42 | + public float[] LabelOffsets() { return new float[] { -6, -6, -6, -6, -3.5f }; } |
| 43 | + } |
| 44 | + |
| 45 | + public new class UxmlFactory : UxmlFactory<Tickmarks, UxmlTraits> { } |
| 46 | + |
| 47 | + public new class UxmlTraits : VisualElement.UxmlTraits { } |
| 48 | + |
| 49 | + public Tickmarks() : base() |
| 50 | + { |
| 51 | + generateVisualContent += context => GenerateVisualContent(context); |
| 52 | + |
| 53 | + RegisterCallback<GeometryChangedEvent>(OnGeometryChanged); |
| 54 | + } |
| 55 | + |
| 56 | + private void OnGeometryChanged(GeometryChangedEvent evt) |
| 57 | + { |
| 58 | + MarkDirtyRepaint(); |
| 59 | + } |
| 60 | + |
| 61 | + static void GenerateVisualContent(MeshGenerationContext context) |
| 62 | + { |
| 63 | + var painter2D = context.painter2D; |
| 64 | + |
| 65 | + var contentRect = context.visualElement.contentRect; |
| 66 | + |
| 67 | + Scale scale = contentRect.width > 350 ? new LargeScale() : (contentRect.width > 175 ? new CompactScale() : new MiniScale()); |
| 68 | + |
| 69 | + var gray = new Color(0.85f, 0.85f, 0.85f, 1.0f); |
| 70 | + |
| 71 | + for (int index = 0; index < scale.DivisionCount(); index += 1) |
| 72 | + { |
| 73 | + var pos = contentRect.width * index / (scale.DivisionCount() - 1.0f); |
| 74 | + |
| 75 | + var rect = new Rect(pos - 0.5f, 5.0f, 1.0f, 8.0f); |
| 76 | + |
| 77 | + painter2D.fillColor = gray; |
| 78 | + painter2D.BeginPath(); |
| 79 | + painter2D.MoveTo(new Vector2(rect.xMin, rect.yMin)); |
| 80 | + painter2D.LineTo(new Vector2(rect.xMax, rect.yMin)); |
| 81 | + painter2D.LineTo(new Vector2(rect.xMax, rect.yMax)); |
| 82 | + painter2D.LineTo(new Vector2(rect.xMin, rect.yMax)); |
| 83 | + painter2D.ClosePath(); |
| 84 | + painter2D.Fill(); |
| 85 | + } |
| 86 | + |
| 87 | + for (int index = 0; index < scale.DivisionCount() - 1; index += 1) |
| 88 | + { |
| 89 | + var pos = contentRect.width * index / (scale.DivisionCount() - 1.0f); |
| 90 | + var spacing = contentRect.width / (scale.DivisionCount() - 1.0f); |
| 91 | + var subDivCount = scale.SubDivisionCount()[index]; |
| 92 | + |
| 93 | + for (int subIndex = 0; subIndex < subDivCount; subIndex++) |
| 94 | + { |
| 95 | + var rect = new Rect(pos - 0.5f + spacing * (subIndex + 1.0f) / (subDivCount + 1.0f), 5.0f, 1.0f, 4.0f); |
| 96 | + |
| 97 | + painter2D.fillColor = gray; |
| 98 | + painter2D.BeginPath(); |
| 99 | + painter2D.MoveTo(new Vector2(rect.xMin, rect.yMin)); |
| 100 | + painter2D.LineTo(new Vector2(rect.xMax, rect.yMin)); |
| 101 | + painter2D.LineTo(new Vector2(rect.xMax, rect.yMax)); |
| 102 | + painter2D.LineTo(new Vector2(rect.xMin, rect.yMax)); |
| 103 | + painter2D.ClosePath(); |
| 104 | + painter2D.Fill(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + for (int index = 0; index < scale.DivisionCount(); index += 1) |
| 109 | + { |
| 110 | + var pos = contentRect.width * index / (scale.DivisionCount() - 1.0f); |
| 111 | + |
| 112 | + var rect = new Rect(pos - 10.0f, 10, 20.0f, 20.0f); |
| 113 | + |
| 114 | + context.DrawText(scale.Labels()[index], new Vector2(pos + scale.LabelOffsets()[index], 14.0f), 10.0f, gray); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments