0 votes

Hello Godoters,

As the name of the post itself says, i created a windows in my game and made this code:

Here is my project tree too:

This code was in the Windows node, but I removed it, because I needed to put other things, so .. I wanted the code to work on the TitleBar node, if not ask for more. :)

Then could someone fix my code, please? The only problem is that the archor point is at (0, 0) from the window instead of the position I clicked.

in Engine by (150 points)

1 Answer

+1 vote

Having an inside boolean for when the cursor is over the title bar is not the right way to do this. You want to use a proper input event so you can track where the mouse was when the user pressed their mouse.

You want to hook _gui_input on the title bar, and track when the user presses down the mouse on the title bar and when they release it. Store the position of the mouse when they pressed down in a variable, and unset it when the user releases. Then you can determine how the window was moved by the difference between the current mouse position and the position when the window was grabbed.


extends Control

var dragPoint = null

func _on_Titlebar_gui_input(ev):
    if ev is InputEventMouseButton:
        if ev.button_index == BUTTON_LEFT:
            if ev.pressed:
                # Grab it.
                dragPoint = get_global_mouse_position() - get_position()
                # Release it.
                dragPoint = null

    if ev is InputEventMouseMotion and dragPoint != null:
        set_position(get_global_mouse_position() - dragPoint)

For reference, this is pretty much a reimplementation of the built-in WindowDialog dragging code

by (112 points)

Might be a dumb question, but I've achieved partial-functionality with this. I'm working in Godot 3.1 and am attempting to be able to click-and-drag the window from the title bar. My tree looks like

---|Title Bar
---|[App contents]

I've implemented your code almost exactly as-is (providing a different name for the title bar and using [event] instead of [ev]), but functionality should be the same.

Problem is, [Node2D] doesn't offer a guiinput(), placing guiinput() on [Control] or [Title Bar] does nothing, and placing guiinput() on [Panel] allows me to move the contents of the window, uh... inside the window. Without the window moving. Placing a Panel as a direct child of [Node2D] before [Control] in the heirarchy and hooking a guiinput() on that up to the script provided does nothing.

I suspect it's a hierarchy problem. That'd explain why the current [Panel], being the object "closest" to the user does it while nothing else does. However, that doesn't explain for me how that moves everything within the window (including the entirety of [App contents]), but not the window itself.

I don't know if anybody's still tracking this question and it's probably a dunderheaded overlook on my part, but any assistance would be appreciated.

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 Frequently asked questions and 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 [email protected] with your username.