pub struct TextMaterial { /* private fields */ }
Expand description

This struct represents a piece of text. You only need to create one peice of text per string you would like to draw as you can draw multpiple instances easily.

Implementations§

source§

impl TextMaterial

source

pub fn new( text: &str, colour: Colour, font_size: f32, line_height: f32, engine: &mut Engine ) -> Self

Examples found in repository?
examples/text.rs (line 18)
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    let mut engine = EngineBuilder::new()
        .set_clear_colour(Colour::BLACK)
        .build()
        .unwrap();

    let comic = Font::new("examples/Comic.ttf", &mut engine);
    let text_mat = TextMaterial::new("this is a test", Colour::RED, 0.5, 0.5 * 1.3, &mut engine);

    let text_example = TextExample { text_mat, comic, font_size: 0.5};

    engine.run(text_example);
}
More examples
Hide additional examples
examples/camera.rs (line 23)
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    let mut engine = EngineBuilder::new().build().unwrap();

    let texture = Texture::new(&mut engine, "examples/bplogo.png");

    let material = MaterialBuilder::new()
        .add_texture(texture)
        .build(&mut engine);
    
    let camera = Camera::new(&engine);

    let text = TextMaterial::new("Mouse pos: 0,0 \n Mouse pos: 0, 0", Colour::WHITE, 15.0, 20.0, &mut engine);

    let game = CameraExample {
        material,
        text,
        camera,
    };

    engine.run(game);
}
source

pub fn set_text(&mut self, text: &str, colour: Colour, engine: &mut Engine)

Sets the text for the widget, using the defualt font. This only needs to be done once, not every frame like Materials.

Examples found in repository?
examples/camera.rs (lines 102-106)
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
    fn update(&mut self, engine_handle: &mut Engine) {
        let dt = engine_handle.get_frame_delta_time();
        let mouse_pos = engine_handle.get_mouse_position();
        let size = engine_handle.get_window_size();

        let move_factor = 15.0;

        if engine_handle.is_key_down(Key::A) {
            self.camera.center.x -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::D) {
            self.camera.center.x += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::W) {
            self.camera.center.y += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::S) {
            self.camera.center.y -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::Left) {
            self.camera.rotation += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::Right) {
            self.camera.rotation -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::L) {
            self.camera.scale += vec2!(2.0 * dt, 2.0 * dt);
        }

        if engine_handle.is_key_down(Key::K) {
            self.camera.scale -= vec2!(2.0 * dt, 2.0 * dt);
        }

        if engine_handle.is_key_pressed(Key::Enter) {
            self.camera.rotation += 45.0;
        }

        let trans_mouse = self.camera.transform_point(mouse_pos, size);

        self.text.set_text(
            &format!("Screen mouse pos: {:.3}, {:.3}\nWorld mouse pos: {:.3}, {:.3}", mouse_pos.x, mouse_pos.y, trans_mouse.x, trans_mouse.y),
            Colour::WHITE,
            engine_handle,
        );

        self.text.prepare(engine_handle);
    }
source

pub fn set_text_with_font( &mut self, text: &str, colour: Colour, font: &ResourceId<Font>, engine: &mut Engine )

Sets the text for the widget, but with a font of your choosing. This only needs to be done once, not every frame like Materials

Examples found in repository?
examples/text.rs (line 58)
50
51
52
53
54
55
56
57
58
59
60
61
62
    fn update(&mut self, engine_handle: &mut Engine) {
        self.font_size += 2.0 * engine_handle.get_frame_delta_time();

        self.text_mat.set_font_size(self.font_size, engine_handle);
        self.text_mat.set_line_height(self.font_size * 1.3, engine_handle);

        if engine_handle.is_mouse_key_pressed(MouseKey::Left) {
            self.text_mat
                .set_text_with_font("hello I am talking to you here???", Colour::GREEN, &self.comic, engine_handle);
        }

        self.text_mat.prepare(engine_handle);
    }
source

pub fn set_bounds(&mut self, position: Vec2<i32>, size: Vec2<i32>)

Sets bounds for the text. Any text drawn outside of the bounds will be cropped

source

pub fn set_font_size(&mut self, new_size: f32, engine: &mut Engine)

Sets the font size of the text

