Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new value block: mouse_position #374

Merged
merged 1 commit into from
Feb 3, 2025
Merged

Add new value block: mouse_position #374

merged 1 commit into from
Feb 3, 2025

Conversation

manuq
Copy link
Contributor

@manuq manuq commented Jan 30, 2025

Under category Input. Considering the viewport scale as explained in [1]. The same thing we do for the viewport_width and other blocks.

[1] https://docs.godotengine.org/en/stable/tutorials/inputs/mouse_and_input_coordinates.html

Fix #373

@manuq manuq requested a review from wjt January 30, 2025 16:40
Comment on lines 14 to 18
code_template = "(func (): var transform: Transform2D = get_viewport_transform(); var scale: Vector2 = transform.get_scale(); return
get_viewport().get_mouse_position() * scale ).call()


"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not write this as:

Suggested change
code_template = "(func (): var transform: Transform2D = get_viewport_transform(); var scale: Vector2 = transform.get_scale(); return
get_viewport().get_mouse_position() * scale ).call()
"
code_template = "get_viewport().get_mouse_position() * get_viewport_transport().get_scale()"

I tried the following block program for the left paddle in pong:

image

The generated script is:

extends SimpleCharacter


func _process(delta):
	position = ((func (): var transform: Transform2D = get_viewport_transform(); var scale: Vector2 = transform.get_scale(); return
	get_viewport().get_mouse_position() * scale ).call()


	)

And this fails to start with:

Parser Error: Expected closing ")" after grouping expression.

If I delete all the trailing newlines in this template, the generated code becomes:

extends SimpleCharacter

func _process(delta):
	position = ((func (): var transform: Transform2D = get_viewport_transform(); var scale: Vector2 = transform.get_scale(); return
	get_viewport().get_mouse_position() * scale ).call())

but the error is the same. Maybe it's the newline after return, how did that get in there...

Godot itself keeps crashing when I try to save/run changes...

When I change the template to what I proposed above, the game runs, but the scaling is wrong. It could be that my program is wrong...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screencast.from.2025-01-30.18-16-11.webm

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we need to add - get_viewport_transform().origin? Thanks for reminding me that Pong is the perfect way to test this, as it has the viewport scaled.

Under category Input. Using the current node
CanvasItem.get_global_mouse_position() which considers viewport offset
and scale.

Fix #373
@manuq manuq force-pushed the mouse-position-block branch from b584095 to d55f2cf Compare February 3, 2025 13:09
@manuq
Copy link
Contributor Author

manuq commented Feb 3, 2025

@wjt this was actually easier, using get_global_position(). Our first block targetting CanvasItem. The docs also mention DisplayServer.mouse_get_position for "screen-space coordinates (e.g. when using a non-embedded Popup)". But I don't think that's the case. I tested it in Pong and in a simpler scene:
Video_2025-02-03_10-07-30.webm

@wjt wjt merged commit 984a15f into main Feb 3, 2025
3 checks passed
@wjt wjt deleted the mouse-position-block branch February 3, 2025 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add mouse position block
2 participants