Struct hex_renderer::Pattern

source ·
pub struct Pattern { /* private fields */ }
Expand description

Represents a pattern to be drawn on a grid

Implementations§

source§

impl Pattern

source

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)
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
fn main() {
    //In order to draw a set of patterns, they need to be in a readable format first
    //the simplest format is just a vector of Patterns.

    //there is some built in parsing for strings, however it can only take in
    //one pattern at a time and accepts the following formats:
    // <starting_direction> <angle_sigs>
    // hexpattern(<starting_direction> <angle_sigs>)

    //where starting_direction can be formated in any of the following ways:
    //  -- north_east | northeast | ne -- caps are ignored
    //and angle sigs are any of the following characters: weqdas
    let patterns =
        "HexPattern(north_east qqq), HexPattern(north_east qqq), HexPattern(north_east qqq)";

    //to convert from a string of the above format into a list of patterns
    //you can split it into each pattern and then try to convert it using Pattern::try_from
    let patterns = patterns
        .split(", ")
        .map(Pattern::try_from)
        .collect::<Result<Vec<Pattern>, _>>()
        .expect("Invalid Pattern List!");

    //alternatively, you can build the patterns by hand
    //angle sigs are mapped as followed:
    // w - Forward
    // e - Right
    // d - BackRight
    // s - Backwards
    // a - BackLeft
    // q - Left

    let starting_direction = pattern_utils::Direction::NorthEast;
    let angle_sigs = vec![
        pattern_utils::Angle::Left,
        pattern_utils::Angle::Left,
        pattern_utils::Angle::Left,
    ];

    let built_pattern = Pattern::new(starting_direction, angle_sigs);

    let built_pattern_list = vec![built_pattern.clone(), built_pattern.clone(), built_pattern];

    //another important component for drawing the patterns is their configuration
    //for this, one of the default options will be used, but custom configuration
    //will be in the next example

    //there are 3 different types of pattern rendering as follows
    // monocolor - simply one color
    // gradient - transitions from one color to another
    // segment - switches colors on conflict

    //each of the above types have their own set of defaults. Including their base and below

    //such as pointed-gradient which just adds points to the gradients,
    //a bent_monocolor that adds bends to the corners of the monocolor renderer
    //and uniform variants of each which don't change color
    //   when going inside nested intro/retro blocks

    let monocolor = &hex_renderer::defaults::MONOCOLOR;
    let gradient = &hex_renderer::defaults::GRADIENT;
    let segment = &hex_renderer::defaults::SEGMENT;

    //Then, to actually the patterns on a grid,
    //there are 2 variatns, a hex grid and a square grid
    //creating a grid is seperate from drawing one
    //when creating a grid, it simply aligns and/or scales
    //all the patterns on the grid so they can be drawn later

    //the hex grid is just a hexagonal grid where all of the
    //paterns are aligned side to side in respect to that grid

    //the square grid on the other hand provides a certain
    //block of space for each pattern and then scales them
    //to fit within their alloted block

    //there are two ways to initialize each with patterns
    //however, this tutorial will stick to the simpler way

    //The HexGrid takes in a set of patterns and a max_width
    //the max_width is simply how many points on the grid it should fill
    // (in the x direction) before putting patterns on the next line down
    let max_width = 50;
    let hex_grid = HexGrid::new_normal(patterns, max_width).expect("Failed to make Hex Grid!");

    //for the square grid, max_width is how many tiles (patterns) long
    //each row is rather than the width of the tiles themselves
    let max_width = 10;
    //max_scale sets a cap of how enlarged a pattern can be
    //it stops really small patterns from being enlarged too much
    //must be between 0 and 1 where 1 is no limit
    let max_scale = 0.4;
    //x_pad and y_pad are how much space should be put
    //between the tiles in the x and y axis respectively
    //it is based on the percentage of the tile (in that axis)
    //that should be devoted to padding
    let x_pad = 0.2;
    let y_pad = 0.1;
    let square_grid =
        SquareGrid::new_normal(built_pattern_list, max_width, max_scale, x_pad, y_pad)
            .expect("Failed to make Square Grid!");

    //now to draw the grid, there are 3 main functions
    //draw_grid -- simply draws the grid to a tiny_skia::Pixmap
    //draw_grid_png -- returns grid as a png represented by a vector of bytes
    //draw_grid_to_file -- saves the grid as a png to the provided file

    //each of these types takes in the scale of the grid
    // + the patterns options talked about above

    //for the hex_grid, the scale is how many pixels apart each
    //point on the hex_grid should be spaced apart
    let scale = 50.0;
    hex_grid
        .draw_grid_to_file("monocolor_hex_grid.png", scale, monocolor)
        .expect("Unable to write file!");

    hex_grid
        .draw_grid_to_file("segment_hex_grid.png", scale, segment)
        .expect("Unable to write to file!");

    //for the square_grid, the scale is how many pixels tall/wide each tile (pattern) should be
    let scale = 200.0;
    square_grid
        .draw_grid_to_file("monocolor_square_grid.png", scale, monocolor)
        .expect("Unable to write to file!");

    square_grid
        .draw_grid_to_file("segment_square_grid.png", scale, segment)
        .expect("Unable to write to file!");

    //if you would rather draw things based on the size of the final image,
    //you can call grid.get_bound_scale to get the scale that fits
    //within the bounds you have provided
    //though, since different draw options have slightly different sizes (mainly due to padding),
    //those need to be provided in order to get the proper scaling

    //500 x 500 pixel bound
    let bound = (500.0, 500.0);

    let hex_scale = hex_grid.get_bound_scale(bound, gradient);
    hex_grid
        .draw_grid_to_file("bound_hex_grid.png", hex_scale, gradient)
        .expect("Unable to write to file!");

    let square_scale = square_grid.get_bound_scale(bound, gradient);
    square_grid
        .draw_grid_to_file("bound_square_grid.png", square_scale, gradient)
        .expect("Unable to write to file!");
}

Trait Implementations§

source§

impl Clone for Pattern

source§

fn clone(&self) -> Pattern

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Pattern

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for Pattern

source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for Pattern

source§

fn eq(&self, other: &Pattern) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Pattern

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryFrom<&str> for Pattern

§

type Error = PatternParseError

The type returned in the event of a conversion error.
source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl StructuralPartialEq for Pattern

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.