pub struct Pattern { /* private fields */ }
Expand description
Represents a pattern to be drawn on a grid
Implementations§
Source§impl Pattern
impl Pattern
Sourcepub fn new(rotation: Direction, links: Vec<Angle>) -> Self
pub fn new(rotation: Direction, links: Vec<Angle>) -> Self
Creates a new pattern with a given start direction and angle_sigs (links)
Examples found in repository?
examples/01_patterns_and_defaults.rs (line 46)
7fn main() {
8 //In order to draw a set of patterns, they need to be in a readable format first
9 //the simplest format is just a vector of Patterns.
10
11 //there is some built in parsing for strings, however it can only take in
12 //one pattern at a time and accepts the following formats:
13 // <starting_direction> <angle_sigs>
14 // hexpattern(<starting_direction> <angle_sigs>)
15
16 //where starting_direction can be formated in any of the following ways:
17 // -- north_east | northeast | ne -- caps are ignored
18 //and angle sigs are any of the following characters: weqdas
19 let patterns =
20 "HexPattern(north_east qqq), HexPattern(north_east qqq), HexPattern(north_east qqq)";
21
22 //to convert from a string of the above format into a list of patterns
23 //you can split it into each pattern and then try to convert it using Pattern::try_from
24 let patterns = patterns
25 .split(", ")
26 .map(Pattern::try_from)
27 .collect::<Result<Vec<Pattern>, _>>()
28 .expect("Invalid Pattern List!");
29
30 //alternatively, you can build the patterns by hand
31 //angle sigs are mapped as followed:
32 // w - Forward
33 // e - Right
34 // d - BackRight
35 // s - Backwards
36 // a - BackLeft
37 // q - Left
38
39 let starting_direction = pattern_utils::Direction::NorthEast;
40 let angle_sigs = vec![
41 pattern_utils::Angle::Left,
42 pattern_utils::Angle::Left,
43 pattern_utils::Angle::Left,
44 ];
45
46 let built_pattern = Pattern::new(starting_direction, angle_sigs);
47
48 let built_pattern_list = vec![built_pattern.clone(), built_pattern.clone(), built_pattern];
49
50 //another important component for drawing the patterns is their configuration
51 //for this, one of the default options will be used, but custom configuration
52 //will be in the next example
53
54 //there are 3 different types of pattern rendering as follows
55 // monocolor - simply one color
56 // gradient - transitions from one color to another
57 // segment - switches colors on conflict
58
59 //each of the above types have their own set of defaults. Including their base and below
60
61 //such as pointed-gradient which just adds points to the gradients,
62 //a bent_monocolor that adds bends to the corners of the monocolor renderer
63 //and uniform variants of each which don't change color
64 // when going inside nested intro/retro blocks
65
66 let monocolor = &hex_renderer::defaults::MONOCOLOR;
67 let gradient = &hex_renderer::defaults::GRADIENT;
68 let segment = &hex_renderer::defaults::SEGMENT;
69
70 //Then, to actually the patterns on a grid,
71 //there are 2 variatns, a hex grid and a square grid
72 //creating a grid is seperate from drawing one
73 //when creating a grid, it simply aligns and/or scales
74 //all the patterns on the grid so they can be drawn later
75
76 //the hex grid is just a hexagonal grid where all of the
77 //paterns are aligned side to side in respect to that grid
78
79 //the square grid on the other hand provides a certain
80 //block of space for each pattern and then scales them
81 //to fit within their alloted block
82
83 //there are two ways to initialize each with patterns
84 //however, this tutorial will stick to the simpler way
85
86 //The HexGrid takes in a set of patterns and a max_width
87 //the max_width is simply how many points on the grid it should fill
88 // (in the x direction) before putting patterns on the next line down
89 let max_width = 50;
90 let hex_grid = HexGrid::new_normal(patterns, max_width).expect("Failed to make Hex Grid!");
91
92 //for the square grid, max_width is how many tiles (patterns) long
93 //each row is rather than the width of the tiles themselves
94 let max_width = 10;
95 //max_scale sets a cap of how enlarged a pattern can be
96 //it stops really small patterns from being enlarged too much
97 //must be between 0 and 1 where 1 is no limit
98 let max_scale = 0.4;
99 //x_pad and y_pad are how much space should be put
100 //between the tiles in the x and y axis respectively
101 //it is based on the percentage of the tile (in that axis)
102 //that should be devoted to padding
103 let x_pad = 0.2;
104 let y_pad = 0.1;
105 let square_grid =
106 SquareGrid::new_normal(built_pattern_list, max_width, max_scale, x_pad, y_pad)
107 .expect("Failed to make Square Grid!");
108
109 //now to draw the grid, there are 3 main functions
110 //draw_grid -- simply draws the grid to a tiny_skia::Pixmap
111 //draw_grid_png -- returns grid as a png represented by a vector of bytes
112 //draw_grid_to_file -- saves the grid as a png to the provided file
113
114 //each of these types takes in the scale of the grid
115 // + the patterns options talked about above
116
117 //for the hex_grid, the scale is how many pixels apart each
118 //point on the hex_grid should be spaced apart
119 let scale = 50.0;
120 hex_grid
121 .draw_grid_to_file("monocolor_hex_grid.png", scale, monocolor)
122 .expect("Unable to write file!");
123
124 hex_grid
125 .draw_grid_to_file("segment_hex_grid.png", scale, segment)
126 .expect("Unable to write to file!");
127
128 //for the square_grid, the scale is how many pixels tall/wide each tile (pattern) should be
129 let scale = 200.0;
130 square_grid
131 .draw_grid_to_file("monocolor_square_grid.png", scale, monocolor)
132 .expect("Unable to write to file!");
133
134 square_grid
135 .draw_grid_to_file("segment_square_grid.png", scale, segment)
136 .expect("Unable to write to file!");
137
138 //if you would rather draw things based on the size of the final image,
139 //you can call grid.get_bound_scale to get the scale that fits
140 //within the bounds you have provided
141 //though, since different draw options have slightly different sizes (mainly due to padding),
142 //those need to be provided in order to get the proper scaling
143
144 //500 x 500 pixel bound
145 let bound = (500.0, 500.0);
146
147 let hex_scale = hex_grid.get_bound_scale(bound, gradient);
148 hex_grid
149 .draw_grid_to_file("bound_hex_grid.png", hex_scale, gradient)
150 .expect("Unable to write to file!");
151
152 let square_scale = square_grid.get_bound_scale(bound, gradient);
153 square_grid
154 .draw_grid_to_file("bound_square_grid.png", square_scale, gradient)
155 .expect("Unable to write to file!");
156}
Trait Implementations§
Source§impl PartialOrd for Pattern
impl PartialOrd for Pattern
impl StructuralPartialEq for Pattern
Auto Trait Implementations§
impl Freeze for Pattern
impl RefUnwindSafe for Pattern
impl Send for Pattern
impl Sync for Pattern
impl Unpin for Pattern
impl UnwindSafe for Pattern
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more