• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] How to get mouse position relative to surface area?

FoxyOfJungle

Kazan Games
The main problem:



As you can see in that GIF, I can scale it so that It will always stay 1:1. But I want to zoom in and out, and this is the problem. I will explain next:

I created a surface instead of using the Draw GUI event (I need to, because later I'll need to copy that created GUI surface), so the whole interface is being drawn on this surface.
Ok, As you can see from the GIF, each color represents different functions to get the mouse position, and draws a circle for each.

Notice in GIf that I zoom in and out the surface using the "resize" method. (notice the GIF that has a variable drawn on the screen called: gui_zoom, in the top left).

How I zoom in and out the surface:

Code:
if surface_exists(global.gui_surface)
{
surface_set_target(global.gui_surface)
draw_clear_alpha(0,0);

//This code above is on the FIRST lines of the normal draw event.


HERE WILL DRAW ALL THE GUI




surface_reset_target();
}
else
{
    global.gui_surface = surface_create(global.ideal_width*global.gui_zoom, global.ideal_height*global.gui_zoom);
 
}
draw_surface_stretched(global.gui_surface,0,0,global.ideal_width,global.ideal_height);

//This code above is on the LAST lines of the normal draw event.

global.ideal_width and global.ideal_height is the window size, same as window_get_width...

global.gui_zoom is the variable that scales the surface. (ranges from 0.7 to 2).

But, when I increase or decrease the zoom variable, the relative position of the mouse on the surface is incorrect (as you can see in GIF).

So, how do you get the mouse position relative to the surface area? (I think I will have to use the variable global.gui_zoom together)
So that the position of the mouse relative to the surface goes from 0 to the size of the surface, like the normal screen?

I created two scripts to get the mouse position relative to the surface, I want them to return like the mouse_x function:
gui_mouse_x();
gui_mouse_y();

Thank you so much!
 
Last edited:

Mert

Member
The only solution is to calculate the mouse position accordingly, by yourself.

gui_mouse_x becomes this:
Code:
var mx = device_mouse_x(mb_left) * global.gui_zoom;
return mx;
Edit : I presume the ideal width and height is same as the window size.
 

FoxyOfJungle

Kazan Games
The only solution is to calculate the mouse position accordingly, by yourself.

gui_mouse_x becomes this:
Code:
var mx = device_mouse_x(mb_left) * global.gui_zoom;
return mx;
Edit : I presume the ideal width and height is same as the window size.
IT WORKS!! Thank you so much! :D

upload_2019-8-4_13-49-25.png

The red circle is the function of gui_mouse_x() and gui_mouse_y()

EDIT: That background image is not mine, it's for tests, sorry image owner.
 
Top