Following the discussion here I had a go at stretchable sprites.
Here is an example that shows a regular StaticSprite2D
(left) and a StretchableSprite2D
(right). The latter handles scaling differently: it tries to leave the borders untouched, scaling only the “inside”, if possible. When the scale goes below the border size, the “inside” is completely removed and the borders are scaled instead. Negative scales are also handled the same way.
Note that this is done on X and Y axis independently, so there are “different levels” of “inside” and “borders”.
Just like BorderImage
, a StretchableSprite2D
is specified with 4 independent borders (left, top, right, bottom), so it can be asymmetric on both axis. Each border is specified as an integer corresponding to the number of pixels from the edge (>= 0).
I implemented this StretchableSprite2D
type by inheriting from StaticSprite2D
and overriding UpdateSourceBatches
, to render it as a set of 4, 6, or 9 square patches:
- 9 patches if both X and Y scales determine a size that is larger than the border size
- 6 patches if either X or Y scales (but not both) determine a size that is below the corresponding border size (left+right or top+bottom)
- 4 patches if both are below border size
Inheriting StaticSprite2D
's interface means dealing with a bunch of special cases though (e.g. manual draw rect, hotspot, spritesheets, flips). These can be combined in many ways that I still didn’t have the chance to test. Some hands on testing would be greatly appreciated. I hope to have a PR coming soon.