Update to imgui 1.81

Changes:

- No more ListBox::calculate_size(...) as this was deprecated upstream
This commit is contained in:
dbr 2021-03-03 15:36:28 +11:00
parent 2839f76cec
commit 507d5d5065
10 changed files with 8403 additions and 130 deletions

View File

@ -515,7 +515,7 @@ fn show_test_window(ui: &Ui, state: &mut State, opened: &mut bool) {
im_str!("Tilefish"), im_str!("Tilefish"),
]; ];
ListBox::new(im_str!("selectables list")).calculate_size(8, 4).build(ui, || { ListBox::new(im_str!("selectables list")).build(ui, || {
for (index, name) in names.iter().enumerate() { for (index, name) in names.iter().enumerate() {
let selected = matches!(state.selected_fish2, Some(i) if i == index ); let selected = matches!(state.selected_fish2, Some(i) if i == index );
if Selectable::new(name).selected(selected).build(ui) { if Selectable::new(name).selected(selected).build(ui) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1 +1 @@
Subproject commit 6791a95f79d51d8ca9df2b32d2b87a0c17a3c093 Subproject commit 25aa25bd835eaacdfabb9289fb33c21cd6e65987

View File

@ -50,7 +50,7 @@ impl<'ui> DrawListMut<'ui> {
pub(crate) fn background(_: &Ui<'ui>) -> Self { pub(crate) fn background(_: &Ui<'ui>) -> Self {
Self::lock_draw_list(); Self::lock_draw_list();
Self { Self {
draw_list: unsafe { sys::igGetBackgroundDrawList() }, draw_list: unsafe { sys::igGetBackgroundDrawListNil() },
_phantom: PhantomData, _phantom: PhantomData,
} }
} }
@ -58,7 +58,7 @@ impl<'ui> DrawListMut<'ui> {
pub(crate) fn foreground(_: &Ui<'ui>) -> Self { pub(crate) fn foreground(_: &Ui<'ui>) -> Self {
Self::lock_draw_list(); Self::lock_draw_list();
Self { Self {
draw_list: unsafe { sys::igGetForegroundDrawList() }, draw_list: unsafe { sys::igGetForegroundDrawListNil() },
_phantom: PhantomData, _phantom: PhantomData,
} }
} }

View File

@ -58,6 +58,8 @@ pub struct FontAtlas {
custom_rects: sys::ImVector_ImFontAtlasCustomRect, custom_rects: sys::ImVector_ImFontAtlasCustomRect,
config_data: sys::ImVector_ImFontConfig, config_data: sys::ImVector_ImFontConfig,
tex_uv_lines: [[f32; 4]; 64], tex_uv_lines: [[f32; 4]; 64],
font_builder_io: *const sys::ImFontBuilderIO,
font_builder_flags: i32,
pack_id_mouse_cursors: i32, pack_id_mouse_cursors: i32,
pack_id_lines: i32, pack_id_lines: i32,
} }
@ -302,7 +304,7 @@ pub struct FontConfig {
/// Maximum advance_x for glyphs /// Maximum advance_x for glyphs
pub glyph_max_advance_x: f32, pub glyph_max_advance_x: f32,
/// Settings for a custom font rasterizer if used /// Settings for a custom font rasterizer if used
pub rasterizer_flags: u32, pub font_builder_flags: u32,
/// Brighten (>1.0) or darken (<1.0) font output /// Brighten (>1.0) or darken (<1.0) font output
pub rasterizer_multiply: f32, pub rasterizer_multiply: f32,
/// Explicitly specify the ellipsis character. /// Explicitly specify the ellipsis character.
@ -324,7 +326,7 @@ impl Default for FontConfig {
glyph_ranges: FontGlyphRanges::default(), glyph_ranges: FontGlyphRanges::default(),
glyph_min_advance_x: 0.0, glyph_min_advance_x: 0.0,
glyph_max_advance_x: f32::MAX, glyph_max_advance_x: f32::MAX,
rasterizer_flags: 0, font_builder_flags: 0,
rasterizer_multiply: 1.0, rasterizer_multiply: 1.0,
ellipsis_char: None, ellipsis_char: None,
name: None, name: None,
@ -343,7 +345,7 @@ impl FontConfig {
raw.GlyphRanges = unsafe { self.glyph_ranges.to_ptr(atlas) }; raw.GlyphRanges = unsafe { self.glyph_ranges.to_ptr(atlas) };
raw.GlyphMinAdvanceX = self.glyph_min_advance_x; raw.GlyphMinAdvanceX = self.glyph_min_advance_x;
raw.GlyphMaxAdvanceX = self.glyph_max_advance_x; raw.GlyphMaxAdvanceX = self.glyph_max_advance_x;
raw.RasterizerFlags = self.rasterizer_flags; raw.FontBuilderFlags = self.font_builder_flags;
raw.RasterizerMultiply = self.rasterizer_multiply; raw.RasterizerMultiply = self.rasterizer_multiply;
raw.EllipsisChar = self.ellipsis_char.map(|x| x as u16).unwrap_or(0xffff); raw.EllipsisChar = self.ellipsis_char.map(|x| x as u16).unwrap_or(0xffff);
if let Some(name) = self.name.as_ref() { if let Some(name) = self.name.as_ref() {
@ -396,8 +398,8 @@ fn test_font_config_default() {
sys_font_config.GlyphMaxAdvanceX sys_font_config.GlyphMaxAdvanceX
); );
assert_eq!( assert_eq!(
font_config.rasterizer_flags, font_config.font_builder_flags,
sys_font_config.RasterizerFlags sys_font_config.FontBuilderFlags
); );
assert_eq!( assert_eq!(
font_config.rasterizer_multiply, font_config.rasterizer_multiply,

View File

@ -99,7 +99,7 @@ pub fn dear_imgui_version() -> &'static str {
#[test] #[test]
fn test_version() { fn test_version() {
// TODO: what's the point of this test? // TODO: what's the point of this test?
assert_eq!(dear_imgui_version(), "1.80"); assert_eq!(dear_imgui_version(), "1.81");
} }
impl Context { impl Context {

View File

@ -9,14 +9,14 @@ use crate::sys;
pub struct DrawData { pub struct DrawData {
/// Only valid after render() is called and before the next new frame() is called. /// Only valid after render() is called and before the next new frame() is called.
valid: bool, valid: bool,
// Array of DrawList.
cmd_lists: *mut *mut DrawList,
/// Number of DrawList to render. /// Number of DrawList to render.
cmd_lists_count: i32, cmd_lists_count: i32,
/// For convenience, sum of all draw list index buffer sizes. /// For convenience, sum of all draw list index buffer sizes.
pub total_idx_count: i32, pub total_idx_count: i32,
/// For convenience, sum of all draw list vertex buffer sizes. /// For convenience, sum of all draw list vertex buffer sizes.
pub total_vtx_count: i32, pub total_vtx_count: i32,
// Array of DrawList.
cmd_lists: *mut *mut DrawList,
/// Upper-left position of the viewport to render. /// Upper-left position of the viewport to render.
/// ///
/// (= upper-left corner of the orthogonal projection matrix to use) /// (= upper-left corner of the orthogonal projection matrix to use)

View File

@ -4,20 +4,12 @@ use crate::string::ImStr;
use crate::sys; use crate::sys;
use crate::Ui; use crate::Ui;
#[derive(Copy, Clone, Debug)]
enum Size {
Vec(sys::ImVec2),
Items {
items_count: i32,
height_in_items: i32,
},
}
/// Builder for a list box widget /// Builder for a list box widget
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
#[must_use] #[must_use]
pub struct ListBox<'a> { pub struct ListBox<'a> {
label: &'a ImStr, label: &'a ImStr,
size: Size, size: sys::ImVec2,
} }
impl<'a> ListBox<'a> { impl<'a> ListBox<'a> {
@ -25,21 +17,9 @@ impl<'a> ListBox<'a> {
pub const fn new(label: &'a ImStr) -> ListBox<'a> { pub const fn new(label: &'a ImStr) -> ListBox<'a> {
ListBox { ListBox {
label, label,
size: Size::Vec(sys::ImVec2::zero()), size: sys::ImVec2::zero(),
} }
} }
/// Sets the list box size based on the number of items that you want to make visible
/// Size default to hold ~7.25 items.
/// We add +25% worth of item height to allow the user to see at a glance if there are more items up/down, without looking at the scrollbar.
/// We don't add this extra bit if items_count <= height_in_items. It is slightly dodgy, because it means a dynamic list of items will make the widget resize occasionally when it crosses that size.
#[inline]
pub const fn calculate_size(mut self, items_count: i32, height_in_items: i32) -> Self {
self.size = Size::Items {
items_count,
height_in_items,
};
self
}
/// Sets the list box size based on the given width and height /// Sets the list box size based on the given width and height
/// If width or height are 0 or smaller, a default value is calculated /// If width or height are 0 or smaller, a default value is calculated
@ -49,7 +29,7 @@ impl<'a> ListBox<'a> {
/// Default: [0.0, 0.0], in which case the combobox calculates a sensible width and height /// Default: [0.0, 0.0], in which case the combobox calculates a sensible width and height
#[inline] #[inline]
pub const fn size(mut self, size: [f32; 2]) -> Self { pub const fn size(mut self, size: [f32; 2]) -> Self {
self.size = Size::Vec(sys::ImVec2::new(size[0], size[1])); self.size = sys::ImVec2::new(size[0], size[1]);
self self
} }
/// Creates a list box and starts appending to it. /// Creates a list box and starts appending to it.
@ -60,15 +40,7 @@ impl<'a> ListBox<'a> {
/// Returns `None` if the list box is not open and no content should be rendered. /// Returns `None` if the list box is not open and no content should be rendered.
#[must_use] #[must_use]
pub fn begin<'ui>(self, ui: &Ui<'ui>) -> Option<ListBoxToken<'ui>> { pub fn begin<'ui>(self, ui: &Ui<'ui>) -> Option<ListBoxToken<'ui>> {
let should_render = unsafe { let should_render = unsafe { sys::igBeginListBox(self.label.as_ptr(), self.size) };
match self.size {
Size::Vec(size) => sys::igListBoxHeaderVec2(self.label.as_ptr(), size),
Size::Items {
items_count,
height_in_items,
} => sys::igListBoxHeaderInt(self.label.as_ptr(), items_count, height_in_items),
}
};
if should_render { if should_render {
Some(ListBoxToken::new(ui)) Some(ListBoxToken::new(ui))
} else { } else {
@ -91,7 +63,7 @@ create_token!(
pub struct ListBoxToken<'ui>; pub struct ListBoxToken<'ui>;
/// Ends a list box /// Ends a list box
drop { sys::igListBoxFooter() } drop { sys::igEndListBox() }
); );
/// # Convenience functions /// # Convenience functions

View File

@ -29,11 +29,11 @@ impl<'ui> Ui<'ui> {
} }
/// Sets the horizontal scrolling position /// Sets the horizontal scrolling position
pub fn set_scroll_x(&self, scroll_x: f32) { pub fn set_scroll_x(&self, scroll_x: f32) {
unsafe { sys::igSetScrollX(scroll_x) }; unsafe { sys::igSetScrollXFloat(scroll_x) };
} }
/// Sets the vertical scroll position /// Sets the vertical scroll position
pub fn set_scroll_y(&self, scroll_y: f32) { pub fn set_scroll_y(&self, scroll_y: f32) {
unsafe { sys::igSetScrollY(scroll_y) }; unsafe { sys::igSetScrollYFloat(scroll_y) };
} }
/// Adjusts the horizontal scroll position to make the current cursor position visible /// Adjusts the horizontal scroll position to make the current cursor position visible
pub fn set_scroll_here_x(&self) { pub fn set_scroll_here_x(&self) {
@ -65,7 +65,7 @@ impl<'ui> Ui<'ui> {
} }
/// Adjusts the horizontal scroll position to make the given position visible /// Adjusts the horizontal scroll position to make the given position visible
pub fn set_scroll_from_pos_x(&self, local_x: f32) { pub fn set_scroll_from_pos_x(&self, local_x: f32) {
unsafe { sys::igSetScrollFromPosX(local_x, 0.5) }; unsafe { sys::igSetScrollFromPosXFloat(local_x, 0.5) };
} }
/// Adjusts the horizontal scroll position to make the given position visible. /// Adjusts the horizontal scroll position to make the given position visible.
/// ///
@ -75,11 +75,11 @@ impl<'ui> Ui<'ui> {
/// - `0.5`: center /// - `0.5`: center
/// - `1.0`: right /// - `1.0`: right
pub fn set_scroll_from_pos_x_with_ratio(&self, local_x: f32, center_x_ratio: f32) { pub fn set_scroll_from_pos_x_with_ratio(&self, local_x: f32, center_x_ratio: f32) {
unsafe { sys::igSetScrollFromPosX(local_x, center_x_ratio) }; unsafe { sys::igSetScrollFromPosXFloat(local_x, center_x_ratio) };
} }
/// Adjusts the vertical scroll position to make the given position visible /// Adjusts the vertical scroll position to make the given position visible
pub fn set_scroll_from_pos_y(&self, local_y: f32) { pub fn set_scroll_from_pos_y(&self, local_y: f32) {
unsafe { sys::igSetScrollFromPosY(local_y, 0.5) }; unsafe { sys::igSetScrollFromPosYFloat(local_y, 0.5) };
} }
/// Adjusts the vertical scroll position to make the given position visible. /// Adjusts the vertical scroll position to make the given position visible.
/// ///
@ -89,6 +89,6 @@ impl<'ui> Ui<'ui> {
/// - `0.5`: center /// - `0.5`: center
/// - `1.0`: bottom /// - `1.0`: bottom
pub fn set_scroll_from_pos_y_with_ratio(&self, local_y: f32, center_y_ratio: f32) { pub fn set_scroll_from_pos_y_with_ratio(&self, local_y: f32, center_y_ratio: f32) {
unsafe { sys::igSetScrollFromPosY(local_y, center_y_ratio) }; unsafe { sys::igSetScrollFromPosYFloat(local_y, center_y_ratio) };
} }
} }