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};
37use std::ops::{Deref, DerefMut};
38
39/// Canvas widget allows its children to have an arbitrary position on an imaginable infinite plane, it also
40/// gives the children constraints of infinite size, which forces them to take all the desired size. This widget
41/// could be used when you need to have an ability to put widgets at arbitrary positions. Canvas widget is the
42/// root widget of the widget hierarchy used in `fyrox-ui`.
43///
44/// ## Examples
45///
46/// A instance of [`Canvas`] widget can be created using [`CanvasBuilder`] with a set of children widgets provided
47/// to [`WidgetBuilder`]:
48///
49/// ```rust
50/// # use fyrox_ui::{
51/// # button::ButtonBuilder, canvas::CanvasBuilder, core::pool::Handle, text::TextBuilder,
52/// # widget::WidgetBuilder, BuildContext, UiNode,
53/// # };
54/// #
55/// fn create_canvas(ctx: &mut BuildContext) -> Handle<UiNode> {
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 .into()
87 })
88 .with_group("Layout")
89 }
90}
91
92crate::define_widget_deref!(Canvas);
93
94impl Control for Canvas {
95 fn measure_override(&self, ui: &UserInterface, _available_size: Vector2<f32>) -> Vector2<f32> {
96 let size_for_child = Vector2::new(f32::INFINITY, f32::INFINITY);
97
98 for child_handle in self.widget.children() {
99 ui.measure_node(*child_handle, size_for_child);
100 }
101
102 Vector2::default()
103 }
104
105 fn arrange_override(&self, ui: &UserInterface, final_size: Vector2<f32>) -> Vector2<f32> {
106 for &child_handle in self.widget.children() {
107 let child = ui.nodes.borrow(child_handle);
108 ui.arrange_node(
109 child_handle,
110 &Rect::new(
111 child.desired_local_position().x,
112 child.desired_local_position().y,
113 child.desired_size().x,
114 child.desired_size().y,
115 ),
116 );
117 }
118
119 final_size
120 }
121
122 fn handle_routed_message(&mut self, ui: &mut UserInterface, message: &mut UiMessage) {
123 self.widget.handle_routed_message(ui, message);
124 }
125}
126
127/// Canvas builder creates new [`Canvas`] widget instances and adds them to the user interface.
128pub struct CanvasBuilder {
129 widget_builder: WidgetBuilder,
130}
131
132impl CanvasBuilder {
133 /// Creates new builder instance.
134 pub fn new(widget_builder: WidgetBuilder) -> Self {
135 Self { widget_builder }
136 }
137
138 /// Finishes canvas widget building and adds the instance to the user interface and returns its handle.
139 pub fn build(self, ctx: &mut BuildContext) -> Handle<UiNode> {
140 let canvas = Canvas {
141 widget: self.widget_builder.build(ctx),
142 };
143 ctx.add_node(UiNode::new(canvas))
144 }
145}
146
147#[cfg(test)]
148mod test {
149 use crate::canvas::CanvasBuilder;
150 use crate::{test::test_widget_deletion, widget::WidgetBuilder};
151
152 #[test]
153 fn test_deletion() {
154 test_widget_deletion(|ctx| CanvasBuilder::new(WidgetBuilder::new()).build(ctx));
155 }
156}