This snippet let's you center an overlay on the X and Y axis.
The snippet is really sort, and probably could be achieved by other ways, but until I come with those, this is the current status of the snippet:
var/list/cached_offsets[0]
proc/get_pixel_x(base, overlay)
if(!base || !overlay) return 0
if(!cached_offsets.Find("[base]"))
cached_offsets.Add("[base]")
cached_offsets["[base]"] = list(
"[overlay]" = null
)
if(!cached_offsets["[base]"]["[overlay]"])
cached_offsets["[base]"]["[overlay]"] = list(
"x" = null,
"y" = null
)
if(cached_offsets["[base]"]["[overlay]"]["x"]==null)
cached_offsets["[base]"]["[overlay]"]["x"] = get_x_offset(icon(base), icon(overlay))
return cached_offsets["[base]"]["[overlay]"]["x"]
proc/get_pixel_y(icon/base, icon/overlay)
if(!base || !overlay) return 0
if(!cached_offsets.Find("[base]"))
cached_offsets.Add("[base]")
cached_offsets["[base]"] = list(
"[overlay]" = null
)
if(!cached_offsets["[base]"]["[overlay]"])
cached_offsets["[base]"]["[overlay]"] = list(
"x" = null,
"y" = null
)
if(cached_offsets["[base]"]["[overlay]"]["y"]==null)
cached_offsets["[base]"]["[overlay]"]["y"] = get_y_offset(icon(base), icon(overlay))
return cached_offsets["[base]"]["[overlay]"]["y"]
proc/get_x_offset(icon/base, icon/overlay)
if(!base || !overlay) return 0
return round(((base.Width() - overlay.Width()) / 2),1)
proc/get_y_offset(icon/base, icon/overlay)
if(!base || !overlay) return 0
return round(((base.Height() - overlay.Height()) / 2),1)
Those two procs returns the number of pixels on both axis, an overlay should be offset by, in order to be centered into it's holder.
The usage of the procs would be as follows:
ob/proc/overlayMe()
src.overlays += image('thebiggericon.dmi',\
pixel_x = get_pixel_x("[src.icon]", "thebiggericon.dmi"),
pixel_y = get_pixel_y("[src.icon]", "thebiggericon.dmi"))
The above snippet would center 'thebiggericon.dmi' on 'src'.
If you just want to center the overlay horizontally, you just need to remove the pixel_y line, and vice-versa.
Pixels offset are rounded, so 1.4 offset would be 1.
Happy overlaying.