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.
- Set Enable Long Click Press As Right Click to True in the Project Settings. (Project Menu -> Project Settings... -> General Tab -> Input Devices -> Pointing)
- Create a Window node.
- Set Borderless to True. (Inspector -> Flags)
- Set Transparent to True. (Inspector -> Flags)
- 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)
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:
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)