fyrox_ui/
build.rs

1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Build context is used to decouple explicit UI state modification. See [`BuildContext`] docs for
22//! more info.
23
24use crate::style::resource::StyleResource;
25use crate::{
26    core::pool::Handle, font::FontResource, message::UiMessage, RestrictionEntry, UiNode,
27    UserInterface,
28};
29use fyrox_graph::BaseSceneGraph;
30use std::{
31    ops::{Index, IndexMut},
32    sync::mpsc::Sender,
33};
34
35/// Build context is used to decouple explicit UI state modification. Its main use is in the various widget
36/// builders. Internally, it is just a mutable reference to the UI state. UI can be modified (add nodes, clone,
37/// link, etc.) via build context. This is needed to explicitly highlight that it used to modify the UI
38/// state. It is **not recommended** to use BuildContext for mutable access to widgets at runtime! _Use message
39/// passing_ to modify widgets at runtime, otherwise you will easily break invariant (inner state) of widgets.
40/// The only place where it's allowed to directly mutate widget's state is at build stage (inside `build`
41/// method of your widget builder).
42///
43/// ## Examples
44///
45/// ```rust
46/// # use fyrox_ui::{
47/// #     core::pool::Handle,
48/// #     core::{visitor::prelude::*, reflect::prelude::*, type_traits::prelude::*,},
49/// #     define_widget_deref,
50/// #     message::UiMessage,
51/// #     widget::{Widget, WidgetBuilder},
52/// #     BuildContext, Control, UiNode, UserInterface,
53/// # };
54/// # use std::{
55/// #     any::{Any, TypeId},
56/// #     ops::{Deref, DerefMut},
57/// # };
58/// # use fyrox_core::uuid_provider;
59/// #
60/// #[derive(Clone, Visit, Reflect, Debug, ComponentProvider)]
61/// struct MyWidget {
62///     widget: Widget,
63/// }
64/// #
65/// # define_widget_deref!(MyWidget);
66/// #
67/// # uuid_provider!(MyWidget = "a93ec1b5-e7c8-4919-ac19-687d8c99f6bd");
68/// #
69/// # impl Control for MyWidget {
70/// #     fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
71/// #         todo!()
72/// #     }
73/// # }
74///
75/// struct MyWidgetBuilder {
76///     widget_builder: WidgetBuilder,
77/// }
78///
79/// impl MyWidgetBuilder {
80///     pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
81///         let my_widget = MyWidget {
82///             widget: self.widget_builder.build(ctx),
83///         };
84///
85///         ctx.add_node(UiNode::new(my_widget))
86///     }
87/// }
88/// ```
89pub struct BuildContext<'a> {
90    ui: &'a mut UserInterface,
91    pub style: StyleResource,
92}
93
94impl Index<Handle<UiNode>> for BuildContext<'_> {
95    type Output = UiNode;
96
97    fn index(&self, index: Handle<UiNode>) -> &Self::Output {
98        &self.ui.nodes[index]
99    }
100}
101
102impl IndexMut<Handle<UiNode>> for BuildContext<'_> {
103    fn index_mut(&mut self, index: Handle<UiNode>) -> &mut Self::Output {
104        &mut self.ui.nodes[index]
105    }
106}
107
108impl<'a> From<&'a mut UserInterface> for BuildContext<'a> {
109    fn from(ui: &'a mut UserInterface) -> Self {
110        Self {
111            style: ui.style.clone(),
112            ui,
113        }
114    }
115}
116
117impl BuildContext<'_> {
118    /// Returns default font instance used by the UI.
119    pub fn default_font(&self) -> FontResource {
120        self.ui.default_font.clone()
121    }
122
123    /// Returns current message sender of the UI, that is used for message passing mechanism. You can
124    /// send messages for your widgets inside your builders, however this has limited use and should
125    /// be avoided in the favor of explicit state modification to not overload message pipeline.
126    pub fn sender(&self) -> Sender<UiMessage> {
127        self.ui.sender()
128    }
129
130    /// Adds a new widget to the UI. See [`UiNode`] docs for more info, [`UiNode::new`] in particular.
131    pub fn add_node(&mut self, node: UiNode) -> Handle<UiNode> {
132        self.ui.add_node(node)
133    }
134
135    /// Links the child widget with the parent widget. Child widget's position and size will be restricted by
136    /// the new parent. When a widget is linked to other widget, its coordinates become relative to it parent.
137    pub fn link(&mut self, child: Handle<UiNode>, parent: Handle<UiNode>) {
138        self.ui.link_nodes(child, parent, false)
139    }
140
141    /// Copies a widget, adds it to the UI, links it to the root node of the UI and returns the handle to it.
142    pub fn copy(&mut self, node: Handle<UiNode>) -> Handle<UiNode> {
143        self.ui.copy_node(node)
144    }
145
146    /// Tries to fetch the node by its handle. Returns `None` if the handle is invalid.
147    pub fn try_get_node(&self, node: Handle<UiNode>) -> Option<&UiNode> {
148        self.ui.try_get(node)
149    }
150
151    /// Tries to fetch the node by its handle. Returns `None` if the handle is invalid.
152    pub fn try_get_node_mut(&mut self, node: Handle<UiNode>) -> Option<&mut UiNode> {
153        self.ui.nodes.try_borrow_mut(node)
154    }
155
156    /// Pushes a new picking restriction to the picking-restriction stack. See [`UserInterface::push_picking_restriction`]
157    /// docs for more info.
158    pub fn push_picking_restriction(&mut self, restriction: RestrictionEntry) {
159        self.ui.push_picking_restriction(restriction)
160    }
161
162    /// Explicitly removes picking restriction for the given node from the picking-restriction stack. See
163    /// [`UserInterface::remove_picking_restriction`] docs for more info.
164    pub fn remove_picking_restriction(&mut self, node: Handle<UiNode>) {
165        self.ui.remove_picking_restriction(node)
166    }
167
168    /// Returns an immutable reference to the user interface.
169    pub fn inner(&self) -> &UserInterface {
170        self.ui
171    }
172
173    /// Returns a mutable reference to the user interface.
174    pub fn inner_mut(&mut self) -> &mut UserInterface {
175        self.ui
176    }
177
178    /// Sends a message during build stage. It has quite limited use, but could be unavoidable in
179    /// for cases when you need to do some action that relies on fully performed layout stage. When a
180    /// widget is being built, you can't fetch any layout info of it since it wasn't calculated yet.
181    /// In this case all you can do is to "postpone" your action for later moment in current frame
182    /// by sending a message.
183    pub fn send_message(&self, message: UiMessage) {
184        self.ui.send_message(message);
185    }
186}