| | 1 | | using DCL; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Controllers; |
| | 4 | | using System; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.UI; |
| | 7 | |
|
| | 8 | | public interface ISceneLimitsController |
| | 9 | | { |
| | 10 | | void Initialize(ISceneLimitsView sceneLimitsView); |
| | 11 | | void Dispose(); |
| | 12 | | void SetParcelScene(IParcelScene parcelScene); |
| | 13 | | void ToggleSceneLimitsInfo(); |
| | 14 | | void Enable(); |
| | 15 | | void Disable(); |
| | 16 | | void UpdateInfo(); |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public class SceneLimitsController : ISceneLimitsController |
| | 20 | | { |
| | 21 | | internal ISceneLimitsView sceneLimitsView; |
| | 22 | | internal IParcelScene currentParcelScene; |
| | 23 | |
|
| | 24 | | public void Initialize(ISceneLimitsView sceneLimitsView) |
| | 25 | | { |
| 4 | 26 | | this.sceneLimitsView = sceneLimitsView; |
| | 27 | |
|
| 4 | 28 | | sceneLimitsView.OnToggleSceneLimitsInfo += ToggleSceneLimitsInfo; |
| | 29 | |
|
| 4 | 30 | | sceneLimitsView.SetUpdateCallback(UpdateInfo); |
| 4 | 31 | | } |
| | 32 | |
|
| 8 | 33 | | public void Dispose() { sceneLimitsView.OnToggleSceneLimitsInfo -= ToggleSceneLimitsInfo; } |
| | 34 | |
|
| | 35 | | public void SetParcelScene(IParcelScene parcelScene) |
| | 36 | | { |
| 2 | 37 | | currentParcelScene = parcelScene; |
| 2 | 38 | | UpdateInfo(); |
| 2 | 39 | | } |
| | 40 | |
|
| | 41 | | public void ToggleSceneLimitsInfo() |
| | 42 | | { |
| 0 | 43 | | if (!sceneLimitsView.isBodyActived) |
| 0 | 44 | | Enable(); |
| | 45 | | else |
| 0 | 46 | | Disable(); |
| 0 | 47 | | } |
| | 48 | |
|
| | 49 | | public void Enable() |
| | 50 | | { |
| 1 | 51 | | sceneLimitsView.SetBodyActive(true); |
| 1 | 52 | | sceneLimitsView.SetDetailsToggleAsOpen(); |
| 1 | 53 | | UpdateInfo(); |
| 1 | 54 | | } |
| | 55 | |
|
| | 56 | | public void Disable() |
| | 57 | | { |
| 1 | 58 | | sceneLimitsView.SetBodyActive(false); |
| 1 | 59 | | sceneLimitsView.SetDetailsToggleAsClose(); |
| 1 | 60 | | } |
| | 61 | |
|
| | 62 | | public void UpdateInfo() |
| | 63 | | { |
| 4 | 64 | | if (currentParcelScene == null) |
| 1 | 65 | | return; |
| | 66 | |
|
| 3 | 67 | | if (IsParcelSceneSquare(currentParcelScene)) |
| | 68 | | { |
| 3 | 69 | | int size = (int)Math.Sqrt(currentParcelScene.sceneData.parcels.Length); |
| 3 | 70 | | int meters = size * 16; |
| 3 | 71 | | sceneLimitsView.SetTitleText($"{size}x{size} LAND <color=#959696>{meters}x{meters}m"); |
| 3 | 72 | | } |
| | 73 | | else |
| | 74 | | { |
| 0 | 75 | | sceneLimitsView.SetTitleText(BIWSettings.CUSTOM_LAND); |
| | 76 | | } |
| | 77 | |
|
| 3 | 78 | | SceneMetricsModel limits = currentParcelScene.metricsCounter.maxCount; |
| 3 | 79 | | SceneMetricsModel usage = currentParcelScene.metricsCounter.currentCount; |
| | 80 | |
|
| 3 | 81 | | string leftDesc = AppendUsageAndLimit("ENTITIES", usage.entities, limits.entities); |
| 3 | 82 | | leftDesc += "\n" + AppendUsageAndLimit("BODIES", usage.bodies, limits.bodies); |
| 3 | 83 | | leftDesc += "\n" + AppendUsageAndLimit("TRIS", usage.triangles, limits.triangles); |
| 3 | 84 | | string rightDesc = AppendUsageAndLimit("TEXTURES", usage.textures, limits.textures); |
| 3 | 85 | | rightDesc += "\n" + AppendUsageAndLimit("MATERIALS", usage.materials, limits.materials); |
| 3 | 86 | | rightDesc += "\n" + AppendUsageAndLimit("GEOMETRIES", usage.meshes, limits.meshes); |
| | 87 | |
|
| 3 | 88 | | sceneLimitsView.SetLeftDescText(leftDesc); |
| 3 | 89 | | sceneLimitsView.SetRightDescText(rightDesc); |
| | 90 | |
|
| 3 | 91 | | SetFillInfo(); |
| 3 | 92 | | } |
| | 93 | |
|
| | 94 | | internal void SetFillInfo() |
| | 95 | | { |
| 3 | 96 | | float percentAmount = GetHigherLimitPercentInfo(); |
| 3 | 97 | | Color colorToUse = sceneLimitsView.lfColor; |
| | 98 | |
|
| 3 | 99 | | if (percentAmount > 66) |
| 0 | 100 | | colorToUse = sceneLimitsView.mfColor; |
| | 101 | |
|
| 3 | 102 | | if (percentAmount > 85) |
| 0 | 103 | | colorToUse = sceneLimitsView.hfColor; |
| | 104 | |
|
| 6 | 105 | | foreach (Image img in sceneLimitsView.limitUsageFillsImages) |
| | 106 | | { |
| 0 | 107 | | if (img == null) |
| | 108 | | continue; |
| | 109 | |
|
| 0 | 110 | | img.fillAmount = percentAmount / 100f; |
| 0 | 111 | | img.color = colorToUse; |
| | 112 | | } |
| 3 | 113 | | } |
| | 114 | |
|
| | 115 | | internal string AppendUsageAndLimit(string name, int usage, int limit) |
| | 116 | | { |
| 18 | 117 | | string currentString = $"{name}: {usage} / <color=#959696>{limit}</color>"; |
| | 118 | |
|
| 18 | 119 | | if (usage > limit) |
| 0 | 120 | | currentString = "<color=red>" + currentString + "</color>"; |
| | 121 | |
|
| 18 | 122 | | return currentString; |
| | 123 | | } |
| | 124 | |
|
| | 125 | | internal float GetHigherLimitPercentInfo() |
| | 126 | | { |
| 3 | 127 | | SceneMetricsModel limits = currentParcelScene.metricsCounter.maxCount; |
| 3 | 128 | | SceneMetricsModel usage = currentParcelScene.metricsCounter.currentCount; |
| | 129 | |
|
| 3 | 130 | | float percentEntities = usage.entities * 100 / limits.entities; |
| 3 | 131 | | float percentBodies = usage.bodies * 100 / limits.bodies; |
| 3 | 132 | | float percentTris = usage.triangles * 100 / limits.triangles; |
| 3 | 133 | | float percentTexture = usage.textures * 100 / limits.textures; |
| 3 | 134 | | float percentmats = usage.materials * 100 / limits.materials; |
| 3 | 135 | | float percentMeshes = usage.meshes * 100 / limits.meshes; |
| | 136 | |
|
| 3 | 137 | | float result = percentEntities; |
| 3 | 138 | | if (percentBodies > result) |
| 0 | 139 | | result = percentBodies; |
| 3 | 140 | | if (percentTris > result) |
| 0 | 141 | | result = percentTris; |
| 3 | 142 | | if (percentTexture > result) |
| 0 | 143 | | result = percentTexture; |
| 3 | 144 | | if (percentmats > result) |
| 0 | 145 | | result = percentmats; |
| 3 | 146 | | if (percentMeshes > result) |
| 0 | 147 | | result = percentMeshes; |
| | 148 | |
|
| 3 | 149 | | return result; |
| | 150 | | } |
| | 151 | |
|
| | 152 | | internal bool IsParcelSceneSquare(IParcelScene scene) |
| | 153 | | { |
| 3 | 154 | | Vector2Int[] parcelsPoints = scene.sceneData.parcels; |
| 3 | 155 | | int minX = int.MaxValue; |
| 3 | 156 | | int minY = int.MaxValue; |
| 3 | 157 | | int maxX = int.MinValue; |
| 3 | 158 | | int maxY = int.MinValue; |
| | 159 | |
|
| 12 | 160 | | foreach (Vector2Int vector in parcelsPoints) |
| | 161 | | { |
| 3 | 162 | | if (vector.x < minX) |
| 3 | 163 | | minX = vector.x; |
| 3 | 164 | | if (vector.y < minY) |
| 3 | 165 | | minY = vector.y; |
| 3 | 166 | | if (vector.x > maxX) |
| 3 | 167 | | maxX = vector.x; |
| 3 | 168 | | if (vector.y > maxY) |
| 3 | 169 | | maxY = vector.y; |
| | 170 | | } |
| | 171 | |
|
| 3 | 172 | | if (maxX - minX != maxY - minY) |
| 0 | 173 | | return false; |
| | 174 | |
|
| 3 | 175 | | int lateralLengh = Math.Abs((maxX - minX) + 1); |
| | 176 | |
|
| 3 | 177 | | if (parcelsPoints.Length != lateralLengh * lateralLengh) |
| 0 | 178 | | return false; |
| | 179 | |
|
| 3 | 180 | | return true; |
| | 181 | | } |
| | 182 | | } |