Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use subscription based events handling #237

Open
wants to merge 27 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
37e0a8c
Used a lambda based events
Mar 1, 2024
9349d97
Merge branch 'dev' into events
markaren Mar 1, 2024
4797df2
Create individual dispatcher for each event
Mar 1, 2024
2b906e4
Add mouse support and refactor.
Mar 1, 2024
0162d1c
fix broken scroll wheel for orbit control
Mar 1, 2024
f0df38e
shorten the event handling util names
Mar 1, 2024
095fbaf
Merge branch 'dev' into events
markaren Mar 2, 2024
897a009
implement ioCapture
markaren Mar 3, 2024
44a76b1
implement keys for Youbot
markaren Mar 3, 2024
44cb476
Fix for instancing crash
markaren Mar 3, 2024
b64cae4
missing conversions
markaren Mar 3, 2024
769a744
missing conversions
markaren Mar 3, 2024
927534b
#239 Fix UB in event dispatching
Mar 4, 2024
03675b2
Merge branch 'events' of github.com:bradphelan/threepp into events
Mar 4, 2024
aca27f9
#239 Fix UB in event dispatching
Mar 4, 2024
66ef3ee
Merge branch 'dev' into events
markaren Mar 4, 2024
3c8baca
Merge branch 'dev' into events
markaren Mar 4, 2024
764f47d
Merge branch 'dev' into events
markaren Mar 5, 2024
f606bc0
Merge branch 'dev' into events
markaren Mar 14, 2024
eb198be
update new code
markaren Mar 14, 2024
9c447c8
formatting and refactor
markaren Mar 14, 2024
014b9cc
Merge branch 'dev' into events
markaren Mar 19, 2024
5755cf9
adapt new code
markaren Mar 19, 2024
e569f77
Merge branch 'dev' into events
markaren Mar 23, 2024
a9c25db
adapt new code
markaren Mar 23, 2024
f6338b1
Merge branch 'dev' into events
markaren Mar 24, 2024
8acd6ff
Adapt DragControls
markaren Mar 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions examples/controls/drag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,37 @@ int main() {
DragControls controls(objects, camera, canvas);
controls.rotateSpeed = 2;

struct HoverListener: public EventListener {
void onEvent(Event& event) override {
struct HoverListener {
void hoverOn(Event& event) {

auto target = static_cast<Object3D*>(event.target);
auto& color = target->material()->as<MaterialWithColor>()->color;

if (event.type == "hoveron") {
prevColor = color;
color = Color::white;
} else if (event.type == "hoveroff") {
color = prevColor;
}
prevColor = color;
color = Color::white;
}

void hoverOff(Event& event) {
auto target = static_cast<Object3D*>(event.target);
auto& color = target->material()->as<MaterialWithColor>()->color;

color = prevColor;
}

private:
Color prevColor;

} hoverListener;

controls.addEventListener("hoveron", &hoverListener);
controls.addEventListener("hoveroff", &hoverListener);
controls.HoverOn.subscribeForever([&hoverListener](auto& evt) {
hoverListener.hoverOn(evt);
});
controls.HoverOff.subscribeForever([&hoverListener](auto& evt) {
hoverListener.hoverOff(evt);
});

KeyAdapter keyAdapter(KeyAdapter::Mode::KEY_PRESSED, [&](KeyEvent evt){

canvas.keys.Pressed.subscribeForever([&](KeyEvent evt) {
if (evt.key == Key::M) {
if (controls.mode == DragControls::Mode::Translate) {
controls.mode = DragControls::Mode::Rotate;
Expand All @@ -95,7 +103,7 @@ int main() {
}
}
});
canvas.addKeyListener(keyAdapter);


canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
Expand Down
35 changes: 10 additions & 25 deletions examples/extras/physics/PxEngine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ class PxEngine: public threepp::Object3D {

explicit PxEngine(float timeStep = 1.f / 100)
: timeStep(timeStep),
sceneDesc(physics->getTolerancesScale()),
onMeshRemovedListener(this) {
sceneDesc(physics->getTolerancesScale()) {

sceneDesc.gravity = physx::PxVec3(0.0f, -9.81f, 0.0f);
sceneDesc.flags |= physx::PxSceneFlag::eENABLE_PCM;
Expand Down Expand Up @@ -257,7 +256,15 @@ class PxEngine: public threepp::Object3D {
}

obj.matrixAutoUpdate = false;
obj.addEventListener("remove", &onMeshRemovedListener);

obj.OnRemove.subscribeOnce([this](threepp::Event& event) {
auto m = static_cast<threepp::Object3D*>(event.target);
if (bodies.count(m)) {
auto rb = bodies.at(m);
scene->removeActor(*rb.front());
bodies.erase(m);
}
});
}

physx::PxJoint* createJoint(const JointCreate& create, threepp::Object3D* o1, threepp::Object3D* o2, threepp::Vector3 anchor, threepp::Vector3 axis) {
Expand Down Expand Up @@ -551,28 +558,6 @@ class PxEngine: public threepp::Object3D {
}
}
}

struct MeshRemovedListener: threepp::EventListener {

explicit MeshRemovedListener(PxEngine* scope): scope(scope) {}

void onEvent(threepp::Event& event) override {
if (event.type == "remove") {
auto m = static_cast<threepp::Object3D*>(event.target);
if (scope->bodies.count(m)) {
auto rb = scope->bodies.at(m);
scope->scene->removeActor(*rb.front());
scope->bodies.erase(m);
}
m->removeEventListener("remove", this);
}
}

private:
PxEngine* scope;
};

MeshRemovedListener onMeshRemovedListener;
};

#endif//THREEPP_PXENGINE_HPP
19 changes: 11 additions & 8 deletions examples/extras/physics/physx_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace threepp;

namespace {

struct KeyController: KeyListener {
struct KeyController {

explicit KeyController(std::vector<physx::PxRevoluteJoint*> joints)
: joints(std::move(joints)) {
Expand All @@ -19,8 +19,8 @@ namespace {
}
}

void onKeyPressed(KeyEvent evt) override;
void onKeyReleased(KeyEvent evt) override;
void onKeyPressed(KeyEvent evt);
void onKeyReleased(KeyEvent evt);

private:
float speed = 2;
Expand Down Expand Up @@ -211,14 +211,15 @@ int main() {
auto* j3 = engine.getJoint<physx::PxRevoluteJoint>(*box3);

KeyController keyListener({j1, j2, j3});
canvas.addKeyListener(keyListener);
canvas.keys.Pressed.subscribeForever([&](KeyEvent evt) { keyListener.onKeyPressed(evt); });
canvas.keys.Released.subscribeForever([&](KeyEvent evt) { keyListener.onKeyReleased(evt); });

OrbitControls controls(camera, canvas);

bool run = false;
KeyAdapter adapter(KeyAdapter::Mode::KEY_PRESSED | KeyAdapter::KEY_REPEAT, [&](KeyEvent evt) {
run = true;
canvas.keys.Pressed.subscribeOnce([&](KeyEvent) { run = true; });

auto keyController = [&](KeyEvent evt){
if (evt.key == Key::SPACE) {
auto obj = spawnObject();
obj->position = camera.position;
Expand All @@ -236,8 +237,10 @@ int main() {
} else if (evt.key == Key::D) {
engine.debugVisualisation = !engine.debugVisualisation;
}
});
canvas.addKeyListener(adapter);
};

canvas.keys.Pressed.subscribeForever(keyController);
canvas.keys.Repeat.subscribeForever(keyController);

canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
Expand Down
3 changes: 1 addition & 2 deletions examples/misc/lut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ int main() {
plane->add(wireframe);
scene.add(plane);

KeyAdapter keyAdapter(KeyAdapter::Mode::KEY_PRESSED, [&](KeyEvent evt) {
canvas.keys.Pressed.subscribeForever([&](KeyEvent evt) {
if (evt.key == Key::NUM_1) {
applyFunc(*planeGeometry, rosenbrock);
applyFunc(*planeGeometry2, rosenbrock);
Expand Down Expand Up @@ -194,7 +194,6 @@ int main() {
normalizeAndApplyLut(*planeGeometry, 2);
normalizeAndApplyLut(*planeGeometry2, 2);
});
canvas.addKeyListener(keyAdapter);

canvas.onWindowResize([&](WindowSize size) {
camera.aspect = size.aspect();
Expand Down
7 changes: 3 additions & 4 deletions examples/misc/morphtargets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,14 @@ int main() {
});

Vector2 mouse{-Infinity<float>, -Infinity<float>};
MouseMoveListener l([&](Vector2 pos) {
auto sub = canvas.mouse.Move.subscribe([&](MouseMoveEvent& evt) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components

auto size = canvas.size();
mouse.x = (pos.x / static_cast<float>(size.width)) * 2 - 1;
mouse.y = -(pos.y / static_cast<float>(size.height)) * 2 + 1;
mouse.x = (evt.pos.x / static_cast<float>(size.width)) * 2 - 1;
mouse.y = -(evt.pos.y / static_cast<float>(size.height)) * 2 + 1;
});
canvas.addMouseListener(l);

Box3 box;
auto helper = Box3Helper::create(box);
Expand Down
83 changes: 38 additions & 45 deletions examples/misc/mouse_key_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,35 @@ using namespace threepp;

namespace {

struct MyMouseListener: MouseListener {
void onMouseDown(MouseButtonEvent & e, double t) {
std::cout << "onMouseDown, button= " << e.button << ", pos=" << e.pos << " at t=" << t << std::endl;
}

float& t;
void onMouseUp(MouseButtonEvent & e, double t) {
std::cout << "onMouseUp, button= " << e.button << ", pos=" << e.pos << " at t=" << t << std::endl;
}

explicit MyMouseListener(float& t): t(t) {}
void onMouseMove(MouseMoveEvent &e, double t) {
std::cout << "onMouseMove, "
<< "pos=" << e.pos << " at t=" << t << std::endl;
}

void onMouseDown(int button, const Vector2& pos) override {
std::cout << "onMouseDown, button= " << button << ", pos=" << pos << " at t=" << t << std::endl;
}

void onMouseUp(int button, const Vector2& pos) override {
std::cout << "onMouseUp, button= " << button << ", pos=" << pos << " at t=" << t << std::endl;
}

void onMouseMove(const Vector2& pos) override {
std::cout << "onMouseMove, "
<< "pos=" << pos << " at t=" << t << std::endl;
}

void onMouseWheel(const Vector2& delta) override {
std::cout << "onMouseWheel, "
<< "delta=" << delta << " at t=" << t << std::endl;
}
};
void onMouseWheel(MouseWheelEvent &e, double t) {
std::cout << "onMouseWheel, "
<< "delta=" << e.offset << " at t=" << t << std::endl;
}

struct MyKeyListener: KeyListener {
void onKeyPressed(KeyEvent evt, double t) {
std::cout << "onKeyPressed at t=" << t << std::endl;
}

float& t;
void onKeyReleased(KeyEvent evt, double t) {
std::cout << "onKeyReleased at t=" << t << std::endl;
}

explicit MyKeyListener(float& t): t(t) {}

void onKeyPressed(KeyEvent evt) override {
std::cout << "onKeyPressed at t=" << t << std::endl;
}

void onKeyReleased(KeyEvent evt) override {
std::cout << "onKeyReleased at t=" << t << std::endl;
}

void onKeyRepeat(KeyEvent evt) override {
std::cout << "onKeyRepeat at t=" << t << std::endl;
}
};
void onKeyRepeat(KeyEvent evt, double t) {
std::cout << "onKeyRepeat at t=" << t << std::endl;
}

}// namespace

Expand All @@ -58,22 +44,29 @@ int main() {
Canvas canvas("Mouse and Key Listeners Demo");
Clock clock;

MyMouseListener ml{clock.elapsedTime};
MyKeyListener kl{clock.elapsedTime};
canvas.addMouseListener(ml);
canvas.addKeyListener(kl);
Subscriptions subs_;
subs_ << canvas.mouse.Down.subscribe([&](auto& e) { onMouseDown(e, clock.elapsedTime); });
subs_ << canvas.mouse.Up.subscribe([&](auto& e) { onMouseUp(e, clock.elapsedTime); });
subs_ << canvas.mouse.Move.subscribe([&](auto& e) { onMouseMove(e, clock.elapsedTime); });
subs_ << canvas.mouse.Wheel.subscribe([&](auto& e) { onMouseWheel(e, clock.elapsedTime); });

Subscriptions key_subs_;
auto subscribe_keys = [&]() {
key_subs_ << canvas.keys.Pressed.subscribe([&](auto& e) {onKeyPressed(e, clock.elapsedTime); });
key_subs_ << canvas.keys.Released.subscribe([&](auto& e) {onKeyReleased(e, clock.elapsedTime); });
key_subs_ << canvas.keys.Repeat.subscribe([&](auto& e) {onKeyRepeat(e, clock.elapsedTime); });
};

bool finish = false;
canvas.animate([&]() {
clock.getElapsedTime();

if (clock.elapsedTime > 2 && clock.elapsedTime < 4) {
if (canvas.removeKeyListener(kl)) {
std::cout << "removed key listener" << std::endl;
}
key_subs_.clear();
std::cout << "removed key listener" << std::endl;
} else if (!finish && clock.elapsedTime > 5) {
subscribe_keys();
std::cout << "re-added key listener" << std::endl;
canvas.addKeyListener(kl);
finish = true;
}
});
Expand Down
7 changes: 3 additions & 4 deletions examples/misc/raycast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ int main() {
});

Vector2 mouse{-Infinity<float>, -Infinity<float>};
MouseMoveListener l([&](Vector2 pos) {
auto sub = canvas.mouse.Move.subscribe([&](MouseMoveEvent& e) {
// calculate mouse position in normalized device coordinates
// (-1 to +1) for both components

auto size = canvas.size();
mouse.x = (pos.x / static_cast<float>(size.width)) * 2 - 1;
mouse.y = -(pos.y / static_cast<float>(size.height)) * 2 + 1;
mouse.x = (e.pos.x / static_cast<float>(size.width)) * 2 - 1;
mouse.y = -(e.pos.y / static_cast<float>(size.height)) * 2 + 1;
});
canvas.addMouseListener(l);

Raycaster raycaster;
raycaster.params.lineThreshold = 0.1f;
Expand Down
17 changes: 9 additions & 8 deletions examples/objects/decal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ namespace {
return decalMaterial;
}

class MyMouseListener: public MouseListener {
class MyMouseListener {

public:
Vector2 mouse{-Infinity<float>, -Infinity<float>};

explicit MyMouseListener(Canvas& canvas): canvas(canvas) {}
explicit MyMouseListener(PeripheralsEventSource& canvas): canvas(canvas) {}

bool mouseClick() {
if (mouseDown) {
Expand All @@ -46,18 +46,18 @@ namespace {
}
}

void onMouseDown(int button, const Vector2& pos) override {
if (button == 0) {// left mousebutton
void onMouseDown(MouseButtonEvent evt) {
if (evt.button == 0) {// left mousebutton
mouseDown = true;
}
}

void onMouseMove(const Vector2& pos) override {
updateMousePos(pos);
void onMouseMove(MouseMoveEvent evt) {
updateMousePos(evt.pos);
}

private:
Canvas& canvas;
PeripheralsEventSource& canvas;
bool mouseDown = false;

void updateMousePos(Vector2 pos) {
Expand Down Expand Up @@ -141,7 +141,8 @@ int main() {
scene->add(line);

MyMouseListener mouseListener(canvas);
canvas.addMouseListener(mouseListener);
canvas.mouse.Move.subscribeForever([&mouseListener](auto& evt) {mouseListener.onMouseMove(evt);});
canvas.mouse.Down.subscribeForever([&mouseListener](auto& evt) {mouseListener.onMouseDown(evt);});

canvas.onWindowResize([&](WindowSize size) {
camera->aspect = size.aspect();
Expand Down
7 changes: 3 additions & 4 deletions examples/objects/instancing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,11 @@ int main() {
});

Vector2 mouse{-Infinity<float>, -Infinity<float>};
MouseMoveListener l([&](auto& pos) {
auto sub = canvas.mouse.Move.subscribe([&](auto& e) {
auto size = canvas.size();
mouse.x = (pos.x / static_cast<float>(size.width)) * 2 - 1;
mouse.y = -(pos.y / static_cast<float>(size.height)) * 2 + 1;
mouse.x = (e.pos.x / static_cast<float>(size.width)) * 2 - 1;
mouse.y = -(e.pos.y / static_cast<float>(size.height)) * 2 + 1;
});
canvas.addMouseListener(l);


Clock clock;
Expand Down
Loading