| | 1 | | using DCL; |
| | 2 | | using DCL.Components; |
| | 3 | | using DCL.Models; |
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.EventSystems; |
| | 9 | | using Newtonsoft.Json; |
| | 10 | | using Newtonsoft.Json.Linq; |
| | 11 | | using DCL.Configuration; |
| | 12 | | using static ProtocolV2; |
| | 13 | | using Environment = DCL.Environment; |
| | 14 | | using System.IO; |
| | 15 | | using System.Linq; |
| | 16 | | using System.Runtime.Serialization.Formatters.Binary; |
| | 17 | | using DCL.Controllers; |
| | 18 | | using UnityEngine.Networking; |
| | 19 | | using UnityEngine.Events; |
| | 20 | |
|
| | 21 | | public static partial class BIWUtils |
| | 22 | | { |
| | 23 | | public static LandRole GetLandOwnershipType(List<LandWithAccess> lands, ParcelScene scene) |
| | 24 | | { |
| 7 | 25 | | LandWithAccess filteredLand = lands.FirstOrDefault(land => scene.sceneData.basePosition == land.baseCoords); |
| 7 | 26 | | return GetLandOwnershipType(filteredLand); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public static LandRole GetLandOwnershipType(LandWithAccess land) |
| | 30 | | { |
| 0 | 31 | | if (land != null) |
| 0 | 32 | | return land.role; |
| 0 | 33 | | return LandRole.OWNER; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | public static Vector3 SnapFilterEulerAngles(Vector3 vectorToFilter, float degrees) |
| | 37 | | { |
| 0 | 38 | | vectorToFilter.x = ClosestNumber(vectorToFilter.x, degrees); |
| 0 | 39 | | vectorToFilter.y = ClosestNumber(vectorToFilter.y, degrees); |
| 0 | 40 | | vectorToFilter.z = ClosestNumber(vectorToFilter.z, degrees); |
| | 41 | |
|
| 0 | 42 | | return vectorToFilter; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | static float ClosestNumber(float n, float m) |
| | 46 | | { |
| | 47 | | // find the quotient |
| 0 | 48 | | int q = Mathf.RoundToInt(n / m); |
| | 49 | |
|
| | 50 | | // 1st possible closest number |
| 0 | 51 | | float n1 = m * q; |
| | 52 | |
|
| | 53 | | // 2nd possible closest number |
| 0 | 54 | | float n2 = (n * m) > 0 ? (m * (q + 1)) : (m * (q - 1)); |
| | 55 | |
|
| | 56 | | // if true, then n1 is the required closest number |
| 0 | 57 | | if (Math.Abs(n - n1) < Math.Abs(n - n2)) |
| 0 | 58 | | return n1; |
| | 59 | |
|
| | 60 | | // else n2 is the required closest number |
| 0 | 61 | | return n2; |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public static Vector2Int GetSceneSize(ParcelScene parcelScene) |
| | 65 | | { |
| 13 | 66 | | int minX = Int32.MaxValue; |
| 13 | 67 | | int maxX = Int32.MinValue; |
| 13 | 68 | | int minY = Int32.MaxValue; |
| 13 | 69 | | int maxY = Int32.MinValue; |
| | 70 | |
|
| 68 | 71 | | foreach (var parcel in parcelScene.sceneData.parcels) |
| | 72 | | { |
| 21 | 73 | | if (parcel.x > maxX) |
| 15 | 74 | | maxX = parcel.x; |
| 21 | 75 | | if (parcel.x < minX) |
| 15 | 76 | | minX = parcel.x; |
| | 77 | |
|
| 21 | 78 | | if (parcel.y > maxY) |
| 15 | 79 | | maxY = parcel.y; |
| 21 | 80 | | if (parcel.y < minY) |
| 15 | 81 | | minY = parcel.y; |
| | 82 | | } |
| | 83 | |
|
| 13 | 84 | | int sizeX = maxX - minX + 1; |
| 13 | 85 | | int sizeY = maxY - minY + 1; |
| 13 | 86 | | return new Vector2Int(sizeX, sizeY); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public static Vector3 CalculateUnityMiddlePoint(IParcelScene parcelScene) |
| | 90 | | { |
| | 91 | | Vector3 position; |
| | 92 | |
|
| 109 | 93 | | float totalX = 0f; |
| 109 | 94 | | float totalY = 0f; |
| 109 | 95 | | float totalZ = 0f; |
| | 96 | |
|
| 109 | 97 | | int minX = int.MaxValue; |
| 109 | 98 | | int minY = int.MaxValue; |
| 109 | 99 | | int maxX = int.MinValue; |
| 109 | 100 | | int maxY = int.MinValue; |
| | 101 | |
|
| 109 | 102 | | if (parcelScene.sceneData == null || parcelScene.sceneData.parcels == null) |
| 0 | 103 | | return Vector3.zero; |
| | 104 | |
|
| 436 | 105 | | foreach (Vector2Int vector in parcelScene.sceneData.parcels) |
| | 106 | | { |
| 109 | 107 | | totalX += vector.x; |
| 109 | 108 | | totalZ += vector.y; |
| 109 | 109 | | if (vector.x < minX) |
| 109 | 110 | | minX = vector.x; |
| 109 | 111 | | if (vector.y < minY) |
| 109 | 112 | | minY = vector.y; |
| 109 | 113 | | if (vector.x > maxX) |
| 109 | 114 | | maxX = vector.x; |
| 109 | 115 | | if (vector.y > maxY) |
| 109 | 116 | | maxY = vector.y; |
| | 117 | | } |
| | 118 | |
|
| 109 | 119 | | float centerX = totalX / parcelScene.sceneData.parcels.Length; |
| 109 | 120 | | float centerZ = totalZ / parcelScene.sceneData.parcels.Length; |
| | 121 | |
|
| 109 | 122 | | position.x = centerX; |
| 109 | 123 | | position.y = totalY; |
| 109 | 124 | | position.z = centerZ; |
| | 125 | |
|
| 109 | 126 | | position = WorldStateUtils.ConvertScenePositionToUnityPosition(parcelScene); |
| | 127 | |
|
| 109 | 128 | | position.x += ParcelSettings.PARCEL_SIZE / 2; |
| 109 | 129 | | position.z += ParcelSettings.PARCEL_SIZE / 2; |
| | 130 | |
|
| 109 | 131 | | return position; |
| | 132 | | } |
| | 133 | |
|
| | 134 | | public static CatalogItem CreateFloorSceneObject() |
| | 135 | | { |
| 0 | 136 | | CatalogItem floorSceneObject = new CatalogItem(); |
| 0 | 137 | | floorSceneObject.id = BIWSettings.FLOOR_ID; |
| | 138 | |
|
| 0 | 139 | | floorSceneObject.model = BIWSettings.FLOOR_MODEL; |
| 0 | 140 | | floorSceneObject.name = BIWSettings.FLOOR_NAME; |
| 0 | 141 | | floorSceneObject.assetPackName = BIWSettings.FLOOR_ASSET_PACK_NAME; |
| | 142 | |
|
| 0 | 143 | | floorSceneObject.contents = new Dictionary<string, string>(); |
| | 144 | |
|
| 0 | 145 | | floorSceneObject.contents.Add(BIWSettings.FLOOR_GLTF_KEY, BIWSettings.FLOOR_GLTF_VALUE); |
| 0 | 146 | | floorSceneObject.contents.Add(BIWSettings.FLOOR_TEXTURE_KEY, BIWSettings.FLOOR_TEXTURE_VALUE); |
| | 147 | |
|
| 0 | 148 | | floorSceneObject.metrics = new SceneObject.ObjectMetrics(); |
| | 149 | |
|
| 0 | 150 | | return floorSceneObject; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | public static Dictionary<string, string> ConvertMappingsToDictionary(ContentServerUtils.MappingPair[] contents) |
| | 154 | | { |
| 0 | 155 | | Dictionary<string, string> mappingDict = new Dictionary<string, string>(); |
| | 156 | |
|
| 0 | 157 | | foreach (ContentServerUtils.MappingPair mappingPair in contents) |
| | 158 | | { |
| 0 | 159 | | mappingDict.Add(mappingPair.file, mappingPair.hash); |
| | 160 | | } |
| | 161 | |
|
| 0 | 162 | | return mappingDict; |
| | 163 | | } |
| | 164 | |
|
| | 165 | | public static void DrawScreenRect(Rect rect, Color color) |
| | 166 | | { |
| 0 | 167 | | GUI.color = color; |
| 0 | 168 | | GUI.DrawTexture(rect, Texture2D.whiteTexture); |
| 0 | 169 | | } |
| | 170 | |
|
| | 171 | | public static void DrawScreenRectBorder(Rect rect, float thickness, Color color) |
| | 172 | | { |
| | 173 | | //Top |
| 0 | 174 | | DrawScreenRect(new Rect(rect.xMin, rect.yMin, rect.width, thickness), color); |
| | 175 | | // Left |
| 0 | 176 | | DrawScreenRect(new Rect(rect.xMin, rect.yMin, thickness, rect.height), color); |
| | 177 | | // Right |
| 0 | 178 | | DrawScreenRect(new Rect(rect.xMax - thickness, rect.yMin, thickness, rect.height), color); |
| | 179 | | // Bottom |
| 0 | 180 | | DrawScreenRect(new Rect(rect.xMin, rect.yMax - thickness, rect.width, thickness), color); |
| 0 | 181 | | } |
| | 182 | |
|
| | 183 | | public static Rect GetScreenRect(Vector3 screenPosition1, Vector3 screenPosition2) |
| | 184 | | { |
| | 185 | | // Move origin from bottom left to top left |
| 0 | 186 | | screenPosition1.y = Screen.height - screenPosition1.y; |
| 0 | 187 | | screenPosition2.y = Screen.height - screenPosition2.y; |
| | 188 | | // Calculate corners |
| 0 | 189 | | var topLeft = Vector3.Min(screenPosition1, screenPosition2); |
| 0 | 190 | | var bottomRight = Vector3.Max(screenPosition1, screenPosition2); |
| | 191 | | // Create Rect |
| 0 | 192 | | return Rect.MinMaxRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y); |
| | 193 | | } |
| | 194 | |
|
| | 195 | | public static Bounds GetViewportBounds(Camera camera, Vector3 screenPosition1, Vector3 screenPosition2) |
| | 196 | | { |
| 2 | 197 | | var v1 = camera.ScreenToViewportPoint(screenPosition1); |
| 2 | 198 | | var v2 = camera.ScreenToViewportPoint(screenPosition2); |
| 2 | 199 | | var min = Vector3.Min(v1, v2); |
| 2 | 200 | | var max = Vector3.Max(v1, v2); |
| 2 | 201 | | min.z = camera.nearClipPlane; |
| 2 | 202 | | max.z = camera.farClipPlane; |
| | 203 | |
|
| 2 | 204 | | var bounds = new Bounds(); |
| | 205 | |
|
| 2 | 206 | | bounds.SetMinMax(min, max); |
| 2 | 207 | | return bounds; |
| | 208 | | } |
| | 209 | |
|
| 0 | 210 | | public static bool IsWithinSelectionBounds(Transform transform, Vector3 lastClickMousePosition, Vector3 mousePositio |
| | 211 | |
|
| | 212 | | public static bool IsWithinSelectionBounds(Vector3 point, Vector3 lastClickMousePosition, Vector3 mousePosition) |
| | 213 | | { |
| 2 | 214 | | Camera camera = Camera.main; |
| 2 | 215 | | var viewPortBounds = GetViewportBounds(camera, lastClickMousePosition, mousePosition); |
| 2 | 216 | | return viewPortBounds.Contains(camera.WorldToViewportPoint(point)); |
| | 217 | | } |
| | 218 | |
|
| | 219 | | public static bool IsBoundInsideCamera(Bounds bound) |
| | 220 | | { |
| 0 | 221 | | Vector3[] points = { bound.max, bound.center, bound.min }; |
| 0 | 222 | | return IsPointInsideCamera(points); |
| | 223 | | } |
| | 224 | |
|
| | 225 | | public static bool IsPointInsideCamera(Vector3[] points) |
| | 226 | | { |
| 0 | 227 | | foreach (Vector3 point in points) |
| | 228 | | { |
| 0 | 229 | | if (IsPointInsideCamera(point)) |
| 0 | 230 | | return true; |
| | 231 | | } |
| 0 | 232 | | return false; |
| | 233 | | } |
| | 234 | |
|
| | 235 | | public static bool IsPointInsideCamera(Vector3 point) |
| | 236 | | { |
| 0 | 237 | | Vector3 topRight = new Vector3(Screen.width, Screen.height, 0); |
| 0 | 238 | | var viewPortBounds = GetViewportBounds(Camera.main, Vector3.zero, topRight); |
| 0 | 239 | | return viewPortBounds.Contains(Camera.main.WorldToViewportPoint(point)); |
| | 240 | | } |
| | 241 | |
|
| | 242 | | public static void CopyGameObjectStatus(GameObject gameObjectToCopy, GameObject gameObjectToReceive, bool copyParent |
| | 243 | | { |
| 5 | 244 | | if (copyParent) |
| 0 | 245 | | gameObjectToReceive.transform.SetParent(gameObjectToCopy.transform.parent); |
| | 246 | |
|
| 5 | 247 | | gameObjectToReceive.transform.position = gameObjectToCopy.transform.position; |
| | 248 | |
|
| 5 | 249 | | if (localRotation) |
| 0 | 250 | | gameObjectToReceive.transform.localRotation = gameObjectToCopy.transform.localRotation; |
| | 251 | | else |
| 5 | 252 | | gameObjectToReceive.transform.rotation = gameObjectToCopy.transform.rotation; |
| | 253 | |
|
| 5 | 254 | | gameObjectToReceive.transform.localScale = gameObjectToCopy.transform.lossyScale; |
| 5 | 255 | | } |
| | 256 | |
|
| | 257 | | public static bool IsPointerOverMaskElement(LayerMask mask) |
| | 258 | | { |
| | 259 | | RaycastHit hitInfo; |
| 2 | 260 | | UnityEngine.Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); |
| 2 | 261 | | return Physics.Raycast(mouseRay, out hitInfo, 5555, mask); |
| | 262 | | } |
| | 263 | |
|
| | 264 | | public static bool IsPointerOverUIElement(Vector3 mousePosition) |
| | 265 | | { |
| 1 | 266 | | var eventData = new PointerEventData(EventSystem.current); |
| 1 | 267 | | eventData.position = mousePosition; |
| 1 | 268 | | var results = new List<RaycastResult>(); |
| 1 | 269 | | EventSystem.current.RaycastAll(eventData, results); |
| 1 | 270 | | return results.Count > 2; |
| | 271 | | } |
| | 272 | |
|
| 1 | 273 | | public static bool IsPointerOverUIElement() { return IsPointerOverUIElement(Input.mousePosition); } |
| | 274 | |
|
| | 275 | | public static string ConvertEntityToJSON(IDCLEntity entity) |
| | 276 | | { |
| 4 | 277 | | EntityData builderInWorldEntityData = new EntityData(); |
| 4 | 278 | | builderInWorldEntityData.entityId = entity.entityId; |
| | 279 | |
|
| | 280 | |
|
| 10 | 281 | | foreach (KeyValuePair<CLASS_ID_COMPONENT, IEntityComponent> keyValuePair in entity.components) |
| | 282 | | { |
| 1 | 283 | | if (keyValuePair.Key == CLASS_ID_COMPONENT.TRANSFORM) |
| | 284 | | { |
| 1 | 285 | | EntityData.TransformComponent entityComponentModel = new EntityData.TransformComponent(); |
| | 286 | |
|
| 1 | 287 | | entityComponentModel.position = WorldStateUtils.ConvertUnityToScenePosition(entity.gameObject.transform. |
| 1 | 288 | | entityComponentModel.rotation = entity.gameObject.transform.localRotation.eulerAngles; |
| 1 | 289 | | entityComponentModel.scale = entity.gameObject.transform.localScale; |
| | 290 | |
|
| 1 | 291 | | builderInWorldEntityData.transformComponent = entityComponentModel; |
| 1 | 292 | | } |
| | 293 | | else |
| | 294 | | { |
| 0 | 295 | | ProtocolV2.GenericComponent entityComponentModel = new ProtocolV2.GenericComponent(); |
| 0 | 296 | | entityComponentModel.componentId = (int) keyValuePair.Key; |
| 0 | 297 | | entityComponentModel.data = keyValuePair.Value.GetModel(); |
| | 298 | |
|
| 0 | 299 | | builderInWorldEntityData.components.Add(entityComponentModel); |
| | 300 | | } |
| | 301 | | } |
| | 302 | |
|
| 8 | 303 | | foreach (KeyValuePair<Type, ISharedComponent> keyValuePair in entity.sharedComponents) |
| | 304 | | { |
| 0 | 305 | | if (keyValuePair.Value.GetClassId() == (int) CLASS_ID.NFT_SHAPE) |
| | 306 | | { |
| 0 | 307 | | EntityData.NFTComponent nFTComponent = new EntityData.NFTComponent(); |
| 0 | 308 | | NFTShape.Model model = (NFTShape.Model) keyValuePair.Value.GetModel(); |
| | 309 | |
|
| 0 | 310 | | nFTComponent.id = keyValuePair.Value.id; |
| 0 | 311 | | nFTComponent.color = new ColorRepresentation(model.color); |
| 0 | 312 | | nFTComponent.assetId = model.assetId; |
| 0 | 313 | | nFTComponent.src = model.src; |
| 0 | 314 | | nFTComponent.style = model.style; |
| | 315 | |
|
| 0 | 316 | | builderInWorldEntityData.nftComponent = nFTComponent; |
| 0 | 317 | | } |
| | 318 | | else |
| | 319 | | { |
| 0 | 320 | | ProtocolV2.GenericComponent entityComponentModel = new ProtocolV2.GenericComponent(); |
| 0 | 321 | | entityComponentModel.componentId = keyValuePair.Value.GetClassId(); |
| 0 | 322 | | entityComponentModel.data = keyValuePair.Value.GetModel(); |
| 0 | 323 | | entityComponentModel.classId = keyValuePair.Value.id; |
| | 324 | |
|
| 0 | 325 | | builderInWorldEntityData.sharedComponents.Add(entityComponentModel); |
| | 326 | | } |
| | 327 | | } |
| | 328 | |
|
| | 329 | |
|
| 4 | 330 | | return JsonConvert.SerializeObject(builderInWorldEntityData); |
| | 331 | | } |
| | 332 | |
|
| 3 | 333 | | public static EntityData ConvertJSONToEntityData(string json) { return JsonConvert.DeserializeObject<EntityData>(jso |
| | 334 | |
|
| | 335 | | public static List<BIWEntity> RemoveGroundEntities(List<BIWEntity> entityList) |
| | 336 | | { |
| 8 | 337 | | List<BIWEntity> newList = new List<BIWEntity>(); |
| | 338 | |
|
| 44 | 339 | | foreach (BIWEntity entity in entityList) |
| | 340 | | { |
| 14 | 341 | | if (entity.isFloor) |
| | 342 | | continue; |
| | 343 | |
|
| 14 | 344 | | newList.Add(entity); |
| | 345 | | } |
| | 346 | |
|
| 8 | 347 | | return newList; |
| | 348 | | } |
| | 349 | |
|
| | 350 | | public static List<BIWEntity> FilterEntitiesBySmartItemComponentAndActions(List<BIWEntity> entityList) |
| | 351 | | { |
| 0 | 352 | | List<BIWEntity> newList = new List<BIWEntity>(); |
| | 353 | |
|
| 0 | 354 | | foreach (BIWEntity entity in entityList) |
| | 355 | | { |
| 0 | 356 | | if (!entity.HasSmartItemComponent() || !entity.HasSmartItemActions()) |
| | 357 | | continue; |
| | 358 | |
|
| 0 | 359 | | newList.Add(entity); |
| | 360 | | } |
| | 361 | |
|
| 0 | 362 | | return newList; |
| | 363 | | } |
| | 364 | |
|
| | 365 | | public static void CopyRectTransform(RectTransform original, RectTransform rectTransformToCopy) |
| | 366 | | { |
| 0 | 367 | | original.anchoredPosition = rectTransformToCopy.anchoredPosition; |
| 0 | 368 | | original.anchorMax = rectTransformToCopy.anchorMax; |
| 0 | 369 | | original.anchorMin = rectTransformToCopy.anchorMin; |
| 0 | 370 | | original.offsetMax = rectTransformToCopy.offsetMax; |
| 0 | 371 | | original.offsetMin = rectTransformToCopy.offsetMin; |
| 0 | 372 | | original.sizeDelta = rectTransformToCopy.sizeDelta; |
| 0 | 373 | | original.pivot = rectTransformToCopy.pivot; |
| 0 | 374 | | } |
| | 375 | |
|
| | 376 | | public static WebRequestAsyncOperation MakeGetCall(string url, Action<string> functionToCall, Dictionary<string, str |
| | 377 | | { |
| 0 | 378 | | return Environment.i.platform.webRequest.Get( |
| | 379 | | url: url, |
| | 380 | | OnSuccess: (webRequestResult) => |
| | 381 | | { |
| 0 | 382 | | if (functionToCall != null) |
| | 383 | | { |
| 0 | 384 | | byte[] byteArray = webRequestResult.downloadHandler.data; |
| 0 | 385 | | string result = System.Text.Encoding.UTF8.GetString(byteArray); |
| 0 | 386 | | functionToCall?.Invoke(result); |
| | 387 | | } |
| 0 | 388 | | }, |
| | 389 | | OnFail: (webRequestResult) => |
| | 390 | | { |
| 0 | 391 | | Debug.Log(webRequestResult.error); |
| 0 | 392 | | }, |
| | 393 | | headers: headers); |
| | 394 | | } |
| | 395 | |
|
| | 396 | | public static void ConfigureEventTrigger(EventTrigger eventTrigger, EventTriggerType eventType, UnityAction<BaseEven |
| | 397 | | { |
| 3774 | 398 | | EventTrigger.Entry entry = new EventTrigger.Entry(); |
| 3774 | 399 | | entry.eventID = eventType; |
| 3774 | 400 | | entry.callback.AddListener(call); |
| 3774 | 401 | | eventTrigger.triggers.Add(entry); |
| 3774 | 402 | | } |
| | 403 | |
|
| 9154 | 404 | | public static void RemoveEventTrigger(EventTrigger eventTrigger, EventTriggerType eventType) { eventTrigger.triggers |
| | 405 | | } |