private VehicleEffectsDefinition GetCleanedDefinition() { if (m_definition == null) { m_definition = new VehicleEffectsDefinition(); } var cleanedDef = m_definition.Copy(); for (int i = cleanedDef.Vehicles.Count - 1; i >= 0; i--) { Logging.LogWarning(i + " " + cleanedDef.Vehicles[i]); string name = cleanedDef.Vehicles[i].ApplyToTrailersOnly ? cleanedDef.Vehicles[i].Name + ALL_TRAILER_POSTFIX : cleanedDef.Vehicles[i].Name; bool inScene = false; foreach (var vehicle in m_vehicles) { if (name == vehicle) { inScene = true; break; } } if (!inScene || cleanedDef.Vehicles[i].Effects.Count < 1) { Logging.Log("Removing definition for " + cleanedDef.Vehicles[i].Name + "\nInScene: " + inScene.ToString()); cleanedDef.Vehicles.RemoveAt(i); } } return(cleanedDef); }
private void AddMissingSceneVehicles() { if (m_definition == null) { m_definition = new VehicleEffectsDefinition(); } // Loop trough scene vehicles for (int i = 0; i < m_vehicles.Length; i++) { if (FindVehicleDefinition(m_vehicles[i]) == null) { var vehicleDef = new VehicleEffectsDefinition.Vehicle(); Logging.Log("Adding definition for " + m_vehicles[i]); if (i == 1) { // Special case for global trailer option vehicleDef.Name = m_vehicles[0]; vehicleDef.ApplyToTrailersOnly = true; } else { vehicleDef.Name = m_vehicles[i]; } m_definition.Vehicles.Add(vehicleDef); } } }
public void Show(string packageName, VehicleEffectsDefinition definition, OnSaveFinished callback = null) { Show(true); m_textField.text = packageName; m_callback = callback; m_definition = definition; }
public new void Hide() { m_callback?.Invoke(); isVisible = false; m_definition = null; m_callback = null; }
void OnLoad() { VehicleEffectsDefinition definition = null; try { var textReader = new StringReader(m_textField.text.Trim()); var xmlSerializer = new XmlSerializer(typeof(VehicleEffectsDefinition)); definition = (VehicleEffectsDefinition)xmlSerializer.Deserialize(textReader); } catch (Exception e) { UIView.library.ShowModal <ExceptionPanel>("ExceptionPanel").SetMessage("Error parsing definition", e.Message + "\r\n" + e.StackTrace, true); Logging.LogException(e); return; } m_callback?.Invoke(definition); m_callback = null; Hide(); }
public bool ApplyPreview(VehicleEffectsDefinition definition, string packageName) { RevertPreview(); m_parseErrors = new HashSet <string>(); VehicleInfo vehicleInfo = ToolsModifierControl.toolController.m_editPrefabInfo as VehicleInfo; if (vehicleInfo != null) { // Find the VehicleInfo's currently in the scene and store them in a dictionary Dictionary <string, VehicleInfo> infoDict = new Dictionary <string, VehicleInfo>(); infoDict.Add(vehicleInfo.name, vehicleInfo); if (vehicleInfo.m_trailers != null) { foreach (var trailer in vehicleInfo.m_trailers) { if (!infoDict.ContainsKey(trailer.m_info.name)) { infoDict.Add(trailer.m_info.name, trailer.m_info); } } } if (definition?.Vehicles == null || definition.Vehicles.Count == 0) { m_parseErrors.Add("Previewer - vehicleEffectDef is null or empty."); } else { m_isApplied = true; foreach (var vehicleDef in definition.Vehicles) { // Check if the vehicle of this definition is in the scene and if so, apply it VehicleInfo info; if (infoDict.TryGetValue(vehicleDef.Name, out info)) { VehicleEffectsMod.ParseVehicleDefinition(vehicleDef, packageName, ref m_changes, ref m_parseErrors, false, info); } else { m_parseErrors.Add("Prefab for " + vehicleDef.Name + " not found!"); m_parseErrors.Add(infoDict.Keys.Aggregate("List of prefabs:\n", (current, error) => current + error + "\n")); } } } } else { m_parseErrors.Add("No prefab found."); } if (m_parseErrors?.Count > 0) { var errorMessage = m_parseErrors.Aggregate("Error while parsing vehicle effect definition preview.\n" + "List of errors:\n", (current, error) => current + (error + '\n')); UIView.library.ShowModal <ExceptionPanel>("ExceptionPanel").SetMessage("Vehicle Effects", errorMessage, false); } return(m_isApplied); }
private void CreateComponents() { int headerHeight = 40; // Label UILabel label = AddUIComponent <UILabel>(); label.text = "Effects"; label.relativePosition = new Vector3(WIDTH / 2 - label.width / 2, 10); // Drag handle UIDragHandle handle = AddUIComponent <UIDragHandle>(); handle.target = this; handle.constrainToScreen = true; handle.width = WIDTH; handle.height = headerHeight; handle.relativePosition = Vector3.zero; // close button UIButton closeButton = UIUtils.CreateButton(this); closeButton.size = new Vector2(30, 30); closeButton.normalBgSprite = "buttonclose"; closeButton.hoveredBgSprite = "buttonclosehover"; closeButton.pressedBgSprite = "buttonclosepressed"; closeButton.relativePosition = new Vector3(WIDTH - 35, 5); closeButton.eventClicked += (c, p) => { isVisible = false; }; // Dropdown m_vehicleDropdown = UIUtils.CreateDropDown(this); m_vehicleDropdown.width = 400; m_vehicleDropdown.relativePosition = new Vector3(10, headerHeight + 10); m_vehicleDropdown.eventSelectedIndexChanged += OnDropdownIndexChanged; m_vehicleDropdown.selectedIndex = -1; float listHeight = HEIGHT - headerHeight - m_vehicleDropdown.height - 30 - 50; float listWidth = 300; float padding = 10; float optionsWidth = WIDTH - (listWidth - 3 * padding); float listTop = headerHeight + 20 + m_vehicleDropdown.height; // Fastlist for Vehicle Effects definitions m_veEffectList = UIFastList.Create <UIEffectDefinitionRow>(this); m_veEffectList.backgroundSprite = "UnlockingPanel"; m_veEffectList.width = listWidth; m_veEffectList.height = listHeight; m_veEffectList.relativePosition = new Vector3(padding, listTop); m_veEffectList.canSelect = true; m_veEffectList.eventSelectedIndexChanged += OnVEEffectSelectionChanged; // Create options panel m_optionsPanel = AddUIComponent <UIEffectOptionsPanel>(); m_optionsPanel.width = optionsWidth; m_optionsPanel.height = listHeight; m_optionsPanel.relativePosition = new Vector3(listWidth + padding * 2, listTop); m_optionsPanel.m_mainPanel = this; m_optionsPanel.CreateComponents(); float footerX = 10; // Button to add effects (footer) m_addEffectButton = UIUtils.CreateButton(this); m_addEffectButton.text = "Add effect"; m_addEffectButton.width = 120; m_addEffectButton.relativePosition = new Vector3(footerX, HEIGHT - 40); m_addEffectButton.eventClicked += (c, b) => { if (!m_effectListPanel.isVisible) { m_effectListPanel.Show((info) => { VehicleEffectsDefinition.Effect effect = new VehicleEffectsDefinition.Effect(); effect.Name = info.name; AddEffect(effect); }); } }; footerX += m_addEffectButton.width + 10; // Button to save definition (footer) m_saveDefinitionButton = UIUtils.CreateButton(this); m_saveDefinitionButton.text = "Save XML"; m_saveDefinitionButton.width = 120; m_saveDefinitionButton.relativePosition = new Vector3(footerX, HEIGHT - 40); m_saveDefinitionButton.eventClicked += (c, b) => { if (!m_savePanel.isVisible) { m_savePanel.Show(Util.GetPackageName(m_vehicles[0]), GetCleanedDefinition()); } }; footerX += m_saveDefinitionButton.width + 10; // Button to load definition (footer) m_loadDefinitionButton = UIUtils.CreateButton(this); m_loadDefinitionButton.text = "Load XML"; m_loadDefinitionButton.width = 120; m_loadDefinitionButton.relativePosition = new Vector3(footerX, HEIGHT - 40); m_loadDefinitionButton.eventClicked += (c, b) => { if (!m_loadPanel.isVisible) { m_loadPanel.Show((definition) => { if (definition != null) { m_definition = definition; AddMissingSceneVehicles(); PopulateVEList(); } }); } }; footerX += m_loadDefinitionButton.width + 10; // Button to preview definition (footer) m_previewButton = UIUtils.CreateButton(this); m_previewButton.text = "Enable Preview"; m_previewButton.width = 180; m_previewButton.relativePosition = new Vector3(WIDTH - 10 - m_previewButton.width, HEIGHT - 40); m_previewButton.eventClicked += (c, b) => { if (m_previewer.IsPreviewing) { m_previewButton.text = "Enable Preview"; m_previewer.RevertPreview(); } else { if (m_previewer.ApplyPreview(GetCleanedDefinition(), "Vehicle Effects Previewer")) { m_previewButton.text = "Disable Preview"; } } }; }