Has anyone done a sliding side panel on Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Alexandr

I tried but it looks (code) too scary. It works as it should, but I think it’s too bad. Here’s the code, if you can, help optimize it.

func side_Bar_Control():
var posMouse = get_global_mouse_position().x
var sizSideBar = sideBar.rect_size.x

if Input.is_mouse_button_pressed(BUTTON_LEFT):
	if not openSideBar:
		if posMouse < sizW.x*0.1 and not firstPos:
			firstPos = posMouse
		if firstPos:
			if sideBar.rect_global_position.x >= 0:
				sideBar.rect_global_position.x = 0
			elif sideBar.rect_global_position.x <= 0:
				sideBar.rect_global_position.x = -sizSideBar + posMouse - firstPos
	elif openSideBar:
		if not firstPos and posMouse >= sizSideBar:
			firstPos = posMouse
		if firstPos:
			if sideBar.rect_global_position.x <= 0:
				sideBar.rect_global_position.x = posMouse - firstPos
			elif sideBar.rect_global_position.x == -sizSideBar:
				sideBar.rect_global_position.x = -sizSideBar
elif not Input.is_mouse_button_pressed(BUTTON_LEFT):
	if firstPos:
		lastPos = sideBar.rect_global_position.x
		if not openSideBar:
			if lastPos > -sizSideBar * 0.5:
				sideBar.rect_global_position.x = 0
				openSideBar = true
			else:
				sideBar.rect_global_position.x = -sizSideBar
		elif openSideBar:
			if lastPos < -sizSideBar * 0.5:
				sideBar.rect_global_position.x = -sizSideBar
				openSideBar = false
			else:
				sideBar.rect_global_position.x = 0
		firstPos = 0
:bust_in_silhouette: Reply From: MysteryGM

Again with the down votes with no reason…

@Alexandr, instead of trying this with code, the animator is intended for this kind of thing.
Animations is a great way to create much better sliding menus

The above is a link to a Godot 3.1 Alpha sliding menu example. It is a zip file just download it, extract and run with your Godot.

In the file you will see this script and a detailed explanation on it:

extends Control

var IsMenuShown = true

func _on_HideMenu_button_up(): 
	if (IsMenuShown == false):
		$MwBAnimator.play("Slide In")
		IsMenuShown = true
	else:
		$MwBAnimator.play("MenuSlideOut")
		IsMenuShown = false

It does not fit a bit since my sidebar was made for android. And it was necessary that the user could push it with a finger. It is necessary that the panel moves behind it. Can this be realized with animation?

Alexandr | 2018-11-03 14:47

Yes, just allow the user to play the animation forward by sliding their finger. Call advance() on the animation you can use a positive float for forward and a negative for backward.

Also what you did above is very complex for such a simple thing. You could have used:
MousePosition.x - ClickedPosition.x = offset.x
Then you would just check that the scrolling object doesn’t go too far with 2 if checks.

If you have some free time, try to learn Linear Algebra. You will use it for almost everything in games. Also awesome things like slicing meshes is done with linear algebra.

MysteryGM | 2018-11-03 17:59