{"id":395,"date":"2010-07-23T21:39:50","date_gmt":"2010-07-24T03:39:50","guid":{"rendered":"http:\/\/www.philhassey.com\/blog\/?p=395"},"modified":"2010-07-28T14:22:44","modified_gmt":"2010-07-28T20:22:44","slug":"android-day-4-video-cleanup-input-handling","status":"publish","type":"post","link":"https:\/\/www.philhassey.com\/blog\/2010\/07\/23\/android-day-4-video-cleanup-input-handling\/","title":{"rendered":"Android Day 4: Video cleanup, Input handling"},"content":{"rendered":"<p>Whew, day 3 was epic!  I&#8217;m glad I got through all that!  Day 4 (I hope) will be a bit easier.  Now that I&#8217;ve got things &#8220;basically&#8221; working, it should be mostly a matter of getting a few more API calls doing what they need to do.<\/p>\n<p><strong>Step 1 &#8211; Basic handling of onPause:<\/strong><\/p>\n<p>First of all, right now when I restart the App without reinstalling it, I get a white screen.  I think this is due to some multitasking feature on the Android where it shoves the App into the background and tells it to pause.  When unpaused, I need to reload the graphics.  Fixing this was pretty trivial, I just added a few lines to the onPause() method to save state and basically shutdown.<\/p>\n<p><strong>Step 2 &#8211; Removing the title bar:<\/strong><\/p>\n<p>When I start my game, there is a title that says &#8220;Galcon&#8221; in text on the top.  I think that is part of the default app generation.  I&#8217;ll want to remove that.  First of all, in &#8220;res\/layout\/main.xml&#8221; I commented out the TextView.  This had no effect, but at least I cleared out some junk.  This page appears to explain how to hide the title bar (I found by adding .Fullscreen it hides the Status Bar as well.)<\/p>\n<p>http:\/\/developer.android.com\/guide\/appendix\/faq\/commontasks.html &#8211; modify AndroidManifest.xml:<\/p>\n<p>&lt;application android:icon=&#8221;@drawable\/icon&#8221; android:theme=&#8221;@android:style\/Theme.NoTitleBar.Fullscreen&#8221;&gt;<\/p>\n<p><strong>Step 3 &#8211; Adding onTouchEvent handling:<\/strong><\/p>\n<p>I really want to start playing the game &#8211; so I&#8217;m going to add some input event handling.  I&#8217;ll do this from the top level in my Activity by adding a onTouchEvent handler.  The documentation for MotionEvent was a bit opaque, but I worked it out and here&#8217;s how to handle events and deal with MultiTouch.  It seems like something is severely broken about the API, but this was the only way I could get MultiTouch to work in Galcon.  It appears to only work with 2 fingers.<\/p>\n<pre>    public boolean onTouchEvent(final MotionEvent e) {\r\n        for (int i = 0; i&lt;e.getPointerCount(); i++) {\r\n            boolean masked = false;\r\n            switch(e.getActionMasked()) {\r\n                case MotionEvent.ACTION_POINTER_DOWN:\r\n                case MotionEvent.ACTION_POINTER_UP:\r\n                    masked = true;\r\n                break;\r\n            }\r\n            if (masked &amp;&amp; i != e.getActionIndex()) { continue; }\r\n            float x = e.getX(i); float y = e.getY(i);\r\n            float dx = 0; float dy = 0;\r\n            if (e.getHistorySize()!=0) {\r\n                dx = x - e.getHistoricalX(i,0);\r\n                dy = y - e.getHistoricalY(i,0);\r\n            }\r\n            int pid = e.getPointerId(i);\r\n            int type = 0;\r\n            switch(e.getActionMasked()) {\r\n                case MotionEvent.ACTION_DOWN: type=EVT_DOWN; break;\r\n                case MotionEvent.ACTION_MOVE: type=EVT_MOTION; break;\r\n                case MotionEvent.ACTION_OUTSIDE: type=EVT_MOTION; break;\r\n                case MotionEvent.ACTION_POINTER_DOWN: type=EVT_DOWN; break;\r\n                case MotionEvent.ACTION_POINTER_UP: type=EVT_UP; break;\r\n                case MotionEvent.ACTION_UP: type=EVT_UP; break;\r\n                case MotionEvent.ACTION_CANCEL: type=EVT_UP; break;\r\n            }\r\n            event(type,(int)x,(int)y,0,pid+1,(int)dx,(int)dy,0,0);\r\n            if (masked) { break; }\r\n        }\r\n        return true;\r\n    }<\/pre>\n<p>This works okay, except my Nexus One has a different screen resolution than my iPhone, so all my code is handling things a bit off.  I added a native call in onSurfaceChanged to tell my game the size of the screen.  In my event handler I translate for the game.<\/p>\n<p><strong>Step 4 &#8211; Keyboard handling:<\/strong><\/p>\n<p>To play the multi-player game, I need to have the pop-up keyboard work.  On the Droid, of course, this isn&#8217;t needed, but for the Nexus One and possibly other future devices it is.<\/p>\n<p>This is apparently easier said than done.  More on it in day 5!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Whew, day 3 was epic! I&#8217;m glad I got through all that! Day 4 (I hope) will be a bit easier. Now that I&#8217;ve got things &#8220;basically&#8221; working, it should be mostly a matter of getting a few more API calls doing what they need to do. Step 1 &#8211; Basic handling of onPause: First [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112,20],"tags":[],"class_list":["post-395","post","type-post","status-publish","format-standard","hentry","category-android","category-galcon"],"_links":{"self":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/comments?post=395"}],"version-history":[{"count":5,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/395\/revisions"}],"predecessor-version":[{"id":431,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/posts\/395\/revisions\/431"}],"wp:attachment":[{"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/media?parent=395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/categories?post=395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.philhassey.com\/blog\/wp-json\/wp\/v2\/tags?post=395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}