Skip to content

Commit fce6f6f

Browse files
Daniele TolomelliDaniele Tolomelli
Daniele Tolomelli
authored and
Daniele Tolomelli
committed
Added color picker tool, activated pressing Ctrl
1 parent 5cbb73d commit fce6f6f

File tree

8 files changed

+74
-0
lines changed

8 files changed

+74
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class_name ColorPickerCursor
2+
extends BaseCursor
3+
4+
# -------------------------------------------------------------------------------------------------
5+
func _on_zoom_changed(zoom_value: float) -> void:
6+
scale = Vector2.ONE / zoom_value
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[gd_scene load_steps=5 format=3 uid="uid://dmybbclki2clv"]
2+
3+
[ext_resource type="Shader" path="res://InfiniteCanvas/Cursor/cursor.gdshader" id="1_q5575"]
4+
[ext_resource type="Script" path="res://InfiniteCanvas/Cursor/ColorPickerCursor/ColorPickerCursor.gd" id="2_labi7"]
5+
[ext_resource type="Texture2D" uid="uid://tcovt1vw06tr" path="res://Assets/Icons/color_picker.png" id="3_rydea"]
6+
7+
[sub_resource type="ShaderMaterial" id="1"]
8+
shader = ExtResource("1_q5575")
9+
10+
[node name="ColorPickerCursor" type="Sprite2D"]
11+
material = SubResource("1")
12+
script = ExtResource("2_labi7")
13+
14+
[node name="Sprite2D" type="Sprite2D" parent="."]
15+
position = Vector2(7, -7)
16+
texture = ExtResource("3_rydea")

lorien/InfiniteCanvas/InfiniteCanvas.gd

+15
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const PLAYER = preload("res://Misc/Player/Player.tscn")
1212
@onready var _circle_tool: CircleTool = $CircleTool
1313
@onready var _eraser_tool: EraserTool = $EraserTool
1414
@onready var _selection_tool: SelectionTool = $SelectionTool
15+
@onready var _color_picker_tool: ColorPickerTool = $ColorPickerTool
1516
@onready var _active_tool: CanvasTool = _brush_tool
1617
@onready var _active_tool_type: int = Types.Tool.BRUSH
1718
@onready var _strokes_parent: Node2D = $SubViewport/Strokes
@@ -88,6 +89,16 @@ func _process_event(event: InputEvent) -> void:
8889
else:
8990
# restore tool from type
9091
use_tool(_active_tool_type)
92+
93+
if event is InputEventKey:
94+
var keyEvent: InputEventKey = event as InputEventKey
95+
if keyEvent.keycode == KEY_CTRL:
96+
if keyEvent.pressed:
97+
var tool_type := _active_tool_type
98+
use_tool(Types.Tool.COLOR_PICKER)
99+
_active_tool_type = tool_type
100+
else:
101+
use_tool(_active_tool_type)
91102

92103
if event.is_action("deselect_all_strokes"):
93104
if _active_tool == _selection_tool:
@@ -133,6 +144,10 @@ func use_tool(tool_type: int) -> void:
133144
Types.Tool.SELECT:
134145
_active_tool = _selection_tool
135146
_use_optimizer = false
147+
Types.Tool.COLOR_PICKER:
148+
_active_tool = _color_picker_tool
149+
_use_optimizer = false
150+
136151

137152
if prev_tool != _active_tool:
138153
prev_tool.enabled = false

lorien/InfiniteCanvas/InfiniteCanvas.tscn

