122 lines
3.4 KiB
C++
122 lines
3.4 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).
|
|
//...
|
|
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->_bSurfaceReady = true;
|
|
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;
|
|
}
|
|
case APP_CMD_GAINED_FOCUS:
|
|
{
|
|
//platform->set_focus(true);
|
|
break;
|
|
}
|
|
case APP_CMD_LOST_FOCUS:
|
|
{
|
|
//platform->set_focus(false);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif |