aboutsummaryrefslogtreecommitdiff
path: root/date_picker/scroll_picker.gd
blob: 1d184b9a7f4e09d4769af996e4f68ba6c3abf07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
extends Control

const MAX_POINTERS = 10

var pointer: Dictionary
var value: float = 0.0
var anchor: float = 0.0
var pointer_id: int = -1
var start_pos: float = 0.0
var pos: float = 0.0

var value_base_position: float
var value_next_base_position: float
var value_previous_base_position: float


func _ready():
	pointer = {
			index = -1,
			initial_position = Vector2.ZERO,
			current_position = Vector2.ZERO,
			relative = Vector2.ZERO,
			velocity = Vector2.ZERO,
			is_active = false,
			timestamp = 0
		}
	value_base_position = ($value as Label).rect_position.y
	value_next_base_position = ($value_next as Label).rect_position.y
	value_previous_base_position = ($value_previous as Label).rect_position.y



func _process(delta: float):
	
	var max_value := 10
	var scroll_unit_height := ($value as Label).rect_size.y
	
	pointer.velocity *= clamp((1.0 - 5.5 * delta), 0.0, 1.0)
	if pointer.is_active:
		value = anchor + (pointer.current_position.y - pointer.initial_position.y)
	elif is_zero_approx(pointer.velocity.y) == false:
		value += pointer.velocity.y * delta
		if abs(pointer.velocity.y) < 50.0:
			var normalized_value := -value / scroll_unit_height
			var normalized_int_value := round(normalized_value) as int
			var logic_value := fposmod(normalized_int_value, max_value)
			var fix := ((normalized_int_value as float) - normalized_value) * scroll_unit_height
			value -= fix * 5.0 * delta
	
#	pointer.timestamp = OS.get_ticks_msec()
#	($test as TextureRect).rect_global_position.y = value
#	$debug.text = "value: %s\nvelocity: %s\n%s" % [value, pointer.velocity, pointer.relative]
	var current_pos = value
	var turn_over := scroll_unit_height * max_value
	if current_pos >= turn_over || current_pos < 0.0:
		value = fposmod(current_pos, turn_over)
#	elif current_pos < 0:
#		value = 800 - current_pos
	
	
	var normalized_value := -value / scroll_unit_height
	var normalized_int_value := round(normalized_value) as int
	var logic_value := fposmod(normalized_int_value, max_value)
	var logic_next_value := fposmod(normalized_int_value + 1, max_value)
	var logic_previous_value := fposmod(normalized_int_value - 1, max_value)
	
#	var value_base_position := ($value_previous as Label).rect_position.y # 140.0 # 180.0
#	var value_next_base_position := ($value_next as Label).rect_position.y #180.0 # 260.0
#	var value_previous_base_position := ($value as Label).rect_position.y #100.0
	
	$value.text = "%d" % logic_value
	$value_next.text = "%d" % logic_next_value
	$value_previous.text = "%d" % logic_previous_value
#	$debug.text = "%s" % normalized_value
	
	($value as Label).rect_position.y = value_base_position - (normalized_value - normalized_int_value) * scroll_unit_height
	($value_previous as Label).rect_position.y = value_previous_base_position - (normalized_value - normalized_int_value) * scroll_unit_height
	($value_next as Label).rect_position.y = value_next_base_position - (normalized_value - normalized_int_value) * scroll_unit_height
	
#	($value as Label).modulate.a = (scroll_unit_height - (normalized_value - normalized_int_value) * scroll_unit_height) / (scroll_unit_height as float)
#	$debug.text = "%s ..." % [normalized_int_value - normalized_value]
	($value_next as Label).modulate.a = ((normalized_value - normalized_int_value) + 0.5)
	($value_previous as Label).modulate.a = ((normalized_int_value - normalized_value) + 0.5)
#	($value_previous as Label).modulate.a = ((normalized_value + normalized_int_value) * scroll_unit_height) / (scroll_unit_height as float)
#
#func _input(event: InputEvent):
#	$log.text += "> input: %s\n" % event.to_string()
#	if event is InputEventScreenTouch && event.pressed == false:
#		pointer.index = -1

func _gui_input(event: InputEvent):
#	get_tree().set_input_as_handled()
#	if event is InputEventScreenTouch && event.pressed == false:
#		$label.text = "AUTCH"
#	$log.text += "> event: %s\n" % event.to_string()

	if event is InputEventScreenTouch:
#	if (event is InputEventScreenTouch && pointer.is_active && pointer.index == event.index
#	|| event is InputEventScreenTouch && pointer.is_active == false):
		var touch := event as InputEventScreenTouch
		pointer.is_active = event.pressed
		pointer.current_position = touch.position
		var time := OS.get_ticks_msec()
		if pointer.is_active:
			pointer.index = touch.index
			pointer.initial_position = touch.position
			anchor = value
		else:
			pointer.index = -1
#		elif abs(pointer.timestamp - time) > 50.0:
#			pointer.velocity = Vector2.ZERO
#		if pointer.is_active == false:
#			$output.text += "%10d: touch %d\n" % [abs(pointer.timestamp - time), touch.index]
	
	if event is InputEventScreenDrag && event.index == pointer.index:
		var drag := event as InputEventScreenDrag
		pointer.current_position = drag.position
		pointer.velocity = drag.speed
#		$label.text = "%s"%drag.speed
		pointer.relative = drag.relative
		pointer.timestamp = OS.get_ticks_msec()
#		$output.text += "%10d: drag %d\n" % [pointer.timestamp, drag.index]