Skip to content

Commit

Permalink
Events API for WASM
Browse files Browse the repository at this point in the history
Query for events before stateMachineInstance.advance:

```js
if (stateMachine) {
  // Check for events before stateMachine.advance.
  var fired = stateMachine.firedEventCount();
  if (fired != 0) {
    for (var i = 0; i < fired; i++) {
      var event = stateMachine.firedEventAt(i);
      console.log("event", event);
      if (event.type == 131) {
        var a = document.createElement("a");
        a.setAttribute("href", event.url);
        a.setAttribute("target", event.target);
        a.click();
      }
    }
  }
  stateMachine.advance(elapsedSeconds);
}
```

Details regarding when "target" window works and doesn't in this thread:
https://2dimensions.slack.com/archives/CHMAP278R/p1692402507928619

Basics are:
- "_blank" works if the event happened in response to a pointer down, it will get pop-up blocked by the browser if you trigger it at the end of an animation/state change.
- Use "_self" for opening urls based on time/not click.

Diffs=
d75d43c73 Events API for WASM (#5850)

Co-authored-by: Luigi Rosso <[email protected]>
  • Loading branch information
luigi-rosso and luigi-rosso committed Aug 22, 2023
1 parent 079f744 commit d93b18a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ad4236501bdf0218046a93c54f2ee4a379747890
d75d43c73b28e956a3a309757a24e4508a13c495
80 changes: 80 additions & 0 deletions wasm/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
#include "rive/shapes/cubic_vertex.hpp"
#include "rive/shapes/path.hpp"
#include "rive/transform_component.hpp"
#include "rive/event.hpp"
#include "rive/open_url_event.hpp"
#include "rive/custom_property_boolean.hpp"
#include "rive/custom_property_string.hpp"
#include "rive/custom_property_number.hpp"

#include "js_alignment.hpp"

Expand Down Expand Up @@ -442,6 +447,81 @@ EMSCRIPTEN_BINDINGS(RiveWASM)
optional_override([](rive::StateMachineInstance& self, double x, double y) {
self.pointerUp(rive::Vec2D((float)x, (float)y));
}))
.function("firedEventCount", &rive::StateMachineInstance::firedEventCount)
.function(
"firedEventAt",
optional_override([](rive::StateMachineInstance& self,
size_t index) -> emscripten::val {
const rive::Event* event = self.firedEventAt(index);
if (event == nullptr)
{
return emscripten::val::undefined();
}
emscripten::val eventObject = emscripten::val::object();
eventObject.set("name", event->name());
if (event->is<rive::OpenUrlEvent>())
{
auto urlEvent = event->as<rive::OpenUrlEvent>();
eventObject.set("type", event->coreType());
eventObject.set("url", urlEvent->url());
const char* target = nullptr;
switch (urlEvent->targetValue())
{
case 0:
target = "_blank";
break;
case 1:
target = "_parent";
break;
case 2:
target = "_self";
break;
case 3:
target = "_top";
break;
}
if (target != nullptr)
{
eventObject.set("target", target);
}
}
bool haveCustom = false;
emscripten::val propertiesObject = emscripten::val::object();
for (auto child : event->children())
{
if (child->is<rive::CustomProperty>())
{
if (!child->name().empty())
{
switch (child->coreType())
{
case rive::CustomPropertyBoolean::typeKey:
propertiesObject.set(
child->name(),
child->as<rive::CustomPropertyBoolean>()->propertyValue());
break;
case rive::CustomPropertyString::typeKey:
propertiesObject.set(
child->name(),
child->as<rive::CustomPropertyString>()->propertyValue());
break;
case rive::CustomPropertyNumber::typeKey:
propertiesObject.set(
child->name(),
child->as<rive::CustomPropertyNumber>()->propertyValue());
break;
}
haveCustom = true;
}
}
}
if (haveCustom)
{
eventObject.set("properties", propertiesObject);
}
return eventObject;
}),
allow_raw_pointers())
.function("stateChangedCount", &rive::StateMachineInstance::stateChangedCount)
.function(
"stateChangedNameByIndex",
Expand Down

0 comments on commit d93b18a

Please sign in to comment.