dear_node_editor/frame/
core.rs1use super::validation::assert_finite_vec2;
2use crate::{EditorContext, context::CurrentEditorGuard, sys, vec2};
3use dear_imgui_rs::Ui;
4use std::{cell::Cell, ffi::CString};
5
6pub struct NodeEditorFrame<'ui> {
8 pub(super) _ui: &'ui Ui,
9 pub(super) _editor: &'ui EditorContext,
10 pub(super) suspended: Cell<bool>,
11 pub(super) ended: bool,
12}
13
14impl<'ui> NodeEditorFrame<'ui> {
15 pub(crate) fn new(
16 ui: &'ui Ui,
17 editor: &'ui EditorContext,
18 id: impl AsRef<str>,
19 size: [f32; 2],
20 ) -> Self {
21 assert_finite_vec2("Ui::node_editor()", "size", size);
22 let id = CString::new(id.as_ref()).expect("node editor id cannot contain NUL bytes");
23 {
24 let _current_editor = editor.bind_current("Ui::node_editor");
25 unsafe { sys::dne_begin(id.as_ptr(), vec2(size)) };
26 }
27 Self {
28 _ui: ui,
29 _editor: editor,
30 suspended: Cell::new(false),
31 ended: false,
32 }
33 }
34
35 pub(super) fn bind(&self, caller: &str) -> CurrentEditorGuard<'_> {
36 self._editor.bind_current(caller)
37 }
38
39 pub fn end(mut self) {
40 self.end_inner();
41 }
42
43 fn end_inner(&mut self) {
44 if !self.ended {
45 let _current_editor = self._editor.bind_current("NodeEditorFrame::end()");
46 unsafe { sys::dne_end() };
47 self.ended = true;
48 }
49 }
50}
51
52impl Drop for NodeEditorFrame<'_> {
53 fn drop(&mut self) {
54 self.end_inner();
55 }
56}