/// <inheritdoc/> public override void OnTool(ToolEvent e, IToolContext context) { switch (e.Type) { case EventType.MouseDown: ToolBase fallbackRestoreTool; Brush pickedBrush = null; if (ToolUtility.ActivePlop != null && ToolUtility.ActivePlop.Brush != null) { fallbackRestoreTool = ToolManager.Instance.Find <PlopTool>(); // Get plop at pointer. pickedBrush = ToolUtility.ActivePlop.Brush; // Pick rotation from tile also! ToolUtility.Rotation = ToolUtility.ActivePlop.PaintedRotation; } else { fallbackRestoreTool = ToolManager.DefaultPaintTool; // Get tile at pointer. var tile = context.TileSystem.GetTile(e.MousePointerTileIndex); if (tile != null) { pickedBrush = tile.brush; // Pick rotation from tile also! ToolUtility.Rotation = tile.PaintedRotation; } } // Select brush in tool window and force auto scroll. if (e.IsLeftButtonPressed) { ToolUtility.SelectedBrush = pickedBrush; ToolUtility.RevealBrush(pickedBrush); } else { ToolUtility.SelectedBrushSecondary = pickedBrush; } ToolUtility.RepaintBrushPalette(); // Switch to previous tool or the "Paint" tool. var toolManager = ToolManager.Instance; if (toolManager.PreviousTool != null && toolManager.PreviousTool != this) { toolManager.CurrentTool = toolManager.PreviousTool; } else { toolManager.CurrentTool = fallbackRestoreTool; } break; } }
private void CreateOrientedBrush(string brushName) { var newOrientedBrush = BrushUtility.CreateOrientedBrush(brushName); ToolUtility.ShowBrushInDesigner(newOrientedBrush); ToolUtility.RepaintBrushPalette(); }
private void CreateAliasBrush(string brushName, Brush targetBrush) { var newAliasBrush = BrushUtility.CreateAliasBrush(brushName, targetBrush); ToolUtility.ShowBrushInDesigner(newAliasBrush); ToolUtility.RepaintBrushPalette(); }
private void CreateAutotileTileset(string tilesetName) { // Ensure that autotile artwork is re-expanded before proceeding to avoid // ignoring changes that have been made to user input. this.ExpandAutotileArtwork(s_BorderSize); // Create folder for autotile brush assets. string tilesetDirectoryName = tilesetName + " Autotile"; string tilesetDirectoryPath = AssetDatabase.GenerateUniqueAssetPath(BrushUtility.GetBrushAssetPath() + tilesetDirectoryName); Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/" + tilesetDirectoryPath); // Create material for tileset. var atlasTexture = EditorInternalUtility.SavePngAsset(tilesetDirectoryPath + "/atlas.png", this.atlasTexture); var atlasMaterial = BrushUtility.CreateTilesetMaterial(atlasTexture, this.enableAlphaBlending); AssetDatabase.CreateAsset(atlasMaterial, tilesetDirectoryPath + "/atlas.mat"); AssetDatabase.ImportAsset(tilesetDirectoryPath + "/atlas.mat"); // Calculate metrics for tileset. var tilesetMetrics = new TilesetMetrics(atlasTexture, this.tileWidth, this.tileHeight, s_BorderSize, s_Delta); // Create tileset. var autotileTileset = ScriptableObject.CreateInstance <AutotileTileset>(); autotileTileset.Initialize(s_SelectedAutotileLayout, s_InnerJoins, atlasMaterial, atlasTexture, tilesetMetrics); autotileTileset.rawTexture = this.autotileTexture; autotileTileset.procedural = true; autotileTileset.ForceClampEdges = s_EnableClampEdges; Object.DestroyImmediate(this.atlasTexture); this.atlasTexture = null; // Save tileset and its material to asset file. string assetPath = tilesetDirectoryPath + "/" + tilesetName + ".asset"; AssetDatabase.CreateAsset(autotileTileset, assetPath); AssetDatabase.ImportAsset(assetPath); // Ensure that changes are persisted immediately. AssetDatabase.SaveAssets(); // Make sure that "Create Brushes" tab is shown. TilesetDesigner.s_SelectedTab = 0; ToolUtility.ShowTilesetInDesigner(autotileTileset); ToolUtility.RepaintBrushPalette(); }
/// <summary> /// Refresh preview image for specified brush and also refresh preview image /// for brushes which depend upon the specified brush. /// </summary> /// <remarks> /// <para>At the moment this only includes alias brushes which target the /// specified brush. In the future this may be updated to include oriented /// brushes which nest the specified brush.</para> /// </remarks> /// <param name="brush">The brush.</param> internal static void RefreshPreviewIncludingDependencies(Brush brush) { BrushAssetRecord record = BrushDatabase.Instance.FindRecord(brush); if (record != null) { AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(brush)); // Refresh preview any alias brushes which target this brush. foreach (var dependencyRecord in FindDependencyBrushes(record.Brush)) { RefreshPreview(dependencyRecord.Brush); } ToolUtility.RepaintBrushPalette(); } }
private void OnRename(string newName) { try { this.inputTilesetName = BrushDatabase.Instance.RenameTileset(this.Tileset, this.inputTilesetName); // Defocus name input field. GUIUtility.keyboardControl = 0; ToolUtility.RepaintBrushPalette(); } catch (ArgumentException ex) { EditorUtility.DisplayDialog( TileLang.ParticularText("Error", " Was unable to rename tileset"), ex.Message, TileLang.ParticularText("Action", "OK") ); } }
private void SelectBrushByIndex(int index) { if (index < 0 && this.BrushListModel.View == BrushListView.Tileset) { this.SelectedObject = null; this.BrushListModel.ScrollPosition = 0f; ToolUtility.RepaintBrushPalette(); this.Repaint(); } else { Brush nextBrush = this.BrushListModel.Records[index].Brush; if (nextBrush != null) { this.SelectedObject = nextBrush; } } }
private void OnUseDefaults() { if (EditorUtility.DisplayDialog( TileLang.ParticularText("Action", "Use Default Preferences"), TileLang.Text("Would you like to use the default preferences?"), TileLang.ParticularText("Action", "Yes"), TileLang.ParticularText("Action", "No") )) { RtsPreferences.ResetToDefaultValues(); // Refresh editor windows. ToolUtility.RepaintToolPalette(); ToolUtility.RepaintBrushPalette(); // Refresh all scene views. SceneView.RepaintAll(); } }
private void CreateTileset(string tilesetName) { // Create folder for atlas assets. string atlasFolder = AssetDatabase.GenerateUniqueAssetPath(BrushUtility.GetBrushAssetPath() + tilesetName); Directory.CreateDirectory(Directory.GetCurrentDirectory() + "/" + atlasFolder); // Create material for tileset. var atlasMaterial = BrushUtility.CreateTilesetMaterial(this.tilesetTexture, this.alpha); AssetDatabase.CreateAsset(atlasMaterial, atlasFolder + "/atlas.mat"); AssetDatabase.ImportAsset(atlasFolder + "/atlas.mat"); // Use edge correction preset? if (this.edgeCorrectionPreset != EdgeCorrectionPreset.Custom) { this.borderSize = 0; this.delta = (this.edgeCorrectionPreset == EdgeCorrectionPreset.InsetUVs) ? 0.5f : 0f; } // Calculate metrics for tileset. this.RecalculateMetrics(); // Create tileset. var tileset = ScriptableObject.CreateInstance <Tileset>(); tileset.Initialize(atlasMaterial, this.tilesetTexture, this.tilesetMetrics); tileset.procedural = this.procedural; // Save tileset and its material to asset file. string assetPath = atlasFolder + "/" + tilesetName + ".asset"; AssetDatabase.CreateAsset(tileset, assetPath); AssetDatabase.ImportAsset(assetPath); // Ensure that changes are persisted immediately. AssetDatabase.SaveAssets(); // Make sure that "Create Brushes" tab is shown. TilesetDesigner.s_SelectedTab = 0; ToolUtility.ShowTilesetInDesigner(tileset); ToolUtility.RepaintBrushPalette(); }
private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { if (!s_EnableScanner) { return; } // Check for new brush/tileset assets. if (ContainsNewBrushOrTileset(importedAssets)) { BrushDatabase.Instance.Rescan(); ToolUtility.RepaintBrushPalette(); } // Check for deleted brush/tileset assets. BrushDatabase.Instance.ClearMissingRecords(); ToolUtility.RepaintBrushPalette(); DesignerWindow.RepaintWindow(); }
/// <summary> /// Delete tileset and associated assets. /// </summary> /// <remarks> /// <para>Can only delete tileset assets of type <see cref="Tileset"/> /// or <see cref="AutotileTileset"/>.</para> /// </remarks> /// <param name="tileset">The tileset.</param> /// <param name="flags">Optional deletion flags.</param> /// <exception cref="System.ArgumentNullException"> /// If <paramref name="tileset"/> is <c>null</c>. /// </exception> /// <exception cref="System.ArgumentException"> /// If attempting to delete an unsuported type of tileset. /// </exception> /// <exception cref="System.Exception"> /// If tileset record was not found for specified tileset. /// </exception> public static void DeleteTileset(Tileset tileset, DeleteTilesetFlag flags = 0) { if (tileset == null) { throw new ArgumentNullException("tileset"); } Type tilesetType = tileset.GetType(); if (tilesetType != typeof(Tileset) && tilesetType != typeof(AutotileTileset)) { throw new ArgumentException("This utility function cannot delete tileset of type '" + tilesetType.FullName + "'", "tileset"); } var tilesetRecord = BrushDatabase.Instance.FindTilesetRecord(tileset); if (tilesetRecord == null) { throw new Exception("Tileset record was not found for '" + tileset.name + "'"); } var autotileTileset = tileset as AutotileTileset; string assetPath = tilesetRecord.AssetPath; string assetFolderPath = assetPath.Substring(0, assetPath.LastIndexOf("/")); string assetFolderPathBase = assetFolderPath + "/"; int slashCount = assetFolderPathBase.CountSubstrings('/'); // Note: Only delete assets where: // - Asset path begins with base path of tileset asset. // - Asset path includes the same number of slashes (to avoid deleting // those that are nested within folders). // Delete associated texture? if (autotileTileset != null && (flags & DeleteTilesetFlag.DeleteTexture) == DeleteTilesetFlag.DeleteTexture && autotileTileset.AtlasTexture != null) { string t = AssetDatabase.GetAssetPath(tileset.AtlasTexture); if (t.StartsWith(assetFolderPathBase) && slashCount == t.CountSubstrings('/')) { AssetDatabase.DeleteAsset(t); } } // Delete associated material? if ((flags & DeleteTilesetFlag.DeleteMaterial) == DeleteTilesetFlag.DeleteMaterial && tileset.AtlasMaterial != null) { string t = AssetDatabase.GetAssetPath(tileset.AtlasMaterial); if (t.StartsWith(assetFolderPathBase) && slashCount == t.CountSubstrings('/')) { AssetDatabase.DeleteAsset(t); } } // Delete associated mesh assets? if ((flags & DeleteTilesetFlag.DeleteMeshAssets) == DeleteTilesetFlag.DeleteMeshAssets && tileset.tileMeshAsset != null) { string t = AssetDatabase.GetAssetPath(tileset.tileMeshAsset); if (t.StartsWith(assetFolderPathBase) && slashCount == t.CountSubstrings('/')) { AssetDatabase.DeleteAsset(t); } } // Delete tileset and contained brush assets. AssetDatabase.DeleteAsset(assetPath); // Delete tileset folder if it is empty. if (IsDirectoryEmpty(Directory.GetCurrentDirectory() + "/" + assetFolderPath)) { AssetDatabase.DeleteAsset(assetFolderPath); } ToolUtility.RepaintBrushPalette(); DesignerWindow.RepaintWindow(); }
/// <summary> /// Selects tool for use within custom system. /// </summary> /// <param name="tool">Tool that is to be selected. Specify `null` to /// revert to previous Unity tool.</param> /// <returns> /// Instance of tool that was selected. /// </returns> public ToolBase SelectTool(ToolBase tool) { if (tool != null) { this.AutoActivateTileSystem(); if (ToolUtility.ActiveTileSystem == null) { // Deselect tool because no tile system is selected! tool = null; } else if (this.currentTool == null) { // Reveal active tile system in scene palette when tool is first activated. // Let's not keep scrolling to the active tile system each time the user // selects another tool since this can be quite annoying! ToolUtility.RevealTileSystem(ToolUtility.ActiveTileSystem, false); // Automatically show tool palette upon activating tool if specified // in user preferences. if (RtsPreferences.AutoShowToolPalette) { ToolUtility.ShowToolPalette(false); } } } if (tool != null) { // Should current Unity tool be preserved whilst custom tool is used? if (UnityEditor.Tools.current != Tool.None) { this.previousUnityTool = UnityEditor.Tools.current; UnityEditor.Tools.current = Tool.None; } } else if (UnityEditor.Tools.current == Tool.None) { // Revert back to former Unity tool because tool has been deselected! UnityEditor.Tools.current = this.previousUnityTool; } // Will tool selection actually change? if (tool == this.currentTool) { return(tool); } // Reset active plop reference to avoid issues when switching tools. ToolUtility.ActivePlop = null; if (this.currentTool != null) { this.previousTool = this.currentTool; } // Switch to specified tool. var oldTool = this.currentTool; this.currentTool = tool; if (oldTool != null) { oldTool.OnDisable(); } if (tool != null) { if (oldTool == null) { // No tool was active prior to using this method to select a tool. this.BeginEditMode(); } tool.OnEnable(); } else if (oldTool != null) { // A tool was active prior to using this method to deselect tool. this.ExitEditMode(); } // Raise event for tool change. if (this.ToolChanged != null) { try { this.ToolChanged(oldTool, this.currentTool); } catch (Exception ex) { Debug.LogException(ex); } } ToolUtility.RepaintToolPalette(); // Brush palette only needs to be repainted when tool is first selected or deselected. if (oldTool == null || tool == null) { ToolUtility.RepaintBrushPalette(); } SceneView.RepaintAll(); return(tool); }