public void SelectSkin(string skinId) { if (CarNode == null || _loadingCar != null) { _selectSkin = skinId; _selectSkinLater = true; return; } CarNode.SelectSkin(DeviceContextHolder, skinId); }
public async Task SetCarAsync(CarObject car, string skinId = Kn5RenderableCar.DefaultSkin, CancellationToken cancellationToken = default(CancellationToken)) { LicensePlateSelected = false; try { _loadingCar = car; if (_carWrapper == null) { _car = car; return; } if (car == null) { ClearExisting(); _car = null; CarNode = null; Scene.UpdateBoundingBox(); return; } Kn5RenderableCar loaded = null; FlatMirror reflection = null; var previous = _previousCars.FirstOrDefault(x => x.Id == car.Id); if (previous != null) { _previousCars.Remove(previous); ClearExisting(); _carWrapper.AddRange(previous.Objects); _car = car; loaded = previous.Objects.OfType <Kn5RenderableCar>().First(); loaded.LightsEnabled = CarNode?.LightsEnabled == true; CarNode = loaded; if (_selectSkinLater) { CarNode.SelectSkin(DeviceContextHolder, _selectSkin); _selectSkinLater = false; } else { CarNode.SelectSkin(DeviceContextHolder, skinId); } Scene.UpdateBoundingBox(); return; } await Task.Run(() => { var carKn5 = Kn5.FromFile(FileUtils.GetMainCarFilename(car.Location)); if (cancellationToken.IsCancellationRequested) { return; } loaded = new Kn5RenderableCar(carKn5, car.Location, Matrix.Identity, _selectSkinLater ? _selectSkin : skinId); _selectSkinLater = false; if (cancellationToken.IsCancellationRequested) { return; } loaded.LightsEnabled = CarNode?.LightsEnabled == true; if (cancellationToken.IsCancellationRequested) { return; } loaded.Draw(DeviceContextHolder, null, SpecialRenderMode.InitializeOnly); if (cancellationToken.IsCancellationRequested) { return; } reflection = new FlatMirror(loaded, new Plane(Vector3.Zero, Vector3.UnitY)); reflection.Draw(DeviceContextHolder, null, SpecialRenderMode.InitializeOnly); }); if (cancellationToken.IsCancellationRequested || _loadingCar != car) { loaded?.Dispose(); return; } ClearExisting(); _carWrapper.Add(loaded); _carWrapper.Add(reflection); _car = car; CarNode = loaded; _selectSkin = null; Scene.UpdateBoundingBox(); } catch (Exception e) { MessageBox.Show(e.ToString()); throw; } finally { if (ReferenceEquals(_loadingCar, car)) { _loadingCar = null; } } }