getLocalBounds 替代文本对象? (SFML)
getLocalBounds alternative for Text Objects? (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; was just to test how far into the loop it got.
getLocalBounds
returns 文本本地坐标中的边界。您需要使用 getGlobalBounds
才能在世界坐标中获取它。
您还需要使用 window 的 mapPixelToCoords
方法将鼠标坐标也转换为世界坐标。
会是这样的:
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
cout << "This works!" << endl;
}
尝试使用 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; was just to test how far into the loop it got.
getLocalBounds
returns 文本本地坐标中的边界。您需要使用 getGlobalBounds
才能在世界坐标中获取它。
您还需要使用 window 的 mapPixelToCoords
方法将鼠标坐标也转换为世界坐标。
会是这样的:
if(equationsButtonText.getGlobalBounds().contains(window.mapPixelToCoords({event.mouseButton.x, event.mouseButton.y}))){
cout << "This works!" << endl;
}