2018-07-28 09:37:51 +02:00
|
|
|
tool
|
2018-07-27 08:49:54 +02:00
|
|
|
extends Control
|
|
|
|
|
|
|
|
class GradientCursor:
|
|
|
|
extends ColorRect
|
|
|
|
|
|
|
|
const WIDTH = 10
|
|
|
|
|
|
|
|
func _ready():
|
|
|
|
rect_position = Vector2(0, 15)
|
2018-09-09 12:09:05 +02:00
|
|
|
rect_size = Vector2(WIDTH, 15)
|
2018-11-03 17:43:32 +01:00
|
|
|
|
2018-07-27 08:49:54 +02:00
|
|
|
func _gui_input(ev):
|
2018-08-06 22:05:26 +02:00
|
|
|
if ev is InputEventMouseButton:
|
|
|
|
if ev.button_index == BUTTON_LEFT && ev.doubleclick:
|
2018-07-29 11:55:17 +02:00
|
|
|
get_parent().select_color(self, ev.global_position)
|
2018-08-06 22:05:26 +02:00
|
|
|
elif ev.button_index == BUTTON_RIGHT && get_parent().get_sorted_cursors().size() > 2:
|
2018-07-28 21:21:05 +02:00
|
|
|
var parent = get_parent()
|
|
|
|
parent.remove_child(self)
|
2018-09-09 12:09:05 +02:00
|
|
|
parent.update_value()
|
2018-07-28 21:21:05 +02:00
|
|
|
queue_free()
|
2018-07-27 08:49:54 +02:00
|
|
|
elif ev is InputEventMouseMotion && (ev.button_mask & 1) != 0:
|
|
|
|
rect_position.x += ev.relative.x
|
|
|
|
rect_position.x = min(max(0, rect_position.x), get_parent().rect_size.x-rect_size.x)
|
2018-09-09 12:09:05 +02:00
|
|
|
get_parent().update_value()
|
2018-07-27 08:49:54 +02:00
|
|
|
|
|
|
|
func get_position():
|
|
|
|
return rect_position.x / (get_parent().rect_size.x - WIDTH)
|
|
|
|
|
|
|
|
func set_color(c):
|
|
|
|
color = c
|
2018-09-09 12:09:05 +02:00
|
|
|
get_parent().update_value()
|
2018-11-03 17:43:32 +01:00
|
|
|
|
2018-07-27 08:49:54 +02:00
|
|
|
static func sort(a, b):
|
|
|
|
if a.get_position() < b.get_position():
|
|
|
|
return true
|
|
|
|
return false
|
2018-11-03 17:43:32 +01:00
|
|
|
|
|
|
|
func _draw():
|
|
|
|
var c = color
|
|
|
|
c.a = 1.0
|
|
|
|
draw_rect(Rect2(0, 0, rect_size.x, rect_size.y), c, false)
|
2018-07-27 08:49:54 +02:00
|
|
|
|
2018-09-09 12:09:05 +02:00
|
|
|
var value = null setget set_value
|
|
|
|
|
2019-08-09 08:16:24 +02:00
|
|
|
signal updated(value)
|
2018-07-28 09:37:51 +02:00
|
|
|
|
2018-07-27 08:49:54 +02:00
|
|
|
func _ready():
|
|
|
|
$Gradient.material = $Gradient.material.duplicate(true)
|
2019-08-09 08:16:24 +02:00
|
|
|
set_value(MMGradient.new())
|
2018-09-09 12:09:05 +02:00
|
|
|
|
|
|
|
func set_value(v):
|
|
|
|
value = v
|
|
|
|
for c in get_children():
|
2018-11-03 17:43:32 +01:00
|
|
|
if c != $Gradient && c != $Background:
|
2018-09-09 12:09:05 +02:00
|
|
|
remove_child(c)
|
|
|
|
c.free()
|
|
|
|
for p in value.points:
|
|
|
|
add_cursor(p.v*(rect_size.x-GradientCursor.WIDTH), p.c)
|
|
|
|
update_shader()
|
|
|
|
|
|
|
|
func update_value():
|
|
|
|
value.clear()
|
|
|
|
for p in get_children():
|
2018-11-03 17:43:32 +01:00
|
|
|
if p != $Gradient && p != $Background:
|
2018-09-09 12:09:05 +02:00
|
|
|
value.add_point(p.rect_position.x/(rect_size.x-GradientCursor.WIDTH), p.color)
|
|
|
|
update_shader()
|
2018-07-27 08:49:54 +02:00
|
|
|
|
|
|
|
func add_cursor(x, color):
|
|
|
|
var cursor = GradientCursor.new()
|
|
|
|
add_child(cursor)
|
|
|
|
cursor.rect_position.x = x
|
|
|
|
cursor.color = color
|
|
|
|
|
|
|
|
func _gui_input(ev):
|
|
|
|
if ev is InputEventMouseButton && ev.button_index == 1 && ev.doubleclick && ev.position.y > 15:
|
2018-07-28 21:21:05 +02:00
|
|
|
var p = max(0, min(ev.position.x, rect_size.x-GradientCursor.WIDTH))
|
2018-11-04 15:43:02 +01:00
|
|
|
add_cursor(p, get_gradient_color(p))
|
2018-09-09 12:09:05 +02:00
|
|
|
update_value()
|
2018-07-27 08:49:54 +02:00
|
|
|
|
|
|
|
# Showing a color picker popup to change a cursor's color
|
|
|
|
|
|
|
|
var active_cursor
|
|
|
|
|
2018-07-29 11:55:17 +02:00
|
|
|
func select_color(cursor, position):
|
2018-07-27 08:49:54 +02:00
|
|
|
active_cursor = cursor
|
2018-08-04 08:05:34 +02:00
|
|
|
$Gradient/Popup/ColorPicker.color = cursor.color
|
2018-07-27 08:49:54 +02:00
|
|
|
$Gradient/Popup/ColorPicker.connect("color_changed", cursor, "set_color")
|
2018-07-29 11:55:17 +02:00
|
|
|
$Gradient/Popup.rect_position = position
|
2018-07-27 08:49:54 +02:00
|
|
|
$Gradient/Popup.popup()
|
|
|
|
|
|
|
|
func _on_Popup_popup_hide():
|
|
|
|
$Gradient/Popup/ColorPicker.disconnect("color_changed", active_cursor, "set_color")
|
|
|
|
|
|
|
|
# Calculating a color from the gradient and generating the shader
|
|
|
|
|
|
|
|
func get_sorted_cursors():
|
|
|
|
var array = get_children()
|
|
|
|
array.erase($Gradient)
|
2018-11-03 17:43:32 +01:00
|
|
|
array.erase($Background)
|
2018-07-27 08:49:54 +02:00
|
|
|
array.sort_custom(GradientCursor, "sort")
|
|
|
|
return array
|
|
|
|
|
2018-11-04 15:43:02 +01:00
|
|
|
func get_gradient_color(x):
|
2018-09-09 12:09:05 +02:00
|
|
|
return value.get_color(x / (rect_size.x - GradientCursor.WIDTH))
|
2018-07-27 08:49:54 +02:00
|
|
|
|
|
|
|
func update_shader():
|
|
|
|
var shader
|
|
|
|
shader = "shader_type canvas_item;\n"
|
2018-09-09 12:09:05 +02:00
|
|
|
shader += value.get_shader("gradient")
|
2018-11-03 17:43:32 +01:00
|
|
|
shader += "void fragment() { COLOR = gradient(UV.x); }"
|
2018-07-27 08:49:54 +02:00
|
|
|
$Gradient.material.shader.set_code(shader)
|
2018-09-09 12:09:05 +02:00
|
|
|
emit_signal("updated", value)
|