Added modifiers to float edit widget

This commit is contained in:
RodZill4 2020-03-27 07:35:56 +01:00
parent cc606137b3
commit d7c749c08e
2 changed files with 49 additions and 12 deletions

View File

@ -8,7 +8,9 @@ export var step : float = 0.0 setget set_step
var sliding : bool = false
var start_position : float
var last_position : float
var start_value : float
var modifiers : int
var from_lower_bound : bool = false
var from_upper_bound : bool = false
@ -45,28 +47,60 @@ func do_update(update_text : bool = true) -> void:
else:
cursor.rect_position.x = 0
func get_modifiers(event):
var modifiers = 0
if event.shift:
modifiers |= 1
if event.control:
modifiers |= 2
if event.alt:
modifiers |= 4
return modifiers
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
last_position = event.position.x
start_position = last_position
start_value = value
sliding = true
from_lower_bound = value <= min_value
from_upper_bound = value >= max_value
modifiers = get_modifiers(event)
else:
sliding = false
elif sliding and event is InputEventMouseMotion and event.button_mask == BUTTON_MASK_LEFT:
var delta : float = event.position.x-start_position
var v : float = start_value+sign(delta)*pow(abs(delta)*0.005, 2)*abs(max_value - min_value)
if step != 0:
v = min_value+floor((v - min_value)/step)*step
if !from_lower_bound and v < min_value:
v = min_value
if !from_upper_bound and v > max_value:
v = max_value
set_value(v)
select(0, 0)
emit_signal("value_changed", value)
var new_modifiers = get_modifiers(event)
if new_modifiers != modifiers:
start_position = last_position
start_value = value
modifiers = new_modifiers
else:
last_position = event.position.x
var delta : float = last_position-start_position
var current_step = step
if event.control:
delta *= 0.2
elif event.shift:
delta *= 5.0
if event.alt:
current_step *= 0.01
var v : float = start_value+sign(delta)*pow(abs(delta)*0.005, 2)*abs(max_value - min_value)
if current_step != 0:
v = min_value+floor((v - min_value)/current_step)*current_step
if !from_lower_bound and v < min_value:
v = min_value
if !from_upper_bound and v > max_value:
v = max_value
set_value(v)
select(0, 0)
emit_signal("value_changed", value)
elif event is InputEventKey and !event.echo:
match event.scancode:
KEY_SHIFT, KEY_CONTROL, KEY_ALT:
start_position = last_position
start_value = value
modifiers = get_modifiers(event)
func _on_LineEdit_text_changed(new_text : String) -> void:
if new_text.is_valid_float():

View File

@ -14,6 +14,9 @@ max_length = 20
context_menu_enabled = false
caret_blink = true
script = ExtResource( 1 )
__meta__ = {
"_edit_use_anchors_": false
}
[node name="Slider" type="ColorRect" parent="."]
anchor_top = 1.0