diff --git a/BloonsTD6 Mod Helper/Api/Commands/ExportImageCommand.cs b/BloonsTD6 Mod Helper/Api/Commands/ExportImageCommand.cs new file mode 100644 index 000000000..7d560fd3d --- /dev/null +++ b/BloonsTD6 Mod Helper/Api/Commands/ExportImageCommand.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.IO; +using BTD_Mod_Helper.Api.Helpers; +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; +namespace BTD_Mod_Helper.Api.Commands; + +internal class ExportImageCommand : ModCommand +{ + public override string Command => "image"; + public override string Help => "Exports UI images raycasted from the current mouse position"; + + public override bool Execute(ref string resultText) + { + try + { + var exported = new List(); + var raycastResults = new Il2CppSystem.Collections.Generic.List(); + EventSystem.current.RaycastAll(new PointerEventData(EventSystem.current) + { + position = Input.mousePosition + }, raycastResults); + + var imagesFolder = Path.Combine(FileIOHelper.sandboxRoot, "Images"); + + foreach (var result in raycastResults) + { + if (result.gameObject.Is(out var gameObject) && + gameObject.HasComponent(out Image image) && + image.sprite != null) + { + var path = Path.Combine(imagesFolder, image.sprite.name + ".png"); + if (image.sprite.TrySaveToPNG(path)) + { + ModHelper.Msg($"Exported {path}"); + exported.Add(image.sprite.name); + } + } + } + + resultText = $"Exported {exported.Join()} to {imagesFolder}"; + + } + catch (Exception e) + { + resultText = e.Message; + ModHelper.Warning(e); + } + + return true; + } +} \ No newline at end of file diff --git a/BloonsTD6 Mod Helper/Extensions/UnityExtensions/SpriteExt.cs b/BloonsTD6 Mod Helper/Extensions/UnityExtensions/SpriteExt.cs index f2cd46d5d..115a44f08 100644 --- a/BloonsTD6 Mod Helper/Extensions/UnityExtensions/SpriteExt.cs +++ b/BloonsTD6 Mod Helper/Extensions/UnityExtensions/SpriteExt.cs @@ -21,7 +21,7 @@ public static void SetTexture(this Sprite sprite, Texture2D newTexture) /// /// Attempts to save a Sprite to a PNG at the given filePath, even if it isn't marked as readable /// - public static void TrySaveToPNG(this Sprite sprite, string filePath) + public static bool TrySaveToPNG(this Sprite sprite, string filePath) { try { @@ -39,10 +39,12 @@ public static void TrySaveToPNG(this Sprite sprite, string filePath) RenderTexture.ReleaseTemporary(tmp); var bytes = myTexture2D.EncodeToPNG(); File.WriteAllBytes(filePath, bytes); + return true; } catch (Exception e) { ModHelper.Warning(e); + return false; } } diff --git a/BloonsTD6 Mod Helper/LATEST.md b/BloonsTD6 Mod Helper/LATEST.md index 2758f5e68..16f7e01fd 100644 --- a/BloonsTD6 Mod Helper/LATEST.md +++ b/BloonsTD6 Mod Helper/LATEST.md @@ -1,4 +1,5 @@ - Added a ModBloonOverlay class for making custom Bloon Overlays - NOTE: Due to a MelonLoader bug, these won't properly display unless you're on ML 0.6.6 or higher - Accounted for a Unity bug that was affecting the internal durations of embedded AudioClips -- Added a Sprite.TrySaveToPNG() extension like the one for Textures except accounting for position and size within a larger texture atlas \ No newline at end of file +- Added a Sprite.TrySaveToPNG() extension like the one for Textures except accounting for position and size within a larger texture atlas +- Added `export image` console commands that exports all UI images underneath your mouse cursor to png files \ No newline at end of file diff --git a/Documentation/BTD_Mod_Helper.Api.Display.ModBloonOverlay.md b/Documentation/BTD_Mod_Helper.Api.Display.ModBloonOverlay.md index f4d52f9db..8a448dd0e 100644 --- a/Documentation/BTD_Mod_Helper.Api.Display.ModBloonOverlay.md +++ b/Documentation/BTD_Mod_Helper.Api.Display.ModBloonOverlay.md @@ -47,7 +47,7 @@ The base Bloon Overlay to copy from.
These come from the [Il2CppAssets.Scripts.Models.Towers.Projectiles.ProjectileBehaviorWithOverlayModel.overlayType](https://docs.microsoft.com/en-us/dotnet/api/Il2CppAssets.Scripts.Models.Towers.Projectiles.ProjectileBehaviorWithOverlayModel.overlayType 'Il2CppAssets.Scripts.Models.Towers.Projectiles.ProjectileBehaviorWithOverlayModel.overlayType') fields of certain projectile behavior models
-To not copy from any Base Overlay, override this to be null/empty and modify [BaseDisplay](BTD_Mod_Helper.Api.Display.ModDisplay.md#BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplay 'BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplay') or [BaseDisplayReference](BTD_Mod_Helper.Api.Display.ModDisplay.md#BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplayReference 'BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplayReference') instead +To not copy from any Base Overlay, keep this as null/empty and modify [BaseDisplay](BTD_Mod_Helper.Api.Display.ModDisplay.md#BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplay 'BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplay') or [BaseDisplayReference](BTD_Mod_Helper.Api.Display.ModDisplay.md#BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplayReference 'BTD_Mod_Helper.Api.Display.ModDisplay.BaseDisplayReference') instead ```csharp public virtual string BaseOverlay { get; } @@ -165,4 +165,17 @@ public virtual void Apply(ProjectileBehaviorWithOverlayModel projectileBehaviorW `projectileBehaviorWithOverlayModel` [Il2CppAssets.Scripts.Models.Towers.Projectiles.ProjectileBehaviorWithOverlayModel](https://docs.microsoft.com/en-us/dotnet/api/Il2CppAssets.Scripts.Models.Towers.Projectiles.ProjectileBehaviorWithOverlayModel 'Il2CppAssets.Scripts.Models.Towers.Projectiles.ProjectileBehaviorWithOverlayModel') -model to add to \ No newline at end of file +model to add to + + + +## ModBloonOverlay.Load() Method + +Load different instances of this type for each difference BloonOverlayClass within [BloonOverlayClasses](BTD_Mod_Helper.Api.Display.ModBloonOverlay.md#BTD_Mod_Helper.Api.Display.ModBloonOverlay.BloonOverlayClasses 'BTD_Mod_Helper.Api.Display.ModBloonOverlay.BloonOverlayClasses') + +```csharp +public override System.Collections.Generic.IEnumerable Load(); +``` + +#### Returns +[System.Collections.Generic.IEnumerable<](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')[ModContent](BTD_Mod_Helper.Api.ModContent.md 'BTD_Mod_Helper.Api.ModContent')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1') \ No newline at end of file diff --git a/Documentation/BTD_Mod_Helper.Extensions.SpriteExt.md b/Documentation/BTD_Mod_Helper.Extensions.SpriteExt.md index 033324879..8d41d23a6 100644 --- a/Documentation/BTD_Mod_Helper.Extensions.SpriteExt.md +++ b/Documentation/BTD_Mod_Helper.Extensions.SpriteExt.md @@ -29,4 +29,26 @@ public static void SetTexture(this Sprite sprite, Texture2D newTexture); -`newTexture` [UnityEngine.Texture2D](https://docs.microsoft.com/en-us/dotnet/api/UnityEngine.Texture2D 'UnityEngine.Texture2D') \ No newline at end of file +`newTexture` [UnityEngine.Texture2D](https://docs.microsoft.com/en-us/dotnet/api/UnityEngine.Texture2D 'UnityEngine.Texture2D') + + + +## SpriteExt.TrySaveToPNG(this Sprite, string) Method + +Attempts to save a Sprite to a PNG at the given filePath, even if it isn't marked as readable + +```csharp +public static bool TrySaveToPNG(this Sprite sprite, string filePath); +``` +#### Parameters + + + +`sprite` [UnityEngine.Sprite](https://docs.microsoft.com/en-us/dotnet/api/UnityEngine.Sprite 'UnityEngine.Sprite') + + + +`filePath` [System.String](https://docs.microsoft.com/en-us/dotnet/api/System.String 'System.String') + +#### Returns +[System.Boolean](https://docs.microsoft.com/en-us/dotnet/api/System.Boolean 'System.Boolean') \ No newline at end of file