I'm trying to get my cursor sprite to try and appear on top of my mouse cursor but for some reason it is not appearing on top of the mouse cursor.
Code:
cursorSprite.setPosition(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y);
As you can see, I'm simply using setPosition() with my sprite, and setting it at the x and y positions of the mouse. However, this is not working and the cursor sprite is appearing at a different location to my actual mouse cursor location.
Why? Could it be something to do with how I'm setting up my window?
window(sf::VideoMode(800, 600, 32), "SFML Test", sf::Style::Default)
There are two main coordinate systems that should be considered when running SFML app (as well as pretty much every other application) in windowed mode: screen coordinates and windowed coordinates.
You are getting wrong results because sf::Mouse::getPosition() return click position in screen coordinates and you want click in window coordinates.
You can manually transform screen coordinates to window coordinates, but it is much better to use this interface provided by SFML:
sf::Vector2i pos = sf::Mouse::getPosition(window);
The pos will be in window coordinates.
Related
I'm trying to make a simple application using c++ and SFML. I would like for the application to work at any resolution - windowed or fullscreen.
The closest thing to a solution I've found in documentation is this simple snippet of code.
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
This creates an effect that looks like this http://i.imgur.com/RvCOuKi.png.
This is a start - but it's not exactly what I'm looking for. When I resize the window, the "original" viewing space is in the top left, with the newly visible objects all below or to the right of the original rectangle.
I think this method of resizing is ugly. When I put something at the center of the screen at one resolution, it should stay in the center even at another resolution. How do I center the view in such a way that this occurs?
A great example of this is minecraft. In minecraft, when you resize the window, the "original" viewport is in the very center and newly visible objects appear around the edges. How can I recreate this in SFML?
Just use another constructor and pass the center of the current view:
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(), sf::Vector2f((float)event.size.width, (float)event.size.height)));
}
How will i get a application x y position relative to the client screen?
I tried but all unsucessful so can anyone help.
RECT pta;
GetWindowRect(hWnd,&pta);
POINT Rpt = { pta.left, pta.top };
ScreenToClient(hWnd, &Rpt);
But this doesn't work.
I want to set my cursor position to middle in the window of my app
If I understand right, you want to call the SetCursorPos() windows API call to center the mouse cursor to your window. That function takes screen coordinates.
GetWindowRect() returns the window top and left coordinates already in the screen coordinates, so no transform is necessary.
To get to your window's center coordinates, you just need to add half of your window's width and height to the top-left point's coordinates. Then you can call SetCursorPos().
I am using Qt Graphics Framework for displaying an image. I have opened a raw image in subclassed QGraphicsScene in QGraphicsView using addPixmap(). I have added zoom feature by using scale function and drag mode is set as scroll hand drag. Now, I need to get the pixel coordinates within the scene on mouse hover such that the x and y value show the pixel in the image (drawn by pixmap) the mouse is currently pointing to. I tried using pos() but it didn't work.
Here is the code from Widget.cpp:
img = openImage(dirPath2.toLocal8Bit().data(),
sizeX,sizeY,file_heade,scan_heade,bpp,sign);
QPixmap x = QPixmap(sizeX,sizeY);
x.convertFromImage(img,Qt::AutoColor);
scene->addPixmap(x);
ui->disp_img->setDragMode(QGraphicsView::ScrollHandDrag);
GraphicsScene.h:
class GraphicsScene : public QGraphicsScene {
public:
GraphicsScene(QWidget *parent) : QGraphicsScene(parent){}
};
(preferably the pixmap coordinates but even that doesn't happen and if the values change when zoomed I will use scale factor to get the original values)
I suggest you start by reading about Qt's Graphics Coordinate System.
There are various layers of coordinate systems and you need to think about those with which you dealing with. At the top layer is the screen (or view), which is where the mouse coordinates reside.
The next layer from the view is the graphics scene. Graphics items, such as the QGraphicsPixmapItem which you added with addPixmap, reside here. The graphics scene can be visualised as a world of items, each with there own position and orientation.
Moving to the last coordinate system is an item's local coordinate system. If, for example, we take a rectangle, it may have local coordinates of (-5, -5, 10, 10) for (x, y, w, h). This item is then placed in the scene at some position. If its position is the origin of the scene (0,0), then the item's local coordinates would read the same as its scene coordinates.
However, if we move the rectangle +5 units in x-axis, its local coordinates are the same, but its scene coordinates would now be (0, -5, 10, 10).
Likewise, the view (QGraphicsScene) is a window into the scene and can be looking at the whole scene, or just part of it. As the view's top left coordinate is (0,0), it may map onto (0,0) of the scene, or may not, depending on what area of the scene the view is looking at.
So, by getting a mouse position you're starting in the view's coordinates and need to convert to the scene's coordinate system. Fortunately, Qt provides lots of useful functions for this at every level.
To convert the mouse coordinates from the view to the scene, you can use the view's mapToScene function.
Using the scene coordinates you can then get an item and map that to the local coordinate's of the item with the item's mapFromScene.
This is continue of my previous question -> Draw mouse pointer icon?
I want to draw mouse in specific window on the desktop, i already know how to draw the mouse and how to track the movement of the real mouse.
But i fail to convert screen coordinates, here is example what i want to do:
When the REAL mouse is in the upper left corner 0,0 the DRAWN mouse to be in 0,0 of my specific window, and when the REAL mouse is in the down right corner 1600,900 the DRAWN mouse to be in 700,400 of my specific window.
I will re-explain if someone is not understanding exactly the problem.
Thanks in advance.
You need to scale the mouse position w.r.t your window dimensions.
Let DX and DY be the desktop size. Let WX and WY be your window size. Let (Dmx,Dmy) be the coordinate of the original mouse position w.r.t the desktop. Then the position of your mouse within your window according to your requirement would be (Dmx/DX * WX, Dmy/DY * WY). When coding, please remember to make sure the division happens with floating point numbers.
How can I draw a selection rectangle on my screen with Qt in X11?
I want to be able to drag a rectangle on my screen (outside of the application) and then save the whole rectangle.
Thanks in advance.
Part of the solution will involve using the grabWindow() function of QPixmap like so:
QPixmap::grabWindow(QApplication::desktop()->winId());
Qt has an example program for this here.
There rest of the solution, drawing the area to grab, can probably be achieved by either using a full screen transparent window to render a mouse drawn rectangle and then taking the section it outlines from the grabbed desktop image or using a full screen window with the entire grabbed screen painted on it.