158 lines
4.8 KiB
C++
158 lines
4.8 KiB
C++
#include "dl_android.h"
|
||
|
||
#if DL_PLATFORM == DL_PLATFORM_ANDROID
|
||
|
||
#include "io/dl_input.h"
|
||
#include "system/dl_system.h"
|
||
|
||
namespace dl
|
||
{
|
||
|
||
int32_t Android_HandleInput(android_app* app)
|
||
{
|
||
//auto* engine = (struct engine*)app->userData;
|
||
auto ib = android_app_swap_input_buffers(app);
|
||
if (ib && ib->motionEventsCount)
|
||
{
|
||
for (int i = 0; i < ib->motionEventsCount; i++)
|
||
{
|
||
auto* event = &ib->motionEvents[i];
|
||
int32_t ptrIdx = 0;
|
||
int32_t act = event->action & AMOTION_EVENT_ACTION_MASK;
|
||
switch (act)
|
||
{
|
||
case AMOTION_EVENT_ACTION_POINTER_DOWN:
|
||
case AMOTION_EVENT_ACTION_POINTER_UP:
|
||
// Retrieve the index for the starting and the ending of any secondary pointers
|
||
ptrIdx = (event->action & AMOTION_EVENT_ACTION_POINTER_INDEX_MASK) >>
|
||
AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
|
||
case AMOTION_EVENT_ACTION_DOWN:
|
||
case AMOTION_EVENT_ACTION_UP:
|
||
{
|
||
int x = GameActivityPointerAxes_getAxisValue(
|
||
&event->pointers[ptrIdx], AMOTION_EVENT_AXIS_X);
|
||
int y = GameActivityPointerAxes_getAxisValue(
|
||
&event->pointers[ptrIdx], AMOTION_EVENT_AXIS_Y);
|
||
dl::log_msg("安卓事件:{},{}", x, y);
|
||
g_input->_set_cursor(x, y, false);
|
||
g_input->_set_key_mouse((int)MouseCode::LEFT, act == AMOTION_EVENT_ACTION_DOWN);
|
||
}
|
||
break;
|
||
case AMOTION_EVENT_ACTION_MOVE:
|
||
// Process the move action: the new coordinates for all active touch pointers
|
||
// are inside the event->pointers[]. Compare with our internally saved
|
||
// coordinates to find out which pointers are actually moved. Note that there is
|
||
// no index embedded inside event->action for AMOTION_EVENT_ACTION_MOVE (there
|
||
// might be multiple pointers moved at the same time).
|
||
//...
|
||
int x = GameActivityPointerAxes_getAxisValue(
|
||
&event->pointers[ptrIdx], AMOTION_EVENT_AXIS_X);
|
||
int y = GameActivityPointerAxes_getAxisValue(
|
||
&event->pointers[ptrIdx], AMOTION_EVENT_AXIS_Y);
|
||
dl::log_msg("安卓事件:{},{}", x, y);
|
||
g_input->_set_cursor(x, y, false);
|
||
break;
|
||
}
|
||
}
|
||
android_app_clear_motion_events(ib);
|
||
}
|
||
|
||
// Process the KeyEvent in a similar way.
|
||
//...
|
||
return 0;
|
||
}
|
||
|
||
bool Android_ProcessEvent(android_app* app)
|
||
{
|
||
// Read all pending events.
|
||
int events;
|
||
struct android_poll_source* source;
|
||
|
||
// If not animating, block forever waiting for events.
|
||
// If animating, loop until all events are read, then continue
|
||
// to draw the next frame of animation.
|
||
while ((ALooper_pollOnce(0, nullptr, &events, (void**)&source)) >= 0)
|
||
{
|
||
// Process this app cycle or inset change event.
|
||
if (source)
|
||
source->process(source->app, source);
|
||
|
||
// Other processing.
|
||
|
||
if (app->destroyRequested)
|
||
return false;
|
||
}
|
||
// Process input events if there are any.
|
||
Android_HandleInput(app);
|
||
return true;
|
||
}
|
||
|
||
void Android_OnAppCmd(android_app* app, int32_t cmd)
|
||
{
|
||
//auto platform = reinterpret_cast<AndroidPlatform*>(app->userData);
|
||
switch (cmd)
|
||
{
|
||
case APP_CMD_INIT_WINDOW:
|
||
{// 启动、丢失后重建会触发
|
||
g_android->_sizeWindow[0] = ANativeWindow_getWidth(app->window);
|
||
g_android->_sizeWindow[1] = ANativeWindow_getHeight(app->window);
|
||
g_android->_surfaceState = 1;
|
||
dl::log_easy(fmt::format("安卓重建Surface:{}, {}", g_android->_sizeWindow[0], g_android->_sizeWindow[1]));
|
||
break;
|
||
}
|
||
case APP_CMD_TERM_WINDOW:
|
||
{// 结束、丢失后会触发
|
||
g_android->_sizeWindow[0] = 0;
|
||
g_android->_sizeWindow[1] = 0;
|
||
g_android->_surfaceState = 0;
|
||
dl::log_easy("安卓销毁Surface");
|
||
break;
|
||
}
|
||
case APP_CMD_CONTENT_RECT_CHANGED:
|
||
{
|
||
// Get the new size
|
||
auto width = app->contentRect.right - app->contentRect.left;
|
||
auto height = app->contentRect.bottom - app->contentRect.top;
|
||
g_android->_sizeWindow[0] = width;
|
||
g_android->_sizeWindow[1] = height;
|
||
g_system->_resize();
|
||
break;
|
||
}
|
||
// 这里的事件需要轮询才能触发,所以直接通过java层的Activity触发,才来得及处理
|
||
// case APP_CMD_CONFIG_CHANGED:
|
||
// {
|
||
// uint32_t ret = AConfiguration_getOrientation(nullptr);
|
||
// g_system->_set_dir(WindowDirection::UP);
|
||
// if(ret == 2)
|
||
// g_system->_set_dir(WindowDirection::RIGHT);
|
||
// else
|
||
// g_system->_set_dir(WindowDirection::UP);
|
||
// break;
|
||
// }
|
||
// case APP_CMD_GAINED_FOCUS:
|
||
// {
|
||
//
|
||
// break;
|
||
// }
|
||
// case APP_CMD_LOST_FOCUS:
|
||
// {
|
||
// break;
|
||
// }
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
extern "C"
|
||
JNIEXPORT void JNICALL
|
||
Java_com_dl_project_MainActivity_DispatchOnPause(JNIEnv* env, jobject thiz) {
|
||
dl::g_android->_bPause = true;
|
||
}
|
||
extern "C"
|
||
JNIEXPORT void JNICALL
|
||
Java_com_dl_project_MainActivity_DispatchOnResume(JNIEnv* env, jobject thiz) {
|
||
dl::g_android->_bPause = false;
|
||
}
|
||
#endif
|