首页 > 解决方案 > 文本对象的 getLocalBounds 替代方案?(SFML)

问题描述

尝试使用 SFML 为 Comp Sci final 制作按钮,并且真的不想在每个按钮上绘制不可见的精灵。

我找到了一些解决方案,但它们都使用旧版本的 sfml,并且这些功能已被删除或更改,并且不确定它们已更改为什么。

while(window.isOpen()){
        Event event;

            while(window.pollEvent(event)){

                switch(event.type)
                {

                    case Event::Closed:
                        window.close();
                        cout << "Window Closed!" << endl;
                        break;

                    case Event::MouseButtonPressed:
                        if(event.mouseButton.button == Mouse::Left){
                            cout << "  if(event.mouseButton.button == Mouse::Left){" << endl;
                            if(equationsButtonText.getLocalBounds().contains(event.mouseButton.x, event.mouseButton.y)){
                                cout << "This works!" << endl;
                                }
                            }

                    default:
                    break;
                    }
                }
            }

cout << " if(event.mouseButton.button == Mouse::Left){" << endl; 只是为了测试它进入循环的距离。

标签: c++user-interfacespritesfml

解决方案


getLocalBounds返回文本本地坐标的边界。您需要使用getGlobalBounds它来获得世界坐标。

您还需要使用mapPixelToCoords窗口的方法将鼠标的坐标也转换为世界坐标。

它会是这样的:

if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
    cout << "This works!" << endl;
}

推荐阅读