Skip to main content

fyrox_ui/
canvas.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//! Canvas widget allows its children to have an arbitrary position on an imaginable infinite plane, it also
22//! gives the children constraints of infinite size, which forces them to take all the desired size. See
23//! [`Canvas`] docs for more info and usage examples.
24
25#![warn(missing_docs)]
26
27use crate::{
28    core::{
29        algebra::Vector2, math::Rect, pool::Handle, reflect::prelude::*, type_traits::prelude::*,
30        visitor::prelude::*,
31    },
32    message::UiMessage,
33    widget::{Widget, WidgetBuilder},
34    BuildContext, Control, UiNode, UserInterface,
35};
36use fyrox_graph::constructor::{ConstructorProvider, GraphNodeConstructor};
37
38/// Canvas widget allows its children to have an arbitrary position on an imaginable infinite plane, it also
39/// gives the children constraints of infinite size, which forces them to take all the desired size. This widget
40/// could be used when you need to have an ability to put widgets at arbitrary positions. Canvas widget is the
41/// root widget of the widget hierarchy used in `fyrox-ui`.
42///
43/// ## Examples
44///
45/// An instance of [`Canvas`] widget can be created using [`CanvasBuilder`] with a set of children widgets provided
46/// to [`WidgetBuilder`]:
47///
48/// ```rust
49/// # use fyrox_ui::{
50/// #     button::ButtonBuilder, canvas::CanvasBuilder, core::pool::Handle, text::TextBuilder,
51/// #     widget::WidgetBuilder, BuildContext, UiNode,
52/// # };
53/// # use fyrox_ui::canvas::Canvas;
54/// #
55/// fn create_canvas(ctx: &mut BuildContext) -> Handle<Canvas> {
56///     CanvasBuilder::new(
57///         WidgetBuilder::new()
58///             .with_child(
59///                 ButtonBuilder::new(WidgetBuilder::new())
60///                     .with_text("Click me!")
61///                     .build(ctx),
62///             )
63///             .with_child(
64///                 TextBuilder::new(WidgetBuilder::new())
65///                     .with_text("Some text")
66///                     .build(ctx),
67///             ),
68///     )
69///     .build(ctx)
70/// }
71/// ```
72#[derive(Default, Clone, Visit, Reflect, Debug, TypeUuidProvider, ComponentProvider)]
73#[type_uuid(id = "6b843a36-53da-467b-b85e-2380fe891ca1")]
74#[reflect(derived_type = "UiNode")]
75pub struct Canvas {
76    /// Base widget of the canvas.
77    pub widget: Widget,
78}
79
80impl ConstructorProvider<UiNode, UserInterface> for Canvas {
81    fn constructor() -> GraphNodeConstructor<UiNode, UserInterface> {
82        GraphNodeConstructor::new::<Self>()
83            .with_variant("Canvas", |ui| {
84                CanvasBuilder::new(WidgetBuilder::new().with_name("Canvas"))
85                    .build(&mut ui.build_ctx())
86                    .to_base()
87                    .into()
88            })
89            .with_group("Layout")
90    }
91}
92
93crate::define_widget_deref!(Canvas);
94
95impl Control for Canvas {
96    fn measure_override(&self, ui: &UserInterface, _available_size: Vector2<f32>) -> Vector2<f32> {
97        let size_for_child = Vector2::new(f32::INFINITY, f32::INFINITY);
98
99        for child_handle in self.widget.children() {
100            ui.measure_node(*child_handle, size_for_child);
101        }
102
103        Vector2::default()
104    }
105
106    fn arrange_override(&self, ui: &UserInterface, final_size: Vector2<f32>) -> Vector2<f32> {
107        for &child_handle in self.widget.children() {
108            let child = ui.nodes.borrow(child_handle);
109            ui.arrange_node(
110                child_handle,
111                &Rect::new(
112                    child.desired_local_position().x,
113                    child.desired_local_position().y,
114                    child.desired_size().x,
115                    child.desired_size().y,
116                ),
117            );
118        }
119
120        final_size
121    }
122
123    fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
124        self.widget.handle_routed_message(ui, message);
125    }
126}
127
128/// Canvas builder creates new [`Canvas`] widget instances and adds them to the user interface.
129pub struct CanvasBuilder {
130    widget_builder: WidgetBuilder,
131}
132
133impl CanvasBuilder {
134    /// Creates new builder instance.
135    pub fn new(widget_builder: WidgetBuilder) -> Self {
136        Self { widget_builder }
137    }
138
139    /// Finishes canvas widget building and adds the instance to the user interface and returns its handle.
140    pub fn build(self, ctx: &mut BuildContext) -> Handle<Canvas> {
141        let canvas = Canvas {
142            widget: self.widget_builder.build(ctx),
143        };
144        ctx.add(canvas)
145    }
146}
147
148#[cfg(test)]
149mod test {
150    use crate::canvas::CanvasBuilder;
151    use crate::{test::test_widget_deletion, widget::WidgetBuilder};
152
153    #[test]
154    fn test_deletion() {
155        test_widget_deletion(|ctx| CanvasBuilder::new(WidgetBuilder::new()).build(ctx));
156    }
157}