我如何在C++中的窗口中捕获(陷阱)鼠标?

发布于 2025-02-07 06:13:52 字数 1252 浏览 3 评论 0原文

我正在用SFML和C ++编写瓷砖地图编辑器。我一直在用鼠标遇到各种麻烦。我正在使用内置的sfml鼠标::静态函数,并且最近设法使一个自定义光标在屏幕上移动并通过执行以下操作准确地指向瓷砖

Sprite cursor;
bool focus = false;

RenderWindow window(VideoMode(512, 288), "Tilemap editor");
window.setFramerateLimit(60);

Texture cursorTexture;


if(!cursorTexture.loadFromFile("Graphics/Cursor.png")) {
    std::cout << "Failed to load cursor texture\n";
    return 0;
}

cursor.setTexture(cursorTexture);

Mouse::setPosition(mousePos);

While(window.isOpen()) {

    window.setMouseCursorVisible(focus);


    if(Mouse::getPosition().x != lastMousePos.x) {
        mousePos.x = mousePos.x + (Mouse::getPosition().x - lastMousePos.x);
    }
    if(Mouse::getPosition().y != lastMousePos.y) {
        mousePos.y = mousePos.y + (Mouse::getPosition().y - lastMousePos.y);
    }

    cursor.setPosition(mousePos.x, mousePos.y);
        
    lastMousePos = Mouse::getPosition();

    window.clear();

    window.draw(cursor)
        
    window.display()
}

:窗口以及我在视图移动的小窗口中使用此应用程序时,我也无法使用。上面的解决方案移动光标独立于桌面,并且在我想移动视图时可以移动光标。

问题在于,当我尝试单击左上角的项目时,我的鼠标将从应用程序的侧面移动。

除非我输入击键(例如VM窗口),否则是否有一个良好的跨平台(我正在Linux BTW上)将鼠标捕获到窗口的中?另外,一般而言,是否有更好的方法可以进行跨平台鼠标支持? SFML有点糟糕。 (显然,代码需要在主要函数中,命名空间必须是SFML/Graphics.hpp的SF)

I am writing a tile map editor in SFML and C++. I have been having all sorts of troubles with the mouse. I am using the built in SFML Mouse:: static functions and recently managed to get a custom cursor moving on the screen and pointing accurately to a tile by doing as follows:`

Sprite cursor;
bool focus = false;

RenderWindow window(VideoMode(512, 288), "Tilemap editor");
window.setFramerateLimit(60);

Texture cursorTexture;


if(!cursorTexture.loadFromFile("Graphics/Cursor.png")) {
    std::cout << "Failed to load cursor texture\n";
    return 0;
}

cursor.setTexture(cursorTexture);

Mouse::setPosition(mousePos);

While(window.isOpen()) {

    window.setMouseCursorVisible(focus);


    if(Mouse::getPosition().x != lastMousePos.x) {
        mousePos.x = mousePos.x + (Mouse::getPosition().x - lastMousePos.x);
    }
    if(Mouse::getPosition().y != lastMousePos.y) {
        mousePos.y = mousePos.y + (Mouse::getPosition().y - lastMousePos.y);
    }

    cursor.setPosition(mousePos.x, mousePos.y);
        
    lastMousePos = Mouse::getPosition();

    window.clear();

    window.draw(cursor)
        
    window.display()
}

The built-in Mouse functions only display relativity to the desktop or the window and as I am using this app in a small window in which my view moves, I can't use either. The solution above moves a cursor independent of the desktop and with the ability to move the cursor if and when I want to move my view.

The issue is that my mouse will move off the side of the app when I try to click items in the top left corner.

Is there a good cross-platform (I'm on Linux BTW) way to trap the mouse inside of the window unless I enter a keystroke (like a VM window)? Also, is there a better way to do cross-platform mouse support in general? SFML kinda sucks. (Code obviously needs to be in a main function and the namespace must be sf with SFML/Graphics.hpp included)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮筵 2025-02-14 06:13:52

已经有一种方法了。

void    setMouseCursorGrabbed (bool grabbed)
// Grab or release the mouse cursor.

您也可以使用这些方法将屏幕坐标转换为鼠标坐标,反之亦然。

Vector2f    mapPixelToCoords (const Vector2i &point) const
// Convert a point from target coordinates to world coordinates, using the current view.
 
Vector2f    mapPixelToCoords (const Vector2i &point, const View &view) const
// Convert a point from target coordinates to world coordinates.
 
Vector2i    mapCoordsToPixel (const Vector2f &point) const
// Convert a point from world coordinates to target coordinates, using the current view.
 
Vector2i    mapCoordsToPixel (const Vector2f &point, const View &view) const
// Convert a point from world coordinates to target coordinates.

There is already a method for that.

void    setMouseCursorGrabbed (bool grabbed)
// Grab or release the mouse cursor.

You can also use these methods to convert your screen coordinates to mouse coordinates and vice versa.

Vector2f    mapPixelToCoords (const Vector2i &point) const
// Convert a point from target coordinates to world coordinates, using the current view.
 
Vector2f    mapPixelToCoords (const Vector2i &point, const View &view) const
// Convert a point from target coordinates to world coordinates.
 
Vector2i    mapCoordsToPixel (const Vector2f &point) const
// Convert a point from world coordinates to target coordinates, using the current view.
 
Vector2i    mapCoordsToPixel (const Vector2f &point, const View &view) const
// Convert a point from world coordinates to target coordinates.

sf::RenderWindow Class Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文