-
-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added color picker tool, activated pressing Ctrl
- Loading branch information
Daniele Tolomelli
committed
Nov 28, 2024
1 parent
5cbb73d
commit 6762a2b
Showing
8 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
lorien/InfiniteCanvas/Cursor/ColorPickerCursor/ColorPickerCursor.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class_name ColorPickerCursor | ||
extends BaseCursor | ||
|
||
# ------------------------------------------------------------------------------------------------- | ||
func _on_zoom_changed(zoom_value: float) -> void: | ||
scale = Vector2.ONE / zoom_value |
16 changes: 16 additions & 0 deletions
16
lorien/InfiniteCanvas/Cursor/ColorPickerCursor/ColorPickerCursor.tscn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[gd_scene load_steps=5 format=3 uid="uid://dmybbclki2clv"] | ||
|
||
[ext_resource type="Shader" path="res://InfiniteCanvas/Cursor/cursor.gdshader" id="1_q5575"] | ||
[ext_resource type="Script" path="res://InfiniteCanvas/Cursor/ColorPickerCursor/ColorPickerCursor.gd" id="2_labi7"] | ||
[ext_resource type="Texture2D" uid="uid://tcovt1vw06tr" path="res://Assets/Icons/color_picker.png" id="3_rydea"] | ||
|
||
[sub_resource type="ShaderMaterial" id="1"] | ||
shader = ExtResource("1_q5575") | ||
|
||
[node name="ColorPickerCursor" type="Sprite2D"] | ||
material = SubResource("1") | ||
script = ExtResource("2_labi7") | ||
|
||
[node name="Sprite2D" type="Sprite2D" parent="."] | ||
position = Vector2(7, -7) | ||
texture = ExtResource("3_rydea") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
class_name ColorPickerTool extends CanvasTool | ||
|
||
|
||
# ------------------------------------------------------------------------------------------------- | ||
func tool_event(event: InputEvent) -> void: | ||
if ( | ||
event is InputEventMouseButton | ||
&& event.button_index == MOUSE_BUTTON_LEFT | ||
&& event.is_pressed() | ||
): | ||
var mouseEvent := event as InputEventMouseButton | ||
var picked_color := _pick_color(mouseEvent.position) | ||
if picked_color != Color.TRANSPARENT: | ||
GlobalSignals.color_changed.emit(picked_color) | ||
|
||
|
||
# ------------------------------------------------------------------------------------------------- | ||
func _pick_color(pos: Vector2) -> Color: | ||
var colorPicked := _canvas._viewport.get_texture().get_image().get_pixel(pos.x, pos.y) | ||
# the exact color picked may be a hue of the original stroke because of antialiasing | ||
# to fix this, search along every stroke for the color that has an more equal color the picked one | ||
var minDelta := 0.5 | ||
var bestColorMatch := Color.TRANSPARENT | ||
var strokes := _canvas.get_all_strokes() | ||
for stroke in strokes: | ||
var c := stroke.color | ||
var colorDelta: float = ( | ||
abs(colorPicked.r - c.r) + abs(colorPicked.g - c.g) + abs(colorPicked.b - c.b) | ||
) | ||
if colorDelta < minDelta: | ||
bestColorMatch = c | ||
minDelta = colorDelta | ||
return bestColorMatch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters