From 9650dae67802c6681989a4031b0a8bf84953a99b Mon Sep 17 00:00:00 2001 From: Emmanouil Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Thu, 5 Sep 2024 03:50:37 +0300 Subject: [PATCH] Optimize the pencil & curve's fill inside option by making them check fewer pixels Same logic as the previous commit --- src/Tools/DesignTools/CurveTool.gd | 13 ++++++++----- src/Tools/DesignTools/Pencil.gd | 13 ++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Tools/DesignTools/CurveTool.gd b/src/Tools/DesignTools/CurveTool.gd index e00958689..80a1d3b81 100644 --- a/src/Tools/DesignTools/CurveTool.gd +++ b/src/Tools/DesignTools/CurveTool.gd @@ -3,6 +3,7 @@ extends "res://src/Tools/BaseDraw.gd" var _curve := Curve2D.new() ## The [Curve2D] responsible for the shape of the curve being drawn. var _drawing := false ## Set to true when a curve is being drawn. var _fill := false ## When true, the inside area of the curve gets filled. +var _fill_inside_rect := Rect2i() ## The bounding box that surrounds the area that gets filled. var _editing_bezier := false ## Needed to determine when to show the control points preview line. var _editing_out_control_point := false ## True when controlling the out control point only. var _thickness := 1 ## The thickness of the curve. @@ -96,6 +97,7 @@ func draw_start(pos: Vector2i) -> void: if !_drawing: _drawing = true _curve.add_point(pos) + _fill_inside_rect = Rect2i(pos, Vector2i.ZERO) func draw_move(pos: Vector2i) -> void: @@ -183,15 +185,15 @@ func _draw_shape() -> void: for point in points: # Reset drawer every time because pixel perfect sometimes breaks the tool _drawer.reset() + _fill_inside_rect = _fill_inside_rect.expand(point) # Draw each point offsetted based on the shape's thickness _draw_pixel(point, images) if _fill: var v := Vector2i() - var image_size := Global.current_project.size - for x in image_size.x: - v.x = x - for y in image_size.y: - v.y = y + for x in _fill_inside_rect.size.x: + v.x = x + _fill_inside_rect.position.x + for y in _fill_inside_rect.size.y: + v.y = y + _fill_inside_rect.position.y if Geometry2D.is_point_in_polygon(v, points): _draw_pixel(v, images) _clear() @@ -206,6 +208,7 @@ func _draw_pixel(point: Vector2i, images: Array[Image]) -> void: func _clear() -> void: _curve.clear_points() + _fill_inside_rect = Rect2i() _drawing = false _editing_out_control_point = false Global.canvas.previews.queue_redraw() diff --git a/src/Tools/DesignTools/Pencil.gd b/src/Tools/DesignTools/Pencil.gd index 145720671..5e80f654a 100644 --- a/src/Tools/DesignTools/Pencil.gd +++ b/src/Tools/DesignTools/Pencil.gd @@ -5,6 +5,7 @@ var _last_position := Vector2i(Vector2.INF) var _changed := false var _overwrite := false var _fill_inside := false +var _fill_inside_rect := Rect2i() ## The bounding box that surrounds the area that gets filled. var _draw_points := PackedVector2Array() var _old_spacing_mode := false ## Needed to reset spacing mode in case we change it @@ -125,6 +126,7 @@ func draw_start(pos: Vector2i) -> void: else: if _fill_inside: _draw_points.append(pos) + _fill_inside_rect = Rect2i(pos, Vector2i.ZERO) draw_tool(pos) _last_position = pos Global.canvas.sprite_changed_this_frame = true @@ -156,6 +158,7 @@ func draw_move(pos_i: Vector2i) -> void: Global.canvas.sprite_changed_this_frame = true if _fill_inside: _draw_points.append(pos) + _fill_inside_rect = _fill_inside_rect.expand(pos) func draw_end(pos: Vector2i) -> void: @@ -178,11 +181,10 @@ func draw_end(pos: Vector2i) -> void: _draw_points.append(pos) if _draw_points.size() > 3: var v := Vector2i() - var image_size := Global.current_project.size - for x in image_size.x: - v.x = x - for y in image_size.y: - v.y = y + for x in _fill_inside_rect.size.x: + v.x = x + _fill_inside_rect.position.x + for y in _fill_inside_rect.size.y: + v.y = y + _fill_inside_rect.position.y if Geometry2D.is_point_in_polygon(v, _draw_points): if _spacing_mode: # use of get_spacing_position() in Pencil.gd is a rare case @@ -190,6 +192,7 @@ func draw_end(pos: Vector2i) -> void: v = get_spacing_position(v) draw_tool(v) + _fill_inside_rect = Rect2i() commit_undo() cursor_text = "" update_random_image()