From 1d6ae67c431eb40dfc50f911c4f2303a96cf8ab8 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Wed, 8 May 2024 22:13:44 +0200 Subject: [PATCH] Implement scrolling support for SuiScroller --- sui/scenesrc/SuiScroller.tscn | 6 ++++++ sui/src/SuiScroller.gd | 33 +++++++++++++++++++++++++++++++++ sui/src/SuiTypes.gd | 6 ++++++ 3 files changed, 45 insertions(+) diff --git a/sui/scenesrc/SuiScroller.tscn b/sui/scenesrc/SuiScroller.tscn index bee9ff0..190b980 100644 --- a/sui/scenesrc/SuiScroller.tscn +++ b/sui/scenesrc/SuiScroller.tscn @@ -22,6 +22,9 @@ offset_top = 226.417 offset_right = 366.417 offset_bottom = 246.667 theme = ExtResource("2_4lteo") +max_value = 386.667 +step = 1.0 +page = 1.0 [node name="VScrollBar" type="VScrollBar" parent="."] modulate = Color(1, 1, 1, 0.501961) @@ -33,3 +36,6 @@ offset_left = -20.25 offset_bottom = 226.417 grow_horizontal = 0 theme = ExtResource("2_4lteo") +max_value = 246.667 +step = 1.0 +page = 1.0 diff --git a/sui/src/SuiScroller.gd b/sui/src/SuiScroller.gd index d666aa5..5a16c2c 100644 --- a/sui/src/SuiScroller.gd +++ b/sui/src/SuiScroller.gd @@ -149,6 +149,39 @@ func update_element() -> void: $HScrollBar.max_value = size.x $VScrollBar.max_value = size.y +func scroll(direction: SuiTypes.ScrollDirection) -> void: + var bar: ScrollBar + var dsign: int = 0 + + # Set 'dsign' + match(direction): + SuiTypes.ScrollDirection.UP: dsign = +1 + SuiTypes.ScrollDirection.DOWN: dsign = -1 + SuiTypes.ScrollDirection.LEFT: dsign = -1 + SuiTypes.ScrollDirection.RIGHT: dsign = +1 + + # Set 'bar' + match(direction): + SuiTypes.ScrollDirection.UP: bar = $VScrollBar + SuiTypes.ScrollDirection.DOWN: bar = $VScrollBar + SuiTypes.ScrollDirection.LEFT: bar = $HScrollBar + SuiTypes.ScrollDirection.RIGHT: bar = $HScrollBar + + bar.value = bar.value + bar.step * 50 * dsign + +func _input(event: InputEvent): + if event is InputEventMouseButton: + if event.is_pressed(): + match(event.button_index): + MouseButton.MOUSE_BUTTON_WHEEL_UP: + if Input.is_key_pressed(Key.KEY_SHIFT): scroll(SuiTypes.ScrollDirection.LEFT) + else: scroll(SuiTypes.ScrollDirection.DOWN) + MouseButton.MOUSE_BUTTON_WHEEL_DOWN: + if Input.is_key_pressed(Key.KEY_SHIFT): scroll(SuiTypes.ScrollDirection.RIGHT) + else: scroll(SuiTypes.ScrollDirection.UP) + MouseButton.MOUSE_BUTTON_WHEEL_LEFT: scroll(SuiTypes.ScrollDirection.RIGHT) + MouseButton.MOUSE_BUTTON_WHEEL_RIGHT: scroll(SuiTypes.ScrollDirection.LEFT) + func _get_configuration_warnings(): if get_child_count() != 3: return ["SuiScroller is intended to operate with one child only."] diff --git a/sui/src/SuiTypes.gd b/sui/src/SuiTypes.gd index 1f6aaec..1628a64 100644 --- a/sui/src/SuiTypes.gd +++ b/sui/src/SuiTypes.gd @@ -11,3 +11,9 @@ enum ScrollMode { MULTIPLY_CUSTOM_VALUE, CUSTOM_VALUE } +enum ScrollDirection { + UP, + DOWN, + LEFT, + RIGHT +}