Added new float_edit widget to replace the spinbox and the hslider

This commit is contained in:
RodZill4 2019-11-05 22:28:55 +01:00
parent e341e25e7f
commit 5141b3e297
3 changed files with 117 additions and 16 deletions

View File

@ -24,12 +24,10 @@ func on_parameter_changed(p, v) -> void:
call_deferred("update_node")
elif controls.has(p):
var o = controls[p]
if o is LineEdit:
if o is MMFloatEdit:
o.value = v
elif o is LineEdit:
o.text = v
elif o is SpinBox:
o.value = v
elif o is HSlider:
o.value = v
elif o is SizeOptionButton:
o.size_value = v
elif o is OptionButton:
@ -56,12 +54,10 @@ func initialize_properties() -> void:
var o = controls[c]
if generator.parameters.has(c):
on_parameter_changed(c, generator.parameters[c])
if o is LineEdit:
if o is MMFloatEdit:
o.connect("value_changed", self, "_on_value_changed", [ o.name ])
elif o is LineEdit:
o.connect("text_changed", self, "_on_text_changed", [ o.name ])
elif o is SpinBox:
o.connect("value_changed", self, "_on_value_changed", [ o.name ])
elif o is HSlider:
o.connect("value_changed", self, "_on_value_changed", [ o.name ])
elif o is SizeOptionButton:
o.connect("size_value_changed", self, "_on_value_changed", [ o.name ])
elif o is OptionButton:
@ -106,15 +102,10 @@ func _on_gradient_changed(new_gradient, variable) -> void:
func create_parameter_control(p : Dictionary) -> Control:
var control = null
if p.type == "float":
if p.has("widget") and p.widget == "spinbox":
control = SpinBox.new()
else:
control = preload("res://addons/material_maker/widgets/hslider.tscn").instance()
control = preload("res://addons/material_maker/widgets/float_edit.tscn").instance()
control.min_value = p.min
control.max_value = p.max
control.step = 0.005 if !p.has("step") else p.step
control.allow_greater = true
control.allow_lesser = true
if p.has("default"):
control.value = p.default
control.rect_min_size.x = 80

View File

@ -0,0 +1,70 @@
tool
extends LineEdit
class_name MMFloatEdit
export var value : float = 0.5 setget set_value
export var min_value : float = 0.0 setget set_min_value
export var max_value : float = 1.0 setget set_max_value
export var step : float = 0.0 setget set_step
var start_position : float
var start_value : float
onready var slider = $Slider
onready var cursor = $Slider/Cursor
signal value_changed
func _ready() -> void:
do_update()
func set_value(v : float) -> void:
value = v
do_update()
func set_min_value(v : float) -> void:
min_value = v
do_update()
func set_max_value(v : float) -> void:
max_value = v
do_update()
func set_step(v : float) -> void:
step = v
do_update()
func do_update(update_text : bool = true) -> void:
if update_text:
text = str(value)
if cursor != null:
cursor.rect_position.x = (clamp(value, min_value, max_value)-min_value)*(slider.rect_size.x-cursor.rect_size.x)/(max_value-min_value)
func _on_LineEdit_gui_input(event : InputEvent) -> void:
if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
if event.is_pressed():
start_position = event.position.x
start_value = value
elif event is InputEventMouseMotion and event.button_mask == BUTTON_MASK_LEFT:
var delta : float = event.position.x-start_position
var v : float = clamp(start_value+sign(delta)*pow(abs(delta)*0.005, 2)*abs(max_value - min_value), min_value, max_value)
if step != 0:
v = min_value+floor((v - min_value)/step)*step
set_value(v)
select(0, 0)
emit_signal("value_changed", value)
func _on_LineEdit_text_changed(new_text : String) -> void:
if new_text.is_valid_float():
value = new_text.to_float()
do_update(false)
func _on_LineEdit_text_entered(new_text : String) -> void:
if new_text.is_valid_float():
value = new_text.to_float()
do_update()
emit_signal("value_changed", value)
func _on_LineEdit_focus_exited() -> void:
do_update()
emit_signal("value_changed", value)

View File

@ -0,0 +1,40 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://addons/material_maker/widgets/float_edit.gd" type="Script" id=1]
[node name="LineEdit" type="LineEdit"]
anchor_left = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = -1280.0
margin_right = -1222.0
margin_bottom = -696.0
text = "0.5"
max_length = 20
context_menu_enabled = false
caret_blink = true
script = ExtResource( 1 )
[node name="Slider" type="ColorRect" parent="."]
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 2.0
margin_top = -3.0
margin_right = -2.0
margin_bottom = -3.0
rect_min_size = Vector2( 0, 2 )
mouse_filter = 2
color = Color( 0.501961, 0.501961, 0.501961, 1 )
[node name="Cursor" type="ColorRect" parent="Slider"]
margin_right = 3.0
margin_bottom = 1.0
rect_min_size = Vector2( 3, 2 )
mouse_filter = 2
[connection signal="focus_exited" from="." to="." method="_on_LineEdit_focus_exited"]
[connection signal="gui_input" from="." to="." method="_on_LineEdit_gui_input"]
[connection signal="resized" from="." to="." method="do_update"]
[connection signal="text_changed" from="." to="." method="_on_LineEdit_text_changed"]
[connection signal="text_entered" from="." to="." method="_on_LineEdit_text_entered"]
[connection signal="resized" from="Slider" to="." method="do_update"]