2018-07-28 09:37:51 +02:00
|
|
|
tool
|
2018-07-27 08:49:54 +02:00
|
|
|
extends Control
|
2019-09-26 22:32:49 +02:00
|
|
|
class_name MMGradientEditor
|
2018-07-27 08:49:54 +02:00
|
|
|
|
|
|
|
class GradientCursor:
|
2019-10-22 22:48:00 +02:00
|
|
|
extends Control
|
|
|
|
|
|
|
|
var color : Color
|
2019-10-20 16:22:06 +02:00
|
|
|
|
2019-10-22 22:48:00 +02:00
|
|
|
const WIDTH : int = 10
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func _ready() -> void:
|
2018-07-27 08:49:54 +02:00
|
|
|
rect_position = Vector2(0, 15)
|
2018-09-09 12:09:05 +02:00
|
|
|
rect_size = Vector2(WIDTH, 15)
|
2019-10-22 22:48:00 +02:00
|
|
|
|
|
|
|
func _draw():
|
|
|
|
var polygon : PoolVector2Array = PoolVector2Array([Vector2(0, 5), Vector2(WIDTH/2, 0), Vector2(WIDTH, 5), Vector2(WIDTH, 15), Vector2(0, 15)])
|
|
|
|
var c = color
|
|
|
|
c.a = 1.0
|
|
|
|
draw_colored_polygon(polygon, c)
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func _gui_input(ev) -> void:
|
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()
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func get_position() -> Vector2:
|
2018-07-27 08:49:54 +02:00
|
|
|
return rect_position.x / (get_parent().rect_size.x - WIDTH)
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
func set_color(c) -> void:
|
2018-07-27 08:49:54 +02:00
|
|
|
color = c
|
2018-09-09 12:09:05 +02:00
|
|
|
get_parent().update_value()
|
2019-10-29 22:10:54 +01:00
|
|
|
update()
|
2019-10-20 16:22:06 +02:00
|
|
|
|
|
|
|
static func sort(a, b) -> bool:
|
|
|
|
return a.get_position() < b.get_position()
|
|
|
|
|
2018-09-09 12:09:05 +02:00
|
|
|
var value = null setget set_value
|
2019-10-06 10:02:09 +02:00
|
|
|
export var embedded : bool = true
|
2018-09-09 12:09:05 +02:00
|
|
|
|
2019-08-09 08:16:24 +02:00
|
|
|
signal updated(value)
|
2018-07-28 09:37:51 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _ready() -> void:
|
2018-07-27 08:49:54 +02:00
|
|
|
$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
|
|
|
|
2019-10-30 23:20:18 +01:00
|
|
|
func get_gradient_from_data(data):
|
|
|
|
if typeof(data) == TYPE_ARRAY:
|
|
|
|
return data
|
|
|
|
elif typeof(data) == TYPE_DICTIONARY:
|
|
|
|
if data.has("parameters") and data.parameters.has("gradient"):
|
|
|
|
return data.parameters.gradient
|
|
|
|
if data.has("type") and data.type == "Gradient":
|
|
|
|
return data
|
|
|
|
return null
|
|
|
|
|
|
|
|
func get_drag_data(position : Vector2):
|
|
|
|
var data = MMType.serialize_value(value)
|
|
|
|
var preview = ColorRect.new()
|
|
|
|
preview.rect_size = Vector2(64, 24)
|
|
|
|
preview.material = $Gradient.material
|
|
|
|
set_drag_preview(preview)
|
|
|
|
return data
|
|
|
|
|
|
|
|
func can_drop_data(position : Vector2, data) -> bool:
|
|
|
|
return get_gradient_from_data(data) != null
|
|
|
|
|
|
|
|
func drop_data(position : Vector2, data) -> void:
|
|
|
|
var gradient = get_gradient_from_data(data)
|
|
|
|
if gradient != null:
|
|
|
|
set_value(MMType.deserialize_value(gradient))
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func set_value(v) -> void:
|
2018-09-09 12:09:05 +02:00
|
|
|
value = v
|
|
|
|
for c in get_children():
|
2019-10-06 10:02:09 +02:00
|
|
|
if c is GradientCursor:
|
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)
|
2019-10-30 22:57:51 +01:00
|
|
|
$Interpolation.selected = value.interpolation
|
2018-09-09 12:09:05 +02:00
|
|
|
update_shader()
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func update_value() -> void:
|
2018-09-09 12:09:05 +02:00
|
|
|
value.clear()
|
2019-10-30 22:57:51 +01:00
|
|
|
for c in get_children():
|
|
|
|
if c is GradientCursor:
|
|
|
|
value.add_point(c.rect_position.x/(rect_size.x-GradientCursor.WIDTH), c.color)
|
2018-09-09 12:09:05 +02:00
|
|
|
update_shader()
|
2018-07-27 08:49:54 +02:00
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func add_cursor(x, color) -> void:
|
2018-07-27 08:49:54 +02:00
|
|
|
var cursor = GradientCursor.new()
|
|
|
|
add_child(cursor)
|
|
|
|
cursor.rect_position.x = x
|
|
|
|
cursor.color = color
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _gui_input(ev) -> void:
|
2019-10-06 10:02:09 +02:00
|
|
|
if ev is InputEventMouseButton && ev.button_index == 1 && ev.doubleclick:
|
|
|
|
if ev.position.y > 15:
|
2019-10-22 22:48:00 +02:00
|
|
|
var p = clamp(ev.position.x, 0, rect_size.x-GradientCursor.WIDTH)
|
2019-10-06 10:02:09 +02:00
|
|
|
add_cursor(p, get_gradient_color(p))
|
|
|
|
update_value()
|
|
|
|
elif embedded:
|
|
|
|
var popup = load("res://addons/material_maker/widgets/gradient_popup.tscn").instance()
|
|
|
|
add_child(popup)
|
|
|
|
var popup_size = popup.rect_size
|
|
|
|
popup.popup(Rect2(ev.global_position, Vector2(0, 0)))
|
|
|
|
popup.set_global_position(ev.global_position-Vector2(popup_size.x / 2, popup_size.y))
|
|
|
|
popup.init(value)
|
|
|
|
popup.connect("updated", self, "set_value")
|
2018-07-27 08:49:54 +02:00
|
|
|
|
|
|
|
# Showing a color picker popup to change a cursor's color
|
|
|
|
|
|
|
|
var active_cursor
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func select_color(cursor, position) -> void:
|
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()
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func _on_Popup_popup_hide() -> void:
|
2018-07-27 08:49:54 +02:00
|
|
|
$Gradient/Popup/ColorPicker.disconnect("color_changed", active_cursor, "set_color")
|
|
|
|
|
|
|
|
# Calculating a color from the gradient and generating the shader
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_sorted_cursors() -> Array:
|
2019-10-30 22:57:51 +01:00
|
|
|
var array = []
|
|
|
|
for c in get_children():
|
|
|
|
if c is GradientCursor:
|
|
|
|
array.append(c)
|
2018-07-27 08:49:54 +02:00
|
|
|
array.sort_custom(GradientCursor, "sort")
|
|
|
|
return array
|
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func get_gradient_color(x) -> Color:
|
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
|
|
|
|
2019-10-20 16:22:06 +02:00
|
|
|
func update_shader() -> void:
|
2018-07-27 08:49:54 +02:00
|
|
|
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)
|
2019-10-30 22:57:51 +01:00
|
|
|
|
|
|
|
func _on_Interpolation_item_selected(ID):
|
|
|
|
value.interpolation = ID
|
|
|
|
update_shader()
|