Add tooltip functionality and is_item_hovered

This commit is contained in:
Joonas Javanainen 2017-11-07 19:53:04 +02:00
parent 5dda36edc8
commit a67f190cd2
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
2 changed files with 74 additions and 1 deletions

View File

@ -2,6 +2,12 @@
## [Unreleased]
### Added
- `is_item_hovered`
- `tooltip`
- `tooltip_text`
### Removed
- Non-namespaced flags

View File

@ -445,7 +445,7 @@ impl<'ui> Ui<'ui> {
// Widgets
impl<'ui> Ui<'ui> {
pub fn text<S: AsRef<str>>(&self, text: S) {
pub fn text<T: AsRef<str>>(&self, text: T) {
let s = text.as_ref();
unsafe {
let start = s.as_ptr();
@ -673,6 +673,54 @@ impl<'ui> Ui<'ui> {
}
}
/// # Tooltips
impl<'ui> Ui<'ui> {
/// Construct a tooltip window that can have any kind of content.
///
/// Typically used with `Ui::is_item_hovered()` or some other conditional check.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate imgui;
/// # use imgui::*;
/// fn user_interface(ui: &Ui) {
/// ui.text("Hover over me");
/// if ui.is_item_hovered() {
/// ui.tooltip(|| {
/// ui.text_colored((1.0, 0.0, 0.0, 1.0), im_str!("I'm red!"));
/// });
/// }
/// }
/// # fn main() {
/// # }
/// ```
pub fn tooltip<F: FnOnce()>(&self, f: F) {
unsafe { sys::igBeginTooltip() };
f();
unsafe { sys::igEndTooltip() };
}
/// Construct a tooltip window with simple text content.
///
/// Typically used with `Ui::is_item_hovered()` or some other conditional check.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate imgui;
/// # use imgui::*;
/// fn user_interface(ui: &Ui) {
/// ui.text("Hover over me");
/// if ui.is_item_hovered() {
/// ui.tooltip_text("I'm a tooltip!");
/// }
/// }
/// # fn main() {
/// # }
/// ```
pub fn tooltip_text<T: AsRef<str>>(&self, text: T) { self.tooltip(|| self.text(text)); }
}
// Widgets: Menus
impl<'ui> Ui<'ui> {
pub fn main_menu_bar<F>(&self, f: F)
@ -1014,3 +1062,22 @@ impl<'ui> Ui<'ui> {
unsafe { sys::igPopStyleColor(color_vars.len() as i32) };
}
}
/// # Utilities
impl<'ui> Ui<'ui> {
/// Returns `true` if the last item is being hovered by the mouse.
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate imgui;
/// # use imgui::*;
/// fn user_interface(ui: &Ui) {
/// ui.text("Hover over me");
/// let is_hover_over_me_text_hovered = ui.is_item_hovered();
/// }
/// # fn main() {
/// # }
/// ```
pub fn is_item_hovered(&self) -> bool { unsafe { sys::igIsItemHovered() } }
}