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
impl SfStr
Sourcepub fn to_rust_string(&self) -> String
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 144)
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,
112 fc.g,
113 fc.b,
114 fc.a,
115 oc.r,
116 oc.g,
117 oc.b,
118 oc.a,
119 text.outline_thickness(),
120 text.style(),
121 show_cursor,
122 font.info().family
123 )
124 };
125 status_text.set_string(&status_string);
126
127 window.clear(Color::BLACK);
128 window.draw(&text);
129 if show_cursor {
130 let mut end = text.find_character_pos(usize::MAX);
131 end.x += 2.0;
132 end.y += 2.0;
133 let mut rs = RectangleShape::new();
134 rs.set_fill_color(Color::TRANSPARENT);
135 rs.set_outline_color(Color::YELLOW);
136 rs.set_outline_thickness(-3.0);
137 rs.set_position(end);
138 rs.set_size((8.0, 24.0));
139 window.draw(&rs);
140 }
141 window.draw(&status_text);
142 window.display();
143 }
144 println!("The final text is {:?}", text.string().to_rust_string());
145 Ok(())
146}
Sourcepub fn try_to_rust_string(&self) -> Result<String, SfStrConvError>
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