Expand description
Crate for easy integration of the Dear ImGui library.
This crate is a bind to the Dear ImGui library only. There is also a matching rendering
library, easy-imgui-renderer, that renders the UI using OpenGl, and a matching
window-integrated library, easy-imgui-window, that enables to build a full desktop
application in just a few lines.
If you don’t know where to start, then start with the latter. Take a look at the examples.
The simplest easy-imgui program would be something like this:
§A note about labels and ids.
In Dear ImGui, many controls take a string argument as both a label and an identifier. You can
use ## or a ### as a separator between the label and the idenfifier if you want to make them
apart.
In easy_imgui, since version 0.8, this is represented by the LblId type, not by a plain
string.
If you want to keep on using the same type just write lbl("foo"), lbl("foo##bar") or
"foo".into() and it will behave the same as before. But if you want to use them
separately, you can write lbl_id("Label", "id") and it will join the two strings for you,
separated by ###. This is particularly nice if you pretend to translate the UI: the labels change,
but the ids should remain constant.
Some functions only take an id, no label. Those will take an argument of type Id, that you
can construct with the function id, that will prepend the ### or raw_id to use the
string as-is. Like before, you can also write "id".into() to behave as previous versions of
this crate.
use easy_imgui_window::{
easy_imgui as imgui,
MainWindow,
MainWindowWithRenderer,
Application, AppHandler, Args, EventResult,
winit,
};
use winit::{
event_loop::{EventLoop, ActiveEventLoop},
event::WindowEvent,
};
// The App type, this will do the actual app. stuff.
struct App;
// This trait handles the UI building.
impl imgui::UiBuilder for App {
// There are other function in this trait, but this is the only one
// mandatory and the most important: it build the UI.
fn do_ui(&mut self, ui: &imgui::Ui<Self>) {
ui.show_demo_window(None);
}
}
// This trait handles the application & event loop stuff.
impl Application for App {
// The user event type, `()` if not needed.
type UserEvent = ();
// Custom data type, `()` if not needed.
type Data = ();
// Create the app object.
fn new(_: Args<()>) -> App {
App
}
// Handle one window event.
// There are a few other functions for other types of events.
fn window_event(&mut self, args: Args<()>, _event: WindowEvent, res: EventResult) {
if res.window_closed {
args.event_loop.exit();
}
}
}
fn main() {
// Create a `winit` event loop.
let event_loop = EventLoop::new().unwrap();
// Create an application handler.
let mut main = AppHandler::<App>::default();
// Optionally set up the window attributes.
main.attributes().title = String::from("Example");
// Run the loop
event_loop.run_app(&mut main);
}§Alternatives
This crate is similar to imgui-rs, and it is inpired by it, but with a few key
differences:
- It doesn’t use any C++-to-C api generator, as
rust-bindgenis able to import simple C++ libraries directly. - It is lower level, there are fewer high-level abstractions over the ImGui API. This means
that:
- This API is less Rusty than imgui-rs’s.
- If you know how to use Dear ImGui, then you know how to use easy-imgui.
- It is far easier to upgrade to new Dear ImGui versions.
§Features
These are the available features for this crate:
freetype: Uses an external freetype font loader for Dear ImGui, instead of the embeddedstb_truetypelibrary.
§Usage
It is easier to use one of the higher level crates [easy-imgui-window] or [easy-imgui-renderer].
But if you intend to render the UI yourself, then you can use this directly.
These are the main pieces of this crate:
Context: It represents the ImGui context. In DearImgui this is a global variable. Here it is a thread-local variable. Still, since it is implicit in most API calls, most uses of this type are unsafe. If you use [easy-imgui-window] or [easy-imgui-renderer] you will rarely need to touch this type directly.Ui: A frame that is being built. Most ImGui functions are members ofUi.UiBuilder: A trait that your application implements do build your user interface.
If you want to use this library directly, just create a Context, set up its properties, and
when you want to render a frame do Context::set_current and then CurrentContext::do_frame.
If you use one of the helper crates then you will just implement UiBuilder and get a Ui for
free.
§Conventions
This crate follows a series of naming conventions to make the API more predictable,
particularly with the Ui member functions:
- A
Pushableis any value that can be made active by a push function and inactive by a corresponding pop function. Examples are styles, colors, fonts… - A
Hashableis any value that can be used to build an ImGui hash id. Ideally there should be one of these everywhere, but the Dear ImGui API it not totally othogonal here… - A function without special prefix or suffix does the same thing as its Dear ImGui
counterpart. For example
Ui::buttoncallsImGui_Button. - A function name that contains the
withword takes a function that is called immediately. It corresponds to a pair of*Beginand*Endfunctions in Dear ImGui. The function is called between these two functions. The value returned will be that of the function.- If the function is called based on some condition, such as with
ImGui_BeginChild, then there will be another function with prefixwith_always_that takes a function with a bool argumentopened: bool, that can be used if you need the function to be called even if the condition is not met.
- If the function is called based on some condition, such as with
- A function name that ends as
_configwill create a builder object (with themust_useannotation). This object will have a few properties to be set and abuildor awithfunction to create the actual UI element. - Most builder object have a
push_for_beginfunction, that will set up the pushable to be used only for thebeginpart of the UI. This is useful for example to set up the style for a window but not for its contents.
When a function takes a value of type String this crate will usually take a generic impl IntoCStr.
This is an optimization that allows you to pass either a String, a &str, a CString or a
&CStr, avoiding an extra allocation if it is not really necessary. If you pass a constant
string and have a recent Rust compiler you can pass a literal CStr with the new syntax c"hello".
Re-exports§
pub use cgmath;pub use easy_imgui_sys;pub use image;pub use mint;
Modules§
- style
- Module with Dear Imgui Style definitions
Structs§
- Arrow
Button - Backend
Flags - Button
- Button
Flags - Checkbox
- Child
- Child
Flags - Collapsing
Header - Color
- A color is stored as a
[r, g, b, a], each value between 0.0 and 1.0. - Color
Edit3 - Color
Edit4 - Color
Edit Flags - Color
Picker3 - Color
Picker4 - Combo
- Combo
Flags - Config
Flags - Context
- The main ImGui context.
- Context
Builder - Builder for a
Context. - Current
Context - A context that we are sure is made current.
- Custom
Rect Index - Identifier for a registered custom rectangle.
- Dock
Builder - Dock
Node - Dock
Node Flags - Drag
Drop Accept Flags - Drag
Drop Payload - The payload of a drag&drop operation.
- Drag
Drop Payload Getter - Helpar class to get the drag&drop payload.
- Drag
Drop Payload Setter - Helper token class that allows to set the drag&drop payload, once.
- Drag
Drop Source Flags - Drag
Float - Drag
Float2 - Drag
Float3 - Drag
Float4 - DragInt
- Drag
Int2 - Drag
Int3 - Drag
Int4 - Draw
Flags - Event
Result - The result of processing an event in the ImGui loop.
- Focused
Flags - Font
- Font
AndSize - Font
Atlas - Font
Atlas Flags - Font
Baked - Font
Flags - Font
Glyph - FontId
- Identifier of a registered font.
- Font
Info - A font to be fed to the ImGui atlas.
- Font
Size - Glyph
Build Flags - Flags to modify the behavior of
GlyphLoaderArg::build(). - Glyph
Loader Arg - Arguments for
GlyphLoader::load_glyph. - Hovered
Flags - Id
- A string that works as a widget identifier.
- Idler
- This struct handles the main loop going to idle when there is no user input for a while.
- Image
- Image
Button - Image
With Bg - Indent
- Input
Flags - Input
Float - Input
Float2 - Input
Float3 - Input
Float4 - Input
Int - Input
Int2 - Input
Int3 - Input
Int4 - Input
OsString - Input
Text - Input
Text Flags - Input
Text Hint - Input
Text Multiline - Invisible
Button - Io
- IoMut
- Safe wrapper for
&mut Io. - Item
Flags - ItemId
- Item
Width - KeyChord
- This is an ImGuiKey plus several ImGuiMods.
- KeyMod
- LblId
- A string that works as both a label and identifier.
- ListBox
- List
Clipper - Menu
- Menu
Item - Multi
Select - Multi
Select Flags - Platform
Io - Popup
- Popup
Context Item - Popup
Context Void - Popup
Context Window - Popup
Flags - Popup
Modal - Progress
Bar - Radio
Button - RawContext
- Safe thin wrapper for ImGuiContext.
- Selectable
- Selectable
Flags - Selection
Basic Storage - Simple multi-selection storage facility.
- Selection
Basic Storage Iter - Selection
Basic Storage With Callback - Selection
External Storage - MultiSelectStorage that forwards the selection data to another place.
- Selection
Request - Size
Callback Data - Slider
Angle - Slider
Flags - Slider
Float - Slider
Float2 - Slider
Float3 - Slider
Float4 - Slider
Int - Slider
Int2 - Slider
Int3 - Slider
Int4 - Small
Button - TabBar
- TabBar
Flags - TabItem
- TabItem
Flags - Table
Column Flags - Table
Column Sort Spec - Return type for
Ui::table_get_sort_specs. - Table
Config - Table
Flags - Table
RowFlags - Text
Wrap Pos - Texture
Id - Texture
Rect - Texture
Unique Id - Tree
Node - Tree
Node Flags - Ui
Uirepresents an ImGui frame that is being built.- Viewport
- Viewport
Flags - Window
- Window
Class - Window
Draw List - Window
Flags
Enums§
- ColorId
- Cond
- Dir
- Drag
Drop Payload Cond - This is a sub-set of
Cond, only for drag&drop payloads. - Either
- The enum
Eitherwith variantsLeftandRightis a general purpose sum type with two cases. - Float
Format - How to convert a float value to a string.
- Key
- Mouse
Button - Mouse
Cursor - Same
Line - Argument for
Ui::same_line_ex(). - Selection
Request Type - Sort
Direction - Style
Value - Style
Var - Table
BgTarget - Texture
Ref
Constants§
- PAYLOAD_
TYPE_ COLOR_ 3F - PAYLOAD_
TYPE_ COLOR_ 4F - VEC2_
ZERO - A zero Vector2.
Traits§
- Glyph
Loader - Trait that implements a custom font loader.
- Hashable
- Represents any type that can be converted to a Dear ImGui hash id.
- IntoC
Str - Represents any type that can be converted into something that can be deref’ed to a
&CStr. - Multi
Select Storage - How the multi-select data will be stored.
- Pushable
- Any value that can be applied with a push function and unapplied with a pop function.
- UiBuilder
- The main trait that the user must implement to create a UI.
Functions§
- id
- Uses the given string as an ImGui id.
- im_
to_ v2 - Helper function to create a
Vector2. - im_vec2
- Helper function to create a
ImVec2. - lbl
- Uses the given string directly as an ImGui parameter that contains a label plus an id.
- lbl_id
- Uses the first string as label, the second one as id.
- raw_id
- Uses the given string as an ImGui id, without prepending
###. - to_v2
- Helper function to create a
Vector2. - v2_
to_ im - Helper function to create a
ImVec2. - vec2
- Helper function to create a
Vector2.
Type Aliases§
- ImGuiID
- ImGui
Selection User Data - Style
- Style
Color - Style
ColorF - Vector2
- A type alias of the
cgmath::Vector2<f32>.