+8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/SelectionTool.gd" id="7"]
1010
[ext_resource type="PackedScene" path="res://InfiniteCanvas/Cursor/SelectionCursor/SelectionCursor.tscn" id="8"]
1111
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/SelectionRectangle.gd" id="9"]
12+
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/ColorPickerTool.gd" id="9_xqyd5"]
1213
[ext_resource type="Shader" path="res://InfiniteCanvas/Tools/selection_rectangle.gdshader" id="10"]
1314
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/RectangleTool.gd" id="11"]
1415
[ext_resource type="Script" path="res://InfiniteCanvas/InfiniteCanvasGrid.gd" id="12"]
1516
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/CircleTool.gd" id="13"]
1617
[ext_resource type="Script" path="res://InfiniteCanvas/Tools/EraserTool.gd" id="14"]
1718
[ext_resource type="Script" path="res://InfiniteCanvas/DebugDraw.gd" id="15"]
19+
[ext_resource type="PackedScene" uid="uid://dmybbclki2clv" path="res://InfiniteCanvas/Cursor/ColorPickerCursor/ColorPickerCursor.tscn" id="16_mifpa"]
1820

1921
[sub_resource type="ShaderMaterial" id="1"]
2022
shader = ExtResource("10")
@@ -55,6 +57,10 @@ script = ExtResource("7")
5557
selection_rectangle_path = NodePath("../SubViewport/SelectionRectangle")
5658
cursor_path = NodePath("../SubViewport/SelectionCursor")
5759

60+
[node name="ColorPickerTool" type="Node" parent="."]
61+
script = ExtResource("9_xqyd5")
62+
cursor_path = NodePath("../SubViewport/ColorPickerCursor")
63+
5864
[node name="SubViewport" type="SubViewport" parent="."]
5965
handle_input_locally = false
6066
size = Vector2i(1920, 1080)
@@ -80,3 +86,5 @@ script = ExtResource("9")
8086
[node name="BrushCursor" parent="SubViewport" instance=ExtResource("4")]
8187

8288
[node name="SelectionCursor" parent="SubViewport" instance=ExtResource("8")]
89+
90+
[node name="ColorPickerCursor" parent="SubViewport" instance=ExtResource("16_mifpa")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class_name ColorPickerTool extends CanvasTool
2+
3+
# -------------------------------------------------------------------------------------------------
4+
func tool_event(event: InputEvent) -> void:
5+
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed():
6+
var mouseEvent = event as InputEventMouseButton
7+
var picked_color = _pick_color(mouseEvent.position)
8+
if picked_color != Color.TRANSPARENT:
9+
GlobalSignals.color_changed.emit(picked_color)
10+
11+
12+
# -------------------------------------------------------------------------------------------------
13+
func _pick_color(pos: Vector2) -> Color:
14+
var colorPicked = _canvas._viewport.get_texture().get_image().get_pixel(pos.x, pos.y)
15+
# the exact color picked may be a hue of the original stroke because of antialiasing
16+
# to fix this, search along every stroke for the color that has an more equal color the picked one
17+
var minDelta = 0.5;
18+
var bestColorMatch = Color.TRANSPARENT
19+
var strokes = _canvas.get_all_strokes()
20+
for stroke in strokes:
21+
var c = stroke.color
22+
var colorDelta = abs(colorPicked.r - c.r) + abs(colorPicked.g - c.g) + abs(colorPicked.b - c.b)
23+
if colorDelta < minDelta:
24+
bestColorMatch = c
25+
minDelta = colorDelta
26+
return bestColorMatch

lorien/Main.gd

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func _ready() -> void:
5252

5353
_brush_color_picker.closed.connect(_on_BrushColorPicker_closed)
5454
_brush_color_picker.color_changed.connect(_on_BrushColorPicker_color_changed)
55+
GlobalSignals.connect("color_changed", self._on_BrushColorPicker_color_changed)
5556

5657
_new_palette_dialog.new_palette_created.connect(_on_NewPaletteDialog_new_palette_created)
5758
_delete_palette_dialog.palette_deleted.connect(_on_DeletePaletteDialog_palette_deleted)

lorien/Misc/GlobalSignals.gd

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ extends Node
33
# -------------------------------------------------------------------------------------------------
44
signal language_changed
55
signal keybinding_changed(action: KeybindingsManager.Action)
6+
signal color_changed(color: Color)

lorien/Misc/Types.gd

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ enum Tool {
99
LINE,
1010
ERASER,
1111
SELECT,
12+
COLOR_PICKER
1213
}
1314

1415
# -------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)