Newer
Older
use bevy::prelude::*;
use cosmic_text::{Buffer, Edit, Shaping};
use crate::{
input::input_mouse, CosmicBuffer, CosmicEditor, CosmicFontSystem, DefaultAttrs, Render,
};
pub struct PasswordPlugin;
impl Plugin for PasswordPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
PreUpdate,
(
hide_password_text.before(input_mouse),
restore_password_text.after(input_mouse),
),
)
.add_systems(
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
PostUpdate,
(
hide_password_text.before(Render),
restore_password_text.after(Render),
),
);
}
}
#[derive(Component)]
pub struct Password {
real_text: String,
glyph: char,
}
impl Default for Password {
fn default() -> Self {
Self {
real_text: Default::default(),
glyph: '*',
}
}
}
fn hide_password_text(
mut q: Query<(
&mut Password,
&mut CosmicBuffer,
&DefaultAttrs,
Option<&mut CosmicEditor>,
)>,
mut font_system: ResMut<CosmicFontSystem>,
) {
for (mut password, mut buffer, attrs, editor_opt) in q.iter_mut() {
if let Some(mut editor) = editor_opt {
editor.with_buffer_mut(|buffer| {
fn get_text(buffer: &mut Buffer) -> String {
let mut text = String::new();
let line_count = buffer.lines.len();
for (i, line) in buffer.lines.iter().enumerate() {
text.push_str(line.text());
if i < line_count - 1 {
text.push('\n');
}
}
text
}
let text = get_text(buffer);
buffer.set_text(
&mut font_system,
password.glyph.to_string().repeat(text.len()).as_str(),
attrs.as_attrs(),
Shaping::Advanced,
);
password.real_text = text;
});
continue;
}
let text = buffer.get_text();
buffer.set_text(
&mut font_system,
password.glyph.to_string().repeat(text.len()).as_str(),
attrs.as_attrs(),
);
password.real_text = text;
}
}
fn restore_password_text(
mut q: Query<(
&Password,
&mut CosmicBuffer,
&DefaultAttrs,
Option<&mut CosmicEditor>,
)>,
mut font_system: ResMut<CosmicFontSystem>,
) {
for (password, mut buffer, attrs, editor_opt) in q.iter_mut() {
if let Some(mut editor) = editor_opt {
editor.with_buffer_mut(|buffer| {
buffer.set_text(
&mut font_system,
password.real_text.as_str(),
attrs.as_attrs(),
Shaping::Advanced,
)
});
continue;
}
buffer.set_text(
&mut font_system,
password.real_text.as_str(),
attrs.as_attrs(),
);
}
}