Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw optimizer #657

Merged
merged 8 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Tools/BaseTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ var cursor_text := ""

var _cursor := Vector2.INF

var _draw_cache: PoolVector2Array = [] # for storing already drawn pixels
var _for_frame := 0 # cache for which frame?


func _ready() -> void:
kname = name.replace(" ", "_").to_lower()
Expand Down Expand Up @@ -40,6 +43,7 @@ func update_config() -> void:


func draw_start(_position: Vector2) -> void:
_draw_cache = []
is_moving = true


Expand All @@ -52,6 +56,7 @@ func draw_move(position: Vector2) -> void:

func draw_end(_position: Vector2) -> void:
is_moving = false
_draw_cache = []


func cursor_move(position: Vector2) -> void:
Expand Down
9 changes: 9 additions & 0 deletions src/Tools/Draw.gd
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extends BaseTool

var _brush := Brushes.get_default_brush()
var _brush_size := 1
var _cache_limit: int = 3
var _brush_interpolate := 0
var _brush_image := Image.new()
var _brush_texture := ImageTexture.new()
Expand Down Expand Up @@ -43,6 +44,7 @@ func _on_Brush_selected(brush: Brushes.Brush) -> void:

func _on_BrushSize_value_changed(value: float) -> void:
_brush_size = int(value)
_cache_limit = (_brush_size * _brush_size) * 3 # This equation seems the best match
update_config()
save_config()

Expand Down Expand Up @@ -345,6 +347,13 @@ func draw_indicator_at(position: Vector2, offset: Vector2, color: Color) -> void


func _set_pixel(position: Vector2, ignore_mirroring := false) -> void:
if position in _draw_cache and _for_frame == Global.current_project.current_frame:
return
if _draw_cache.size() > _cache_limit or _for_frame != Global.current_project.current_frame:
_draw_cache = []
_for_frame = Global.current_project.current_frame
_draw_cache.append(position) # Store the position of pixel

var project: Project = Global.current_project
if project.tile_mode and project.get_tile_mode_rect().has_point(position):
position = position.posmodv(project.size)
Expand Down