1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 09:09:47 +00:00

Optimize the pencil & curve's fill inside option by making them check fewer pixels

Same logic as the previous commit
This commit is contained in:
Emmanouil Papadeas 2024-09-05 03:50:37 +03:00
parent 1e9c8487ba
commit 9650dae678
2 changed files with 16 additions and 10 deletions

View file

@ -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 _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 _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 := 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_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 _editing_out_control_point := false ## True when controlling the out control point only.
var _thickness := 1 ## The thickness of the curve. var _thickness := 1 ## The thickness of the curve.
@ -96,6 +97,7 @@ func draw_start(pos: Vector2i) -> void:
if !_drawing: if !_drawing:
_drawing = true _drawing = true
_curve.add_point(pos) _curve.add_point(pos)
_fill_inside_rect = Rect2i(pos, Vector2i.ZERO)
func draw_move(pos: Vector2i) -> void: func draw_move(pos: Vector2i) -> void:
@ -183,15 +185,15 @@ func _draw_shape() -> void:
for point in points: for point in points:
# Reset drawer every time because pixel perfect sometimes breaks the tool # Reset drawer every time because pixel perfect sometimes breaks the tool
_drawer.reset() _drawer.reset()
_fill_inside_rect = _fill_inside_rect.expand(point)
# Draw each point offsetted based on the shape's thickness # Draw each point offsetted based on the shape's thickness
_draw_pixel(point, images) _draw_pixel(point, images)
if _fill: if _fill:
var v := Vector2i() var v := Vector2i()
var image_size := Global.current_project.size for x in _fill_inside_rect.size.x:
for x in image_size.x: v.x = x + _fill_inside_rect.position.x
v.x = x for y in _fill_inside_rect.size.y:
for y in image_size.y: v.y = y + _fill_inside_rect.position.y
v.y = y
if Geometry2D.is_point_in_polygon(v, points): if Geometry2D.is_point_in_polygon(v, points):
_draw_pixel(v, images) _draw_pixel(v, images)
_clear() _clear()
@ -206,6 +208,7 @@ func _draw_pixel(point: Vector2i, images: Array[Image]) -> void:
func _clear() -> void: func _clear() -> void:
_curve.clear_points() _curve.clear_points()
_fill_inside_rect = Rect2i()
_drawing = false _drawing = false
_editing_out_control_point = false _editing_out_control_point = false
Global.canvas.previews.queue_redraw() Global.canvas.previews.queue_redraw()

View file

@ -5,6 +5,7 @@ var _last_position := Vector2i(Vector2.INF)
var _changed := false var _changed := false
var _overwrite := false var _overwrite := false
var _fill_inside := 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 _draw_points := PackedVector2Array()
var _old_spacing_mode := false ## Needed to reset spacing mode in case we change it 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: else:
if _fill_inside: if _fill_inside:
_draw_points.append(pos) _draw_points.append(pos)
_fill_inside_rect = Rect2i(pos, Vector2i.ZERO)
draw_tool(pos) draw_tool(pos)
_last_position = pos _last_position = pos
Global.canvas.sprite_changed_this_frame = true 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 Global.canvas.sprite_changed_this_frame = true
if _fill_inside: if _fill_inside:
_draw_points.append(pos) _draw_points.append(pos)
_fill_inside_rect = _fill_inside_rect.expand(pos)
func draw_end(pos: Vector2i) -> void: func draw_end(pos: Vector2i) -> void:
@ -178,11 +181,10 @@ func draw_end(pos: Vector2i) -> void:
_draw_points.append(pos) _draw_points.append(pos)
if _draw_points.size() > 3: if _draw_points.size() > 3:
var v := Vector2i() var v := Vector2i()
var image_size := Global.current_project.size for x in _fill_inside_rect.size.x:
for x in image_size.x: v.x = x + _fill_inside_rect.position.x
v.x = x for y in _fill_inside_rect.size.y:
for y in image_size.y: v.y = y + _fill_inside_rect.position.y
v.y = y
if Geometry2D.is_point_in_polygon(v, _draw_points): if Geometry2D.is_point_in_polygon(v, _draw_points):
if _spacing_mode: if _spacing_mode:
# use of get_spacing_position() in Pencil.gd is a rare case # 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) v = get_spacing_position(v)
draw_tool(v) draw_tool(v)
_fill_inside_rect = Rect2i()
commit_undo() commit_undo()
cursor_text = "" cursor_text = ""
update_random_image() update_random_image()