victordiaz / PHONK

PHONK is a coding playground for new and old Android devices
https://phonk.app
GNU General Public License v3.0
461 stars 25 forks source link

Press release events button #110

Closed polhomarkho closed 2 years ago

polhomarkho commented 2 years ago

Hi!

I needed to have two kind of new callbacks triggered for a button: "onPress" and "onRelease".
I'm currently extending the midi capabilities of Phonk to create a midi controller to drive my soft synth on my computer and I needed to add these callbacks to trigger the notes. The midi part will be in an other PR as I need to add more methods (but it's working, yay!).

The new callbacks are fully compatible with the existing "onLongPress" and "onClick" :

ui.addTitle(app.name)

const button = ui.addButton('press me!', 0.1, 0.1, 0.8, 0.8);
button.onClick(function () {
  console.log("onClick");
});

button.onPress(function () {
  console.log("onPress");
});

button.onRelease(function () {
  console.log("onRelease");
});

button.onLongPress(function () {
  console.log("onLongPress");
});

Will produce:

polhomarkho commented 2 years ago

Hi!
I just added a small change to consider MotionEvent.ACTION_CANCEL as an MotionEvent.ACTION_UP as the onRelease callback didn't fire sometimes.
I noticed in the doc that it should be handled like that : https://developer.android.com/reference/android/view/MotionEvent#ACTION_CANCEL

victordiaz commented 2 years ago

It works perfectly in my setup so I merged it. Sorry it took so long, I even didnt have android studio installed in my system 😅

I will keep checking the other pull requests.

What it would be nice is to include all of this new changes as examples. I'm planning to clean a bit this repo during the week and merge the examples and WebIDE repo here.

So I will ping you again once everything is merged :) Thanks again!

polhomarkho commented 2 years ago

Hi!
Thank you for checking it out!
I need to figure out how the documentation and examples work but I'll add them for sure.
Don't worry about the time it took! Big 👍 for reinstalling android studio and taking the time to review and merge 😉