Skip to content

Commit

Permalink
Add ExportImageCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
doombubbles committed Oct 24, 2024
1 parent 320588d commit ac7d1c2
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 5 deletions.
54 changes: 54 additions & 0 deletions BloonsTD6 Mod Helper/Api/Commands/ExportImageCommand.cs
Original file line number Diff line number Diff line change
@@ -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<ExportCommand>
{
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<string>();
var raycastResults = new Il2CppSystem.Collections.Generic.List<RaycastResult>();
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;
}
}
4 changes: 3 additions & 1 deletion BloonsTD6 Mod Helper/Extensions/UnityExtensions/SpriteExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void SetTexture(this Sprite sprite, Texture2D newTexture)
/// <summary>
/// Attempts to save a Sprite to a PNG at the given filePath, even if it isn't marked as readable
/// </summary>
public static void TrySaveToPNG(this Sprite sprite, string filePath)
public static bool TrySaveToPNG(this Sprite sprite, string filePath)
{
try
{
Expand All @@ -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;
}
}

Expand Down
3 changes: 2 additions & 1 deletion BloonsTD6 Mod Helper/LATEST.md
Original file line number Diff line number Diff line change
@@ -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
- 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
17 changes: 15 additions & 2 deletions Documentation/BTD_Mod_Helper.Api.Display.ModBloonOverlay.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ The base Bloon Overlay to copy from.
<br/>
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
<br/>
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; }
Expand Down Expand Up @@ -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
model to add to

<a name='BTD_Mod_Helper.Api.Display.ModBloonOverlay.Load()'></a>

## 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<BTD_Mod_Helper.Api.ModContent> Load();
```

#### Returns
[System.Collections.Generic.IEnumerable&lt;](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')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')
Expand Down
24 changes: 23 additions & 1 deletion Documentation/BTD_Mod_Helper.Extensions.SpriteExt.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,26 @@ public static void SetTexture(this Sprite sprite, Texture2D newTexture);
<a name='BTD_Mod_Helper.Extensions.SpriteExt.SetTexture(thisSprite,Texture2D).newTexture'></a>

`newTexture` [UnityEngine.Texture2D](https://docs.microsoft.com/en-us/dotnet/api/UnityEngine.Texture2D 'UnityEngine.Texture2D')
`newTexture` [UnityEngine.Texture2D](https://docs.microsoft.com/en-us/dotnet/api/UnityEngine.Texture2D 'UnityEngine.Texture2D')
<a name='BTD_Mod_Helper.Extensions.SpriteExt.TrySaveToPNG(thisSprite,string)'></a>

## 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

<a name='BTD_Mod_Helper.Extensions.SpriteExt.TrySaveToPNG(thisSprite,string).sprite'></a>

`sprite` [UnityEngine.Sprite](https://docs.microsoft.com/en-us/dotnet/api/UnityEngine.Sprite 'UnityEngine.Sprite')
<a name='BTD_Mod_Helper.Extensions.SpriteExt.TrySaveToPNG(thisSprite,string).filePath'></a>

`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')

0 comments on commit ac7d1c2

Please sign in to comment.