Skip to content
This repository was archived by the owner on Apr 5, 2025. It is now read-only.

Commit d2fd043

Browse files
committed
first version push
1 parent e81f7c6 commit d2fd043

5 files changed

Lines changed: 106 additions & 17 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ license = "Apache-2.0"
1212
name = "blue_engine_imgui"
1313

1414
[dependencies]
15-
blue_engine = { path = "../Blue Engine" }
15+
blue_engine = { version = "0.4.9" }
1616
imgui-wgpu = { version = "0.20.0" }
1717
imgui-winit-support = { version = "0.8.1-alpha.0", path = "imgui-winit-support" }
1818
imgui = { version = "0.8.2" }

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Blue Engine ImGUI plugin
2+
3+
This is a plugin that adds [ImGUI](https://github.com/ocornut/imgui) support to the [Blue Engine](https://githb.com/AryanpurTech/BlueEngine).
4+
5+
## Getting started
6+
7+
To get started, create a new struct that will contain your GUI code. For our example:
8+
9+
```rust
10+
struct Counter {
11+
count: u16
12+
}
13+
```
14+
15+
Then We'll implement the `Gui` trait of the plugin:
16+
17+
```rust
18+
impl Gui for MyGUI {
19+
// This is the starting point for your UI code, it passes almost all variables of the engine as well
20+
fn update(
21+
// for accessing the values
22+
&mut self,
23+
_window: &mut blue_engine::Window,
24+
_renderer: &mut blue_engine::Renderer,
25+
// We can add underscore to ones we don't use, so they won't emit warnings
26+
objects: &mut std::collections::HashMap<&'static str, blue_engine::Object>,
27+
_input: &blue_engine::InputHelper,
28+
_camera: &mut blue_engine::Camera,
29+
ui: &blue_engine_imgui::winit::Ui,
30+
) {
31+
/* Your UI code goes here */
32+
}
33+
}
34+
```
35+
36+
And finally your ImGUI code:
37+
38+
```rust
39+
// Create a new imgui window to contain the UI
40+
gui::Window::new("Counter Window").build(ui, || {
41+
// Add a text to display the counter
42+
ui.text(format!("The count is at: {}", self.counter));\
43+
44+
// + 1 per click
45+
if ui.button("Add 1") {
46+
self.counter += 1;
47+
}
48+
});
49+
```
50+
51+
A few more steps are left to be done to have the plugin working. First we need to initialize the plugin before update loop:
52+
53+
```rust
54+
let gui_context = blue_engine_imgui::ImGUI::new(&engine.window, &mut engine.renderer);
55+
```
56+
57+
This will essentially initializes the imgui and create things required to run the plugin. Next step is inside the update loop, we'll need to update the plugin with encoder so that it can create a renderpass for displaying the UI. Here we will pass our UI struct as well:
58+
59+
```rust
60+
// Here we update the imgui context every frame
61+
event_fetchers[0].update(
62+
window,
63+
renderer,
64+
objects,
65+
input,
66+
camera,
67+
encoder,
68+
view,
69+
// This is where you add your gui struct
70+
&mut my_gui,
71+
);
72+
```
73+
74+
> The `event_fetchers` is the last component in second list in update loop: [ [ _, _, _ ] , [ _, _, _, event_fetcher ] ]
75+
76+
And finally, last step is to add the context too the list of event fetchers, so that it'll get access to all the events immediately and fetch all inputs.
77+
78+
## Style Block
79+
80+
*The guide will come soon, it's cool I promise!*
81+
82+
## Examples
83+
84+
Check the [examples](https://github.com/AryanpurTech/BlueEngineImGUI/tree/master/examples) folder for potential UIs and as template for your new projects.
85+
86+
## Dependency justification
87+
88+
* `blue_engine`: Used obiously for exporting some components and struct declrations required to design the API
89+
* `imgui-wgpu`: Used to assist in applying ImGUI to wgpu graphics backend. Which is same graphics backend used in Blue Engine.
90+
* `imgui-winit-support`: Support for Winit windowing. Which is same windowing system used in Blue Engine.
91+
* `imgui`: The imgui itself, required to obtain components and declrations for api design.

examples/hello_gui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn main() {
7474
};
7575

7676
// Start the imgui context
77-
let gui_context = blue_engine_imgui::ImGUI::init(&engine.window, &mut engine.renderer);
77+
let gui_context = blue_engine_imgui::ImGUI::new(&engine.window, &mut engine.renderer);
7878

7979
// Update loop
8080
engine

examples/styling.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/* =======================================
2+
* DO NOT USE THIS EXAMPLE YET
3+
* =======================================
4+
*/
5+
16
use blue_engine::{
27
gui,
38
gui::StyleColor, // for colors

src/lib.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use blue_engine::{
66
pub use imgui;
77
use imgui::{FontSource, Ui};
88

9+
/// Allows you to write UI code understandable by this library.
10+
/// The only function is `update` function, passing all normal components as well as `ui`.
911
pub trait Gui {
1012
fn update(
1113
&mut self,
@@ -18,6 +20,7 @@ pub trait Gui {
1820
);
1921
}
2022

23+
/// The imgui plugin
2124
pub struct ImGUI {
2225
pub context: imgui::Context,
2326
pub platform: imgui_winit_support::WinitPlatform,
@@ -26,7 +29,8 @@ pub struct ImGUI {
2629
}
2730

2831
impl ImGUI {
29-
pub fn init(window: &Win, renderer: &mut Renderer) -> Self {
32+
/// Creates the imgui context and platform details
33+
pub fn new(window: &Win, renderer: &mut Renderer) -> Self {
3034
let mut imgui = imgui::Context::create();
3135
let mut platform = imgui_winit_support::WinitPlatform::init(&mut imgui);
3236

@@ -67,20 +71,7 @@ impl ImGUI {
6771
}
6872
}
6973

70-
/*
71-
(
72-
&mut Renderer,
73-
&mut Window,
74-
&mut std::collections::HashMap<&'static str, Object>,
75-
),
76-
// Utils
77-
(
78-
&winit_input_helper::WinitInputHelper,
79-
&mut Camera,
80-
(&mut wgpu::CommandEncoder, &wgpu::TextureView),
81-
&mut Vec<T>,
82-
) */
83-
74+
/// Updates the imgui with custom renderpass and renders UI code
8475
pub fn update<T: Gui + 'static>(
8576
&mut self,
8677
window: &mut Win,
@@ -134,6 +125,7 @@ impl ImGUI {
134125
}
135126

136127
impl UpdateEvents for ImGUI {
128+
/// updates the inputs and events
137129
fn update_events<T>(
138130
&mut self,
139131
_renderer: &mut Renderer,
@@ -151,6 +143,7 @@ impl UpdateEvents for ImGUI {
151143

152144
// ===============================================================================================
153145

146+
/// custom dark theme
154147
fn imgui_redesign(imgui: &mut imgui::Context, hidpi_factor: f64) {
155148
let font_size = (13.0 * hidpi_factor) as f32;
156149

0 commit comments

Comments
 (0)