From f5ed221b4e1ce1c17d5dae4ee56491b0a33c684e Mon Sep 17 00:00:00 2001 From: Benjamin Adamson Date: Fri, 7 Jul 2017 21:32:03 -0700 Subject: [PATCH] Expose color variable stack to user. In order to have colored and wrapped text, I needed to use this functionality in my game. This PR implements a version similar to with_style_var(). From my testing it is safe to use a Vec4 for all the variants of the enumeration, so I didn't bother putting an enumeration in imgui-rs, I just reused the enumeration defined in imgui-sys directly. Let me know if you have any feedback on this. --- src/lib.rs | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 639cf93..587e467 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ pub use imgui_sys::{ImDrawIdx, ImDrawVert, ImGuiInputTextFlags, ImGuiInputTextFl ImGuiInputTextFlags_ReadOnly, ImGuiKey, ImGuiSelectableFlags, ImGuiSelectableFlags_DontClosePopups, ImGuiSelectableFlags_SpanAllColumns, ImGuiSetCond, ImGuiSetCond_Always, ImGuiSetCond_Appearing, - ImGuiSetCond_FirstUseEver, ImGuiSetCond_Once, ImGuiStyle, ImGuiTreeNodeFlags, + ImGuiSetCond_FirstUseEver, ImGuiSetCond_Once, ImGuiCol, ImGuiStyle, ImGuiTreeNodeFlags, ImGuiTreeNodeFlags_AllowOverlapMode, ImGuiTreeNodeFlags_Bullet, ImGuiTreeNodeFlags_CollapsingHeader, ImGuiTreeNodeFlags_DefaultOpen, ImGuiTreeNodeFlags_Framed, ImGuiTreeNodeFlags_Leaf, @@ -738,3 +738,42 @@ impl<'ui> Ui<'ui> { /// ``` pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'p> { ProgressBar::new(fraction) } } + +impl<'ui> Ui<'ui> { + /// Runs a function after temporarily pushing a value to the color stack. + /// + /// # Example + /// ```rust,no_run + /// # use imgui::*; + /// # let mut imgui = ImGui::init(); + /// # let ui = imgui.frame((0, 0), (0, 0), 0.1); + /// ui.with_color_var(ImGuiCol::Text, ImVec4::from((1.0, 0.0, 0.0, 1.0)), || { + /// ui.text_wrapped(im_str!("AB")); + /// }); + /// ``` + pub fn with_color_var(&self, var: ImGuiCol, color: ImVec4, f: F) { + unsafe { imgui_sys::igPushStyleColor(var, color); } + f(); + unsafe {imgui_sys::igPopStyleColor(1); } + } + + /// Runs a function after temporarily pushing an array of values to the color stack. + /// + /// # Example + /// ```rust,no_run + /// # use imgui::*; + /// # let mut imgui = ImGui::init(); + /// # let ui = imgui.frame((0, 0), (0, 0), 0.1); + /// # let vars = [ImGuiCol::Text, ImGuiCol::TextDisabled]; + /// ui.with_color_vars(&vars, ImVec4::from((1.0, 0.0, 0.0, 1.0)), || { + /// ui.text_wrapped(im_str!("AB")); + /// }); + /// ``` + pub fn with_color_vars(&self, color_vars: &[ImGuiCol], color: ImVec4, f: F) { + for &color_var in color_vars { + unsafe { imgui_sys::igPushStyleColor(color_var, color); } + } + f(); + unsafe { imgui_sys::igPopStyleColor(color_vars.len() as i32) }; + } +}