diff options
| author | dam <dam@gudinoff> | 2021-12-05 02:54:05 +0000 |
|---|---|---|
| committer | dam <dam@gudinoff> | 2021-12-05 02:54:05 +0000 |
| commit | 1a575d34a178fdcc0d6e72a22e8de1e6da7c88c4 (patch) | |
| tree | 22173f764b75c696790d872e1d6821cd574ba586 /date_picker | |
| parent | 54515ea48a167a931c0f1aeb9d0170af7e147547 (diff) | |
| download | surgery-log-1a575d34a178fdcc0d6e72a22e8de1e6da7c88c4.tar.zst surgery-log-1a575d34a178fdcc0d6e72a22e8de1e6da7c88c4.zip | |
Implement date picker.
Diffstat (limited to 'date_picker')
| -rw-r--r-- | date_picker/date_picker.tscn | 136 | ||||
| -rw-r--r-- | date_picker/scroll_picker.gd | 85 |
2 files changed, 87 insertions, 134 deletions
diff --git a/date_picker/date_picker.tscn b/date_picker/date_picker.tscn index 3f77742..3414283 100644 --- a/date_picker/date_picker.tscn +++ b/date_picker/date_picker.tscn @@ -10,7 +10,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="year_scroll" type="Control" parent="."] +[node name="year_value" type="Control" parent="."] anchor_right = 0.333 anchor_bottom = 1.0 script = ExtResource( 1 ) @@ -19,7 +19,7 @@ __meta__ = { } max_value = 9999 -[node name="value_previous" type="Label" parent="year_scroll"] +[node name="previous" type="Label" parent="year_value"] anchor_right = 1.0 anchor_bottom = 0.333 mouse_filter = 1 @@ -29,16 +29,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="year_scroll/value_previous"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 0, 1, 1, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="value" type="Label" parent="year_scroll"] +[node name="current" type="Label" parent="year_value"] anchor_top = 0.333 anchor_right = 1.0 anchor_bottom = 0.666 @@ -49,22 +40,16 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="year_scroll/value"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 1, 1, 1, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="input" type="LineEdit" parent="year_scroll/value"] +[node name="input" type="LineEdit" parent="year_value/current"] visible = false anchor_right = 1.0 anchor_bottom = 1.0 +focus_next = NodePath("../../../month_value/current/input") align = 1 +max_length = 4 +caret_blink = true -[node name="value_next" type="Label" parent="year_scroll"] +[node name="next" type="Label" parent="year_value"] anchor_top = 0.666 anchor_right = 1.0 anchor_bottom = 1.0 @@ -75,20 +60,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="year_scroll/value_next"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 1, 1, 0, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="debug" type="Label" parent="year_scroll"] -anchor_right = 1.0 -anchor_bottom = 1.0 - -[node name="month_scroll" type="Control" parent="."] +[node name="month_value" type="Control" parent="."] anchor_left = 0.333 anchor_right = 0.666 anchor_bottom = 1.0 @@ -96,7 +68,7 @@ script = ExtResource( 1 ) min_value = 1 max_value = 12 -[node name="value_previous" type="Label" parent="month_scroll"] +[node name="previous" type="Label" parent="month_value"] anchor_right = 1.0 anchor_bottom = 0.333 mouse_filter = 1 @@ -106,16 +78,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="month_scroll/value_previous"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 0, 1, 1, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="value" type="Label" parent="month_scroll"] +[node name="current" type="Label" parent="month_value"] anchor_top = 0.333 anchor_right = 1.0 anchor_bottom = 0.666 @@ -126,22 +89,17 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="month_scroll/value"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 1, 1, 1, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="input" type="LineEdit" parent="month_scroll/value"] +[node name="input" type="LineEdit" parent="month_value/current"] visible = false anchor_right = 1.0 anchor_bottom = 1.0 +focus_next = NodePath("../../../day_value/current/input") +focus_previous = NodePath("../../../year_value/current/input") align = 1 +max_length = 2 +caret_blink = true -[node name="value_next" type="Label" parent="month_scroll"] +[node name="next" type="Label" parent="month_value"] anchor_top = 0.666 anchor_right = 1.0 anchor_bottom = 1.0 @@ -152,20 +110,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="month_scroll/value_next"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 1, 1, 0, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="debug" type="Label" parent="month_scroll"] -anchor_right = 1.0 -anchor_bottom = 1.0 - -[node name="day_scroll" type="Control" parent="."] +[node name="day_value" type="Control" parent="."] anchor_left = 0.666 anchor_right = 1.0 anchor_bottom = 1.0 @@ -176,7 +121,7 @@ __meta__ = { min_value = 1 max_value = 31 -[node name="value_previous" type="Label" parent="day_scroll"] +[node name="previous" type="Label" parent="day_value"] anchor_right = 1.0 anchor_bottom = 0.333 mouse_filter = 1 @@ -186,16 +131,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="day_scroll/value_previous"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 0, 1, 1, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="value" type="Label" parent="day_scroll"] +[node name="current" type="Label" parent="day_value"] anchor_top = 0.333 anchor_right = 1.0 anchor_bottom = 0.666 @@ -206,22 +142,16 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="day_scroll/value"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 1, 1, 1, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="input" type="LineEdit" parent="day_scroll/value"] +[node name="input" type="LineEdit" parent="day_value/current"] visible = false anchor_right = 1.0 anchor_bottom = 1.0 +focus_previous = NodePath("../../../month_value/current/input") align = 1 +max_length = 2 +caret_blink = true -[node name="value_next" type="Label" parent="day_scroll"] +[node name="next" type="Label" parent="day_value"] anchor_top = 0.666 anchor_right = 1.0 anchor_bottom = 1.0 @@ -232,22 +162,6 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="ColorRect" type="ColorRect" parent="day_scroll/value_next"] -anchor_right = 1.0 -anchor_bottom = 1.0 -mouse_filter = 2 -color = Color( 1, 1, 0, 0.0980392 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="debug" type="Label" parent="day_scroll"] -anchor_right = 1.0 -anchor_bottom = 1.0 -__meta__ = { -"_edit_use_anchors_": false -} - [node name="split_previous" type="ColorRect" parent="."] anchor_top = 0.333 anchor_right = 1.0 diff --git a/date_picker/scroll_picker.gd b/date_picker/scroll_picker.gd index 4ae22c0..7d3004a 100644 --- a/date_picker/scroll_picker.gd +++ b/date_picker/scroll_picker.gd @@ -12,9 +12,15 @@ var anchor: float var value: int var offset: float -var current_value_base_position: float -var previous_value_base_position: float -var next_value_base_position: float +var input: LineEdit +var label_previous: Label +var label_current: Label +var label_next: Label + +var scroll_unit_height: float +var label_previous_base_position: float +var label_current_base_position: float +var label_next_base_position: float func _ready(): @@ -23,20 +29,30 @@ func _ready(): initial_position = Vector2.ZERO, current_position = Vector2.ZERO, velocity = Vector2.ZERO, + was_dragged = false, is_active = false, } - current_value_base_position = ($value as Label).rect_position.y - previous_value_base_position = ($value_previous as Label).rect_position.y - next_value_base_position = ($value_next as Label).rect_position.y + + input = get_node("current/input") + input.connect("text_entered", self, "input_text_entered") + input.connect("focus_entered", self, "input_focus_entered") + input.connect("focus_exited", self, "input_focus_exited") + + label_previous = get_node("previous") + label_current = get_node("current") + label_next = get_node("next") + + scroll_unit_height = label_current.rect_size.y + label_previous_base_position = label_previous.rect_position.y + label_current_base_position = label_current.rect_position.y + label_next_base_position = label_next.rect_position.y + value = min_value offset = 0.0 func _process(delta: float): - # @DAM Review variable name, and maybe move it elsewhere. - var scroll_unit_height := ($value as Label).rect_size.y - if pointer.is_active: var dragged_distance: float = - (pointer.current_position.y - pointer.initial_position.y) offset = anchor + (dragged_distance / scroll_unit_height) @@ -53,46 +69,69 @@ func _process(delta: float): value = wrapi(value + cummulative_displacement, min_value, max_value + 1) offset -= cummulative_displacement - # @DAM TODO Change all '$access' with variables set during on '_ready'. - $value.text = "%d" % value - $value_next.text = "%d" % wrapi(value + 1, min_value, max_value + 1) - $value_previous.text = "%d" % wrapi(value - 1, min_value, max_value + 1) + label_current.text = "%d" % value + label_next.text = "%d" % wrapi(value + 1, min_value, max_value + 1) + label_previous.text = "%d" % wrapi(value - 1, min_value, max_value + 1) var offset_position := offset * scroll_unit_height - ($value as Label).rect_position.y = current_value_base_position - offset_position - ($value_previous as Label).rect_position.y = previous_value_base_position - offset_position - ($value_next as Label).rect_position.y = next_value_base_position - offset_position + label_current.rect_position.y = label_current_base_position - offset_position + label_previous.rect_position.y = label_previous_base_position - offset_position + label_next.rect_position.y = label_next_base_position - offset_position - ($value_previous as Label).modulate.a = 0.5 - offset - ($value_next as Label).modulate.a = offset + 0.5 + label_previous.modulate.a = 0.5 - offset + label_next.modulate.a = offset + 0.5 func _gui_input(event: InputEvent): - # @DAM TODO TEST + # @DAM A bug on GODOT-3.X makes events from non-mouse-emulated pointers (index > 0) unreliable. + if event is InputEventScreenTouch || event is InputEventScreenDrag: + if event.index != 0: + return + if event is InputEventScreenTouch && (pointer.is_active == false || pointer.index == event.index): var touch := event as InputEventScreenTouch pointer.is_active = event.pressed pointer.current_position = touch.position if pointer.is_active: + input.release_focus() pointer.index = touch.index pointer.initial_position = touch.position anchor = value + offset else: + if pointer.was_dragged == false: # Click detected. + input.grab_focus() pointer.index = -1 + pointer.was_dragged = false if event is InputEventScreenDrag && event.index == pointer.index: var drag := event as InputEventScreenDrag pointer.current_position = drag.position pointer.velocity = drag.speed + pointer.was_dragged = true + + +func input_text_entered(new_text: String): + input.release_focus() + + +func input_focus_entered(): + input.text = "%d" % value + input.visible = true + input.select_all() + + +func input_focus_exited(): + if input.text.is_valid_integer(): + value = int(input.text) + input.visible = false -func set_value(value: int): - breakpoint # @DAM TODO +func set_value(new_value: int): + value = new_value func get_value() -> int: - breakpoint # @DAM TODO - return 0 + return value |
