-
-
Notifications
You must be signed in to change notification settings - Fork 248
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 d87b93d
Showing
8 changed files
with
111 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,63 @@ | ||
class_name ColorPickerTool extends CanvasTool | ||
|
||
# ------------------------------------------------------------------------------------------------- | ||
func tool_event(event: InputEvent) -> void: | ||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.is_pressed(): | ||
var picked_color = _pick_color(_cursor.global_position) | ||
if picked_color != Color.TRANSPARENT: | ||
GlobalSignals.color_changed.emit(picked_color) | ||
|
||
|
||
# ------------------------------------------------------------------------------------------------- | ||
func _pick_color(pos: Vector2) -> Color: | ||
var strokes = _canvas.get_all_strokes() | ||
# search the strokes in reverse order to prioritize latest strokes | ||
# since these are drawn on top and then are prioritized | ||
for i in range(strokes.size() - 1, -1, -1): | ||
var stroke = strokes[i] | ||
if stroke.points.is_empty(): | ||
continue | ||
|
||
# bounding box check | ||
var bbox = Rect2(stroke.top_left_pos, stroke.bottom_right_pos - stroke.top_left_pos) | ||
# grow the bounding box to enclose not only the stroke points but also its size | ||
bbox.grow(stroke.size) | ||
if not bbox.has_point(pos): | ||
continue | ||
|
||
# if the stroke has only 1 point, check the distance to this single point | ||
if stroke.points.size() == 1: | ||
if pos.distance_to(pos) < 0.5 * stroke.size: | ||
return stroke.color | ||
else: | ||
continue | ||
|
||
# if the stroke has more points, check if the distance between the cursor the and line | ||
# between two points is less than the stroke size | ||
# is used the formula to compute the distance between a point and a line | ||
for p in range(stroke.points.size() - 1): | ||
# the picker must be between the line points | ||
# so the dot product between the direction from the picker to the points must be large | ||
var p1: Vector2 = stroke.points[p] | ||
var p2: Vector2 = stroke.points[p + 1] | ||
# check first distance the distance between the first point | ||
if p1.distance_squared_to(pos) < (stroke.size * stroke.size): | ||
return stroke.color | ||
var dir1 = pos.direction_to(p1) | ||
var dir2 = pos.direction_to(p2) | ||
if dir1.dot(dir2) > 0.5: | ||
continue | ||
|
||
# formula to check the distance from the point to the line | ||
var a = p2.y - p1.y | ||
var b = p2.x - p1.x | ||
var c = p2.x * p1.y - p2.y * p1.x | ||
var denom = sqrt(a * a + b * b) | ||
if denom == 0.0: | ||
continue | ||
var dist = abs(a * pos.x - b * pos.y + c) / denom | ||
if dist < 0.5 * stroke.size: | ||
return stroke.color | ||
|
||
# if the pos is too far away from any stroke, return a transparent color instead | ||
return Color.TRANSPARENT |
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