SOLVED mouse_x and mouse_y returning WRONG value???

FoxyOfJungle

Kazan Games
Hello, everyone!

Objective:

Obtain the mouse position within the room.



Problem:


if I use the "view" in normal size and with application_surface enabled, the mouse_x and mouse_y work normally. (for now ok).

But, if I disable the application_surface and I create a new surface and use that surface using view_set_surface_id() as a view, the values of mouse_x and mouse_y change:


See:


Look at the RED area. Note that when I assign the view to the surface, the coordinates change immediately.

There is a "view" that is associated with a "camera", in which it view the room. And this "view" is being drawn on the "surface". I used view_set_surface_id() to do that.

I noticed that the value returned from mouse_x and mouse_y, always has the highest value equal to the size of the room.







CREATE EVENT:

GML:
// Initialize Surfaces
workplace_surface = -1; //workplace view surface
workplace_width = 500;
workplace_height = 280;
workplace_x = 100;
workplace_y = 50;


// Setup Views and Cameras
application_surface_draw_enable(true); //setup application surface
view_enabled = true; //enable the use of views
view_set_visible(0, true); //make view 0 visible
workplace_camera = camera_create_view(0, 0, workplace_width, workplace_height, 0, -1, -1, -1, 0, 0); //create workplace camera

view_set_camera(0, workplace_camera);
view_set_wport(0, workplace_width); //(the surface used should be the size of the view camera itself (not the view port))
view_set_hport(0, workplace_height);

workplace_zoomlevel = 1;
workplace_zoomwidth_default = workplace_width;
workplace_zoomheight_default = workplace_height;
workplace_follow_x = 0;
workplace_follow_y = 0;

DRAW GUI EVENT:

GML:
// workplace surface draw
if surface_exists(workplace_surface)
{
    draw_surface(workplace_surface, workplace_x, workplace_y);
    draw_rectangle_color(workplace_x, workplace_y, workplace_x+workplace_width, workplace_y+workplace_height, c_white,c_white,c_white,c_white,true);
}
else
{
    workplace_surface = surface_create(workplace_width, workplace_height);
    surface_set_target(workplace_surface);
    draw_clear_alpha(c_black, 0);
    surface_reset_target();

    view_set_surface_id(0,workplace_surface);
}



It can be some calculation that uses the coordinates of the GUI, mouse_x and y and the size of the surface or the room size, but I don't know what to do. I spent hours trying to solve this, I need help 😥

Thank you
 
Last edited:

angelwire

Member
The Create Event has application_surface_draw_enable(true); but you mentioned disabling it, is that a typo?
 

FoxyOfJungle

Kazan Games
The Create Event has application_surface_draw_enable(true); but you mentioned disabling it, is that a typo?
This was disabled, I activated it to do a test, I forgot to change the variable back lol, but unfortunately the problem is not this 🙁 Thanks for answer!
 

angelwire

Member
Unfortunately I don't have any insight into how mouse_x and mouse_y works.

But I tried punching in some numbers based on your video and I think this will work:
Code:
actual_mouse_x = ((gui_mouse_x - workplace_x) *  (_workplace_cam_w / workplace_width)) + _workplace_cam_x;
actual_mouse_y = ((gui_mouse_y - workplace_y) *  (_workplace_cam_h / workplace_height)) + _workplace_cam_y
The numbers seemed to be right but I was unable to test it out fully.
 

FoxyOfJungle

Kazan Games
Unfortunately I don't have any insight into how mouse_x and mouse_y works.

But I tried punching in some numbers based on your video and I think this will work:
Code:
actual_mouse_x = ((gui_mouse_x - workplace_x) *  (_workplace_cam_w / workplace_width)) + _workplace_cam_x;
actual_mouse_y = ((gui_mouse_y - workplace_y) *  (_workplace_cam_h / workplace_height)) + _workplace_cam_y
The numbers seemed to be right but I was unable to test it out fully.
OMG!! This worked beautifully, I knew it was a complicated calculation: 😆



GML:
var gui_mx = ((gui_mouse_x() - workplace_x) *  (_workplace_cam_w / workplace_width)) + _workplace_cam_x;
var gui_my = ((gui_mouse_y() - workplace_y) *  (_workplace_cam_h / workplace_height)) + _workplace_cam_y;
It was initially missing the "()" in the gui_mouse_x() and was returning huge values, so I put them in and it fit perfectly!



Thank you so much!! 😍
 
Last edited:

Rob

Member
OMG!! This worked beautifully, I knew it was a complicated calculation: 😆



GML:
var gui_mx = ((gui_mouse_x() - workplace_x) *  (_workplace_cam_w / workplace_width)) + _workplace_cam_x;
var gui_my = ((gui_mouse_y() - workplace_y) *  (_workplace_cam_h / workplace_height)) + _workplace_cam_y;
It was initially missing the "()" in the gui_mouse_x() and was returning huge values, so I put them in and it fit perfectly!



Thank you so much!! 😍
Incidentally, the reason you got those huge values is because gamemaker reads functions as a number. Draw the string of any function without the usual parentheses that the function requires and youll see what I mean.
 

FoxyOfJungle

Kazan Games
Incidentally, the reason you got those huge values is because gamemaker reads functions as a number. Draw the string of any function without the usual parentheses that the function requires and youll see what I mean.
I didn't know that, interesting! Thanks


Unfortunately I don't have any insight into how mouse_x and mouse_y works.
GML:
function gui_mouse_x()
{
    return device_mouse_x_to_gui(0);
}

function gui_mouse_y()
{
    return device_mouse_y_to_gui(0);
}
 
Top