1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Derive macro that allows you to quickly build immediate mode UIs (based on
//! the [imgui] crate).
//!
//! [imgui]: https://crates.io/crates/imgui
//!
//! # Basic usage
//!
//! ```
//! #[derive(imgui_ext::Gui)]
//! struct Example {
//!     #[imgui(slider(min = 0.0, max = 4.0))]
//!     x: f32,
//!     #[imgui(input(step = 2))]
//!     y: i32,
//!     #[imgui(drag(label = "Drag 2D"))]
//!     drag_2d: [f32; 2],
//!     #[imgui(checkbox(label = "Turbo mode"), display(label = "Is turbo enabled?"))]
//!     turbo: bool,
//! }
//! ```
//!
//! ![][result]
//!
//! [result]: https://i.imgur.com/Xrl1Nt0.png
//!
//! # Input events
//!
//! Rendering a UI with `imgui` & `imgui-ext` returns a type with all the
//! triggered input events (which are generally stored as booleans):
//!
//! ```
//! use imgui_ext::UiExt;
//!
//! #[derive(imgui_ext::Gui)]
//! struct Example {
//!     #[imgui(checkbox(label = "Checkbox"))]
//!     check: bool,
//! }
//!
//! # struct A;
//! # struct B;
//! # impl A { fn draw_gui<T>(&self, _: &mut T) -> B { B } }
//! # impl B { fn check(&self) -> bool { true } }
//! # let ui = A;
//! let mut example = Example { check: false };
//!
//! let events = ui.draw_gui(&mut example);
//!
//! if events.check() {
//!     println!("checkbox state changed.");
//! }
//! ```
//!
//! The checkbox event is mapped to the method `check` on the returned type. The
//! name of the field & method on the returned type matches the name of the
//! field from the UI type.
//!
//! You can override this default naming by defining the "catch" attribute on
//! the annotation (all widgets support this attribute, not just checkbox):
//!
//! ```no_run
//! use imgui_ext::UiExt;
//!
//! #[derive(imgui_ext::Gui)]
//! struct Example {
//!     #[imgui(checkbox(label = "Checkbox", catch = "checkbox_event"))]
//!     check: bool,
//! }
//!
//! # struct A;
//! # struct B;
//! # impl A { fn draw_gui<T>(&self, _: &mut T) -> B { B } }
//! # impl B { fn checkbox_event(&self) -> bool { true } }
//! # let ui = A;
//! let mut example = Example { check: false };
//!
//! let events = ui.draw_gui(&mut example);
//!
//! if events.checkbox_event() {
//!     println!("checkbox state changed.");
//! }
//! ```
//!
//! [repo]: https://github.com/germangb/imgui-ext
#![deny(warnings)]

use imgui::Ui;

pub use imgui_ext_derive::Gui;

include!("macros.rs");

/// `vars(...)` docs.
pub mod vars {
    //!
    //! Pushes style and color parameters to the stack, so they can be applied
    //! to the widgets contained in the annotation.
    //!
    //! This is a rather complex annotation. It's not meant to be used
    //! extensively though..
    //!
    //! # Params
    //!
    //! - `content(...)` widgets inside this annotation will have the style and
    //!   color params applied.
    //!
    //! # Optional params
    //!
    //! - `style = "..."` path to a function that returns the style parameters.
    //! - `color = "..."` path to a function that returns the color parameters.
    //!
    //! # Example
    //!
    //! ```
    //! use imgui::{ImGuiCol, StyleVar};
    //!
    //! #[derive(imgui_ext::Gui)]
    //! struct Example {
    //!     #[imgui(vars(
    //!         style = "example_style",
    //!         color = "example_color",
    //!         content(
    //!             input(label = "foo##input"),
    //!             drag(label = "foo##drag"),
    //!             slider(label = "foo##slider", min = "-1.0", max = "1.0")
    //!         )
    //!     ))]
    //!     foo: f32,
    //! }
    //!
    //! fn example_style() -> &'static [StyleVar] {
    //!     &[StyleVar::FrameRounding(4.0)]
    //! }
    //!
    //! fn example_color() -> &'static [(ImGuiCol, [f32; 4])] {
    //!     &[(ImGuiCol::Button, [1.0, 0.0, 1.0, 1.0])]
    //! }
    //! ```
    //!
    //! ## Result
    //!
    //! ![](https://i.imgur.com/TMmjOUg.png)
}
/// `tree(...)` docs.
pub mod tree {
    //!
    //! Annotation to build static Tree UIs.
    //!
    //! This is a rather complex annotation. It's not meant to be used
    //! extensively though..
    //!
    //! # Optional params
    //!
    //! - `label = ".."` Node label
    //! - `flags = ".."` Local function returning [`ImGuiTreeNodeFlags`]
    //! - `node(..)` Nested content (any of the supported annotations).
    //! - `cond` One of the [`ImGuiCond`] variants.
    //!
    //! [`ImGuiCond`]: https://docs.rs/imgui/*/imgui/struct.ImGuiCond.html
    //! [`ImGuiTreeNodeFlags`]: https://docs.rs/imgui/*/imgui/struct.ImGuiTreeNodeFlags.html
    //!
    //! # Example
    //!
    //! ```
    //! use imgui::{ImGuiTreeNodeFlags, ImString};
    //!
    //! #[derive(imgui_ext::Gui)]
    //! pub struct Tree {
    //!     #[imgui(tree(
    //!         label = "Sliders",
    //!         cond = "FirstUseEver",
    //!         flags = "flags",
    //!         node(nested)
    //!     ))]
    //!     sliders: Sliders,
    //!     #[imgui(tree(label = "Inputs", flags = "flags", node(nested)))]
    //!     inputs: Inputs,
    //!     #[imgui(tree(label = "Color picker", flags = "flags", node(color(picker))))]
    //!     color: [f32; 3],
    //! }
    //!
    //! fn flags() -> ImGuiTreeNodeFlags {
    //!     ImGuiTreeNodeFlags::Framed
    //! }
    //!
    //! #[derive(imgui_ext::Gui)]
    //! pub struct Sliders {
    //!     #[imgui(text("Slider widgets:"), slider(min = 0.0, max = 3.0))]
    //!     s1: f32,
    //!     #[imgui(slider(min = "-4", max = 4))]
    //!     s2: [i32; 3],
    //!     #[imgui(slider(min = "-1.0", max = 1.0))]
    //!     s3: [f64; 2],
    //! }
    //!
    //! #[derive(imgui_ext::Gui)]
    //! pub struct Inputs {
    //!     #[imgui(text("Input widgets:"), input)]
    //!     i1: f32,
    //!     #[imgui(input)]
    //!     i2: imgui::ImString,
    //!     #[imgui(input)]
    //!     i3: [f32; 8],
    //! }
    //! ```
    //!
    //! # Result
    //!
    //! ![](https://i.imgur.com/Rn2RJJG.png)
}
/// `checkbox(...)` docs.
pub mod checkbox;
/// `color(...)` docs.
pub mod color;
/// `drag(...)` docs.
pub mod drag;
/// `image(...)` docs.
pub mod image;
/// `image_button(...)` docs.
pub mod image_button;
/// `input(...)` docs.
pub mod input;
/// `progress(...)` docs.
pub mod progress;
/// `slider(...)` docs.
pub mod slider;
/// `text(...)` & `text_wrap(...)` docs.
pub mod text {
    //!
    //! # Variants
    //!
    //! - `text(...)`
    //! - `text_wrap(...)` Same as `text(...)`, but the text content wraps
    //!
    //! # Params
    //!
    //! * `lit` Literal text. If this value is set, this value is displayed
    //!   instead of the annotated type.
    //!
    //! You can also write this annotation as:
    //!
    //! * `#[imgui(text("literal..."))]`
    //!
    //! which is a shorthand form for `text(lit = "literal...")`.
    //!
    //! # Example
    //!
    //! ```
    //! #[derive(imgui_ext::Gui)]
    //! struct Example {
    //!     #[imgui(text_wrap("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc metus sem, facilisis hendrerit elementum et, egestas."),
    //!             separator(),
    //!             text("Input num:"),
    //!             slider(min = "-1.0", max = 1.0),
    //!             button(label = "Submit"))]
    //!     number: f32,
    //! }
    //! ```
    //!
    //! # Result
    //!
    //! ![](https://i.imgur.com/0uvMFIm.png)
}
pub mod misc {
    //! []()
    //!
    //! * `#[imgui(separator)]` inserts a separator
    //! * `#[imgui(new_line)]` inserts an empty line
}
/// `display(...)` docs.
pub mod display {
    //!
    //! `display(...)` is used to display a field.
    //!
    //! # Optional fields
    //!
    //! * `label`
    //! * `display` formatted text (followed by the parameters).
    //!
    //! # Example
    //!
    //! ```
    //! #[derive(imgui_ext::Gui)]
    //! struct Labels {
    //!     #[imgui(display)]
    //!     foo: f32,
    //!
    //!     // Use inner fields to format the text.
    //!     #[imgui(display(label = "Tuple", display = "({}, {}, {})", 0, 1, 2))]
    //!     bar: (f32, bool, usize),
    //!
    //!     // when display() is the only annotation, it can be abbreviated:
    //!     #[imgui(label = "String param")]
    //!     baz: String,
    //! }
    //! ```
    //!
    //! ![][result]
    //!
    //! [result]: https://i.imgur.com/Wf4Uze7.png
}
/// `nested(...)` docs (used to build nested UIs).
pub mod nested {
    //!
    //! Types that `#[derive(Ui)]` can be nested.
    //!
    //! # Optional fields
    //!
    //! * `catch`
    //!
    //! # Example
    //!
    //! ```
    //! #[derive(imgui_ext::Gui)]
    //! struct Form {
    //!     #[imgui(input)]
    //!     user: imgui::ImString,
    //!     #[imgui(
    //!         input(flags = "passwd_flags"),
    //!         button(label = "Login", catch = "login_btn")
    //!     )]
    //!     passwd: imgui::ImString,
    //! }
    //!
    //! fn passwd_flags() -> imgui::ImGuiInputTextFlags {
    //!     imgui::ImGuiInputTextFlags::Password
    //! }
    //!
    //! #[derive(imgui_ext::Gui)]
    //! struct Example {
    //!     #[imgui(nested, separator)]
    //!     login_form: Form,
    //!     #[imgui(checkbox(label = "Remember login?"))]
    //!     remember: bool,
    //! }
    //! ```
    //!
    //! ## Result
    //!
    //! ![][result]
    //!
    //! [result]: https://i.imgur.com/l6omyf4.png
    //!
    //! # Nested input events
    //!
    //! You can access input events from nested UIs:
    //!
    //! ```ignore
    //! // initialize imgui (ui) ...
    //!
    //! let mut example = Example { ... };
    //! let events: Events<Example> = ui.imgui_ext(&mut example);
    //!
    //! if events.login_form().login_btn() {
    //!     validate_user(
    //!         &example.login_form.user,
    //!         &example.login_form.passwd,
    //!     )
    //! }
    //! ```
}
/// `button(...)` docs.
pub mod button {
    //!
    //! `button(...)` is not associated to any particular type, but its position
    //! within an annotation will determine where it is rendered in the
    //! final UI.
    //!
    //! # Fields
    //!
    //! - `label`
    //!
    //! # Optional fields
    //!
    //! - `size` path to a function that returns the button size.
    //! - `catch`
    //!
    //! # Example
    //!
    //! ```
    //! use imgui_ext::UiExt;
    //!
    //! #[derive(imgui_ext::Gui)]
    //! struct Button {
    //!     #[imgui(
    //!         button(size = "button_size", label = "Click me!", catch = "click"),
    //!         separator,
    //!         display(label = "Clicks")
    //!     )]
    //!     count: i32,
    //! }
    //!
    //! const fn button_size() -> (f32, f32) {
    //!     (100.0, 20.0)
    //! }
    //!
    //! # struct A;
    //! # struct B;
    //! # impl A { fn draw_gui<T>(&self, _: &mut T) -> B { B } }
    //! # impl B { fn click(&self) -> bool { true } }
    //! # let ui = A;
    //! let mut buttons = Button { count: 0 };
    //!
    //! let events = ui.draw_gui(&mut buttons);
    //!
    //! if events.click() {
    //!     buttons.count += 1;
    //! }
    //! ```
    //!
    //! ![][image]
    //!
    //! [image]: https://i.imgur.com/PpOcZK8.png
}
/// `bullet(...)` docs.
pub mod bullet {
    //!
    //! Used to build bulleted lists. It has two variants:
    //!
    //! * `bullet(text = "...")` Bullet text.
    //! * `bullet(...)` Nested.
    //!
    //! [issue]: #
    //!
    //! # Example
    //!
    //! ```
    //! #[derive(imgui_ext::Gui)]
    //! struct Bullet {
    //!     #[imgui(
    //!         bullet(text = "Be nice to others."),
    //!         bullet(text = "Don't repeat your password"),
    //!         bullet(text = "Kill all humans."),
    //!         bullet(slider(min = 0.0, max = 1.0))
    //!     )]
    //!     foo: f32,
    //! }
    //! ```
    //!
    //! ## Result
    //!
    //! ![][result]
    //!
    //! [result]: https://i.imgur.com/CLPl993.png
}

/// Trait implemented by the derive macro.
pub trait Gui {
    type Events;
    fn draw_gui(ui: &Ui, ext: &mut Self) -> Self::Events;
}

impl<T: Gui> Gui for Option<T> {
    type Events = T::Events;

    // TODO remove unsafe
    fn draw_gui(ui: &Ui, ext: &mut Self) -> Self::Events {
        if let Some(ref mut ext) = ext {
            T::draw_gui(ui, ext)
        } else {
            unsafe { std::mem::zeroed() }
        }
    }
}

impl<T: Gui> Gui for Box<T> {
    type Events = T::Events;
    #[inline]
    fn draw_gui(ui: &Ui, ext: &mut Self) -> Self::Events {
        T::draw_gui(ui, ext.as_mut())
    }
}

/// Extension trait for imgui's [`Ui`](https://docs.rs/imgui/*/imgui/struct.Ui.html).
///
/// ```
/// use imgui_ext::UiExt;
///
/// #[derive(imgui_ext::Gui)]
/// struct Example {
///     // ...
/// }
///
/// # struct A;
/// # struct B;
/// # impl A { fn draw_gui<T>(&self, _: &mut T) -> B { B } }
/// # impl B { fn click(&self) -> bool { true } }
/// # fn init_imgui() -> A { A }
///
/// // Initialize the imgui crate...
/// let ui = init_imgui();
///
/// // initialize Example...
/// let mut example = Example { /* ... */ };
///
/// ui.draw_gui(&mut example);
/// ```
pub trait UiExt {
    fn draw_gui<U: Gui>(&self, ext: &mut U) -> U::Events;
}

impl UiExt for Ui<'_> {
    #[inline]
    fn draw_gui<U: Gui>(&self, ext: &mut U) -> U::Events {
        U::draw_gui(self, ext)
    }
}