godoru 0.1.0

UI Framework for Rust using Godot
use super::*;

use std::ptr;

pub(crate) struct GodotUiTree<A = crate::NoAction> {
    pub(crate) bridge: GodotBridge,
    pub(crate) root: GodotObject,
    pub(crate) resources: Vec<GodotObject>,
    pub(crate) interactiveNodes: Vec<InteractiveNode<A>>,
}

impl<A: Clone + 'static> GodotUiTree<A> {
    pub(crate) fn queueFreeForShutdown(&mut self) {
        self.resources.clear();
        self.interactiveNodes.clear();
        if !self.root.0.is_null() {
            let _ = self.bridge.queueFree(self.root);
        }
        self.root = GodotObject(ptr::null_mut());
    }

    pub(crate) fn replaceUi(&mut self, root: &Container<A>, theme: &AppTheme) -> GodoruResult<()> {
        for node in &self.interactiveNodes {
            if !node.object.0.is_null() {
                let _ = self.bridge.releaseFocus(node.object);
            }
        }
        self.resources.clear();
        self.interactiveNodes.clear();
        if !self.root.0.is_null() {
            let _ = self.bridge.queueFree(self.root);
        }
        let mut next = self.bridge.mountUiReady(root, theme)?;
        self.root = next.root;
        next.root = GodotObject(ptr::null_mut());
        self.resources = std::mem::take(&mut next.resources);
        self.interactiveNodes = std::mem::take(&mut next.interactiveNodes);
        Ok(())
    }
}