mirror of
https://github.com/eliasstepanik/imgui-rs.git
synced 2026-01-11 21:48:36 +00:00
Initial idea for supporting child frame's for windows.
Imgui allows the user to create child frame's, and render into them. This isn't exposed currently, this is my first idea at how rust can support child frame's. Presently it's not the easiest to use, to have a window with a child frame supported, internally the imgui library must have called beginWindow() for the parent window, before beginChild() is ever called for the child frame. Doing this without causing unneeded allocations/complexity, and making the API ergonomic is something I hope to work on next / get some feedback on.
This commit is contained in:
parent
5b42d5d079
commit
2f1cc2e9a8
135
src/child_frame.rs
Normal file
135
src/child_frame.rs
Normal file
@ -0,0 +1,135 @@
|
||||
use imgui_sys;
|
||||
use ImStr;
|
||||
use ImVec2;
|
||||
use ImGuiWindowFlags;
|
||||
|
||||
use super::{ImGuiWindowFlags_NoTitleBar, ImGuiWindowFlags_NoResize, ImGuiWindowFlags_NoMove,
|
||||
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoScrollWithMouse,
|
||||
ImGuiWindowFlags_NoCollapse, ImGuiWindowFlags_AlwaysAutoResize,
|
||||
ImGuiWindowFlags_ShowBorders, ImGuiWindowFlags_NoSavedSettings,
|
||||
ImGuiWindowFlags_NoInputs, ImGuiWindowFlags_MenuBar,
|
||||
ImGuiWindowFlags_HorizontalScrollbar, ImGuiWindowFlags_NoFocusOnAppearing,
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus, ImGuiWindowFlags_AlwaysVerticalScrollbar,
|
||||
ImGuiWindowFlags_AlwaysHorizontalScrollbar, ImGuiWindowFlags_AlwaysUseWindowPadding};
|
||||
|
||||
#[must_use]
|
||||
pub struct ChildFrame<'p> {
|
||||
name: &'p ImStr,
|
||||
size: ImVec2,
|
||||
flags: ImGuiWindowFlags,
|
||||
}
|
||||
|
||||
impl<'p> ChildFrame<'p> {
|
||||
pub fn new(name: &'p ImStr, size: ImVec2) -> ChildFrame<'p> {
|
||||
ChildFrame {
|
||||
name: name,
|
||||
size: size,
|
||||
flags: ImGuiWindowFlags::empty(),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn show_title(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoTitleBar, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn resizable(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoResize, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn movable(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoMove, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn show_scrollbar(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoScrollbar, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn show_scrollbar_with_mouse(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoScrollWithMouse, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn collapsible(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoCollapse, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn always_resizable(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_AlwaysAutoResize, value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn show_borders(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_ShowBorders, value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn save_settings(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoSavedSettings, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn input_allow(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoInputs, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn show_menu(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_MenuBar, value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn scrollbar_horizontal(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_HorizontalScrollbar, value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn focus_on_appearing(mut self, value: bool) -> Self {
|
||||
self.flags.set(ImGuiWindowFlags_NoFocusOnAppearing, !value);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn bring_to_front_on_focus(mut self, value: bool) -> Self {
|
||||
self.flags.set(
|
||||
ImGuiWindowFlags_NoBringToFrontOnFocus,
|
||||
!value,
|
||||
);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn always_show_vertical_scroll_bar(mut self, value: bool) -> Self {
|
||||
self.flags.set(
|
||||
ImGuiWindowFlags_AlwaysVerticalScrollbar,
|
||||
value,
|
||||
);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn always_show_horizontal_scroll_bar(mut self, value: bool) -> Self {
|
||||
self.flags.set(
|
||||
ImGuiWindowFlags_AlwaysHorizontalScrollbar,
|
||||
value,
|
||||
);
|
||||
self
|
||||
}
|
||||
#[inline]
|
||||
pub fn always_use_window_padding(mut self, value: bool) -> Self {
|
||||
self.flags.set(
|
||||
ImGuiWindowFlags_AlwaysUseWindowPadding,
|
||||
value,
|
||||
);
|
||||
self
|
||||
}
|
||||
pub fn build<F: FnOnce()>(self, f: F) {
|
||||
let show_border = self.flags.contains(ImGuiWindowFlags_ShowBorders);
|
||||
let render_child_frame = unsafe { imgui_sys::igBeginChild(self.name.as_ptr(), self.size, show_border, self.flags) };
|
||||
if render_child_frame {
|
||||
f();
|
||||
}
|
||||
unsafe { imgui_sys::igEndChild() };
|
||||
}
|
||||
}
|
||||
26
src/lib.rs
26
src/lib.rs
@ -35,6 +35,7 @@ pub use imgui_sys::{ImDrawIdx, ImDrawVert, ImGuiInputTextFlags, ImGuiInputTextFl
|
||||
ImGuiWindowFlags_NoSavedSettings, ImGuiWindowFlags_NoScrollWithMouse,
|
||||
ImGuiWindowFlags_NoScrollbar, ImGuiWindowFlags_NoTitleBar,
|
||||
ImGuiWindowFlags_ShowBorders, ImVec2, ImVec4};
|
||||
pub use child_frame::ChildFrame;
|
||||
pub use input::{ColorEdit3, ColorEdit4, InputFloat, InputFloat2, InputFloat3, InputFloat4,
|
||||
InputInt, InputInt2, InputInt3, InputInt4, InputText};
|
||||
pub use menus::{Menu, MenuItem};
|
||||
@ -47,6 +48,7 @@ pub use string::{ImStr, ImString};
|
||||
pub use trees::{CollapsingHeader, TreeNode};
|
||||
pub use window::Window;
|
||||
|
||||
mod child_frame;
|
||||
mod input;
|
||||
mod menus;
|
||||
mod plothistogram;
|
||||
@ -738,3 +740,27 @@ impl<'ui> Ui<'ui> {
|
||||
/// ```
|
||||
pub fn progress_bar<'p>(&self, fraction: f32) -> ProgressBar<'p> { ProgressBar::new(fraction) }
|
||||
}
|
||||
|
||||
impl<'ui> Ui<'ui> {
|
||||
/// Creates a child frame. Size is size of child_frame within parent window.
|
||||
/// # Example
|
||||
/// ```rust,no_run
|
||||
/// # use imgui::*;
|
||||
/// # let mut imgui = ImGui::init();
|
||||
/// # let ui = imgui.frame((0, 0), (0, 0), 0.1);
|
||||
/// ui.window(im_str!("ChatWindow"))
|
||||
/// .title_bar(true)
|
||||
/// .scrollable(false)
|
||||
/// .build(|| {
|
||||
/// ui.separator();
|
||||
///
|
||||
/// ui.child_frame(im_str!("child frame"), ImVec2::new(400.0, 100.0))
|
||||
/// .show_borders(true)
|
||||
/// .always_show_vertical_scroll_bar(true)
|
||||
/// .show_title(true)
|
||||
/// .build(|| {
|
||||
/// ui.text_colored((1.0, 0.0, 0.0, 1.0), im_str!("hello mate!"));
|
||||
/// });
|
||||
/// });
|
||||
pub fn child_frame<'p>(&self, name: &'p ImStr, size: ImVec2) -> ChildFrame<'p> { ChildFrame::new(name, size) }
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user