1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Add an interpolation OptionButton to GradientEdit

This commit is contained in:
Emmanouil Papadeas 2022-08-09 02:02:46 +03:00
parent 494397502a
commit c66cac2fd8
5 changed files with 69 additions and 31 deletions

View file

@ -453,6 +453,9 @@ msgstr ""
msgid "Trilinear"
msgstr ""
msgid "Constant"
msgstr ""
msgid "General"
msgstr ""

View file

@ -44,7 +44,7 @@ _global_script_classes=[ {
"language": "GDScript",
"path": "res://src/Classes/Frame.gd"
}, {
"base": "TextureRect",
"base": "Control",
"class": "GradientEditNode",
"language": "GDScript",
"path": "res://src/UI/Nodes/GradientEdit.gd"

View file

@ -50,7 +50,6 @@ anchor_bottom = 0.0
margin_top = 204.0
margin_right = 278.0
margin_bottom = 234.0
rect_min_size = Vector2( 0, 30 )
[node name="OptionsContainer" type="GridContainer" parent="VBoxContainer"]
margin_top = 238.0

View file

@ -2,7 +2,7 @@
# gdlint: ignore=max-line-length
# https://github.com/RodZill4/material-maker/blob/master/material_maker/widgets/gradient_editor/gradient_editor.gd
class_name GradientEditNode
extends TextureRect
extends Control
signal updated(gradient, cc)
@ -10,6 +10,8 @@ var continuous_change := true
var active_cursor: GradientCursor # Showing a color picker popup to change a cursor's color
onready var x_offset: float = rect_size.x - GradientCursor.WIDTH
onready var texture_rect: TextureRect = $TextureRect
onready var texture: Texture = $TextureRect.texture
onready var gradient: Gradient = texture.gradient
onready var color_picker: ColorPicker = $Popup.get_node("ColorPicker")
@ -20,7 +22,10 @@ class GradientCursor:
const WIDTH := 10
var color: Color
var sliding := false
onready var label: Label = get_parent().get_node("Value")
onready var parent: TextureRect = get_parent()
onready var grand_parent: Container = parent.get_parent()
onready var label: Label = parent.get_node("Value")
func _ready() -> void:
rect_position = Vector2(0, 15)
@ -47,20 +52,19 @@ class GradientCursor:
if ev is InputEventMouseButton:
if ev.button_index == BUTTON_LEFT:
if ev.doubleclick:
get_parent().select_color(self, ev.global_position)
grand_parent.select_color(self, ev.global_position)
elif ev.pressed:
get_parent().continuous_change = false
grand_parent.continuous_change = false
sliding = true
label.visible = true
label.text = "%.03f" % get_cursor_position()
else:
sliding = false
label.visible = false
elif ev.button_index == BUTTON_RIGHT and get_parent().get_sorted_cursors().size() > 2:
var parent = get_parent()
elif ev.button_index == BUTTON_RIGHT and grand_parent.get_sorted_cursors().size() > 2:
parent.remove_child(self)
parent.continuous_change = false
parent.update_from_value()
grand_parent.continuous_change = false
grand_parent.update_from_value()
queue_free()
elif ev is InputEventMouseMotion and (ev.button_mask & BUTTON_MASK_LEFT) != 0 and sliding:
rect_position.x += get_local_mouse_position().x
@ -68,18 +72,18 @@ class GradientCursor:
rect_position.x = (
round(get_cursor_position() * 20.0)
* 0.05
* (get_parent().rect_size.x - WIDTH)
* (parent.rect_size.x - WIDTH)
)
rect_position.x = min(max(0, rect_position.x), get_parent().rect_size.x - rect_size.x)
get_parent().update_from_value()
rect_position.x = min(max(0, rect_position.x), parent.rect_size.x - rect_size.x)
grand_parent.update_from_value()
label.text = "%.03f" % get_cursor_position()
func get_cursor_position() -> float:
return rect_position.x / (get_parent().rect_size.x - WIDTH)
return rect_position.x / (parent.rect_size.x - WIDTH)
func set_color(c: Color) -> void:
color = c
get_parent().update_from_value()
grand_parent.update_from_value()
update()
static func sort(a, b) -> bool:
@ -97,9 +101,9 @@ func _ready() -> void:
func create_cursors() -> void:
for c in get_children():
for c in texture_rect.get_children():
if c is GradientCursor:
remove_child(c)
texture_rect.remove_child(c)
c.queue_free()
for i in gradient.get_point_count():
var p: float = gradient.get_offset(i)
@ -116,7 +120,7 @@ func _gui_input(ev: InputEvent) -> void:
func update_from_value() -> void:
gradient.offsets = []
for c in get_children():
for c in texture_rect.get_children():
if c is GradientCursor:
var point: float = c.rect_position.x / x_offset
gradient.add_point(point, c.color)
@ -126,7 +130,7 @@ func update_from_value() -> void:
func add_cursor(x: float, color: Color) -> void:
var cursor := GradientCursor.new()
add_child(cursor)
texture_rect.add_child(cursor)
cursor.rect_position.x = x
cursor.color = color
@ -144,7 +148,7 @@ func select_color(cursor: GradientCursor, position: Vector2) -> void:
func get_sorted_cursors() -> Array:
var array := []
for c in get_children():
for c in texture_rect.get_children():
if c is GradientCursor:
array.append(c)
array.sort_custom(GradientCursor, "sort")
@ -164,3 +168,7 @@ func _on_GradientEdit_resized() -> void:
return
x_offset = rect_size.x - GradientCursor.WIDTH
create_cursors()
func _on_InterpolationOptionButton_item_selected(index: int) -> void:
gradient.interpolation_mode = index

View file

@ -7,12 +7,28 @@
[sub_resource type="GradientTexture" id=2]
gradient = SubResource( 1 )
[node name="GradientEdit" type="TextureRect"]
[node name="GradientEdit" type="VBoxContainer"]
anchor_right = 1.0
anchor_bottom = 1.0
script = ExtResource( 1 )
[node name="TextureRect" type="TextureRect" parent="."]
margin_right = 1280.0
margin_bottom = 696.0
rect_min_size = Vector2( 0, 30 )
size_flags_vertical = 3
texture = SubResource( 2 )
expand = true
script = ExtResource( 1 )
[node name="Value" type="Label" parent="TextureRect"]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -20.0
margin_top = -7.0
margin_right = 20.0
margin_bottom = 7.0
[node name="Popup" type="PopupPanel" parent="."]
margin_right = 316.0
@ -24,15 +40,27 @@ margin_top = 4.0
margin_right = 312.0
margin_bottom = 466.0
[node name="Value" type="Label" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = -20.0
margin_top = -7.0
margin_right = 20.0
margin_bottom = 7.0
[node name="InterpolationContainer" type="HBoxContainer" parent="."]
margin_top = 700.0
margin_right = 1280.0
margin_bottom = 720.0
[node name="Label" type="Label" parent="InterpolationContainer"]
margin_top = 3.0
margin_right = 87.0
margin_bottom = 17.0
text = "Interpolation:"
[node name="InterpolationOptionButton" type="OptionButton" parent="InterpolationContainer"]
margin_left = 91.0
margin_right = 1280.0
margin_bottom = 20.0
mouse_default_cursor_shape = 2
size_flags_horizontal = 3
text = "Linear"
items = [ "Linear", null, false, 0, null, "Constant", null, false, 1, null, "Cubic", null, false, 2, null ]
selected = 0
[connection signal="resized" from="." to="." method="_on_GradientEdit_resized"]
[connection signal="color_changed" from="Popup/ColorPicker" to="." method="_on_ColorPicker_color_changed"]
[connection signal="item_selected" from="InterpolationContainer/InterpolationOptionButton" to="." method="_on_InterpolationOptionButton_item_selected"]