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

Optimize the drawing of large amounts of pixels #554

Closed
Closed
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
29 changes: 29 additions & 0 deletions src/Tools/BaseTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,32 @@ func _add_polylines_segment(lines : Array, start : Vector2, end : Vector2) -> vo
line.append(start)
return
lines.append([start, end])


class Cache:
var cache = {}

func clear():
self.cache = {}

func store(position: Vector2):
self.cache[position] = true

func is_cached(position: Vector2) -> bool:
return self.cache.get(position, false)


var cache = Cache.new()


func draw_start(_position : Vector2) -> void:
pass


func draw_move(_position : Vector2) -> void:
pass


func draw_end(_position : Vector2) -> void:
cache.clear()

9 changes: 1 addition & 8 deletions src/Tools/Bucket.gd
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func update_pattern() -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.canvas.selection.transform_content_confirm()
if !Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn() or !Global.current_project.tile_mode_rects[Global.TileMode.NONE].has_point(position):
return
Expand All @@ -107,14 +108,6 @@ func draw_start(position : Vector2) -> void:
commit_undo("Draw", undo_data)


func draw_move(_position : Vector2) -> void:
pass


func draw_end(_position : Vector2) -> void:
pass


func fill_in_color(position : Vector2) -> void:
var project : Project = Global.current_project
var color : Color = _get_draw_image().get_pixelv(position)
Expand Down
6 changes: 2 additions & 4 deletions src/Tools/ColorPicker.gd
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,15 @@ func update_config() -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
_pick_color(position)


func draw_move(position : Vector2) -> void:
.draw_move(position)
_pick_color(position)


func draw_end(_position : Vector2) -> void:
pass


func _pick_color(position : Vector2) -> void:
var project : Project = Global.current_project
if project.tile_mode and project.get_tile_mode_rect().has_point(position):
Expand Down
5 changes: 4 additions & 1 deletion src/Tools/Draw.gd
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ func draw_indicator_at(position : Vector2, offset : Vector2, color : Color) -> v
canvas.draw_polyline(pool, color)
canvas.draw_set_transform(canvas.position, canvas.rotation, canvas.scale)


func _set_pixel(position : Vector2, ignore_mirroring := false) -> void:
if cache.is_cached(position):
return

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 All @@ -350,6 +352,7 @@ func _set_pixel(position : Vector2, ignore_mirroring := false) -> void:
else:
for image in images:
_drawer.set_pixel(image, position, tool_slot.color, ignore_mirroring)
cache.store(position)


func _draw_brush_image(_image : Image, _src_rect: Rect2, _dst: Vector2) -> void:
Expand Down
5 changes: 4 additions & 1 deletion src/Tools/Eraser.gd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func set_config(config : Dictionary) -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.canvas.selection.transform_content_confirm()
update_mask()
_changed = false
Expand All @@ -56,6 +57,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if _draw_line:
var d = _line_angle_constraint(_line_start, position)
_line_end = d.position
Expand All @@ -68,7 +70,8 @@ func draw_move(position : Vector2) -> void:
Global.canvas.sprite_changed_this_frame = true


func draw_end(_position : Vector2) -> void:
func draw_end(position : Vector2) -> void:
.draw_end(position)
if _draw_line:
draw_tool(_line_start)
draw_fill_gap(_line_start, _line_end)
Expand Down
5 changes: 4 additions & 1 deletion src/Tools/LineTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func _input(event : InputEvent) -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.canvas.selection.transform_content_confirm()
update_mask()

Expand All @@ -80,6 +81,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if _drawing:
if _displace_origin:
_original_pos += position - _offset
Expand All @@ -93,7 +95,8 @@ func draw_move(position : Vector2) -> void:
_offset = position


func draw_end(_position : Vector2) -> void:
func draw_end(position : Vector2) -> void:
.draw_end(position)
if _drawing:
_draw_shape()

Expand Down
3 changes: 3 additions & 0 deletions src/Tools/Move.gd
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func _input(event : InputEvent) -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
if !Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn():
return
_start_pos = position
Expand All @@ -39,6 +40,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if !Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn():
return
# This is true if content transformation has been confirmed (pressed Enter for example)
Expand All @@ -63,6 +65,7 @@ func draw_move(position : Vector2) -> void:


func draw_end(position : Vector2) -> void:
.draw_end(position)
if !Global.current_project.layers[Global.current_project.current_layer].can_layer_get_drawn():
return
if _start_pos != Vector2.INF and _content_transformation_check == selection_node.is_moving_content:
Expand Down
10 changes: 4 additions & 6 deletions src/Tools/Pan.gd
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
extends BaseTool


func draw_start(_position : Vector2) -> void:
func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.camera.drag = true
Global.camera2.drag = true


func draw_move(_position : Vector2) -> void:
pass


