diff --git a/CHANGELOG.markdown b/CHANGELOG.markdown index 04dd3aa..11d9c24 100644 --- a/CHANGELOG.markdown +++ b/CHANGELOG.markdown @@ -11,6 +11,7 @@ - Redesigned window API - Redesigned progress bar API - Redesigned color editor/picker API +- Updated layout API - Renderer errors implement std::error::Error - Glium renderer re-exports imgui and glium - Gfx renderer re-exports imgui and gfx diff --git a/imgui-examples/examples/test_drawing_channels_split.rs b/imgui-examples/examples/test_drawing_channels_split.rs index 7ba8684..bbe409d 100644 --- a/imgui-examples/examples/test_drawing_channels_split.rs +++ b/imgui-examples/examples/test_drawing_channels_split.rs @@ -15,7 +15,7 @@ fn main() { // the white circle. draw_list.channels_split(2, |channels| { const RADIUS: f32 = 100.0; - let canvas_pos = ui.get_cursor_screen_pos(); + let canvas_pos = ui.cursor_screen_pos(); channels.set_current(1); draw_list .add_line( diff --git a/imgui-examples/examples/test_window_impl.rs b/imgui-examples/examples/test_window_impl.rs index 88b6fd9..1322d26 100644 --- a/imgui-examples/examples/test_window_impl.rs +++ b/imgui-examples/examples/test_window_impl.rs @@ -857,7 +857,7 @@ fn show_example_app_custom_rendering(ui: &Ui, state: &mut CustomRenderingState, // TODO: Add DragFloat to change value of sz ColorEdit::new(im_str!("Color"), &mut state.col).build(ui); let draw_list = ui.get_window_draw_list(); - let p = ui.get_cursor_screen_pos(); + let p = ui.cursor_screen_pos(); let spacing = 8.0; let mut y = p[1] + 4.0; for n in 0..2 { @@ -1008,7 +1008,7 @@ fn show_example_app_custom_rendering(ui: &Ui, state: &mut CustomRenderingState, // SetCursorPos(max). // ImDrawList API uses screen coordinates! - let canvas_pos = ui.get_cursor_screen_pos(); + let canvas_pos = ui.cursor_screen_pos(); // Resize canvas to what's available let mut canvas_size = ui.content_region_avail(); if canvas_size[0] < 50.0 { diff --git a/src/layout.rs b/src/layout.rs index 22f2f45..3bc9d22 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -1,7 +1,20 @@ +use std::marker::PhantomData; + use crate::sys; use crate::Ui; -/// # Cursor/layout +/// Represents a layout group +pub struct GroupToken<'ui> { + _ui: PhantomData<&'ui Ui<'ui>>, +} + +impl<'ui> Drop for GroupToken<'ui> { + fn drop(&mut self) { + unsafe { sys::igEndGroup() }; + } +} + +/// # Cursor / Layout impl<'ui> Ui<'ui> { /// Renders a separator (generally horizontal). /// @@ -51,4 +64,53 @@ impl<'ui> Ui<'ui> { pub fn unindent_by(&self, width: f32) { unsafe { sys::igUnindent(width) }; } + /// Group items together as a single item. + /// + /// May be useful to handle the same mouse event on a group of items, for example. + pub fn group(&self) -> GroupToken { + unsafe { sys::igBeginGroup() }; + GroupToken { _ui: PhantomData } + } + /// Returns the cursor position (in window coordinates) + pub fn cursor_pos(&self) -> [f32; 2] { + unsafe { sys::igGetCursorPos_nonUDT2().into() } + } + /// Set the cursor position (in window coordinates). + /// + /// This sets the point on which the next widget will be drawn. + pub fn set_cursor_pos(&self, pos: [f32; 2]) { + unsafe { sys::igSetCursorPos(pos.into()) }; + } + /// Returns the initial cursor position (in window coordinates) + pub fn cursor_start_pos(&self) -> [f32; 2] { + unsafe { sys::igGetCursorStartPos_nonUDT2().into() } + } + /// Returns the cursor position (in absolute screen coordinates). + /// + /// This is especially useful for drawing, as the drawing API uses screen coordinates. + pub fn cursor_screen_pos(&self) -> [f32; 2] { + unsafe { sys::igGetCursorScreenPos_nonUDT2().into() } + } + /// Set the cursor position (in absolute screen coordinates) + pub fn set_cursor_screen_pos(&self, pos: [f32; 2]) { + unsafe { sys::igSetCursorScreenPos(pos.into()) } + } + /// Vertically align text baseline so that it will align properly to regularly frame items. + /// + /// Call this if you have text on a line before a framed item. + pub fn align_text_to_frame_padding(&self) { + unsafe { sys::igAlignTextToFramePadding() }; + } + pub fn text_line_height(&self) -> f32 { + unsafe { sys::igGetTextLineHeight() } + } + pub fn text_line_height_with_spacing(&self) -> f32 { + unsafe { sys::igGetTextLineHeightWithSpacing() } + } + pub fn frame_height(&self) -> f32 { + unsafe { sys::igGetFrameHeight() } + } + pub fn frame_height_with_spacing(&self) -> f32 { + unsafe { sys::igGetFrameHeightWithSpacing() } + } } diff --git a/src/legacy.rs b/src/legacy.rs index 239612a..eda5d6c 100644 --- a/src/legacy.rs +++ b/src/legacy.rs @@ -331,3 +331,23 @@ impl<'ui> Ui<'ui> { ColorButton::new(desc_id, color.into()) } } + +impl<'ui> Ui<'ui> { + #[deprecated(since = "0.2.0", note = "use Ui::cursor_screen_pos instead")] + pub fn get_cursor_screen_pos(&self) -> [f32; 2] { + let size = unsafe { sys::igGetCursorScreenPos_nonUDT2() }; + size.into() + } + #[deprecated(since = "0.2.0", note = "use Ui::cursor_pos instead")] + pub fn get_cursor_pos(&self) -> [f32; 2] { + let size = unsafe { sys::igGetCursorPos_nonUDT2() }; + size.into() + } + #[deprecated( + since = "0.2.0", + note = "use Ui::text_line_height_with_spacing instead" + )] + pub fn get_text_line_height_with_spacing(&self) -> f32 { + unsafe { sys::igGetTextLineHeightWithSpacing() } + } +} diff --git a/src/lib.rs b/src/lib.rs index cf95548..7569396 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -224,34 +224,6 @@ impl<'ui> Ui<'ui> { pub fn get_columns_count(&self) -> i32 { unsafe { sys::igGetColumnsCount() } } - - /// Get cursor position on the screen, in screen coordinates. - /// This sets the point on which the next widget will be drawn. - /// - /// This is especially useful for drawing, as the drawing API uses - /// screen coordiantes. - pub fn get_cursor_screen_pos(&self) -> [f32; 2] { - let size = unsafe { sys::igGetCursorScreenPos_nonUDT2() }; - size.into() - } - - /// Set cursor position on the screen, in screen coordinates. - /// This sets the point on which the next widget will be drawn. - pub fn set_cursor_screen_pos(&self, pos: [f32; 2]) { - unsafe { sys::igSetCursorScreenPos(pos.into()) } - } - - /// Get cursor position on the screen, in window coordinates. - pub fn get_cursor_pos(&self) -> [f32; 2] { - let size = unsafe { sys::igGetCursorPos_nonUDT2() }; - size.into() - } - - /// Set cursor position on the screen, in window coordinates. - /// This sets the point on which the next widget will be drawn. - pub fn set_cursor_pos(&self, pos: [f32; 2]) { - unsafe { sys::igSetCursorPos(pos.into()) } - } } pub enum ImId<'a> { @@ -760,10 +732,6 @@ impl<'ui> Ui<'ui> { } impl<'ui> Ui<'ui> { - /// Get height of a line of previously drawn text item - pub fn get_text_line_height_with_spacing(&self) -> f32 { - unsafe { sys::igGetTextLineHeightWithSpacing() } - } /// Get previously drawn item's size pub fn get_item_rect_size(&self) -> [f32; 2] { let size = unsafe { sys::igGetItemRectSize_nonUDT2() }; @@ -831,19 +799,6 @@ impl<'ui> Ui<'ui> { sys::igSetItemAllowOverlap(); } } - - /// Group items together as a single item. - /// - /// May be useful to handle the same mouse event on a group of items, for example. - pub fn group(&self, f: F) { - unsafe { - sys::igBeginGroup(); - } - f(); - unsafe { - sys::igEndGroup(); - } - } } /// # Draw list for custom drawing