Added value label to gradient editor cursors + position rounding when Control is held

This commit is contained in:
RodZill4 2021-02-08 21:04:48 +01:00
parent a1f1407e3d
commit dd91551f53
1 changed files with 14 additions and 2 deletions

View File

@ -6,11 +6,17 @@ class GradientCursor:
var color : Color
var sliding : bool = false
var label : Label = Label.new()
const WIDTH : int = 10
func _ready() -> void:
rect_position = Vector2(0, 15)
rect_size = Vector2(WIDTH, 15)
label.rect_position = Vector2(-20, 20)
label.rect_min_size = Vector2(50, 20)
label.align = Label.ALIGN_LEFT
add_child(label)
func _draw() -> void:
# warning-ignore:integer_division
@ -27,19 +33,25 @@ class GradientCursor:
get_parent().select_color(self, ev.global_position)
elif ev.pressed:
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()
parent.remove_child(self)
parent.update_value()
queue_free()
elif ev is InputEventMouseMotion and (ev.button_mask & BUTTON_MASK_LEFT) != 0 and sliding:
rect_position.x += ev.relative.x
rect_position.x += get_local_mouse_position().x
if ev.control:
rect_position.x = round(get_cursor_position()*20.0)*0.05*(get_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_value()
label.text = "%.03f" % get_cursor_position()
func get_position() -> Vector2:
func get_cursor_position() -> float:
return rect_position.x / (get_parent().rect_size.x - WIDTH)
func set_color(c) -> void: