Changes

→‎Edit an image: update PatchMode.Overlay for alpha blending in newer versions
Line 346: Line 346:  
| ''(optional)'' How the image should be patched. The possible values...
 
| ''(optional)'' How the image should be patched. The possible values...
 
* <samp>PatchMode.Replace</samp> (default): erase the original content within the area before pasting in the new content;
 
* <samp>PatchMode.Replace</samp> (default): erase the original content within the area before pasting in the new content;
* <samp>PatchMode.Overlay</samp>: draw the new content over the original content, so the original content shows through any ''fully'' transparent pixels.
+
* <samp>PatchMode.Overlay</samp>: draw the new content over the original content, so the original content shows through transparent or semi-transparent pixels.
 
|}
 
|}
    
:; ExtendImage
 
:; ExtendImage
:: Extend the image if needed to fit the given size. Note that '''this is an expensive operation''', creates a new texture instance, and extending a spritesheet horizontally may cause game errors or bugs. For example:
+
:: Extend the image if needed to fit the given size. Note that '''resizing the image is an expensive operation''', creates a new texture instance, and extending a spritesheet horizontally may cause game errors or bugs. For example:
 
:: <syntaxhighlight lang="C#">
 
:: <syntaxhighlight lang="C#">
public void Edit<T>(IAssetData asset)
+
 
 +
e.Edit(asset =>
 
{
 
{
 
   var editor = asset.AsImage();
 
   var editor = asset.AsImage();
Line 358: Line 359:  
   // make sure the image is at least 1000px high
 
   // make sure the image is at least 1000px high
 
   editor.ExtendImage(minWidth: editor.Data.Width, minHeight: 1000);
 
   editor.ExtendImage(minWidth: editor.Data.Width, minHeight: 1000);
}
+
});
 
</syntaxhighlight>
 
</syntaxhighlight>
 
:: Available method arguments:
 
:: Available method arguments:
Line 382: Line 383:  
:: Edit or replace part of the map. This is basically a copy & paste operation, so the source map is applied over the loaded map. For example:
 
:: Edit or replace part of the map. This is basically a copy & paste operation, so the source map is applied over the loaded map. For example:
 
:: <syntaxhighlight lang="C#">
 
:: <syntaxhighlight lang="C#">
public void Edit<T>(IAssetData asset)
+
e.Edit(asset =>
 
{
 
{
 
   var editor = asset.AsMap();
 
   var editor = asset.AsMap();
Line 388: Line 389:  
   Map sourceMap = this.Helper.ModContent.Load<Map>("custom-map.tmx");
 
   Map sourceMap = this.Helper.ModContent.Load<Map>("custom-map.tmx");
 
   editor.PatchMap(sourceMap, targetArea: new Rectangle(30, 10, 20, 20));
 
   editor.PatchMap(sourceMap, targetArea: new Rectangle(30, 10, 20, 20));
}
+
});
 
</syntaxhighlight>
 
</syntaxhighlight>
   Line 422: Line 423:  
:: Extend the map if needed to fit the given size. Note that '''this is an expensive operation''' and resizes the map in-place. For example:
 
:: Extend the map if needed to fit the given size. Note that '''this is an expensive operation''' and resizes the map in-place. For example:
 
:: <syntaxhighlight lang="C#">
 
:: <syntaxhighlight lang="C#">
public void Edit<T>(IAssetData asset)
+
 
 +
e.Edit(asset =>
 
{
 
{
 
   var editor = asset.AsMap();
 
   var editor = asset.AsMap();
    
   // make sure the map is at least 256 tiles high
 
   // make sure the map is at least 256 tiles high
   editor.ExtendImage(minHeight: 256);
+
   editor.ExtendMap(minHeight: 256);
}
+
});
 
</syntaxhighlight>
 
</syntaxhighlight>
 
:: Available method arguments:
 
:: Available method arguments:
Line 476: Line 478:  
IRawTextureData image = this.Helper.ModContent.Load<IRawTextureData>("assets/image.png");
 
IRawTextureData image = this.Helper.ModContent.Load<IRawTextureData>("assets/image.png");
   −
for (int i = 0; i < image.Data.Length; i++)
+
int pixelCount = image.Width * image.Height;
 +
for (int i = 0; i < pixelCount; i++)
 
{
 
{
 
     Color color = image.Data[i];
 
     Color color = image.Data[i];
Line 486: Line 489:  
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
 +
(Note: while SMAPI's implementation of IRawTextureData is exactly as long as it needs to be, this may not be the case for implementations of IRawTextureData from mods.)
    
===Compare asset names===
 
===Compare asset names===
Line 510: Line 515:  
===Cache invalidation===
 
===Cache invalidation===
 
You can reload an asset by invalidating it from the cache. It will be reloaded next time the game requests it (and mods will have another chance to intercept it), and SMAPI will automatically update references to the asset in many cases. For example, this lets you change what clothes an NPC is wearing (by invalidating their cached sprites or portraits).
 
You can reload an asset by invalidating it from the cache. It will be reloaded next time the game requests it (and mods will have another chance to intercept it), and SMAPI will automatically update references to the asset in many cases. For example, this lets you change what clothes an NPC is wearing (by invalidating their cached sprites or portraits).
 +
 +
Please be aware that in some cases a localized version of the asset will be cached and simply invalidating the default asset will not work for any language other than english.
    
Reloading assets is fairly expensive, so use this API judiciously to avoid impacting game performance. Definitely don't do this every update tick.
 
Reloading assets is fairly expensive, so use this API judiciously to avoid impacting game performance. Definitely don't do this every update tick.
translators
8,404

edits