From f7a7961ceec5bcdf2e21f8161446e3f9dc2a6df6 Mon Sep 17 00:00:00 2001 From: Joonas Javanainen Date: Thu, 27 Jun 2019 17:29:09 +0300 Subject: [PATCH] Replace ImCond with Condition --- imgui-examples/examples/hello_gfx.rs | 2 +- imgui-glium-examples/examples/color_button.rs | 12 +++++----- .../examples/custom_textures.rs | 2 +- imgui-glium-examples/examples/hello_world.rs | 2 +- .../examples/test_window_impl.rs | 12 +++++----- imgui-sys/src/flags.rs | 17 ------------- imgui-sys/src/lib.rs | 7 +++--- src/lib.rs | 24 ++++++++++++++++--- src/trees.rs | 12 +++++----- src/window.rs | 22 ++++++++--------- 10 files changed, 57 insertions(+), 55 deletions(-) diff --git a/imgui-examples/examples/hello_gfx.rs b/imgui-examples/examples/hello_gfx.rs index e425e17..5d059f6 100644 --- a/imgui-examples/examples/hello_gfx.rs +++ b/imgui-examples/examples/hello_gfx.rs @@ -16,7 +16,7 @@ fn hello_world<'a>(ui: &Ui<'a>) -> bool { let window_title = im_str!("Hello world (DirectX)"); ui.window(window_title) - .size((300.0, 100.0), ImGuiCond::FirstUseEver) + .size((300.0, 100.0), Condition::FirstUseEver) .build(|| { ui.text(im_str!("Hello world!")); ui.text(im_str!("こんにちは世界!")); diff --git a/imgui-glium-examples/examples/color_button.rs b/imgui-glium-examples/examples/color_button.rs index 18e86de..16076a2 100644 --- a/imgui-glium-examples/examples/color_button.rs +++ b/imgui-glium-examples/examples/color_button.rs @@ -39,8 +39,8 @@ fn main() { fn example_selector(state: &mut State, ui: &Ui) { ui.window(im_str!("Color button examples")) - .position((20.0, 20.0), ImGuiCond::Appearing) - .size((700.0, 80.0), ImGuiCond::Appearing) + .position((20.0, 20.0), Condition::Appearing) + .size((700.0, 80.0), Condition::Appearing) .resizable(false) .build(|| { let ex1 = ui.radio_button(im_str!("Example 1: Basics"), &mut state.example, 1); @@ -53,8 +53,8 @@ fn example_selector(state: &mut State, ui: &Ui) { fn example_1(state: &mut State, ui: &Ui) { ui.window(im_str!("Example 1: Basics")) - .size((700.0, 300.0), ImGuiCond::Appearing) - .position((20.0, 120.0), ImGuiCond::Appearing) + .size((700.0, 300.0), Condition::Appearing) + .position((20.0, 120.0), Condition::Appearing) .build(|| { ui.text_wrapped(im_str!( "Color button is a widget that displays a color value as a clickable rectangle. \ @@ -101,8 +101,8 @@ fn example_1(state: &mut State, ui: &Ui) { fn example_2(ui: &Ui) { ui.window(im_str!("Example 2: Alpha component")) - .size((700.0, 320.0), ImGuiCond::Appearing) - .position((20.0, 140.0), ImGuiCond::Appearing) + .size((700.0, 320.0), Condition::Appearing) + .position((20.0, 140.0), Condition::Appearing) .build(|| { ui.text_wrapped(im_str!( "The displayed color is passed to the button as four float values between \ diff --git a/imgui-glium-examples/examples/custom_textures.rs b/imgui-glium-examples/examples/custom_textures.rs index ca594fe..4fd7b55 100644 --- a/imgui-glium-examples/examples/custom_textures.rs +++ b/imgui-glium-examples/examples/custom_textures.rs @@ -71,7 +71,7 @@ impl CustomTexturesApp { fn show_textures(&self, ui: &Ui) { ui.window(im_str!("Hello textures")) - .size((400.0, 600.0), ImGuiCond::FirstUseEver) + .size((400.0, 600.0), Condition::FirstUseEver) .build(|| { ui.text(im_str!("Hello textures!")); if let Some(my_texture_id) = self.my_texture_id { diff --git a/imgui-glium-examples/examples/hello_world.rs b/imgui-glium-examples/examples/hello_world.rs index 95de9d8..b92035d 100644 --- a/imgui-glium-examples/examples/hello_world.rs +++ b/imgui-glium-examples/examples/hello_world.rs @@ -12,7 +12,7 @@ fn main() { fn hello_world<'a>(ui: &Ui<'a>) -> bool { ui.window(im_str!("Hello world")) - .size((300.0, 100.0), ImGuiCond::FirstUseEver) + .size((300.0, 100.0), Condition::FirstUseEver) .build(|| { ui.text(im_str!("Hello world!")); ui.text(im_str!("こんにちは世界!")); diff --git a/imgui-glium-examples/examples/test_window_impl.rs b/imgui-glium-examples/examples/test_window_impl.rs index 544dd7d..b08c24d 100644 --- a/imgui-glium-examples/examples/test_window_impl.rs +++ b/imgui-glium-examples/examples/test_window_impl.rs @@ -284,7 +284,7 @@ fn show_test_window(ui: &Ui, state: &mut State, opened: &mut bool) { .scroll_bar(!state.no_scrollbar) .collapsible(!state.no_collapse) .menu_bar(!state.no_menu) - .size((550.0, 680.0), ImGuiCond::FirstUseEver); + .size((550.0, 680.0), Condition::FirstUseEver); if !state.no_close { window = window.opened(opened) } @@ -810,7 +810,7 @@ fn show_example_app_fixed_overlay(ui: &Ui, opened: &mut bool) { ui.with_color_var(ImGuiCol::WindowBg, (0.0, 0.0, 0.0, 0.3), || { ui.window(im_str!("Example: Fixed Overlay")) .opened(opened) - .position(window_pos, ImGuiCond::Always) + .position(window_pos, Condition::Always) .title_bar(false) .resizable(false) .always_auto_resize(true) @@ -830,7 +830,7 @@ fn show_example_app_fixed_overlay(ui: &Ui, opened: &mut bool) { fn show_example_app_manipulating_window_title(ui: &Ui) { ui.window(im_str!("Same title as another window##1")) - .position((100.0, 100.0), ImGuiCond::FirstUseEver) + .position((100.0, 100.0), Condition::FirstUseEver) .build(|| { ui.text( "This is window 1. @@ -838,7 +838,7 @@ My title is the same as window 2, but my identifier is unique.", ); }); ui.window(im_str!("Same title as another window##2")) - .position((100.0, 200.0), ImGuiCond::FirstUseEver) + .position((100.0, 200.0), Condition::FirstUseEver) .build(|| { ui.text( "This is window 2. @@ -850,13 +850,13 @@ My title is the same as window 1, but my identifier is unique.", let num = ui.imgui().get_frame_count(); // The C++ version uses rand() here let title = im_str!("Animated title {} {}###AnimatedTitle", chars[ch_idx], num); ui.window(title) - .position((100.0, 300.0), ImGuiCond::FirstUseEver) + .position((100.0, 300.0), Condition::FirstUseEver) .build(|| ui.text("This window has a changing title")); } fn show_example_app_custom_rendering(ui: &Ui, state: &mut CustomRenderingState, opened: &mut bool) { ui.window(im_str!("Example: Custom rendering")) - .size((350.0, 560.0), ImGuiCond::FirstUseEver) + .size((350.0, 560.0), Condition::FirstUseEver) .opened(opened) .build(|| { ui.text("Primitives"); diff --git a/imgui-sys/src/flags.rs b/imgui-sys/src/flags.rs index 33d1f5c..74ed052 100644 --- a/imgui-sys/src/flags.rs +++ b/imgui-sys/src/flags.rs @@ -96,23 +96,6 @@ bitflags!( } ); -bitflags!( - /// Condition flags - #[repr(C)] - pub struct ImGuiCond: c_int { - /// Set the variable - const Always = 1; - /// Set the variable once per runtime session (only the first call with succeed) - const Once = 1 << 1; - /// Set the variable if the object/window has no persistently saved data (no entry in .ini - /// file) - const FirstUseEver = 1 << 2; - /// Set the variable if the object/window is appearing after being hidden/inactive (or the - /// first time) - const Appearing = 1 << 3; - } -); - bitflags!( /// Configuration flags #[repr(C)] diff --git a/imgui-sys/src/lib.rs b/imgui-sys/src/lib.rs index a91ba7f..193cef3 100644 --- a/imgui-sys/src/lib.rs +++ b/imgui-sys/src/lib.rs @@ -37,9 +37,10 @@ pub use bindings::{ ImFontAtlas_GetGlyphRangesDefault, ImFontAtlas_GetGlyphRangesJapanese, ImFontAtlas_GetGlyphRangesKorean, ImFontAtlas_GetGlyphRangesThai, ImFontAtlas_GetGlyphRangesVietnamese, ImFontAtlas_GetTexDataAsRGBA32, ImFontConfig, - ImFontGlyphRangesBuilder, ImGuiContext, ImGuiID, ImGuiIO, ImGuiIO_AddInputCharacter, - ImGuiIO_AddInputCharactersUTF8, ImGuiIO_ClearInputCharacters, ImGuiInputTextCallback, - ImGuiInputTextCallbackData, ImGuiInputTextCallbackData_DeleteChars, + ImFontGlyphRangesBuilder, ImGuiCond, ImGuiCond_, ImGuiCond_Always, ImGuiCond_Appearing, + ImGuiCond_FirstUseEver, ImGuiCond_Once, ImGuiContext, ImGuiID, ImGuiIO, + ImGuiIO_AddInputCharacter, ImGuiIO_AddInputCharactersUTF8, ImGuiIO_ClearInputCharacters, + ImGuiInputTextCallback, ImGuiInputTextCallbackData, ImGuiInputTextCallbackData_DeleteChars, ImGuiInputTextCallbackData_HasSelection, ImGuiInputTextCallbackData_ImGuiInputTextCallbackData, ImGuiInputTextCallbackData_InsertChars, ImGuiInputTextCallbackData_destroy, ImGuiListClipper, ImGuiPayload, ImGuiSizeCallback, ImGuiStorage, ImGuiTextBuffer, ImGuiTextFilter, ImTextureID, diff --git a/src/lib.rs b/src/lib.rs index d0951ea..8a8635c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,9 +36,9 @@ pub use self::sliders::{ pub use self::string::{ImStr, ImString}; pub use self::style::StyleVar; pub use self::sys::{ - ImDrawIdx, ImDrawVert, ImGuiCol, ImGuiColorEditFlags, ImGuiCond, ImGuiFocusedFlags, - ImGuiHoveredFlags, ImGuiInputTextFlags, ImGuiKey, ImGuiMouseCursor, ImGuiSelectableFlags, - ImGuiStyle, ImGuiTreeNodeFlags, ImGuiWindowFlags, ImVec2, ImVec4, + ImDrawIdx, ImDrawVert, ImGuiCol, ImGuiColorEditFlags, ImGuiFocusedFlags, ImGuiHoveredFlags, + ImGuiInputTextFlags, ImGuiKey, ImGuiMouseCursor, ImGuiSelectableFlags, ImGuiStyle, + ImGuiTreeNodeFlags, ImGuiWindowFlags, ImVec2, ImVec4, }; pub use self::trees::{CollapsingHeader, TreeNode}; pub use self::window::Window; @@ -1730,3 +1730,21 @@ impl<'ui> Ui<'ui> { WindowDrawList::new(self) } } + +/// Condition for applying a setting +#[repr(i8)] +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub enum Condition { + /// Never apply the setting + Never = -1, + /// Always apply the setting + Always = sys::ImGuiCond_Always as i8, + /// Apply the setting once per runtime session (only the first call will succeed) + Once = sys::ImGuiCond_Once as i8, + /// Apply the setting if the object/window has no persistently saved data (no entry in .ini + /// file) + FirstUseEver = sys::ImGuiCond_FirstUseEver as i8, + /// Apply the setting if the object/window is appearing after being hidden/inactive (or the + /// first time) + Appearing = sys::ImGuiCond_Appearing as i8, +} diff --git a/src/trees.rs b/src/trees.rs index 06a7701..10898de 100644 --- a/src/trees.rs +++ b/src/trees.rs @@ -1,14 +1,14 @@ use std::marker::PhantomData; use sys; -use super::{ImGuiCond, ImGuiTreeNodeFlags, ImStr, Ui}; +use super::{Condition, ImGuiTreeNodeFlags, ImStr, Ui}; #[must_use] pub struct TreeNode<'ui, 'p> { id: &'p ImStr, label: Option<&'p ImStr>, opened: bool, - opened_cond: ImGuiCond, + opened_cond: Condition, flags: ImGuiTreeNodeFlags, _phantom: PhantomData<&'ui Ui<'ui>>, } @@ -19,7 +19,7 @@ impl<'ui, 'p> TreeNode<'ui, 'p> { id, label: None, opened: false, - opened_cond: ImGuiCond::empty(), + opened_cond: Condition::Never, flags: ImGuiTreeNodeFlags::empty(), _phantom: PhantomData, } @@ -30,7 +30,7 @@ impl<'ui, 'p> TreeNode<'ui, 'p> { self } #[inline] - pub fn opened(mut self, opened: bool, cond: ImGuiCond) -> Self { + pub fn opened(mut self, opened: bool, cond: Condition) -> Self { self.opened = opened; self.opened_cond = cond; self @@ -97,8 +97,8 @@ impl<'ui, 'p> TreeNode<'ui, 'p> { } pub fn build(self, f: F) { let render = unsafe { - if !self.opened_cond.is_empty() { - sys::igSetNextItemOpen(self.opened, self.opened_cond); + if self.opened_cond != Condition::Never { + sys::igSetNextItemOpen(self.opened, self.opened_cond as _); } sys::igTreeNodeExStrStr( self.id.as_ptr(), diff --git a/src/window.rs b/src/window.rs index c00b5dc..c21d231 100644 --- a/src/window.rs +++ b/src/window.rs @@ -2,15 +2,15 @@ use std::marker::PhantomData; use std::ptr; use sys; -use super::{ImGuiCond, ImGuiWindowFlags, ImStr, Ui}; +use super::{Condition, ImGuiWindowFlags, ImStr, Ui}; #[must_use] pub struct Window<'ui, 'p> { pos: (f32, f32), - pos_cond: ImGuiCond, + pos_cond: Condition, pos_pivot: (f32, f32), size: (f32, f32), - size_cond: ImGuiCond, + size_cond: Condition, name: &'p ImStr, opened: Option<&'p mut bool>, flags: ImGuiWindowFlags, @@ -21,10 +21,10 @@ impl<'ui, 'p> Window<'ui, 'p> { pub fn new(_: &Ui<'ui>, name: &'p ImStr) -> Window<'ui, 'p> { Window { pos: (0.0, 0.0), - pos_cond: ImGuiCond::empty(), + pos_cond: Condition::Never, pos_pivot: (0.0, 0.0), size: (0.0, 0.0), - size_cond: ImGuiCond::empty(), + size_cond: Condition::Never, name, opened: None, flags: ImGuiWindowFlags::empty(), @@ -32,7 +32,7 @@ impl<'ui, 'p> Window<'ui, 'p> { } } #[inline] - pub fn position(mut self, pos: (f32, f32), cond: ImGuiCond) -> Self { + pub fn position(mut self, pos: (f32, f32), cond: Condition) -> Self { self.pos = pos; self.pos_cond = cond; self @@ -43,7 +43,7 @@ impl<'ui, 'p> Window<'ui, 'p> { self } #[inline] - pub fn size(mut self, size: (f32, f32), cond: ImGuiCond) -> Self { + pub fn size(mut self, size: (f32, f32), cond: Condition) -> Self { self.size = size; self.size_cond = cond; self @@ -144,11 +144,11 @@ impl<'ui, 'p> Window<'ui, 'p> { } pub fn build(self, f: F) { let render = unsafe { - if !self.pos_cond.is_empty() { - sys::igSetNextWindowPos(self.pos.into(), self.pos_cond, self.pos_pivot.into()); + if self.pos_cond != Condition::Never { + sys::igSetNextWindowPos(self.pos.into(), self.pos_cond as _, self.pos_pivot.into()); } - if !self.size_cond.is_empty() { - sys::igSetNextWindowSize(self.size.into(), self.size_cond); + if self.size_cond != Condition::Never { + sys::igSetNextWindowSize(self.size.into(), self.size_cond as _); } sys::igBegin( self.name.as_ptr(),