From f0f97e310c9658cf5cd504db81e90e7f5f983540 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Thu, 5 Sep 2024 17:41:24 +0800 Subject: [PATCH] chore: add nested PickCallbackHandler to Gizmo --- thermion_dart/native/include/Gizmo.hpp | 24 ++++++++++++++++++++++++ thermion_dart/native/src/Gizmo.cpp | 15 +++------------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/thermion_dart/native/include/Gizmo.hpp b/thermion_dart/native/include/Gizmo.hpp index 4e1af5ca..fd27d03d 100644 --- a/thermion_dart/native/include/Gizmo.hpp +++ b/thermion_dart/native/include/Gizmo.hpp @@ -31,6 +31,30 @@ class Gizmo { enum Axis { X, Y, Z}; + class PickCallbackHandler { + public: + PickCallbackHandler(Gizmo* gizmo, void (*callback)(EntityId entityId, int x, int y)) : _gizmo(gizmo), _callback(callback) {}; + void handle(filament::View::PickingQueryResult const &result) { + auto x = static_cast(result.fragCoords.x); + auto y= static_cast(result.fragCoords.y); + for(int i = 4; i < 7; i++) { + if(_gizmo->_entities[i] == result.renderable) { + _gizmo->highlight(_gizmo->_entities[i - 4]); + _callback(Entity::smuggle(_gizmo->_entities[i - 4]), x, y); + return; + } + } + _gizmo->unhighlight(); + _callback(0, x, y); + delete(this); + } + + private: + Gizmo* _gizmo; + void (*_callback)(EntityId entityId, int x, int y); + + }; + public: Gizmo(Engine& engine, View *view, Scene *scene); ~Gizmo(); diff --git a/thermion_dart/native/src/Gizmo.cpp b/thermion_dart/native/src/Gizmo.cpp index d339deab..260659e3 100644 --- a/thermion_dart/native/src/Gizmo.cpp +++ b/thermion_dart/native/src/Gizmo.cpp @@ -359,20 +359,11 @@ void Gizmo::destroy() _engine.destroy(_material); } - void Gizmo::pick(uint32_t x, uint32_t y, void (*callback)(EntityId entityId, int x, int y)) { - auto * gizmo = this; - _view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { - for(int i = 4; i < 7; i++) { - if(_entities[i] == result.renderable) { - gizmo->highlight(_entities[i - 4]); - callback(Entity::smuggle(_entities[i - 4]), x, y); - return; - } - } - gizmo->unhighlight(); - callback(0, x, y); + auto handler = new Gizmo::PickCallbackHandler(this, callback); + _view->pick(x, y, [=](filament::View::PickingQueryResult const &result) { + handler->handle(result); }); }