Trait Shape

Source
pub trait Shape<'texture>: Drawable + Transformable {
Show 16 methods // Required methods fn set_texture(&mut self, texture: &'texture Texture, reset_rect: bool); fn disable_texture(&mut self); fn set_texture_rect(&mut self, rect: IntRect); fn set_fill_color(&mut self, color: Color); fn set_outline_color(&mut self, color: Color); fn set_outline_thickness(&mut self, thickness: f32); fn texture(&self) -> Option<&'texture Texture>; fn texture_rect(&self) -> IntRect; fn fill_color(&self) -> Color; fn outline_color(&self) -> Color; fn outline_thickness(&self) -> f32; fn point_count(&self) -> usize; fn point(&self, index: usize) -> Vector2f; fn local_bounds(&self) -> FloatRect; fn global_bounds(&self) -> FloatRect; // Provided method fn points<'a>(&'a self) -> impl Iterator<Item = Vector2f> + 'a { ... }
}
Expand description

Trait for textured shapes with outline.

Required Methods§

Source

fn set_texture(&mut self, texture: &'texture Texture, reset_rect: bool)

Changes the source texture of the shape.

If reset_rect is true, the texture_rect property of the shape is automatically adjusted to the size of the new texture. If it is false, the texture rect is left unchanged.

Source

fn disable_texture(&mut self)

Disables texturing for this shape.

Source

fn set_texture_rect(&mut self, rect: IntRect)

Sets the sub-rectangle of the texture that the shape will display.

The texture rect is useful when you don’t want to display the whole texture, but rather a part of it. By default, the texture rect covers the entire texture.

Source

fn set_fill_color(&mut self, color: Color)

Sets the fill color of the shape.

This color is modulated (multiplied) with the shape’s texture if any. It can be used to colorize the shape, or change its global opacity. You can use Color::TRANSPARENT to make the inside of the shape transparent, and have the outline alone. By default, the shape’s fill color is opaque white.

Source

fn set_outline_color(&mut self, color: Color)

Sets the outline color of the shape.

By default, the shape’s outline color is opaque white.

Source

fn set_outline_thickness(&mut self, thickness: f32)

Sets the thickness of the shape’s outline.

Note that negative values are allowed (so that the outline expands towards the center of the shape), and using zero disables the outline. By default, the outline thickness is 0.

Source

fn texture(&self) -> Option<&'texture Texture>

Gets the source texture of the shape.

If the shape has no source texture, None is returned.

Source

fn texture_rect(&self) -> IntRect

Gets the sub-rectangle of the texture displayed by the shape.

Source

fn fill_color(&self) -> Color

Gets the fill color of this shape.

Source

fn outline_color(&self) -> Color

Gets the outline color of this shape.

Source

fn outline_thickness(&self) -> f32

Gets the outline thickness of this shape.

Source

fn point_count(&self) -> usize

Gets the total number of points of the shape.

Source

fn point(&self, index: usize) -> Vector2f

Gets a point of the shape.

The returned point is in local coordinates, that is, the shape’s transforms (position, rotation, scale) are not taken into account. The result is unspecified if index is out of the valid range (0..point_count()).

Source

fn local_bounds(&self) -> FloatRect

Gets the local bounding rectangle of the entity.

The returned rectangle is in local coordinates, which means that it ignores the transformations (translation, rotation, scale, …) that are applied to the entity. In other words, this function returns the bounds of the entity in the entity’s coordinate system.

Source

fn global_bounds(&self) -> FloatRect

Gets the global (non-minimal) bounding rectangle of the entity.

The returned rectangle is in global coordinates, which means that it takes into account the transformations (translation, rotation, scale, …) that are applied to the entity. In other words, this function returns the bounds of the shape in the global 2D world’s coordinate system.

This function does not necessarily return the minimal bounding rectangle. It merely ensures that the returned rectangle covers all the vertices (but possibly more). This allows for a fast approximation of the bounds as a first check; you may want to use more precise checks on top of that.

Provided Methods§

Source

fn points<'a>(&'a self) -> impl Iterator<Item = Vector2f> + 'a

Returns an immutable iterator over all points

Examples found in repository?
examples/borrowed-resources.rs (line 91)
12fn main() -> SfResult<()> {
13    example_ensure_right_working_dir();
14
15    let mut window = RenderWindow::new(
16        (800, 600),
17        "Borrowed resources",
18        Style::CLOSE,
19        &Default::default(),
20    )?;
21    window.set_vertical_sync_enabled(true);
22
23    // Create a new texture. (Hey Frank!)
24    let frank = Texture::from_file("frank.jpeg")?;
25
26    // Create a font.
27    let font = Font::from_file("sansation.ttf")?;
28
29    // Create a circle with the Texture.
30    let mut circle = CircleShape::with_texture(&frank);
31    circle.set_radius(70.0);
32    circle.set_position((100.0, 100.0));
33
34    // Create a Sprite.
35    let mut sprite = Sprite::new();
36    // Have it use the same texture as the circle.
37    sprite.set_texture(&frank, true);
38    sprite.set_position((400.0, 300.0));
39    sprite.set_scale(0.5);
40
41    // Create a ConvexShape using the same texture.
42    let mut convex_shape = ConvexShape::with_texture(6, &frank);
43    convex_shape.set_point(0, (400., 100.));
44    convex_shape.set_point(1, (500., 70.));
45    convex_shape.set_point(2, (450., 100.));
46    convex_shape.set_point(3, (580., 150.));
47    convex_shape.set_point(4, (420., 230.));
48    convex_shape.set_point(5, (420., 120.));
49
50    // Create an initialized text using the font.
51    let title = Text::new("Borrowed resources example!", &font, 50);
52
53    // Create a second text using the same font.
54    // This time, we create and initialize it separately.
55    let mut second_text = Text::default();
56    second_text.set_string("This text shares the same font with the title!");
57    second_text.set_font(&font);
58    second_text.set_fill_color(Color::GREEN);
59    second_text.set_position((10.0, 350.0));
60    second_text.set_character_size(20);
61
62    // Create a third text using the same font.
63    let mut third_text = Text::new("This one too!", &font, 20);
64    third_text.set_position((300.0, 100.0));
65    third_text.set_fill_color(Color::RED);
66
67    'mainloop: loop {
68        while let Some(event) = window.poll_event() {
69            match event {
70                Event::Closed
71                | Event::KeyPressed {
72                    code: Key::Escape, ..
73                } => break 'mainloop,
74                _ => {}
75            }
76        }
77
78        window.clear(Color::BLACK);
79        window.draw(&circle);
80        window.draw(&sprite);
81        window.draw(&convex_shape);
82        window.draw(&title);
83        window.draw(&second_text);
84        window.draw(&third_text);
85
86        // Little test here for `Shape::points`
87        let mut circ = CircleShape::new(4.0, 30);
88        circ.set_origin(2.0);
89        circ.set_fill_color(Color::YELLOW);
90
91        for p in convex_shape.points() {
92            circ.set_position(p);
93            window.draw(&circ);
94        }
95
96        window.display();
97    }
98    Ok(())
99}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'s> Shape<'s> for CircleShape<'s>

Source§

impl<'s> Shape<'s> for ConvexShape<'s>

Source§

impl<'s> Shape<'s> for CustomShape<'s>

Source§

impl<'s> Shape<'s> for RectangleShape<'s>