Better touch events support
This commit is contained in:
parent
c3a859d358
commit
4e9a551452
10 changed files with 109 additions and 27 deletions
|
|
@ -50,10 +50,20 @@ public class MainActivity extends Activity {
|
|||
|
||||
@Override
|
||||
public boolean onTouchEvent(MotionEvent e) {
|
||||
if (e.getAction() == MotionEvent.ACTION_DOWN) {
|
||||
MainActivity.this.nativeApp.touch((int)e.getX(), (int)e.getY());
|
||||
switch (e.getActionMasked()) {
|
||||
case MotionEvent.ACTION_DOWN:
|
||||
case MotionEvent.ACTION_POINTER_DOWN:
|
||||
MainActivity.this.nativeApp.touchDown((int)e.getX(), (int)e.getY());
|
||||
return true;
|
||||
case MotionEvent.ACTION_UP:
|
||||
case MotionEvent.ACTION_POINTER_UP:
|
||||
MainActivity.this.nativeApp.touchUp((int)e.getX(), (int)e.getY());
|
||||
return true;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
MainActivity.this.nativeApp.touchMove((int)e.getX(), (int)e.getY());
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,9 @@ public class PsemekApplication {
|
|||
private static native long createNativeApp();
|
||||
private static native void destroyNativeApp(long ptr);
|
||||
private static native void resizeNative(long ptr, int width, int height);
|
||||
private static native void touchNative(long ptr, int x, int y);
|
||||
private static native void touchDownNative(long ptr, int x, int y);
|
||||
private static native void touchUpNative(long ptr, int x, int y);
|
||||
private static native void touchMoveNative(long ptr, int x, int y);
|
||||
private static native void drawFrameNative(long ptr);
|
||||
|
||||
private static native int audioFrequencyNative();
|
||||
|
|
@ -65,13 +67,15 @@ public class PsemekApplication {
|
|||
|
||||
@Override
|
||||
public void run() {
|
||||
while (true) {
|
||||
while (!interrupted()) {
|
||||
int samples = PsemekApplication.audioGetSamples(buffer, 0, buffer.length);
|
||||
PsemekApplication.this.audioTrack.write(buffer, 0, samples, AudioTrack.WRITE_BLOCKING);
|
||||
try {
|
||||
Thread.sleep(1);
|
||||
}
|
||||
catch (InterruptedException e) {}
|
||||
catch (InterruptedException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -81,17 +85,17 @@ public class PsemekApplication {
|
|||
setAssetManager(assetManager);
|
||||
|
||||
AudioAttributes audioAttributes = new AudioAttributes.Builder()
|
||||
.setUsage(AudioAttributes.USAGE_GAME)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
||||
.build();
|
||||
.setUsage(AudioAttributes.USAGE_GAME)
|
||||
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
|
||||
.build();
|
||||
|
||||
AudioFormat audioFormat = new AudioFormat.Builder()
|
||||
.setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
|
||||
.setSampleRate(audioFrequencyNative())
|
||||
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
|
||||
.build();
|
||||
AudioFormat audioFormat = new AudioFormat.Builder()
|
||||
.setEncoding(AudioFormat.ENCODING_PCM_FLOAT)
|
||||
.setSampleRate(audioFrequencyNative())
|
||||
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
|
||||
.build();
|
||||
|
||||
int bufferSize = AudioTrack.getMinBufferSize(audioFrequencyNative(), AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_FLOAT);
|
||||
int bufferSize = AudioTrack.getMinBufferSize(audioFrequencyNative(), AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_FLOAT);
|
||||
|
||||
audioTrack = new AudioTrack.Builder()
|
||||
.setAudioAttributes(audioAttributes)
|
||||
|
|
@ -115,9 +119,19 @@ public class PsemekApplication {
|
|||
resizeNative(nativeApp, width, height);
|
||||
}
|
||||
|
||||
public void touch(int x, int y)
|
||||
public void touchDown(int x, int y)
|
||||
{
|
||||
touchNative(nativeApp, x, y);
|
||||
touchDownNative(nativeApp, x, y);
|
||||
}
|
||||
|
||||
public void touchUp(int x, int y)
|
||||
{
|
||||
touchUpNative(nativeApp, x, y);
|
||||
}
|
||||
|
||||
public void touchMove(int x, int y)
|
||||
{
|
||||
touchMoveNative(nativeApp, x, y);
|
||||
}
|
||||
|
||||
public void drawFrame() {
|
||||
|
|
@ -127,7 +141,7 @@ public class PsemekApplication {
|
|||
}
|
||||
|
||||
public void destroy() {
|
||||
audioThread.stop();
|
||||
audioThread.interrupt();
|
||||
if (nativeApp != 0)
|
||||
destroyNativeApp(nativeApp);
|
||||
nativeApp = 0;
|
||||
|
|
|
|||
|
|
@ -93,11 +93,27 @@ extern "C" void Java_psemek_app_PsemekApplication_resizeNative(JNIEnv * env, jcl
|
|||
});
|
||||
}
|
||||
|
||||
extern "C" void Java_psemek_app_PsemekApplication_touchNative(JNIEnv * env, jclass, jlong ptr, jint x, jint y)
|
||||
extern "C" void Java_psemek_app_PsemekApplication_touchDownNative(JNIEnv * env, jclass, jlong ptr, jint x, jint y)
|
||||
{
|
||||
try_native(env, [&]{
|
||||
auto app = reinterpret_cast<psemek::app::application *>(ptr);
|
||||
app->on_event(psemek::app::touch_event{{x, y}});
|
||||
app->on_event(psemek::app::touch_down_event{{x, y}});
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" void Java_psemek_app_PsemekApplication_touchUpNative(JNIEnv * env, jclass, jlong ptr, jint x, jint y)
|
||||
{
|
||||
try_native(env, [&]{
|
||||
auto app = reinterpret_cast<psemek::app::application *>(ptr);
|
||||
app->on_event(psemek::app::touch_up_event{{x, y}});
|
||||
});
|
||||
}
|
||||
|
||||
extern "C" void Java_psemek_app_PsemekApplication_touchMoveNative(JNIEnv * env, jclass, jlong ptr, jint x, jint y)
|
||||
{
|
||||
try_native(env, [&]{
|
||||
auto app = reinterpret_cast<psemek::app::application *>(ptr);
|
||||
app->on_event(psemek::app::touch_move_event{{x, y}});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,9 @@ namespace psemek::app
|
|||
void on_event(mouse_move_event const &) override;
|
||||
void on_event(mouse_wheel_event const &) override;
|
||||
void on_event(mouse_button_event const &) override;
|
||||
void on_event(touch_event const &) override;
|
||||
void on_event(touch_down_event const &) override;
|
||||
void on_event(touch_up_event const &) override;
|
||||
void on_event(touch_move_event const &) override;
|
||||
void on_event(key_event const &) override;
|
||||
|
||||
void stop() override;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,9 @@ namespace psemek::app
|
|||
virtual void on_event(mouse_move_event const &) {}
|
||||
virtual void on_event(mouse_wheel_event const &) {}
|
||||
virtual void on_event(mouse_button_event const &) {}
|
||||
virtual void on_event(touch_event const &) {}
|
||||
virtual void on_event(touch_down_event const &) {}
|
||||
virtual void on_event(touch_up_event const &) {}
|
||||
virtual void on_event(touch_move_event const &) {}
|
||||
virtual void on_event(key_event const &) {}
|
||||
|
||||
virtual ~event_handler() {}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,13 @@ namespace psemek::app
|
|||
state.mouse_button_down.erase(event.button);
|
||||
}
|
||||
|
||||
inline void apply(event_state &, touch_event const &)
|
||||
inline void apply(event_state &, touch_down_event const &)
|
||||
{}
|
||||
|
||||
inline void apply(event_state &, touch_up_event const &)
|
||||
{}
|
||||
|
||||
inline void apply(event_state &, touch_move_event const &)
|
||||
{}
|
||||
|
||||
inline void apply(event_state & state, key_event const & event)
|
||||
|
|
|
|||
|
|
@ -39,7 +39,17 @@ namespace psemek::app
|
|||
bool down;
|
||||
};
|
||||
|
||||
struct touch_event
|
||||
struct touch_down_event
|
||||
{
|
||||
geom::point<int, 2> position;
|
||||
};
|
||||
|
||||
struct touch_up_event
|
||||
{
|
||||
geom::point<int, 2> position;
|
||||
};
|
||||
|
||||
struct touch_move_event
|
||||
{
|
||||
geom::point<int, 2> position;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -14,7 +14,9 @@ namespace psemek::app
|
|||
void on_event(mouse_move_event const &) override;
|
||||
void on_event(mouse_wheel_event const &) override;
|
||||
void on_event(mouse_button_event const &) override;
|
||||
void on_event(touch_event const &) override;
|
||||
void on_event(touch_down_event const &) override;
|
||||
void on_event(touch_up_event const &) override;
|
||||
void on_event(touch_move_event const &) override;
|
||||
void on_event(key_event const &) override;
|
||||
|
||||
void stop() override;
|
||||
|
|
|
|||
|
|
@ -28,7 +28,17 @@ namespace psemek::app
|
|||
apply(state_, event);
|
||||
}
|
||||
|
||||
void application_base::on_event(touch_event const & event)
|
||||
void application_base::on_event(touch_down_event const & event)
|
||||
{
|
||||
apply(state_, event);
|
||||
}
|
||||
|
||||
void application_base::on_event(touch_up_event const & event)
|
||||
{
|
||||
apply(state_, event);
|
||||
}
|
||||
|
||||
void application_base::on_event(touch_move_event const & event)
|
||||
{
|
||||
apply(state_, event);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,17 @@ namespace psemek::app
|
|||
on_event_impl(event);
|
||||
}
|
||||
|
||||
void scene_application::on_event(touch_event const & event)
|
||||
void scene_application::on_event(touch_down_event const & event)
|
||||
{
|
||||
on_event_impl(event);
|
||||
}
|
||||
|
||||
void scene_application::on_event(touch_up_event const & event)
|
||||
{
|
||||
on_event_impl(event);
|
||||
}
|
||||
|
||||
void scene_application::on_event(touch_move_event const & event)
|
||||
{
|
||||
on_event_impl(event);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue