Skip to content

Commit

Permalink
Add seamless polygon border line (#4)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo Locurcio <[email protected]>
  • Loading branch information
Koyper and Calinou authored Jun 26, 2022
1 parent d1c99c8 commit 80b737f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 27 deletions.
11 changes: 11 additions & 0 deletions addons/antialiased_line2d/antialiased_line2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ extends Line2D
func _ready() -> void:
texture = AntialiasedLine2DTexture.texture
texture_mode = Line2D.LINE_TEXTURE_TILE


static func construct_closed_line(p_polygon: PoolVector2Array) -> PoolVector2Array:
var end_point: Vector2 = p_polygon[p_polygon.size() - 1]
var distance: float = end_point.distance_to(p_polygon[0]) # distance to start point
var bridge_point: Vector2 = end_point.move_toward(p_polygon[0], distance * 0.5)
# Close the polygon drawn by the line by adding superimposed bridge points between the start and end points.
var polygon_line := p_polygon
polygon_line.push_back(bridge_point)
polygon_line.insert(0, bridge_point)
return polygon_line
15 changes: 2 additions & 13 deletions addons/antialiased_line2d/antialiased_polygon2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,14 @@ var line_2d := Line2D.new()
func _ready() -> void:
line_2d.texture = AntialiasedLine2DTexture.texture
line_2d.texture_mode = Line2D.LINE_TEXTURE_TILE
# Make the closed polyline look more closed.
line_2d.begin_cap_mode = Line2D.LINE_CAP_ROUND

# Close the polygon drawn by the line by adding the first point to the end.
var polygon_line := polygon
if polygon.size() >= 1:
polygon_line.push_back(polygon[0])
line_2d.points = polygon_line

line_2d.points = AntialiasedLine2D.construct_closed_line(polygon)
add_child(line_2d)


func _set(property: String, value) -> bool:
if property == "polygon":
# Close the polygon drawn by the line by adding the first point to the end.
var polygon_line := polygon
polygon_line.push_back(polygon[0])
line_2d.points = polygon_line

line_2d.points = AntialiasedLine2D.construct_closed_line(polygon)
return false


Expand Down
16 changes: 2 additions & 14 deletions addons/antialiased_line2d/antialiased_regular_polygon2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,13 @@ var line_2d := Line2D.new()
func _ready() -> void:
line_2d.texture = AntialiasedLine2DTexture.texture
line_2d.texture_mode = Line2D.LINE_TEXTURE_TILE
# Make the closed polyline look more closed.
line_2d.begin_cap_mode = Line2D.LINE_CAP_ROUND

update_points()

add_child(line_2d)


func _set(property: String, value) -> bool:
if property == "polygon":
# Close the polygon drawn by the line by adding the first point to the end.
var polygon_line := polygon
polygon_line.push_back(polygon[0])
line_2d.points = polygon_line

line_2d.points = AntialiasedLine2D.construct_closed_line(polygon)
return false


Expand All @@ -45,12 +37,8 @@ func update_points() -> void:
if not is_equal_approx(angle_degrees, 360.0):
points.push_back(Vector2.ZERO)
polygon = points

# Force an update of the Line2D here to prevent it from getting out of sync.
# Close the polygon drawn by the line by adding the first point to the end.
var polygon_line := polygon
polygon_line.push_back(polygon[0])
line_2d.points = polygon_line
line_2d.points = AntialiasedLine2D.construct_closed_line(polygon)


func set_size(p_size: Vector2) -> void:
Expand Down

0 comments on commit 80b737f

Please sign in to comment.