From c79ac9f4306524e73c0c1be4875ffe0b79e1aad6 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Mon, 25 Mar 2024 17:43:13 +0100 Subject: [PATCH] Add center_object function --- docs/docs/reference/misc.md | 13 +++++++++++++ src/Misc.gd | 3 +++ 2 files changed, 16 insertions(+) diff --git a/docs/docs/reference/misc.md b/docs/docs/reference/misc.md index 05abeb5..db5c4e7 100644 --- a/docs/docs/reference/misc.md +++ b/docs/docs/reference/misc.md @@ -50,3 +50,16 @@ Converts an array into a string array. If an item is found that is not of type `String`, an empty array is returned. ### *Array* stringarray_to_array(*Array[String]* array) Converts a string array into an array. +### *Vector2* center_object(*Vector2* parent_size, *Vector2* child_size) +Calculates the center of the child in the area of the parent. + +Example: +```gdscript +extends Control + +var core: Core = get_node("/root/CORE") +var misc: CoreBaseModule = core.misc + +func _ready() -> void: + position = misc.center_object(get_parent().size, size) +``` diff --git a/src/Misc.gd b/src/Misc.gd index d128cbe..36b1a51 100644 --- a/src/Misc.gd +++ b/src/Misc.gd @@ -79,3 +79,6 @@ func stringarray_to_array(array: Array[String]) -> Array: output.append(item) return output + +func center_object(parent_size: Vector2, child_size: Vector2) -> Vector2: + return Vector2(parent_size.x/2-child_size.x/2, parent_size.y/2-child_size.y/2)