在固定宽高比的虚拟屏幕上处理输入
问题描述:
我在我的相机中使用虚拟屏幕分辨率,固定的放大比率。初始化看起来是这样的:在固定宽高比的虚拟屏幕上处理输入
this.cam = new OrthographicCamera(Config.VIRTUAL_VIEW_WIDTH,
Config.VIRTUAL_VIEW_HEIGHT);
this.cam.setToOrtho(false, Config.VIRTUAL_VIEW_WIDTH,
Config.VIRTUAL_VIEW_HEIGHT);
// create our stage
this.stage = new Stage(0, 0, true);
this.stage.clear(); // always clear the stage too to not get double
// stuff in a stage
stage.setCamera(cam);
//some more stuff
@Override
public void resize(int width, int height) {
// this.cam.setToOrtho(false, width, height);
// calculate new viewport
float aspectRatio = (float) width/(float) height;
float scale = 1f;
Vector2 crop = new Vector2(0f, 0f);
if (aspectRatio > Config.ASPECT_RATIO) {
scale = (float) height/(float) Config.VIRTUAL_VIEW_HEIGHT;
crop.x = (width - Config.VIRTUAL_VIEW_WIDTH * scale)/2f;
} else if (aspectRatio < Config.ASPECT_RATIO) {
scale = (float) width/(float) Config.VIRTUAL_VIEW_WIDTH;
crop.y = (height - Config.VIRTUAL_VIEW_HEIGHT * scale)/2f;
} else {
scale = (float) width/(float) Config.VIRTUAL_VIEW_WIDTH;
}
float w = (float) Config.VIRTUAL_VIEW_WIDTH * scale;
float h = (float) Config.VIRTUAL_VIEW_HEIGHT * scale;
viewport = new Rectangle(crop.x, crop.y, w, h);
}
渲染本身是短暂的:
@Override
public void render(float delta) {
cam.update();
// setting the viewport
Gdx.gl.glViewport((int) viewport.x, (int) viewport.y,
(int) viewport.width, (int) viewport.height);
// the following code clears the screen with the given RGB color (black)
Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// update and draw the stage actors
stage.act(delta);
stage.draw();
这工作真的很好,我可以用我的虚拟解决方案的工作。但我遇到了触摸板的问题。我只是初始化它像这样:
Gdx.input.setInputProcessor(stage);
skin = new Skin();
skin.add("knob", new Texture("touchpad/touchKnob.png"));
skin.add("background", new Texture("touchpad/touchBackground.png"));
style = new TouchpadStyle();
// skin.add
style.knob = skin.getDrawable("knob");
style.background = skin.getDrawable("background");
pad = new Touchpad(10, style);
pad.setBounds(0, Config.VIRTUAL_VIEW_HEIGHT - 150, 150, 150);
this.stage.addActor(pad);
如果屏幕没有安装到虚拟屏幕它不上的触摸正确或者甚至不反应,因为垫将在左上边缘的反应,但在虚拟凸轮在reallity。
任何建议如何解决?
这是一张照片。你看到的区域永远不会改变,所以你总能看到整个地图。我为触摸板“所在”区域着色。 Menü的按钮也遇到了同样的问题。
答
与从其他论坛上一些帮助解决它自己。我改变了调整大小的方法,以便相机放大/缩小:
@Override
public void resize(int width, int height) {
cam.viewportHeight = height;
cam.viewportWidth = width;
if (Config.VIRTUAL_VIEW_WIDTH/cam.viewportWidth < Config.VIRTUAL_VIEW_HEIGHT
/cam.viewportHeight) {
cam.zoom = Config.VIRTUAL_VIEW_HEIGHT/cam.viewportHeight;
} else {
cam.zoom = Config.VIRTUAL_VIEW_WIDTH/cam.viewportWidth;
}
cam.update(); // update it ;)
}