layout/core/format.rs
1//! Defines the interfaces for accessing and querying shapes.
2
3use super::{
4 geometry::{Point, Position},
5 style::StyleAttr,
6};
7
8/// This is the trait that all elements that can be arranged need to implement.
9pub trait Visible {
10 /// \return the Position of the shape.
11 fn position(&self) -> Position;
12 /// \return the mutable reference to the Position of the shape.
13 fn position_mut(&mut self) -> &mut Position;
14 /// Return true if the element is a connector.
15 fn is_connector(&self) -> bool;
16 /// Swap the coordinates of the location and size.
17 fn transpose(&mut self);
18 /// Update the size of the shape.
19 fn resize(&mut self);
20}
21/// This is the trait that all elements that can be rendered on a canvas need to
22/// implement.
23pub trait Renderable {
24 /// Render the shape into a canvas.
25 /// If \p debug is set then extra markers will be rendered.
26 fn render(&self, debug: bool, canvas: &mut dyn RenderBackend);
27
28 /// \Return the coordinate for the connection point of an arrow that's
29 /// coming from the direction of \p from.
30 /// The format of the path is (x, y, cx, cy), where cx and cy, are the
31 /// control points of the bezier curve.
32 /// \p force is the magnitude of the edge direction.
33 /// \p port is the optional port name (for named records).
34 fn get_connector_location(
35 &self,
36 from: Point,
37 force: f64,
38 port: &Option<String>,
39 ) -> (Point, Point);
40
41 /// Computes the coordinate for the connection point of an arrow that's
42 /// passing through this edge.
43 /// coming from the direction of \p from.
44 /// \returns the bezier path in the format (x, y, cx, cy), where cx and cy,
45 /// are the control points for the entry path of the bezier curve. The exit
46 /// path is assumed to be the mirror point for the center (first point).
47 /// \p force is the magnitude of the edge direction.
48 /// This works with the get_connector_location method for drawing edges.
49 fn get_passthrough_path(
50 &self,
51 from: Point,
52 to: Point,
53 force: f64,
54 ) -> (Point, Point);
55}
56
57pub type ClipHandle = usize;
58
59/// This is the trait that all rendering backends need to implement.
60pub trait RenderBackend {
61 /// Draw a rectangle. The top-left point of the rectangle is \p xy. The shape
62 /// style (color, edge-width) are passed in \p look. The parameter \p clip
63 /// is an optional clip region (see: create_clip).
64 fn draw_rect(
65 &mut self,
66 xy: Point,
67 size: Point,
68 look: &StyleAttr,
69 properties: Option<String>,
70 clip: Option<ClipHandle>,
71 );
72
73 /// Draw a line between \p start and \p stop.
74 fn draw_line(
75 &mut self,
76 start: Point,
77 stop: Point,
78 look: &StyleAttr,
79 properties: Option<String>,
80 );
81
82 /// Draw an ellipse with the center \p xy, and size \p size.
83 fn draw_circle(
84 &mut self,
85 xy: Point,
86 size: Point,
87 look: &StyleAttr,
88 properties: Option<String>,
89 );
90
91 /// Draw a labe.
92 fn draw_text(&mut self, xy: Point, text: &str, look: &StyleAttr);
93
94 /// Draw an arrow, with a label, with the style parameters in \p look.
95 fn draw_arrow(
96 &mut self,
97 path: &[(Point, Point)],
98 dashed: bool,
99 head: (bool, bool),
100 look: &StyleAttr,
101 properties: Option<String>,
102 text: &str,
103 );
104
105 /// Generate a clip region that shapes can use to create complex shapes.
106 fn create_clip(
107 &mut self,
108 xy: Point,
109 size: Point,
110 rounded_px: usize,
111 ) -> ClipHandle;
112}