DEV Community

Dragonforge Development
Dragonforge Development

Posted on

How to Press and Drag to Reposition a Window in Godot

Ever wanted to create a floating window that stayed on the desktop or phone screen, but could be moved around by pressing and holding? While the process is similar to clicking and dragging a 2D texture, the process is a little different. (Example written for Godot 4.3 RC3 and later.)

A coin rendered in a floating window.
A coin rendered in a floating window.

  1. Set Enable Long Click Press As Right Click to True in the Project Settings. (Project Menu -> Project Settings... -> General Tab -> Input Devices -> Pointing)
  2. Create a Window node.
  3. Set Borderless to True. (Inspector -> Flags)
  4. Set Transparent to True. (Inspector -> Flags)
  5. Add the following code to your script:
extends Window
var is_dragging = false
var mouse_offset


func _input(event: InputEvent) -> void:
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT:
        if event.is_pressed():
            if get_visible_rect().has_point(event.position):
                is_dragging = true
                mouse_offset = event.position
        else:
            is_dragging = false
    if event is InputEventMouseMotion and is_dragging:
        position += Vector2i(event.position - mouse_offset)
Enter fullscreen mode Exit fullscreen mode

That's all there is to it! You can now right-click and drag to move the window on any OS, or long press and then drag, just like you would an app on Android!

Like this? Feel free to show your appreciation:
Buy Me A Coffee

Want to learn more Godot? Interested in making an RPG? Take our course Godot: Learn To Be A Professional Game Developer by Making a 3D RPG From Scratch.

Top comments (0)