c++ - 文本对象的 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; 只是为了测试它进入循环的距离。
解决方案
getLocalBounds
返回文本本地坐标的边界。您需要使用getGlobalBounds
它来获得世界坐标。
您还需要使用mapPixelToCoords
窗口的方法将鼠标的坐标也转换为世界坐标。
它会是这样的:
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
cout << "This works!" << endl;
}
推荐阅读
- sql - 可以使用多个参数从 PowerShell 运行 SQL 文件
- php - 是否可以在我的 wordpress plugin.php 文件中实现 init.php?
- javascript - 如何在 Nuxt.js 中使用外包和可编辑的脚本?
- sql - 使用 Self-Join 在表中查找更新
- ios - 用户使用登录链接后出现“无效动态链接”错误页面
- reactjs - 如何在 redux 商店中保留模式的内容和行为?
- javascript - 在以下情况下了解不变性和虚拟 DOM
- c++ - C++20 中是否有 boost::irange 等价物?
- amazon-web-services - aws-lambda 实例何时销毁?
- hadoop - 为什么在输出文件中自动跳过标题