drewaburden / Cat-apps

http://drewaburden.github.com/Cat-apps
1 stars 0 forks source link

"Petting" implementation #8

Closed drewaburden closed 12 years ago

drewaburden commented 12 years ago

We need an implementation to actually "pet" the cat by rubbing the screen instead of just poking the cat. The "petting" should only work when it's contacting the cat (i.e., it shouldn't register as a pet if you rub the screen too far away from the cat). The threshold of a registered "pet" action should be independent of screen size, and readily adjustable. This might be of interest: http://stackoverflow.com/questions/937313/android-basic-gesture-detection

You should be able to keep the cat image as a borderless button. But plan ahead, because later we will likely use a custom UI component that extends an ImageView. Your code should work independent of the UI class that's being used (or at least be extremely trivial to update to use a new class). Of course, we might not know how exactly we will need to use this implementation yet, so planning for scalability and the unknown are key here. This is really important in everything that we do since we're doing a kind of incremental design and prototyping. I really can't stress this enough: think ahead. Write it all out beforehand if you have to. It will help to cut down on excess work for us in the future.

Daniel, I've assigned this to Matt (because you can't assign multiple people, which is stupid), but you can negotiate with him if you'd like to work on this too.

Should you have questions about anything related to our current UI design/implementation, you know how to contact me.

jamesmatthewgarner commented 12 years ago

Have this working almost 100%, simply need the latest updates from everyone else to work it into!

jamesmatthewgarner commented 12 years ago

Alright, I am closing this issue as I am about to commit and push and here are a few notes. I am including them in the push as well, for overall documentation. Firstly, I did not use gestures at all, I used MotionEvents because I found the documentation much more clear for it, and it's got a lot of flexibility. The way petting is implemented now is thus: In MainActivity.onCreate() I create a RelativeLayout called petLayout. This gets the ID for the overall relativelayout in our layout(I used the same ID for the topmost relative layouts in the layout-land and layout so that the program can get it dynamically without any logic checking). Once the RelativeLayout has been identified and stored, I set it's OnTouchListener to one of my own design, PetListener. **_Something of importance to note here is that since it is simply setting the OnTouchListener of the relative layout, it does NOT need to be done in onResume() and then destroyed in onPause(), as once it is instantiated it, that relative layout's OnTouchListener will not change._** Every time an ACTION_DOWN is caught, it is in a new motion. It stores the X and Y values of the action and waits for the ACTION_UP. While waiting on the ACTION_UP, if any ACTION_MOVE occurs, it is tested to see if it is within the confines of the rectangle given by the ImageView imageCat. The boolean true or false is then stored in a List, for calculations later. Once the ACTION_UP is seen, the following calculations occur: 1) If the ACTION_DOWN and ACTION_UP were both within the confines of the rectangle, the onCatButtonClick method is called and the text Meow! appears and the hearts go up by 5. 2) The number of times an ACTION_MOVE event is within the confines of the ImageView imageCat is divided by the number of times an ACTION_MOVE event occurs, effectively getting the percentage of ACTION_MOVE events that occur "on" the "cat." 3) If this percentage is greater than or equal to the percentPet, then it is a pet. Hearts is incremented by 10 and updated. 4) Regardless of whether or not the whole MotionEvent sequence was classified as a pet or not, the values in all the tracking variables is cleared for the next pet.