{"id":617,"date":"2017-08-21T17:57:31","date_gmt":"2017-08-21T16:57:31","guid":{"rendered":"http:\/\/www.pixnbgames.com\/blog\/?p=617"},"modified":"2017-08-21T17:57:31","modified_gmt":"2017-08-21T16:57:31","slug":"fixing-libgdx-issue-on-ios-version-with-landscape-orientation","status":"publish","type":"post","link":"https:\/\/www.pixnbgames.com\/blog\/libgdx\/fixing-libgdx-issue-on-ios-version-with-landscape-orientation\/","title":{"rendered":"Fixing libGDX issue on iOS version with Landscape orientation"},"content":{"rendered":"<p>I&#8217;m currently working on a platform game made with libGDX-1.9.3. This game runs in <em>Landscape<\/em> mode.<\/p>\n<figure id=\"attachment_612\" aria-describedby=\"caption-attachment-612\" style=\"width: 1014px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-612\" src=\"http:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2017\/08\/Captura-de-pantalla-2017-08-21-a-las-13.33.22.png\" alt=\"Rainbow Diamonds screenshot\" width=\"1014\" height=\"560\" srcset=\"https:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2017\/08\/Captura-de-pantalla-2017-08-21-a-las-13.33.22.png 1014w, https:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2017\/08\/Captura-de-pantalla-2017-08-21-a-las-13.33.22-300x166.png 300w, https:\/\/www.pixnbgames.com\/blog\/wp-content\/uploads\/2017\/08\/Captura-de-pantalla-2017-08-21-a-las-13.33.22-768x424.png 768w\" sizes=\"(max-width: 1014px) 100vw, 1014px\" \/><figcaption id=\"caption-attachment-612\" class=\"wp-caption-text\">Rainbow Diamonds screenshot<\/figcaption><\/figure>\n<p>The iOS compilation has worked right, and the tests on the iPhone simulator (with iOS 9) are also OK. But, when I test the game in my iPhone 4 with iOS 7, I find an error with touch event listeners. Touch events are received as if the phone were in <em>Portrait<\/em> mode instead of <em>Landscape<\/em> mode.<\/p>\n<p>After searching some info about it, it seems this a known bug of libGDX, introduced in the version 1.9.0, and affect to iOS 7 and 8. You can follow the state of this issue on <a href=\"https:\/\/github.com\/libgdx\/libgdx\/pull\/3590\" target=\"_blank\" rel=\"noopener\">libGDX official site<\/a>. On the issue page there is a workaround to fix the problem until it is resolved on libGDX distribution.<\/p>\n<p>If is useful for you, next I put the code I use to temporarily fix this issue.<\/p>\n<p>&nbsp;<\/p>\n<h3>Knowing the iOS version of the device<\/h3>\n<p>We do this in the iOS app launcher, class <code>IOSLauncher<\/code> of the project <em>MyGdxGame<\/em>-ios. Here we check if our game is running on an iOS 7 or 8 device. The result is stored on a static boolean variable, that we can read on any point of our app.<\/p>\n<pre lang=\"Java\">...\r\nimport org.robovm.apple.foundation.FoundationVersionNumber\r\n...\r\npublic class IOSLauncher extends IOSApplication.Delegate {\r\n\r\n    @Override\r\n    protected IOSApplication createApplication() {\r\n        \/\/ GameConfig.ios7or8 is a static boolean variable\r\n        GameConfig.ios7or8 = \r\n            FoundationVersionNumber.getVersion() >= FoundationVersionNumber.Version_iOS_7_0\r\n            && FoundationVersionNumber.getVersion() <= FoundationVersionNumber.Version_iOS_8_3;\r\n\r\n        IOSApplicationConfiguration config = new IOSApplicationConfiguration();\r\n        return new IOSApplication(new MyGdxGame(), config);\r\n    }\r\n    ...\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Applying coordinates transformation<\/h3>\n<p>Now that we know if the device has iOS 7 or 8, the next thing is to translate the received coordinates when this condition is true. About this, some considerations:<\/p>\n<ul>\n<li>It's needed to make the transformation on all <code>InputProcessor<\/code> we use. Remember that if you use <code>Stage<\/code>, this class is also an implementation of <code>InputProcessor<\/code>. It's not necessary to make the coordinates translation on each event listener we have, but only on the <code>InputProcessor<\/code> and it will do the event propagation with the right coordinates.<\/li>\n<li>We must override the methods <code>touchDown()<\/code>, <code>touchUp()<\/code> and <code>touchDragged()<\/code> accordingly the methods we use in our event listeners. If you are not sure which methods you use, you can override them all.<\/li>\n<li>If it's used <code>Touchpad<\/code> as game input control, then it's needed to overwrite the three methods in order to make the touchpad works succesfully.<\/li>\n<\/ul>\n<p>The coordinates transformation han been taken from the libGDX issue page and it should be noted that there are two landscape orientation modes: <em>LandscapeLeft<\/em> (home button on left side) and <em>LandscapeRight<\/em> (home button on right side). To know which the current orientation is, it's checked the device rotation, because the transformation differs on each case.<\/p>\n<pre lang=\"Java\">...\r\npublic class MyGameLayer extends WidgetGroup implements InputProcessor {\r\n    ...\r\n    @Override\r\n    public boolean touchDown(int screenX, int screenY, int pointer, int button) {\r\n        if (GameConfig.ios7or8) {\r\n            int rotation = Gdx.input.getRotation();\r\n            if (rotation == 90) { \/\/ Landscape right\r\n                int ox = screenX;\r\n                screenX = screenY;\r\n                screenY = Gdx.graphics.getHeight() - ox;\r\n            } else if (rotation == 270) { \/\/ Landscape left\r\n                int oy = screenY;\r\n                screenY = screenX;\r\n                screenX = Gdx.graphics.getWidth() - oy;\r\n            }\r\n        }\r\n        return super.touchDown(screenX, screenY, pointer, button);\r\n    }\r\n    ...\r\n}\r\n<\/pre>\n<p>The previous fragment of code correspond to the method <code>touchDown()<\/code>. The methods <code>touchUp()<\/code> and <code>touchDragged()<\/code> are equals, but in the last line calling to the corresponding superclass method.<\/p>\n<p>Regards!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m currently working on a platform game made with libGDX-1.9.3. This game runs in Landscape mode. The iOS compilation has worked right, and the tests on the iPhone simulator (with iOS 9) are also OK. But, when I test the <a class=\"more-link\" href=\"https:\/\/www.pixnbgames.com\/blog\/libgdx\/fixing-libgdx-issue-on-ios-version-with-landscape-orientation\/\">Seguir leyendo <span class=\"screen-reader-text\">  Fixing libGDX issue on iOS version with Landscape orientation<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":612,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[51,15],"language":[4],"_links":{"self":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/617"}],"collection":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/comments?post=617"}],"version-history":[{"count":5,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions"}],"predecessor-version":[{"id":624,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/posts\/617\/revisions\/624"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media\/612"}],"wp:attachment":[{"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/media?parent=617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/categories?post=617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/tags?post=617"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/www.pixnbgames.com\/blog\/wp-json\/wp\/v2\/language?post=617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}