-
-
Notifications
You must be signed in to change notification settings - Fork 160
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
chore: Add example to create mask and use set_mouse_callback
#473
Conversation
examples/create_mask.rs
Outdated
/// Converts an `i32` to a `opencv::highgui::MouseEventTypes` | ||
/// | ||
/// # Panics | ||
/// | ||
/// Panics if the argument less than 0 or greater than 11. | ||
fn mouse_event_from_i32(value: i32) -> opencv::highgui::MouseEventTypes { | ||
(value.gt(&(opencv::highgui::MouseEventTypes::EVENT_MOUSEHWHEEL as i32) /* 11 */) | ||
|| (value.lt(&(opencv::highgui::MouseEventTypes::EVENT_MOUSEMOVE as i32) /* 0 */))) | ||
.then(|| panic!("Invalid cv::highgui::MouseEventTypes value: {}", value)); | ||
|
||
// Safe because of the previous check | ||
unsafe { std::mem::transmute(value) } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this function be generated automatically during the build process?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely! I’ll take a look.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
-
With this automatic implementation, we can easily use
highgui::MouseEventTypes
enum and pattern matching together to take advantage of exhaustiveness checking, enabling us to discover issues earlier. This approach is preferred over comparing values of type i32 with if-else statements and certain constants(e.g.if event == highgui::EVENT_MOUSEMOVE {}
).I think the same principle can be applied to other enums within
opencv::highgui
module, such ashighgui::MouseEventFlags
、highgui::QtButtonTypes
etc.impl From<i32> for crate::highgui::MouseEventTypes { fn from(v: i32) -> Self { (v.gt(&(crate::highgui::MouseEventTypes::EVENT_MOUSEHWHEEL as i32)) || (v.lt(&(crate::highgui::MouseEventTypes::EVENT_MOUSEMOVE as i32)))) .then(|| panic!("Invalid cv::highgui::MouseEventTypes value: {}", v)); // Safe because of the previous check unsafe { std::mem::transmute(v) } } }
-
And the type
MouseCallback
may can be redefinedpub type MouseCallback = Option<Box<dyn FnMut(i32, i32, i32, i32) + Send + Sync + 'static>>;
to
pub type MouseCallback = Option<Box<dyn FnMut(highgui::MouseEventTypes, i32, i32, highgui::MouseEventFlags) + Send + Sync + 'static>>;
But it will make breaking changes!
No description provided.