Skip to content

Commit fc0e527

Browse files
authored
Disable interactions on GLView (#2465)
disable interactions on GLView
1 parent 5c092ba commit fc0e527

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

core/platform/GLView.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ GLView::GLView()
116116
, _scaleX(1.0f)
117117
, _scaleY(1.0f)
118118
, _resolutionPolicy(ResolutionPolicy::UNKNOWN)
119+
, _interactive(true)
119120
{}
120121

121122
GLView::~GLView() {}
@@ -294,6 +295,9 @@ std::string_view GLView::getViewName() const
294295

295296
void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
296297
{
298+
if (!_interactive)
299+
return;
300+
297301
intptr_t id = 0;
298302
float x = 0.0f;
299303
float y = 0.0f;
@@ -348,6 +352,9 @@ void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[])
348352

349353
void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[])
350354
{
355+
if (!_interactive)
356+
return;
357+
351358
intptr_t id = 0;
352359
float x = 0.0f;
353360
float y = 0.0f;
@@ -501,4 +508,44 @@ void GLView::queueOperation(AsyncOperation /*op*/, void* /*param*/)
501508
{
502509
}
503510

511+
void GLView::setInteractive(bool interactive)
512+
{
513+
if (_interactive && !interactive)
514+
{
515+
cancelAllTouches();
516+
}
517+
518+
_interactive = interactive;
519+
}
520+
521+
void GLView::cancelAllTouches()
522+
{
523+
EventTouch touchEvent;
524+
touchEvent._touches = getAllTouchesVector();
525+
touchEvent._eventCode = EventTouch::EventCode::CANCELLED;
526+
527+
if (touchEvent._touches.empty())
528+
{
529+
AXLOGD("cancelling all touches: size = 0");
530+
return;
531+
}
532+
533+
auto dispatcher= Director::getInstance()->getEventDispatcher();
534+
dispatcher->dispatchEvent(&touchEvent);
535+
536+
for (auto&& touch : touchEvent._touches)
537+
{
538+
// release the touch object.
539+
touch->release();
540+
}
541+
542+
g_touchIdReorderMap.clear();
543+
g_indexBitsUsed = 0;
544+
545+
for (int i = 0; i < EventTouch::MAX_TOUCHES; ++i)
546+
{
547+
g_touches[i] = nullptr;
548+
}
549+
}
550+
504551
}

core/platform/GLView.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,17 @@ class AX_DLL GLView : public Object
442442
*/
443443
void renderScene(Scene* scene, Renderer* renderer);
444444

445+
/**
446+
* Enable or disable interactions.
447+
* When disabled, it prevents touches to be dispatched and will cancel current touches
448+
*/
449+
void setInteractive(bool interactive);
450+
445451
protected:
446452
float transformInputX(float x) { return (x - _viewPortRect.origin.x) / _scaleX; }
447453
float transformInputY(float y) { return (y - _viewPortRect.origin.y) / _scaleY; }
448454

449-
/**
455+
/**
450456
* queue a priority operation in render thread for non-PC platforms, even through app in background
451457
* invoked by Director
452458
*/
@@ -468,6 +474,11 @@ class AX_DLL GLView : public Object
468474
float _scaleX;
469475
float _scaleY;
470476
ResolutionPolicy _resolutionPolicy;
477+
478+
private:
479+
void cancelAllTouches();
480+
481+
bool _interactive;
471482
};
472483

473484
// end of platform group

0 commit comments

Comments
 (0)