Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mouse Coordinate scaling?  (Read 2616 times)

0 Members and 1 Guest are viewing this topic.

keyforge

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Mouse Coordinate scaling?
« on: July 05, 2011, 01:38:19 am »
Hello :)

I have a 3D rendering in the background and a 2D GUI drawn on top. When I resize the Window it resizes the GUI objects and 3D objects like it should, but when I hover it only works if I put my mouse over the original coordinates and not the resized or scaled ones. Does anyone know how to fix this? I'm using OpenGL for 3D rendering and drawing the GUIs.
Need a place to upload your code, files or screenshots? Use SFML Uploads!

Haikarainen

  • Guest
Mouse Coordinate scaling?
« Reply #1 on: July 05, 2011, 12:35:08 pm »
sf::RenderWindow::GetWidth() and sf::RenderWindow::GetHeight() should cut it ?

float OriginalWidth = 800;
float OriginalHeight = 600;
Scale window to 1024x768;
float ScaleX =  WindowWidth/OriginalWidth;

if(MouseX == PositionX*ScaleX)

Havent tested this at all, just wrote it as i was thinking at the moment. Something like that should work tho :)

keyforge

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Mouse Coordinate scaling?
« Reply #2 on: July 05, 2011, 05:00:56 pm »
Hmm... that isn't really working for me, it's making the hover area smaller after getting bigger and it gets larger after shrinking it... it works before resizing though with the default 1024 * 600 dimensions.

Code: [Select]
if(myRect.Contains(Game::GetInput().GetMouseX() * Game::GetScale().x, Game::GetInput().GetMouseY() * Game::GetScale().y)) {
myState = WidgetState::HOVERED;
} else if(!myRect.Contains(Game::GetInput().GetMouseX() * Game::GetScale().x, Game::GetInput().GetMouseY() * Game::GetScale().y) && myState == WidgetState::HOVERED) {
myState = WidgetState::NORMAL;
}


And the collision code and the scaling code is what you said above with the Y position too.

EDIT: Nevermind, I did another method instead of .Contains() and it works now.
Need a place to upload your code, files or screenshots? Use SFML Uploads!

Haikarainen

  • Guest
Mouse Coordinate scaling?
« Reply #3 on: July 05, 2011, 06:39:23 pm »
Quote from: "keyforge"
Hmm... that isn't really working for me, it's making the hover area smaller after getting bigger and it gets larger after shrinking it... it works before resizing though with the default 1024 * 600 dimensions.

Code: [Select]
if(myRect.Contains(Game::GetInput().GetMouseX() * Game::GetScale().x, Game::GetInput().GetMouseY() * Game::GetScale().y)) {
myState = WidgetState::HOVERED;
} else if(!myRect.Contains(Game::GetInput().GetMouseX() * Game::GetScale().x, Game::GetInput().GetMouseY() * Game::GetScale().y) && myState == WidgetState::HOVERED) {
myState = WidgetState::NORMAL;
}


And the collision code and the scaling code is what you said above with the Y position too.

EDIT: Nevermind, I did another method instead of .Contains() and it works now.


Nice it worked, but didnt really mean u shouldve used that as pure code. I was just trying to speculate to an answer :P

keyforge

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Mouse Coordinate scaling?
« Reply #4 on: July 05, 2011, 07:23:54 pm »
I didn't use that as the pure code, I used a formula similar to what you did. I added a static member function to my Game class called GetScale() which returns the scale of the window from its original size. It's updated when Game::ResetGL() is called.

Would you recommend a GUI stays the same size though regardless of the window size? I've seen it both ways and I'm not sure which is `professional`...
Need a place to upload your code, files or screenshots? Use SFML Uploads!

keyforge

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Mouse Coordinate scaling?
« Reply #5 on: July 05, 2011, 08:43:52 pm »
I have another issue with Buttons... the SFML Input provides no way to notify of a Mouse Button release other than !MouseButtonDown(), but that's a bad method of doing it since it would always return true when the mouse button is down. My code is:

How should I setup the GUI Input system without passing an sf::Event?

Code: [Select]
if(Game::GetInput().GetMouseX() <= ((myRect.Left + myRect.Right) * Game::GetScale().x) && Game::GetInput().GetMouseX() >= myRect.Left && Game::GetInput().GetMouseY() <= ((myRect.Top + myRect.Bottom) * Game::GetScale().y) && Game::GetInput().GetMouseY() >= myRect.Top) {
if(Game::GetInput().IsMouseButtonDown(sf::Mouse::Left)) {
myState = WidgetState::ACTIVE;
} else {
myState = WidgetState::HOVERED;
}
} else {
myState = WidgetState::NORMAL;
}


Code: [Select]
switch(myState) {
case WidgetState::NORMAL:
myColor = sf::Color::Blue;
myBorderColor = sf::Color::White;
break;
case WidgetState::ACTIVE:
myColor = sf::Color::Red;
myBorderColor = sf::Color::White;
myActive = true;
break;
case WidgetState::HOVERED:
myColor = sf::Color::Yellow;
myBorderColor = sf::Color::White;
break;
}
Need a place to upload your code, files or screenshots? Use SFML Uploads!

 

anything