0 votes

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
in Engine by (111 points)

1 Answer

+3 votes

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

https://drive.google.com/open?id=1nDnmi7zPs8l0-ws2EeSWJld38apRQqGM
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
by (1,451 points)

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?

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.