Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
e2afc100f5
|
|||
|
95c39ded76
|
|||
|
a58248f57c
|
|||
|
192687b05f
|
|||
|
9e5a51839f
|
|||
|
bac0be6e2e
|
|||
|
f10c4aab1c
|
|||
|
8be64c01d1
|
|||
|
0f87a64d5c
|
|||
|
a6c4d56650
|
|||
|
4ebc80d7ee
|
|||
|
38235be363
|
|||
|
4190112800
|
|||
|
354a82d2f8
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
# Godot 4+ specific ignores
|
# Godot 4+ specific ignores
|
||||||
.godot/
|
.godot/
|
||||||
/android/
|
/android/
|
||||||
|
.vscode/
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
# tween-scroll-test
|
# tween-scroll-test
|
||||||
|
Simple test of how to create a scrollable/draggable menu similar to Arknights' IS map and many other draggable menus within the game (Operators, Moments, etc.)
|
||||||
|
|
||||||
|
Godot 4.5
|
||||||
|
|||||||
@@ -1,31 +1,116 @@
|
|||||||
extends Control
|
extends Control
|
||||||
|
# INFO name can be anything, doesn't need to be "SpaceNode"
|
||||||
class_name SpaceNode
|
class_name SpaceNode
|
||||||
|
|
||||||
var initial_pos: Vector2
|
# adapted from pc_tile code (project-elysium-main)
|
||||||
var offset: Vector2
|
|
||||||
var draggable: bool = false
|
var draggable: bool = false
|
||||||
|
var velocity: float = 0
|
||||||
|
|
||||||
|
# variables for velocity calculation
|
||||||
|
var mouse_first_location: Vector2
|
||||||
|
var mouse_last_location: Vector2
|
||||||
|
var mouse_release_location: Vector2
|
||||||
|
var mouse_n_minus_one_location: Vector2
|
||||||
|
var time_since_first_location: float = -1
|
||||||
|
|
||||||
|
# Export variables, for ease of setting without going into the code
|
||||||
|
## adjusts how much velocity you want to have on a slide, a higher number = less velocity
|
||||||
|
@export var velocity_coeffecient: int = 500
|
||||||
|
## the maximum displacement of the item, to the right
|
||||||
|
@export var max_x: int = 1000
|
||||||
|
## the minimum displacement, can be adjusted to accomodate margins
|
||||||
|
@export var min_x: int = 0
|
||||||
|
## ease out time for the velocity curve
|
||||||
|
@export var ease_out_time: float = 0.5
|
||||||
|
## whether you want the UI to bounce
|
||||||
|
@export var bounce: bool = false
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
draggable = true
|
draggable = true
|
||||||
return
|
return
|
||||||
|
|
||||||
func _process(delta: float) -> void:
|
func _process(delta: float) -> void:
|
||||||
|
# Every frame, update the position based upon the velocity
|
||||||
|
# setting
|
||||||
|
global_position.x += velocity
|
||||||
|
|
||||||
|
# if the current item is being dragged
|
||||||
if draggable:
|
if draggable:
|
||||||
|
# when the item is first clicked
|
||||||
|
if (Input.is_action_just_pressed("click")):
|
||||||
|
mouse_first_location = get_global_mouse_position()
|
||||||
|
|
||||||
if Input.is_action_pressed("click", true):
|
# basically a "while" the click is held
|
||||||
self.global_position = Vector2(get_global_mouse_position().x, self.global_position.y)
|
if Input.is_action_pressed("click"):
|
||||||
|
# update the mouse location
|
||||||
|
mouse_first_location = get_global_mouse_position()
|
||||||
|
# then update global position, for mouse tracking
|
||||||
|
# this is based upon the last location the mouse was, subtracked by the current
|
||||||
|
# location
|
||||||
|
global_position.x += -(mouse_last_location.x - mouse_first_location.x)
|
||||||
|
|
||||||
|
# when the click is released
|
||||||
elif Input.is_action_just_released("click"):
|
elif Input.is_action_just_released("click"):
|
||||||
|
# make note of where the mouse was released
|
||||||
|
# since we're using a sample of N, N-1 frames, rather than an
|
||||||
|
# average over N frames, we use the in-built
|
||||||
|
# `delta` parameter
|
||||||
|
mouse_release_location = get_global_mouse_position()
|
||||||
|
|
||||||
is_dragging.is_dragging = false
|
# simple if/else to compare which direction to go
|
||||||
|
if mouse_release_location.x < mouse_first_location.x:
|
||||||
|
# displacement calc
|
||||||
|
var distance = mouse_release_location.x - mouse_first_location.x
|
||||||
|
velocity = (distance /
|
||||||
|
# velocity coefficient to adjust sensitivity
|
||||||
|
(velocity_coeffecient * delta))
|
||||||
|
# update to not draggable, to let tween resolve
|
||||||
|
draggable = false
|
||||||
|
|
||||||
|
# other side of the if/else
|
||||||
|
elif mouse_release_location.x > mouse_first_location.x:
|
||||||
|
var distance = abs(mouse_first_location.x
|
||||||
|
- mouse_release_location.x)
|
||||||
|
velocity = (distance /
|
||||||
|
(velocity_coeffecient * delta))
|
||||||
|
draggable = false
|
||||||
|
|
||||||
|
# when the drag is complete, create a tween to ease out towards zero
|
||||||
var tween = get_tree().create_tween()
|
var tween = get_tree().create_tween()
|
||||||
|
|
||||||
tween.tween_property(self, "global_position", Vector2(get_global_mouse_position().x, self.global_position.y), 0.5).set_ease(Tween.EASE_IN_OUT)
|
# ease out the velocity value to zero.
|
||||||
|
tween.tween_property(self, "velocity",
|
||||||
|
0, ease_out_time).set_ease(Tween.EASE_OUT)
|
||||||
|
|
||||||
else:
|
# reset location values
|
||||||
|
mouse_first_location = Vector2(0,0)
|
||||||
|
mouse_release_location = Vector2(0,0)
|
||||||
|
|
||||||
|
# able to be dragged again
|
||||||
|
draggable = true
|
||||||
|
|
||||||
|
# if/else to handle reaching the "end" of the canvas
|
||||||
|
# as notated by max_x and min_x (see export vars)
|
||||||
|
if global_position.x >= max_x && velocity > 0:
|
||||||
|
if bounce:
|
||||||
|
# if bounce is enable, half the velocity and negate it
|
||||||
|
velocity = -(velocity)/2
|
||||||
|
print("velocity negated, bouncing")
|
||||||
|
# set the position to be within the right bounds
|
||||||
|
global_position.x = max_x - 1
|
||||||
|
time_since_first_location = -1
|
||||||
|
# draggable again, possibly redundant
|
||||||
|
draggable = true
|
||||||
return
|
return
|
||||||
|
|
||||||
func reset_draggable():
|
elif global_position.x < min_x && velocity < 0:
|
||||||
if not is_dragging.is_dragging:
|
if bounce:
|
||||||
is_dragging.is_dragging = false
|
velocity = -(velocity)/2
|
||||||
draggable = false
|
print("velocity negated, bouncing")
|
||||||
|
global_position.x = min_x + 1
|
||||||
|
time_since_first_location = -1
|
||||||
|
draggable = true
|
||||||
|
return
|
||||||
|
|
||||||
|
# finally, update the last known position of the mouse
|
||||||
|
mouse_last_location = get_global_mouse_position()
|
||||||
|
|||||||
@@ -23,3 +23,4 @@ offset_top = 299.0
|
|||||||
offset_right = 455.0
|
offset_right = 455.0
|
||||||
offset_bottom = 299.0
|
offset_bottom = 299.0
|
||||||
script = ExtResource("2_mej84")
|
script = ExtResource("2_mej84")
|
||||||
|
velocity_coeffecient = 110
|
||||||
|
|||||||
Reference in New Issue
Block a user