Struct SfStr

Source
pub struct SfStr(/* private fields */);
Expand description

A borrowed string type that’s compatible with sf::String.

It uses UTF-32 encoding, which is compatible with sf::String’s internal representation.

Implementations§

Source§

impl SfStr

Source

pub fn to_rust_string(&self) -> String

Convert to a UTF-8 String from the Rust standard library.

§Panics

Panics if the string is not valid UTF-32.

Examples found in repository?
examples/unicode-text-entry.rs (line 138)
12fn main() -> SfResult<()> {
13    example_ensure_right_working_dir();
14
15    let mut window = RenderWindow::new(
16        (800, 600),
17        "◢◤ Unicode text entry ◥◣",
18        Style::CLOSE,
19        &Default::default(),
20    )?;
21    window.set_vertical_sync_enabled(true);
22
23    // Showcase delayed initialization of font
24    let mut font = Font::new()?;
25
26    match std::env::args().nth(1) {
27        Some(path) => font.load_from_file(&path)?,
28        None => font.load_from_memory_static(include_bytes!("resources/sansation.ttf"))?,
29    };
30    let mut string = String::from("This text can be edited.\nTry it!");
31
32    let mut text = Text::new(&string, &font, 24);
33    text.set_fill_color(Color::RED);
34    text.set_outline_color(Color::YELLOW);
35    text.set_outline_thickness(2.0);
36    let mut status_text = Text::new("", &font, 16);
37    status_text.set_position((0., window.size().y as f32 - 64.0));
38    let mut bold = false;
39    let mut italic = false;
40    let mut underlined = false;
41    let mut strikethrough = false;
42    let mut show_cursor = true;
43
44    'mainloop: loop {
45        while let Some(ev) = window.poll_event() {
46            match ev {
47                Event::Closed => break 'mainloop,
48                Event::TextEntered { unicode } => {
49                    if unicode == 0x08 as char {
50                        string.pop();
51                    } else if unicode == 0xD as char {
52                        string.push('\n');
53                    }
54                    // Ignore ctrl+v/ctrl+v generated chars
55                    else if unicode != 0x16 as char && unicode != 0x03 as char {
56                        string.push(unicode);
57                    }
58                    text.set_string(&string);
59                }
60                Event::KeyPressed {
61                    code: Key::V,
62                    ctrl: true,
63                    ..
64                } => {
65                    string.push_str(&clipboard::get_string());
66                    text.set_string(&string);
67                }
68                Event::KeyPressed {
69                    code: Key::C,
70                    ctrl: true,
71                    ..
72                } => {
73                    clipboard::set_string(text.string());
74                }
75                Event::KeyPressed { code, .. } => {
76                    match code {
77                        Key::Escape => break 'mainloop,
78                        Key::F1 => bold ^= true,
79                        Key::F2 => italic ^= true,
80                        Key::F3 => underlined ^= true,
81                        Key::F4 => strikethrough ^= true,
82                        Key::F5 => show_cursor ^= true,
83                        _ => {}
84                    }
85                    let mut style = TextStyle::default();
86                    if bold {
87                        style |= TextStyle::BOLD;
88                    }
89                    if italic {
90                        style |= TextStyle::ITALIC;
91                    }
92                    if underlined {
93                        style |= TextStyle::UNDERLINED;
94                    }
95                    if strikethrough {
96                        style |= TextStyle::STRIKETHROUGH;
97                    }
98                    text.set_style(style);
99                }
100                _ => {}
101            }
102        }
103
104        let status_string = {
105            let fc = text.fill_color();
106            let oc = text.outline_color();
107            format!(
108            "fill: {:02x}{:02x}{:02x}{:02x} outline: {:02x}{:02x}{:02x}{:02x} outline thickness: {}\n\
109            style: {:?} (F1-F4) cursor: {} (F5)\n\
110            font family: {}",
111            fc.r, fc.g, fc.b, fc.a,
112            oc.r, oc.g, oc.b, oc.a,
113            text.outline_thickness(),
114            text.style(),
115            show_cursor,
116            font.info().family
117        )
118        };
119        status_text.set_string(&status_string);
120
121        window.clear(Color::BLACK);
122        window.draw(&text);
123        if show_cursor {
124            let mut end = text.find_character_pos(usize::MAX);
125            end.x += 2.0;
126            end.y += 2.0;
127            let mut rs = RectangleShape::new();
128            rs.set_fill_color(Color::TRANSPARENT);
129            rs.set_outline_color(Color::YELLOW);
130            rs.set_outline_thickness(-3.0);
131            rs.set_position(end);
132            rs.set_size((8.0, 24.0));
133            window.draw(&rs);
134        }
135        window.draw(&status_text);
136        window.display();
137    }
138    println!("The final text is {:?}", text.string().to_rust_string());
139    Ok(())
140}
Source

pub fn try_to_rust_string(&self) -> Result<String, SfStrConvError>

Convert to a UTF-8 String from the Rust standard library.

Returns a Result and errors if the string is not valid UTF-32

Trait Implementations§

Source§

impl Debug for SfStr

Source§

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

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

impl SfStrConv for &SfStr

Auto Trait Implementations§

§

impl Freeze for SfStr

§

impl RefUnwindSafe for SfStr

§

impl Send for SfStr

§

impl !Sized for SfStr

§

impl Sync for SfStr

§

impl Unpin for SfStr

§

impl UnwindSafe for SfStr

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