From 4f02ea1b9757bc5d144dffb1e9157db94f6a8d03 Mon Sep 17 00:00:00 2001 From: Manolis Papadeas <35376950+OverloadedOrama@users.noreply.github.com> Date: Sun, 14 Nov 2021 03:30:00 +0200 Subject: [PATCH] Alt to pick colors with draw tools - Closes #125 --- src/Tools/Bucket.gd | 24 ++++++++++++++++++++++++ src/Tools/Draw.gd | 21 +++++++++++++++++++++ src/Tools/Eraser.gd | 14 ++++++++++++++ src/Tools/LineTool.gd | 14 ++++++++++++++ src/Tools/Pencil.gd | 15 +++++++++++++++ src/Tools/Shading.gd | 14 ++++++++++++++ src/Tools/ShapeDrawer.gd | 14 ++++++++++++++ 7 files changed, 116 insertions(+) diff --git a/src/Tools/Bucket.gd b/src/Tools/Bucket.gd index 363cd853d..d03b8269a 100644 --- a/src/Tools/Bucket.gd +++ b/src/Tools/Bucket.gd @@ -108,6 +108,10 @@ func update_pattern() -> void: func draw_start(position : Vector2) -> void: + if Input.is_action_pressed("alt"): + _pick_color(position) + return + 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 @@ -247,3 +251,23 @@ func _get_undo_data() -> Dictionary: data[image] = image.data image.lock() return data + + +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): + position = position.posmodv(project.size) + + if position.x < 0 or position.y < 0: + return + + var image := Image.new() + image.copy_from(_get_draw_image()) + if position.x > image.get_width() - 1 or position.y > image.get_height() - 1: + return + + image.lock() + var color := image.get_pixelv(position) + image.unlock() + var button := BUTTON_LEFT if Tools._slots[BUTTON_LEFT].tool_node == self else BUTTON_RIGHT + Tools.assign_color(color, button, false) diff --git a/src/Tools/Draw.gd b/src/Tools/Draw.gd index 9b8a597ef..d1785810c 100644 --- a/src/Tools/Draw.gd +++ b/src/Tools/Draw.gd @@ -7,6 +7,7 @@ var _brush_interpolate := 0 var _brush_image := Image.new() var _brush_texture := ImageTexture.new() var _strength := 1.0 +var _picking_color := false var _undo_data := {} var _drawer := Drawer.new() @@ -526,3 +527,23 @@ func _get_undo_data() -> Dictionary: data[image] = image.data image.lock() return data + + +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): + position = position.posmodv(project.size) + + if position.x < 0 or position.y < 0: + return + + var image := Image.new() + image.copy_from(_get_draw_image()) + if position.x > image.get_width() - 1 or position.y > image.get_height() - 1: + return + + image.lock() + var color := image.get_pixelv(position) + image.unlock() + var button := BUTTON_LEFT if Tools._slots[BUTTON_LEFT].tool_node == self else BUTTON_RIGHT + Tools.assign_color(color, button, false) diff --git a/src/Tools/Eraser.gd b/src/Tools/Eraser.gd index fcbfb3c2a..ce64e22d7 100644 --- a/src/Tools/Eraser.gd +++ b/src/Tools/Eraser.gd @@ -35,6 +35,12 @@ func set_config(config : Dictionary) -> void: func draw_start(position : Vector2) -> void: + if Input.is_action_pressed("alt"): + _picking_color = true + _pick_color(position) + return + _picking_color = false + Global.canvas.selection.transform_content_confirm() update_mask(_strength == 1) _changed = false @@ -56,6 +62,11 @@ func draw_start(position : Vector2) -> void: func draw_move(position : Vector2) -> void: + if _picking_color: # Still return even if we released Alt + if Input.is_action_pressed("alt"): + _pick_color(position) + return + if _draw_line: var d = _line_angle_constraint(_line_start, position) _line_end = d.position @@ -69,6 +80,9 @@ func draw_move(position : Vector2) -> void: func draw_end(_position : Vector2) -> void: + if _picking_color: + return + if _draw_line: draw_tool(_line_start) draw_fill_gap(_line_start, _line_end) diff --git a/src/Tools/LineTool.gd b/src/Tools/LineTool.gd index 08dbe6f0c..caad1e1a1 100644 --- a/src/Tools/LineTool.gd +++ b/src/Tools/LineTool.gd @@ -69,6 +69,12 @@ func _input(event : InputEvent) -> void: func draw_start(position : Vector2) -> void: + if Input.is_action_pressed("alt"): + _picking_color = true + _pick_color(position) + return + _picking_color = false + Global.canvas.selection.transform_content_confirm() update_mask() @@ -80,6 +86,11 @@ func draw_start(position : Vector2) -> void: func draw_move(position : Vector2) -> void: + if _picking_color: # Still return even if we released Alt + if Input.is_action_pressed("alt"): + _pick_color(position) + return + if _drawing: if _displace_origin: _original_pos += position - _offset @@ -94,6 +105,9 @@ func draw_move(position : Vector2) -> void: func draw_end(_position : Vector2) -> void: + if _picking_color: + return + if _drawing: _draw_shape() diff --git a/src/Tools/Pencil.gd b/src/Tools/Pencil.gd index 327ee78af..f9a6cdeaf 100644 --- a/src/Tools/Pencil.gd +++ b/src/Tools/Pencil.gd @@ -8,6 +8,7 @@ var _overwrite := false var _fill_inside := false var _draw_points := Array() + class PencilOp extends Drawer.ColorOp: var changed := false var overwrite := false @@ -69,6 +70,12 @@ func update_config() -> void: func draw_start(position : Vector2) -> void: + if Input.is_action_pressed("alt"): + _picking_color = true + _pick_color(position) + return + _picking_color = false + Global.canvas.selection.transform_content_confirm() update_mask() _changed = false @@ -94,6 +101,11 @@ func draw_start(position : Vector2) -> void: func draw_move(position : Vector2) -> void: + if _picking_color: # Still return even if we released Alt + if Input.is_action_pressed("alt"): + _pick_color(position) + return + if _draw_line: var d = _line_angle_constraint(_line_start, position) _line_end = d.position @@ -109,6 +121,9 @@ func draw_move(position : Vector2) -> void: func draw_end(_position : Vector2) -> void: + if _picking_color: + return + if _draw_line: draw_tool(_line_start) draw_fill_gap(_line_start, _line_end) diff --git a/src/Tools/Shading.gd b/src/Tools/Shading.gd index d5e2b9523..f9e6e8e82 100644 --- a/src/Tools/Shading.gd +++ b/src/Tools/Shading.gd @@ -215,6 +215,12 @@ func update_strength() -> void: func draw_start(position : Vector2) -> void: + if Input.is_action_pressed("alt"): + _picking_color = true + _pick_color(position) + return + _picking_color = false + Global.canvas.selection.transform_content_confirm() update_mask(false) _changed = false @@ -236,6 +242,11 @@ func draw_start(position : Vector2) -> void: func draw_move(position : Vector2) -> void: + if _picking_color: # Still return even if we released Alt + if Input.is_action_pressed("alt"): + _pick_color(position) + return + if _draw_line: var d = _line_angle_constraint(_line_start, position) _line_end = d.position @@ -249,6 +260,9 @@ func draw_move(position : Vector2) -> void: func draw_end(_position : Vector2) -> void: + if _picking_color: + return + if _draw_line: draw_tool(_line_start) draw_fill_gap(_line_start, _line_end) diff --git a/src/Tools/ShapeDrawer.gd b/src/Tools/ShapeDrawer.gd index ef990a981..a7da963a9 100644 --- a/src/Tools/ShapeDrawer.gd +++ b/src/Tools/ShapeDrawer.gd @@ -84,6 +84,12 @@ func _input(event : InputEvent) -> void: func draw_start(position : Vector2) -> void: + if Input.is_action_pressed("alt"): + _picking_color = true + _pick_color(position) + return + _picking_color = false + Global.canvas.selection.transform_content_confirm() update_mask() @@ -94,6 +100,11 @@ func draw_start(position : Vector2) -> void: func draw_move(position : Vector2) -> void: + if _picking_color: # Still return even if we released Alt + if Input.is_action_pressed("alt"): + _pick_color(position) + return + if _drawing: if _displace_origin: _start += position - _offset @@ -103,6 +114,9 @@ func draw_move(position : Vector2) -> void: func draw_end(position : Vector2) -> void: + if _picking_color: + return + if _drawing: _draw_shape(_start, position)