Examples found in repository?
examples/text.rs (line 53)
50
51
52
53
54
55
56
57
58
59
60
61
62
    fn update(&mut self, engine_handle: &mut Engine) {
        self.font_size += 2.0 * engine_handle.get_frame_delta_time();

        self.text_mat.set_font_size(self.font_size, engine_handle);
        self.text_mat.set_line_height(self.font_size * 1.3, engine_handle);

        if engine_handle.is_mouse_key_pressed(MouseKey::Left) {
            self.text_mat
                .set_text_with_font("hello I am talking to you here???", Colour::GREEN, &self.comic, engine_handle);
        }

        self.text_mat.prepare(engine_handle);
    }
source

pub fn set_line_height(&mut self, new_height: f32, engine: &mut Engine)

Sets the line hieght of the tex

Examples found in repository?
examples/text.rs (line 54)
50
51
52
53
54
55
56
57
58
59
60
61
62
    fn update(&mut self, engine_handle: &mut Engine) {
        self.font_size += 2.0 * engine_handle.get_frame_delta_time();

        self.text_mat.set_font_size(self.font_size, engine_handle);
        self.text_mat.set_line_height(self.font_size * 1.3, engine_handle);

        if engine_handle.is_mouse_key_pressed(MouseKey::Left) {
            self.text_mat
                .set_text_with_font("hello I am talking to you here???", Colour::GREEN, &self.comic, engine_handle);
        }

        self.text_mat.prepare(engine_handle);
    }
source

pub fn get_measurements(&self) -> Vec2<u32>

Measuers the text contained within the widget

source

pub fn add_instance( &mut self, position: Vec2<f32>, tint: Colour, render: &RenderInformation<'_, '_> )

Queues a peice of text at the specified position. Its size will be the size of the entire text.

Examples found in repository?
examples/text.rs (line 39)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    fn render<'pass, 'others>(
        &'others mut self,
        mut render_handle: RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        self.text_mat
            .add_instance(vec2! { 0.0 }, Colour::WHITE, &render_handle);
        self.text_mat.add_instance_with_rotation(
            Vec2 { x: 100.0, y: 0.0 },
            Colour::WHITE,
            45.0,
            &render_handle,
        );

        self.text_mat.draw(&mut render_handle);
    }
More examples
Hide additional examples
examples/camera.rs (line 53)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    fn render<'pass, 'others>(
        &'others mut self,
        mut render_handle: RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        self.material.add_rectangle(Vec2 { x: 0.0, y: 0.0 }, Vec2{x: 300.0, y: 300.0}, Colour::WHITE, &render_handle);

        self.camera.set_active(&mut render_handle);
        self.material.draw(&mut render_handle);

        render_handle.reset_camera();
        self.text.add_instance(vec2!(0.0), Colour::WHITE, &render_handle);
        self.text.draw(&mut render_handle);
    }
source

pub fn add_instance_with_rotation( &mut self, position: Vec2<f32>, tint: Colour, degrees: f32, render: &RenderInformation<'_, '_> )

Queues a piece of text at the specified postion, with rotation. Its size will be the size of the text.

Examples found in repository?
examples/text.rs (lines 40-45)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    fn render<'pass, 'others>(
        &'others mut self,
        mut render_handle: RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        self.text_mat
            .add_instance(vec2! { 0.0 }, Colour::WHITE, &render_handle);
        self.text_mat.add_instance_with_rotation(
            Vec2 { x: 100.0, y: 0.0 },
            Colour::WHITE,
            45.0,
            &render_handle,
        );

        self.text_mat.draw(&mut render_handle);
    }
source

pub fn add_instance_with_uv( &mut self, position: Vec2<f32>, size: Vec2<f32>, uv_pos: Vec2<f32>, uv_size: Vec2<f32>, tint: Colour, render: &RenderInformation<'_, '_> )

Queues a piece of text at the specified postion. This also allows you to control the uv coordinates to the texture that the text has been rendered to.

source

pub fn add_instace_ex( &mut self, position: Vec2<f32>, size: Vec2<f32>, uv_pos: Vec2<f32>, uv_size: Vec2<f32>, degrees: f32, tint: Colour, render: &RenderInformation<'_, '_> )

Queues a piece of text at the position with, uv controll, and rotation.

source

pub fn add_instance_custom( &mut self, points: [Vec2<f32>; 4], uv_points: [Vec2<f32>; 4], degrees: f32, tint: Colour, render: &RenderInformation<'_, '_> )

Queues a peice with complete controll over the points, rotation, and uv coordinates. This can allow for non rectanglular shapes and the points must be in top left, top right, bottom right, bottom left order otherwise it will not draw properly.

source

pub fn prepare(&mut self, engine: &mut Engine)

Examples found in repository?
examples/text.rs (line 61)
50
51
52
53
54
55
56
57
58
59
60
61
62
    fn update(&mut self, engine_handle: &mut Engine) {
        self.font_size += 2.0 * engine_handle.get_frame_delta_time();

        self.text_mat.set_font_size(self.font_size, engine_handle);
        self.text_mat.set_line_height(self.font_size * 1.3, engine_handle);

        if engine_handle.is_mouse_key_pressed(MouseKey::Left) {
            self.text_mat
                .set_text_with_font("hello I am talking to you here???", Colour::GREEN, &self.comic, engine_handle);
        }

        self.text_mat.prepare(engine_handle);
    }
More examples
Hide additional examples
examples/camera.rs (line 108)
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
    fn update(&mut self, engine_handle: &mut Engine) {
        let dt = engine_handle.get_frame_delta_time();
        let mouse_pos = engine_handle.get_mouse_position();
        let size = engine_handle.get_window_size();

        let move_factor = 15.0;

        if engine_handle.is_key_down(Key::A) {
            self.camera.center.x -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::D) {
            self.camera.center.x += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::W) {
            self.camera.center.y += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::S) {
            self.camera.center.y -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::Left) {
            self.camera.rotation += move_factor * dt;
        }

        if engine_handle.is_key_down(Key::Right) {
            self.camera.rotation -= move_factor * dt;
        }

        if engine_handle.is_key_down(Key::L) {
            self.camera.scale += vec2!(2.0 * dt, 2.0 * dt);
        }

        if engine_handle.is_key_down(Key::K) {
            self.camera.scale -= vec2!(2.0 * dt, 2.0 * dt);
        }

        if engine_handle.is_key_pressed(Key::Enter) {
            self.camera.rotation += 45.0;
        }

        let trans_mouse = self.camera.transform_point(mouse_pos, size);

        self.text.set_text(
            &format!("Screen mouse pos: {:.3}, {:.3}\nWorld mouse pos: {:.3}, {:.3}", mouse_pos.x, mouse_pos.y, trans_mouse.x, trans_mouse.y),
            Colour::WHITE,
            engine_handle,
        );

        self.text.prepare(engine_handle);
    }
source

pub fn draw<'pass, 'others>( &'others mut self, information: &mut RenderInformation<'pass, 'others> )
where 'others: 'pass,

Draws all queued text instances to the screen

Examples found in repository?
examples/text.rs (line 47)
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    fn render<'pass, 'others>(
        &'others mut self,
        mut render_handle: RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        self.text_mat
            .add_instance(vec2! { 0.0 }, Colour::WHITE, &render_handle);
        self.text_mat.add_instance_with_rotation(
            Vec2 { x: 100.0, y: 0.0 },
            Colour::WHITE,
            45.0,
            &render_handle,
        );

        self.text_mat.draw(&mut render_handle);
    }
More examples
Hide additional examples
examples/camera.rs (line 54)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    fn render<'pass, 'others>(
        &'others mut self,
        mut render_handle: RenderInformation<'pass, 'others>,
    ) where
        'others: 'pass,
    {
        self.material.add_rectangle(Vec2 { x: 0.0, y: 0.0 }, Vec2{x: 300.0, y: 300.0}, Colour::WHITE, &render_handle);

        self.camera.set_active(&mut render_handle);
        self.material.draw(&mut render_handle);

        render_handle.reset_camera();
        self.text.add_instance(vec2!(0.0), Colour::WHITE, &render_handle);
        self.text.draw(&mut render_handle);
    }

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> Downcast<T> for T

source§

fn downcast(&self) -> &T

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, 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.
source§

impl<T> Upcast<T> for T

source§

fn upcast(&self) -> Option<&T>

source§

impl<T> WasmNotSend for T
where T: Send,

source§

impl<T> WasmNotSync for T
where T: Sync,