Update layout API

This commit is contained in:
Joonas Javanainen 2019-07-12 20:59:19 +03:00
parent 86ee32273f
commit f1c041b3f3
No known key found for this signature in database
GPG Key ID: D39CCA5CB19B9179
6 changed files with 87 additions and 49 deletions

View File

@ -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

View File

@ -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(

View File

@ -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 {

View File

@ -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() }
}
}

View File

@ -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() }
}
}

View File

@ -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<F: FnOnce()>(&self, f: F) {
unsafe {
sys::igBeginGroup();
}
f();
unsafe {
sys::igEndGroup();
}
}
}
/// # Draw list for custom drawing