pn_editor_core/
renderer.rs

1use text_editing::TextLine;
2
3use crate::{state_info::CallableState, Editor};
4
5/// Represents from which type of element to which type of element an arrow should be drawn.
6pub enum ArrowKind {
7    /// An arrow from a place to a transition.
8    PlaceToTransition,
9    /// An arrow from a place to a placeplace.
10    TransitionToPlace,
11    /// An arrow from a place to a point.
12    PlaceToPoint,
13    /// An arrow from a point to a place.
14    PointToPlace,
15    /// An arrow from a transition to a point.
16    TransitionToPoint,
17    /// An arrow from a point to a transition.
18    PointToTransition,
19}
20
21/// The renderer trait is used to display the elements of the petri net in a specific way.
22///
23/// Every drawing method starts with `draw_` and takes the `context` as the first argument.
24/// The positions are always given relative to the center without zoom applied.
25/// Zoom has to be applied by the renderer.
26pub trait Renderer {
27    /// Renders some indications if simulation mode is enabled.
28    fn draw_simulated(&mut self, simulated: Option<usize>);
29
30    /// This draws a place of the petri net.
31    ///
32    /// * `pos`: The center of the place.
33    /// * `radius`: The radius of the place.
34    /// * `zoom`: The zoom used for rendering.
35    /// * `name`: The name of the place as char array.
36    /// * `cursor`: The position of the cursor inside the name. If no position is supplied, the cussor isn't shown.
37    /// * `count`:
38    ///     The token count of the place.
39    ///     A different visualization is recommended if it contains at least one token.
40    /// * `selected`: Indicates if the place is currently selected.
41    fn draw_place(
42        &mut self,
43        pos: [f32; 2],
44        radius: f32,
45        zoom: f32,
46        name: &TextLine,
47        cursor: Option<usize>,
48        count: usize,
49        selected: bool,
50    );
51
52    /// This draws a transition of the petri net.
53    ///
54    /// * `pos`: The center of the transition.
55    /// * `width`: The width of the transition.
56    /// * `height`: The height of the transition.
57    /// * `zoom`: The zoom used for rendering.
58    /// * `name`: The name of the transition as char array.
59    /// * `cursor`: The position of the cursor inside the name. If no position is supplied, the cussor isn't shown.
60    /// * `selected`: Indicates if the transition is currently selected.
61    /// * `callable`: Indicates if the transition can fire forward and backward.
62    fn draw_transition(
63        &mut self,
64        pos: [f32; 2],
65        width: f32,
66        height: f32,
67        zoom: f32,
68        name: &TextLine,
69        cursor: Option<usize>,
70        selected: bool,
71        callable: CallableState,
72    );
73
74    /// This draws a transition frame around a set of transitions.
75    ///
76    /// * `left`: The left bounds of the frame.
77    /// * `right`: The left bounds of the frame.
78    /// * `top`: The left bounds of the frame.
79    /// * `bottom`: The left bounds of the frame.
80    /// * `zoom`: The zoom used for rendering.
81    /// * `name`: The name of the frame.
82    fn draw_transition_frame(
83        &mut self,
84        left: f32,
85        right: f32,
86        top: f32,
87        bottom: f32,
88        zoom: f32,
89        name: &str,
90    );
91
92    /// This draws a line from position `from` to position `to`.
93    /// Lines are used for visualizing nodes with multiple instances.
94    fn draw_line(&mut self, from: [f32; 2], to: [f32; 2], zoom: f32);
95
96    /// This draws an arrow from an element at position `from` to an element at position `to`.
97    /// An element is either a place, a transition or a point.
98    /// Both elements are of a different type. The element types are specified by `arrow_kind`.
99    /// Depending on the element type, there might need to be different offsets or different curvatures.
100    fn draw_arrow(
101        &mut self,
102        from: [f32; 2],
103        to: [f32; 2],
104        radius: f32,
105        width: f32,
106        height: f32,
107        zoom: f32,
108        arrow_kind: ArrowKind,
109    );
110
111    /// This draws a global input text field. The position is not further specified, so it can be anywhere.
112    ///
113    /// * `text`: The text in the field.
114    /// * `cursor`: The position of the cursor inside the text.
115    fn draw_field(&mut self, text: &TextLine, cursor: usize);
116
117    /// This draws a box for when the user selects multiple objects using a box.
118    /// `corner1` and `corner2` are the positions of two opposite corners of the box.
119    fn draw_select_box(&mut self, corner1: [f32; 2], corner2: [f32; 2], zoom: f32);
120
121    /// This draws a text box, which might contains multiple lines of text.
122    ///
123    /// * `text`: The text as a list of lines, which are each represented as a list of lines.
124    /// * `cursor`: The cursor position on the specified line.
125    /// * `line`: The line, where the cursor currently is.
126    /// * `offset`: Indicates by how many lines the text is moved up.
127    fn draw_text_box(&mut self, text: &[TextLine], cursor: usize, line: usize, offset: usize);
128
129    /// Finishes rendering of a single frame.
130    fn finish(&mut self, editor: &Editor);
131}