func draw_end(_position : Vector2) -> void:
func draw_end(position : Vector2) -> void:
.draw_end(position)
Global.camera.drag = false
Global.camera2.drag = false
7 changes: 5 additions & 2 deletions src/Tools/Pencil.gd
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func update_config() -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.canvas.selection.transform_content_confirm()
update_mask()
_changed = false
Expand All @@ -80,6 +81,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if _draw_line:
var d = _line_angle_constraint(_line_start, position)
_line_end = d.position
Expand All @@ -94,14 +96,15 @@ func draw_move(position : Vector2) -> void:
_draw_points.append(position)


func draw_end(_position : Vector2) -> void:
func draw_end(position : Vector2) -> void:
.draw_end(position)
if _draw_line:
draw_tool(_line_start)
draw_fill_gap(_line_start, _line_end)
_draw_line = false
else:
if _fill_inside:
_draw_points.append(_position)
_draw_points.append(position)
if _draw_points.size() > 3:
var v = Vector2()
var image_size = Global.current_project.size
Expand Down
3 changes: 3 additions & 0 deletions src/Tools/SelectionTools/PolygonSelect.gd
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func _input(event : InputEvent) -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
if !$DoubleClickTimer.is_stopped():
return
.draw_start(position)
Expand All @@ -37,12 +38,14 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if selection_node.arrow_key_move:
return
.draw_move(position)


func draw_end(position : Vector2) -> void:
.draw_end(position)
if selection_node.arrow_key_move:
return
if !_move and _draw_points:
Expand Down
7 changes: 5 additions & 2 deletions src/Tools/SelectionTools/SelectionTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func set_spinbox_values() -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
if selection_node.arrow_key_move:
return
var project : Project = Global.current_project
Expand Down Expand Up @@ -114,6 +115,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if selection_node.arrow_key_move:
return
# This is true if content transformation has been confirmed (pressed Enter for example)
Expand All @@ -140,14 +142,15 @@ func draw_move(position : Vector2) -> void:
_set_cursor_text(selection_node.big_bounding_rectangle)


func draw_end(_position : Vector2) -> void:
func draw_end(position : Vector2) -> void:
.draw_end(position)
if selection_node.arrow_key_move:
return
if _content_transformation_check == selection_node.is_moving_content:
if _move:
selection_node.move_borders_end()
else:
apply_selection(_position)
apply_selection(position)

_move = false
_snap_to_grid = false
Expand Down
5 changes: 4 additions & 1 deletion src/Tools/Shading.gd
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ func update_strength() -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.canvas.selection.transform_content_confirm()
update_mask(false)
_changed = false
Expand All @@ -221,6 +222,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if _draw_line:
var d = _line_angle_constraint(_line_start, position)
_line_end = d.position
Expand All @@ -233,7 +235,8 @@ func draw_move(position : Vector2) -> void:
Global.canvas.sprite_changed_this_frame = true


func draw_end(_position : Vector2) -> void:
func draw_end(position : Vector2) -> void:
.draw_end(position)
if _draw_line:
draw_tool(_line_start)
draw_fill_gap(_line_start, _line_end)
Expand Down
3 changes: 3 additions & 0 deletions src/Tools/ShapeDrawer.gd
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func _input(event : InputEvent) -> void:


func draw_start(position : Vector2) -> void:
.draw_start(position)
Global.canvas.selection.transform_content_confirm()
update_mask()

Expand All @@ -94,6 +95,7 @@ func draw_start(position : Vector2) -> void:


func draw_move(position : Vector2) -> void:
.draw_move(position)
if _drawing:
if _displace_origin:
_start += position - _offset
Expand All @@ -103,6 +105,7 @@ func draw_move(position : Vector2) -> void:


func draw_end(position : Vector2) -> void:
.draw_end(position)
if _drawing:
_draw_shape(_start, position)

Expand Down
13 changes: 3 additions & 10 deletions src/Tools/Zoom.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,13 @@ func update_config() -> void:
$ModeOptions.selected = _zoom_mode


func draw_start(_position : Vector2) -> void:
func draw_start(position : Vector2) -> void:
.draw_start(position)
var mouse_pos := get_global_mouse_position()
var viewport_rect := Rect2(Global.main_viewport.rect_global_position, Global.main_viewport.rect_size)
var viewport_rect_2 := Rect2(Global.second_viewport.rect_global_position, Global.second_viewport.rect_size)

if viewport_rect.has_point(mouse_pos):
Global.camera.zoom_camera(_zoom_mode * 2 - 1)
elif viewport_rect_2.has_point(mouse_pos):
Global.camera2.zoom_camera(_zoom_mode * 2 - 1)


func draw_move(_position : Vector2) -> void:
pass


func draw_end(_position : Vector2) -> void:
pass
Global.camera2.zoom_camera(_zoom_mode * 2 - 1)