Last active
August 3, 2019 00:00
-
-
Save regakakobigman/32001fb3d0739d7e5fde536dd35596f3 to your computer and use it in GitHub Desktop.
Convert between position + rect extents and margins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This helps a lot when trying to use Controls to represent a translated CollisionShape2D for a tool script | |
# I don't know if it's useful for anything else | |
static func pos_to_margin(position: Vector2, extents: Vector2) -> Dictionary: | |
var margin_left := position.x - extents.x | |
var margin_right := position.x + extents.x | |
var margin_top := position.y - extents.y | |
var margin_bottom := position.y + extents.y | |
return { | |
"margin_left": margin_left, | |
"margin_right": margin_right, | |
"margin_top": margin_top, | |
"margin_bottom": margin_bottom | |
} | |
static func margin_to_pos(margin_left: float, margin_right: float, margin_top: float, margin_bottom: float, centered: bool) -> Dictionary: | |
var position := Vector2( | |
(margin_left + margin_right) / 2, | |
(margin_top + margin_bottom) / 2) | |
var extents := Vector2( | |
margin_right - margin_left, | |
margin_bottom - margin_top) | |
if centered: | |
extents /= 2 | |
else: | |
position -= extents/2 | |
return { | |
"position": position, | |
"extents": extents | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment