Skip to content
Snippets Groups Projects
Commit a4b28e39 authored by sam edelsten's avatar sam edelsten
Browse files

add password feature

parent d3bffa58
No related branches found
No related tags found
No related merge requests found
......@@ -14,6 +14,7 @@ exclude = ["assets/*"]
[features]
multicam = []
placeholder = []
password = []
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
......
use bevy::prelude::*;
use bevy_cosmic_edit::{password::Password, *};
use util::{change_active_editor_sprite, deselect_editor_on_esc, print_editor_text};
fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
// Sprite editor
commands.spawn((
CosmicEditBundle {
max_lines: CosmicMaxLines(1),
mode: CosmicMode::InfiniteLine,
sprite_bundle: SpriteBundle {
// Sets size of text box
sprite: Sprite {
custom_size: Some(Vec2::new(300., 100.)),
..default()
},
// Position of text box
transform: Transform::from_xyz(0., 100., 0.),
..default()
},
..default()
},
Password::default(),
));
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(CosmicEditPlugin {
change_cursor: CursorConfig::Default,
..default()
})
.add_systems(Startup, setup)
.add_systems(
Update,
(
change_active_editor_sprite,
deselect_editor_on_esc,
print_editor_text,
),
)
.run();
}
......@@ -203,6 +203,9 @@ impl Default for CosmicFontConfig {
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
pub struct KbInput;
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Render;
/// Plugin struct that adds systems and initializes resources related to cosmic edit functionality.
#[derive(Default)]
pub struct CosmicEditPlugin {
......@@ -248,6 +251,7 @@ impl Plugin for CosmicEditPlugin {
render_texture,
)
.chain()
.in_set(Render)
.after(TransformSystem::TransformPropagate),
)
.init_resource::<FocusedWidget>()
......@@ -287,6 +291,9 @@ fn add_feature_plugins(app: &mut App) -> &mut App {
#[cfg(feature = "placeholder")]
app.add_plugins(plugins::placeholder::PlaceholderPlugin);
#[cfg(feature = "password")]
app.add_plugins(plugins::password::PasswordPlugin);
app
}
......
#[cfg(feature = "password")]
pub mod password;
#[cfg(feature = "placeholder")]
pub mod placeholder;
use bevy::prelude::*;
use cosmic_text::{Buffer, Edit, Shaping};
use crate::{CosmicBuffer, CosmicEditor, CosmicFontSystem, DefaultAttrs, Render};
pub struct PasswordPlugin;
impl Plugin for PasswordPlugin {
fn build(&self, app: &mut App) {
app.add_systems(
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(),
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment