Skip to content

Commit 4754d76

Browse files
committed
Handle sprite size changes in import graphics
Also adds support for full-image bounding boxes when importing to an existing game sprite.
1 parent a27bb5c commit 4754d76

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

UndertaleModTool/Scripts/Resource Repackers/ImportGraphics.csx

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,19 @@ try
178178

179179
sprite.Textures[frame] = texentry;
180180

181+
// Update sprite dimensions
182+
uint oldWidth = sprite.Width, oldHeight = sprite.Height;
183+
sprite.Width = (uint)n.Texture.BoundingWidth;
184+
sprite.Height = (uint)n.Texture.BoundingHeight;
185+
bool changedSpriteDimensions = (oldWidth != sprite.Width || oldHeight != sprite.Height);
186+
181187
// Grow bounding box depending on how much is trimmed
182188
bool grewBoundingBox = false;
183-
int marginLeft = n.Texture.TargetX;
184-
int marginRight = n.Texture.TargetX + n.Bounds.Width - 1;
185-
int marginTop = n.Texture.TargetY;
186-
int marginBottom = n.Texture.TargetY + n.Bounds.Height - 1;
189+
bool fullImageBbox = sprite.BBoxMode == 1;
190+
int marginLeft = fullImageBbox ? 0 : n.Texture.TargetX;
191+
int marginRight = fullImageBbox ? ((int)sprite.Width - 1) : (n.Texture.TargetX + n.Bounds.Width - 1);
192+
int marginTop = fullImageBbox ? 0 : n.Texture.TargetY;
193+
int marginBottom = fullImageBbox ? ((int)sprite.Height - 1) : (n.Texture.TargetY + n.Bounds.Height - 1);
187194
if (marginLeft < sprite.MarginLeft)
188195
{
189196
sprite.MarginLeft = marginLeft;
@@ -210,9 +217,12 @@ try
210217
sprite.SepMasks is not (UndertaleSprite.SepMaskType.AxisAlignedRect or UndertaleSprite.SepMaskType.RotatedRect) ||
211218
sprite.CollisionMasks.Count > 0)
212219
{
213-
if ((bboxMasks && grewBoundingBox) || (sprite.SepMasks is UndertaleSprite.SepMaskType.Precise && sprite.CollisionMasks.Count == 0))
220+
if ((bboxMasks && grewBoundingBox) ||
221+
(sprite.SepMasks is UndertaleSprite.SepMaskType.Precise && sprite.CollisionMasks.Count == 0) ||
222+
(!bboxMasks && changedSpriteDimensions))
214223
{
215-
// Use this node for the sprite's collision mask if the bounding box grew (or if no collision mask exists for a precise sprite)
224+
// Use this node for the sprite's collision mask if the bounding box grew, if no collision mask exists for a precise sprite,
225+
// or if the sprite's dimensions have been changed altogether when bbox masks are not active.
216226
maskNodes[sprite] = n;
217227
}
218228
}

UndertaleModTool/Scripts/Resource Repackers/ImportGraphicsAdvanced.csx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,14 @@ try
263263
sprite.GMS2PlaybackSpeed = animSpd;
264264
sprite.IsSpecialType = isSpecial;
265265
sprite.SVersion = specialVer;
266+
267+
// Update sprite dimensions
268+
uint oldWidth = sprite.Width, oldHeight = sprite.Height;
269+
sprite.Width = (uint)n.Texture.BoundingWidth;
270+
sprite.Height = (uint)n.Texture.BoundingHeight;
271+
bool changedSpriteDimensions = (oldWidth != sprite.Width || oldHeight != sprite.Height);
272+
273+
// Update origin
266274
switch (offresult)
267275
{
268276
case ("Top Left"):
@@ -305,10 +313,11 @@ try
305313

306314
// Grow bounding box depending on how much is trimmed
307315
bool grewBoundingBox = false;
308-
int marginLeft = n.Texture.TargetX;
309-
int marginRight = n.Texture.TargetX + n.Bounds.Width - 1;
310-
int marginTop = n.Texture.TargetY;
311-
int marginBottom = n.Texture.TargetY + n.Bounds.Height - 1;
316+
bool fullImageBbox = sprite.BBoxMode == 1;
317+
int marginLeft = fullImageBbox ? 0 : n.Texture.TargetX;
318+
int marginRight = fullImageBbox ? ((int)sprite.Width - 1) : (n.Texture.TargetX + n.Bounds.Width - 1);
319+
int marginTop = fullImageBbox ? 0 : n.Texture.TargetY;
320+
int marginBottom = fullImageBbox ? ((int)sprite.Height - 1) : (n.Texture.TargetY + n.Bounds.Height - 1);
312321
if (marginLeft < sprite.MarginLeft)
313322
{
314323
sprite.MarginLeft = marginLeft;
@@ -335,7 +344,9 @@ try
335344
sprite.SepMasks is not (UndertaleSprite.SepMaskType.AxisAlignedRect or UndertaleSprite.SepMaskType.RotatedRect) ||
336345
sprite.CollisionMasks.Count > 0)
337346
{
338-
if ((bboxMasks && grewBoundingBox) || (sprite.SepMasks is UndertaleSprite.SepMaskType.Precise && sprite.CollisionMasks.Count == 0))
347+
if ((bboxMasks && grewBoundingBox) ||
348+
(sprite.SepMasks is UndertaleSprite.SepMaskType.Precise && sprite.CollisionMasks.Count == 0) ||
349+
(!bboxMasks && changedSpriteDimensions))
339350
{
340351
// Use this node for the sprite's collision mask if the bounding box grew (or if no collision mask exists for a precise sprite)
341352
maskNodes[sprite] = n;

0 commit comments

Comments
 (0)