【发布时间】:2019-06-28 09:30:31
【问题描述】:
根据文档 (https://github.com/libgdx/libgdx/wiki/Continuous-&-non-continuous-rendering),应该可以限制进行的渲染调用。但是,每次我移动鼠标时,仍然会调用渲染方法。我想知道是否可以将渲染限制为仅在发生某些 action 时才进行(或按需添加必要的 requestRendering )。
在下面的示例中,我将连续渲染设置为 false,并在舞台上调用 setActionsRequestRendering 将其设置为 false。
public class TestApp extends ApplicationAdapter {
private Stage stage;
private Drawable createDrawable(Color color) {
Pixmap labelColor = new Pixmap(100, 100, Pixmap.Format.RGB888);
labelColor.setColor(color);
labelColor.fill();
return new TextureRegionDrawable(new Texture(labelColor));
}
@Override
public void create() {
Gdx.graphics.setContinuousRendering(false);
stage = new Stage();
stage.setActionsRequestRendering(false);
Gdx.input.setInputProcessor(stage);
Drawable imageUp = createDrawable(Color.WHITE);
Drawable imageOver = createDrawable(Color.RED);
ImageButtonStyle style = new ImageButtonStyle();
style.imageUp = imageUp;
style.imageOver = imageOver;
ImageButton button = new ImageButton(style);
button.setSize(100, 100);
button.setPosition(50, 50);
stage.addActor(button);
}
@Override
public void render() {
System.out.println("render");
Gdx.gl.glClearColor(0f, 0f, 0f, 1.f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void dispose () {
stage.dispose();
}
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.fullscreen = false;
config.width = 200;
config.height = 200;
new LwjglApplication(new TestApp(), config);
}
}
根据文档:
如果连续渲染设置为 false,render() 方法将 仅在以下情况发生时调用。
- 触发了输入事件
- Gdx.graphics.requestRendering() 被调用
- Gdx.app.postRunnable() 被调用
我假设移动鼠标算作输入事件。
我希望仅在按钮实际需要更改其渲染状态(按钮向上/按钮上方)时才调用渲染方法。如果那不可能,至少当鼠标位置不是hitting 按钮时不应该调用渲染。
【问题讨论】:
-
Gdx.input.setInputProcessor(stage);应该在 create() 方法中而不是在 render() 方法中。super.render()没用,因为ApplicationAdapter.render()什么都不做 -
@Morchul:感谢您的提示,我根据您的建议清理了示例。
-
我认为这不可能是你想要的。因为每次移动鼠标时如果不调用 render() 就无法检测到悬停
-
是的,如果没有获得鼠标位置输入事件,它无法计算箭头何时在按钮上方。