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