Last active
April 29, 2022 21:50
-
-
Save nisovin/a1abbf24449db357c2da0ee2d613808f to your computer and use it in GitHub Desktop.
Godot Screen Mouse Position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends Node2D | |
func _process(delta: float) -> void: | |
$Label.text = var2str(ScreenMouse.mouse_position) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Godot; | |
using System.Runtime.InteropServices; | |
// This script should be added as a Singleton in Godot | |
public class ScreenMouse : Godot.Node | |
{ | |
public Vector2 mouse_position { | |
get { | |
return GetCursorPosition(); | |
} | |
} | |
[StructLayout(LayoutKind.Sequential)] | |
public struct POINT | |
{ | |
public int X; | |
public int Y; | |
public static implicit operator Vector2(POINT point) | |
{ | |
return new Vector2(point.X, point.Y); | |
} | |
} | |
[DllImport("user32.dll")] | |
public static extern bool GetCursorPos(out POINT lpPoint); | |
public static Vector2 GetCursorPosition() | |
{ | |
POINT lpPoint; | |
GetCursorPos(out lpPoint); | |
return lpPoint; | |
} | |
// Source: https://stackoverflow.com/questions/1316681/getting-mouse-position-in-c-sharp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment