Skip to content

Instantly share code, notes, and snippets.

@shdwcat
Last active October 17, 2020 19:29
Show Gist options
  • Save shdwcat/6e446830fe2d243953bed90e5f2fe7b7 to your computer and use it in GitHub Desktop.
Save shdwcat/6e446830fe2d243953bed90e5f2fe7b7 to your computer and use it in GitHub Desktop.
/// @description draw a sprite using four- or nine-slicing
/// @arg sprite
/// @arg subimg
/// @arg x
/// @arg y
/// @arg width
/// @arg height
/// @arg color
/// @arg alpha
/// @arg [slice_top_left] if not supplied, the origin will be used for four-slicing
/// @arg [slice_width] if width and height are not supplied, the slice_top_left will be used for four slicing
/// @arg [slice_height]
function draw_sprite_sliced(
sprite,
subimg,
_x,
_y,
width,
height,
color,
alpha
) {
// full nine-slicing
if argument_count == 11 {
var slice_coords = argument[8];
var slice_width = argument[9];
var slice_height = argument[10];
var slice_xy = { x: slice_coords[0], y: slice_coords[1] };
var slice_top_left = slice_xy;
var slice_top_right = { x: slice_xy.x + slice_width, y: slice_xy.y };
var slice_bottom_left = { x: slice_xy.x, y: slice_xy.y + slice_height };
var slice_bottom_right = { x: slice_xy.x + slice_width, y: slice_xy.y + slice_height };
}
// four-slicing
else if argument_count == 9 {
var slice_coords = argument[8];
var slice_xy = { x: slice_coords[0], y: slice_coords[1] };
var slice_top_left = slice_xy;
var slice_top_right = slice_xy;
var slice_bottom_left = slice_xy;
var slice_bottom_right = slice_xy;
}
// four-slice by origin
else if argument_count == 8 {
var slice_xy = { x: sprite_get_xoffset(sprite), y: sprite_get_yoffset(sprite) };
var slice_top_left = slice_xy;
var slice_top_right = slice_xy;
var slice_bottom_left = slice_xy;
var slice_bottom_right = slice_xy;
}
else {
throw "Invalid parameters for draw_sprite_sliced - expecting 8, 9, or 11 parameters";
}
var spr_width = sprite_get_width(sprite);
var spr_height = sprite_get_height(sprite);
// the inner target width is the desired width minus the left and right widths of the slice
var inner_target_width = width - (slice_top_left.x + (spr_width - slice_top_right.x));
// the inner target height is the desired width minus the top and bottom heights of the slice
var inner_target_height = height - (slice_top_left.y + (spr_height - slice_bottom_left.y));
var inner_slice_width = slice_top_right.x - slice_top_left.x;
var inner_slice_height = slice_bottom_left.y - slice_top_left.y;
// draw minimum 1 pixel from the inner slice
if inner_slice_width == 0 inner_slice_width = 1;
if inner_slice_height == 0 inner_slice_height = 1;
var inner_x_scale = inner_target_width / inner_slice_width;
var inner_y_scale = inner_target_height / inner_slice_height;
// top left
draw_sprite_part_ext(
sprite, subimg,
0, 0,
slice_top_left.x, slice_top_left.y,
_x, _y,
1, 1,
color, alpha);
// top middle
draw_sprite_part_ext(
sprite, subimg,
slice_top_left.x, 0, // top of the sprite, indented to the slice left x
inner_slice_width, slice_top_left.y,
_x + slice_top_left.x, _y,
inner_x_scale, 1,
color, alpha);
// top right
draw_sprite_part_ext(
sprite, subimg,
slice_top_right.x, 0,
spr_width - slice_top_right.x, slice_top_right.y,
_x + slice_top_left.x + inner_target_width, _y,
1, 1,
color, alpha);
// middle left
draw_sprite_part_ext(
sprite, subimg,
0, slice_top_left.y,
slice_top_left.x, inner_slice_height,
_x, _y + slice_top_left.y,
1, inner_y_scale,
color, alpha);
// middle inner
draw_sprite_part_ext(
sprite, subimg,
slice_top_left.x, slice_top_left.y,
inner_slice_width, inner_slice_height,
_x + slice_top_left.x, _y + slice_top_left.y,
inner_x_scale, inner_y_scale,
color, alpha);
// middle right
draw_sprite_part_ext(
sprite, subimg,
slice_top_right.x, slice_top_right.y,
spr_width - slice_top_right.x, inner_slice_height,
_x + slice_top_left.x + inner_target_width, _y + slice_top_left.y,
1, inner_y_scale,
color, alpha);
// bottom left
draw_sprite_part_ext(
sprite, subimg,
0, slice_bottom_left.y,
slice_bottom_left.x, spr_height - slice_bottom_right.y,
_x, _y + slice_top_left.y + inner_target_height,
1, 1,
color, alpha);
// bottom middle
draw_sprite_part_ext(
sprite, subimg,
slice_bottom_left.x, slice_bottom_left.y,
inner_slice_width, spr_height - slice_bottom_right.y,
_x + slice_bottom_left.x, _y + slice_top_left.y + inner_target_height,
inner_x_scale, 1,
color, alpha);
// bottom right
draw_sprite_part_ext(
sprite, subimg,
slice_bottom_right.x, slice_bottom_right.y,
spr_width - slice_bottom_right.x, spr_height - slice_bottom_right.y,
_x + slice_bottom_left.x + inner_target_width, _y + slice_top_right.y + inner_target_height,
1, 1,
color, alpha);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment