Skip to content

Instantly share code, notes, and snippets.

@dominicfreeston
Created March 16, 2023 22:02
Show Gist options
  • Save dominicfreeston/198fc76cff36019dfc76cdfd9bc38d6a to your computer and use it in GitHub Desktop.
Save dominicfreeston/198fc76cff36019dfc76cdfd9bc38d6a to your computer and use it in GitHub Desktop.
nine-slice (stretching) for dragonruby
def nine_slice sprite
x = sprite.x
y = sprite.y
w = sprite.w
h = sprite.h
path = sprite.path
t_x = sprite.tile_x
t_y = sprite.tile_y
o_w = sprite.tile_w
o_h = sprite.tile_h
c_w = sprite.corner_w || sprite.corner_s
c_h = sprite.corner_h || sprite.corner_s
return [
# center
{
x: x + c_w,
y: y + c_h,
w: w - 2 * c_w,
h: h - 2 * c_h,
path: path,
tile_x: t_x + c_w,
tile_y: t_y + c_h,
tile_w: o_w - 2 * c_w,
tile_h: o_h - 2 * c_h,
section: "center",
},
# corners
{
x: x,
y: y + h - c_h,
w: c_w,
h: c_h,
path: path,
tile_x: t_x,
tile_y: t_y,
tile_w: c_w,
tile_h: c_h,
section: "top_left_corner",
},
{
x: x + w - c_w,
y: y + h - c_h,
w: c_w,
h: c_h,
path: path,
tile_x: t_x + o_w - c_w,
tile_y: t_y,
tile_w: c_w,
tile_h: c_h,
section: "top_right_corner",
},
{
x: x,
y: y,
w: c_w,
h: c_h,
path: path,
tile_x: t_x,
tile_y: t_y + o_h - c_h,
tile_w: c_w,
tile_h: c_h,
section: "bottom_left_corner",
},
{
x: x + w - c_w,
y: y,
w: c_w,
h: c_h,
path: path,
tile_x: t_x + o_w - c_w,
tile_y: t_y + o_h - c_h,
tile_w: c_w,
tile_h: c_h,
section: "bottom_right_corner",
},
# edges
{
x: x + c_w,
y: y + h - c_h,
w: w - 2 * c_w,
h: c_h,
path: path,
tile_x: t_x + c_w,
tile_y: t_y,
tile_w: o_w - 2 * c_w,
tile_h: c_h,
section: "top_edge",
},
{
x: x + c_w,
y: y,
w: w - 2 * c_w,
h: c_h,
path: path,
tile_x: t_x + c_w,
tile_y: t_y + o_w - c_h,
tile_w: o_w - 2 * c_w,
tile_h: c_h,
section: "bottom_edge",
},
{
x: x,
y: y + c_h,
w: c_w,
h: h - 2 * c_h,
path: path,
tile_x: t_x,
tile_y: t_y + c_h,
tile_w: c_w,
tile_h: o_h - 2 * c_h,
section: "left_edge",
},
{
x: x + w - c_w,
y: y + c_h,
w: c_w,
h: h - 2 * c_h,
path: path,
tile_x: t_x + o_w - c_w,
tile_y: t_y + c_h,
tile_w: c_w,
tile_h: o_h - 2 * c_h,
section: "right_edge",
}
]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment