extends Control @export var stage_pages: Array[Container] @export var stage_page_scroller: ScrollContainer const PAGE_COUNT = 5 const PAGE_WIDTH = 1080 const DRAG_TOLERANCE = 360 const SWIPE_DURATION: float = 2 const SWIPE_DELAY = 2000 # msec var g_tween: Tween = null var g_dragged_start: Vector2 = Vector2.ZERO var g_scroll_start: int = 0 var g_current_page: int = 0 var g_last_scroll_updated: int = 0 var f_check_swipe: bool = false var g_swipe_time_delta: float = 0 var g_counters: Dictionary[String, Counter] = { "swipe": Counter.new(), } func _ready() -> void: pass func _init() -> void: pass #page_scroller.scroll_horizontal = 3240 func _process(delta: float) -> void: var actions = _check_counters(delta) _do_actions(actions) func _check_counters(delta: float) -> Array: var actions: Array = [] for k in g_counters: var cnt: Counter = g_counters[k] if not cnt.is_active: continue cnt.value += delta if cnt.value >= cnt.sec: actions.append(k) cnt.value = 0 cnt.is_active = false return actions func _do_actions(actions: Array) -> void: for action: String in actions: match action: "swipe": _swipe_to_nearest_page() func button_pressed() -> void: print(name) func _swipe_to_nearest_page() -> void: if stage_page_scroller.scroll_horizontal % PAGE_WIDTH == 0: return var new_page: int = round(1.0 * stage_page_scroller.scroll_horizontal / PAGE_WIDTH) #print(stage_page_scroller.scroll_horizontal, " | ", 1.0 * stage_page_scroller.scroll_horizontal / PAGE_WIDTH, " | ", new_page) if g_tween != null: if g_tween.is_running(): return g_tween = stage_page_scroller.get_tree().create_tween() g_tween.tween_property(stage_page_scroller, "scroll_horizontal", new_page * PAGE_WIDTH, SWIPE_DURATION).set_trans(Tween.TRANS_CUBIC).set_ease(Tween.EASE_OUT) func _input(event: InputEvent) -> void: if event is InputEventScreenDrag: stage_page_scroller.scroll_horizontal = clamp( stage_page_scroller.scroll_horizontal - PAGE_COUNT * event.screen_relative.x, 0, PAGE_WIDTH * (PAGE_COUNT - 1)) g_counters.swipe.is_active = true g_counters.swipe.value = 0 elif event is InputEventPanGesture: g_counters.swipe.is_active = true g_counters.swipe.value = 0 elif event is InputEventScreenTouch: pass