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

Feat/sm layer detection #219

Merged
merged 17 commits into from
Sep 16, 2024
Merged

Feat/sm layer detection #219

merged 17 commits into from
Sep 16, 2024

Conversation

samuelOsborne
Copy link
Member

@samuelOsborne samuelOsborne commented Aug 20, 2024

This PR adds layer hit detection, along with a few refactors

Features:

  • Layer hit check based on a layer name, x & y coordinate
  • Added getLayerBounds API function. Pass a layer name and it returns a vec where the first 4 values correspond to x, y, width, height
  • Added perform_hit_check(&self, target: &str, x: f32, y: f32) which returns true if the supplied target string (layer name) and x, y coordinate are within its bounding box, otherwise returns false

Refactors:

  • Removed postEventPayload for web, replaced with individual functions
  • Created and split events in to "Internal" events and "Events". This was to accommodate for the fact that we need different event definitions depending if we're managing the configuration file or at runtime. The was mostly the fact for pointer events where we declare a pointer event and a "target" property, but at runtime we send a pointer event with x and y coordinates.
  • Split out the state machine demoes in to self-contained runnable examples from the bin folder.

Copy link

changeset-bot bot commented Sep 2, 2024

⚠️ No Changeset found

Latest commit: 5373027

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@samuelOsborne
Copy link
Member Author

Web tests

Screen.Recording.2024-09-10.at.12.10.46.mov

@samuelOsborne
Copy link
Member Author

samuelOsborne commented Sep 10, 2024

iOS tests

Screen.Recording.2024-09-10.at.15.54.07.mov

@samuelOsborne
Copy link
Member Author

Rust tests

Screen.Recording.2024-09-10.at.16.05.46.mov

@samuelOsborne samuelOsborne marked this pull request as ready for review September 10, 2024 14:11
Comment on lines 185 to 190
let result = self
.renderer
.get_layer_bounds(layer_name)
.unwrap_or((0.0, 0.0, 0.0, 0.0));

vec![result.0, result.1, result.2, result.3]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Suggestion]

We can implement the From trait for LayerBoundingBox, which will allow automatic conversion to a Vec<f32> depending on the execution context.

struct LayerBoundingBox(f32, f32, f32, f32);

impl From<LayerBoundingBox> for Vec<f32> {
    fn from(bbox: LayerBoundingBox) -> Vec<f32> {
        vec![bbox.0, bbox.1, bbox.2, bbox.3]
    }
}

impl Default for LayerBoundingBox {
    fn default() -> Self {
        LayerBoundingBox(0.0, 0.0, 0.0, 0.0)
    }
}

Then in get_layer_bounds, we can do something like this:

let bbox = self.renderer.get_layer_bounds(layer_name).unwrap_or_default(); // ensure LayerBoundingBox implements the Default trait
bbox.into() // This will use the conversion mechanism provided by the From trait

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit to adding this? I feel like it makes the code harder to read


let key = values[0];
let numeric_value = values[1].parse::<f32>().map_err(|_| (false)).unwrap();
pub fn post_pointer_exit_event(&self, x: f32, y: f32) -> i32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]
This is going to be used to notify the sm that the pointer leaves a certain layer ?
How this would be integrated in dotlottie-web for example ?
Do we need to keep track of the layers we enter and leave in this case ?

key: key.to_string(),
value: numeric_value,
};
pub fn post_set_numeric_context(&self, key: &str, value: f32) -> i32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[questions]
We only have set_numeric_context, Are there any types of context types, like bool, string ..etc ?

Event::OnComplete => "OnCompleteEvent".to_string(),
Event::SetNumericContext { key, value } => format!("{}, {}", key, value),
}
}
}

impl InternalEvent {
pub fn as_str(&self) -> String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]
Why do we need to convert to String ?

[suggestion]
if there is a need, probably you may want to impl the Display trait for the InternalEvent, as_str makes it as if it returns a String slice, not an owned String as in the curr implementation

// A layer name was provided, we need to check if the pointer is within the layer
let pointer_target = target;

let p = self.player.as_ref().unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]
is it safe to unwrap here ?

let received_event_values = if let Event::OnPointerDown { x, y } = event {
Event::OnPointerDown { x: *x, y: *y }
} else {
Event::OnPointerDown { x: 0.0, y: 0.0 }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]
if we are sure that the posted event doesn't match the sm event, why do we return x: 0, y: 0 as a received one ?

String { value: String },
Numeric { value: f32 },
OnPointerDown { target: Option<String> },
OnPointerUp { target: Option<String> },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[question]
The internal and external pointer event are very confusing, specially in the way they are defined.
for example: OnPointerUp internally has a target of Option but in the external takes x, y as f32.
Maybe can you help me understand the reason for choosing this approach ?

@samuelOsborne samuelOsborne merged commit e7423c1 into main Sep 16, 2024
1 check passed
@samuelOsborne samuelOsborne deleted the feat/sm-layer-detection branch September 16, 2024 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants