Skip to content

Commit

Permalink
Test: required components
Browse files Browse the repository at this point in the history
  • Loading branch information
rparrett committed Aug 27, 2024
1 parent a409d2a commit 68e8564
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 105 deletions.
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ exclude = [".github"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

# Bevy needs either its `x11` or `wayland` features enabled to compile on linux.
# By default, Bevy includes `x11`, so we will too.
[features]
default = ["x11"]
x11 = ["bevy/x11"]

[dependencies.bevy]
git = "https://github.com/bevyengine/bevy"
default-features = false
Expand All @@ -23,6 +29,7 @@ features = ["bevy_ui", "bevy_asset", "bevy_text"]
git = "https://github.com/bevyengine/bevy"
default-features = true


[lints.rust]
missing_docs = "warn"

Expand Down
5 changes: 3 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use bevy::prelude::*;
use bevy_simple_text_input::{
TextInputBundle, TextInputPlugin, TextInputSubmitEvent, TextInputSystem,
TextInput, TextInputPlugin, TextInputSubmitEvent, TextInputSystem, TextInputTextStyle,
};

const BORDER_COLOR_ACTIVE: Color = Color::srgb(0.75, 0.52, 0.99);
Expand Down Expand Up @@ -45,7 +45,8 @@ fn setup(mut commands: Commands) {
background_color: BACKGROUND_COLOR.into(),
..default()
},
TextInputBundle::default().with_text_style(TextStyle {
TextInput,
TextInputTextStyle(TextStyle {
font_size: 34.,
color: TEXT_COLOR,
..default()
Expand Down
22 changes: 13 additions & 9 deletions examples/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use bevy::prelude::*;
use bevy_simple_text_input::{
TextInputBundle, TextInputInactive, TextInputPlugin, TextInputSystem,
TextInput, TextInputInactive, TextInputPlaceholder, TextInputPlugin, TextInputSystem,
TextInputTextStyle,
};

const BORDER_COLOR_ACTIVE: Color = Color::srgb(0.75, 0.52, 0.99);
Expand Down Expand Up @@ -54,14 +55,17 @@ fn setup(mut commands: Commands) {
focus_policy: bevy::ui::FocusPolicy::Block,
..default()
},
TextInputBundle::default()
.with_text_style(TextStyle {
font_size: 34.,
color: TEXT_COLOR,
..default()
})
.with_placeholder("Click Me", None)
.with_inactive(true),
TextInput,
TextInputTextStyle(TextStyle {
font_size: 34.,
color: TEXT_COLOR,
..default()
}),
TextInputPlaceholder {
value: "Click Me".to_string(),
text_style: None,
},
TextInputInactive(true),
));
});
}
Expand Down
26 changes: 14 additions & 12 deletions examples/password.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! An example showing a masked input for passwords.

use bevy::prelude::*;
use bevy_simple_text_input::{TextInputBundle, TextInputPlugin, TextInputSettings};
use bevy_simple_text_input::{
TextInput, TextInputPlugin, TextInputSettings, TextInputTextStyle, TextInputValue,
};

const BORDER_COLOR_ACTIVE: Color = Color::srgb(0.75, 0.52, 0.99);
const TEXT_COLOR: Color = Color::srgb(0.9, 0.9, 0.9);
Expand Down Expand Up @@ -42,17 +44,17 @@ fn setup(mut commands: Commands) {
background_color: BACKGROUND_COLOR.into(),
..default()
},
TextInputBundle::default()
.with_value("password")
.with_text_style(TextStyle {
font_size: 34.,
color: TEXT_COLOR,
..default()
})
.with_settings(TextInputSettings {
mask_character: Some('*'),
retain_on_submit: true,
}),
TextInput,
TextInputValue("password".to_string()),
TextInputTextStyle(TextStyle {
font_size: 34.,
color: TEXT_COLOR,
..default()
}),
TextInputSettings {
mask_character: Some('*'),
retain_on_submit: true,
},
));
});
}
18 changes: 10 additions & 8 deletions examples/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! An example showing a text input that is updated by a button.

use bevy::prelude::*;
use bevy_simple_text_input::{TextInputBundle, TextInputPlugin, TextInputSettings, TextInputValue};
use bevy_simple_text_input::{
TextInput, TextInputPlugin, TextInputSettings, TextInputTextStyle, TextInputValue,
};

const BORDER_COLOR_ACTIVE: Color = Color::srgb(0.75, 0.52, 0.99);
const BORDER_COLOR_INACTIVE: Color = Color::srgb(0.25, 0.25, 0.25);
Expand Down Expand Up @@ -55,13 +57,13 @@ fn setup(mut commands: Commands) {
background_color: BACKGROUND_COLOR.into(),
..default()
},
TextInputBundle::default()
.with_text_style(text_style.clone())
.with_value("1")
.with_settings(TextInputSettings {
retain_on_submit: true,
..default()
}),
TextInput,
TextInputTextStyle(text_style.clone()),
TextInputValue("1".to_string()),
TextInputSettings {
retain_on_submit: true,
..default()
},
));

parent
Expand Down
106 changes: 32 additions & 74 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//!
//! ```no_run
//! use bevy::prelude::*;
//! use bevy_simple_text_input::{TextInputBundle, TextInputPlugin};
//! use bevy_simple_text_input::{TextInput, TextInputPlugin};
//!
//! fn main() {
//! App::new()
Expand All @@ -18,7 +18,7 @@
//!
//! fn setup(mut commands: Commands) {
//! commands.spawn(Camera2dBundle::default());
//! commands.spawn((NodeBundle::default(), TextInputBundle::default()));
//! commands.spawn((NodeBundle::default(), TextInput));
//! }
//! ```

Expand All @@ -33,7 +33,7 @@ use bevy::{
window::{PrimaryWindow, WindowRef},
};

/// A Bevy `Plugin` providing the systems and assets required to make a [`TextInputBundle`] work.
/// A Bevy `Plugin` providing the systems and assets required to make a [`TextInput`] work.
pub struct TextInputPlugin;

/// Label for systems that update text inputs.
Expand Down Expand Up @@ -77,83 +77,31 @@ impl Plugin for TextInputPlugin {

const CURSOR_HANDLE: Handle<Font> = Handle::weak_from_u128(10482756907980398621);

/// A bundle providing the additional components required for a text input.
/// Marker component for a Text Input entity.
///
/// Add this to a Bevy `NodeBundle`.
/// Add this to a Bevy `NodeBundle`. In addition to its [required components](TextInput#impl-Component-for-TextInput), some other
/// components may also be spawned with it: [`TextInputCursorPos`].
///
/// # Example
///
/// ```rust
/// # use bevy::prelude::*;
/// use bevy_simple_text_input::TextInputBundle;
/// use bevy_simple_text_input::TextInput;
/// fn setup(mut commands: Commands) {
/// commands.spawn((NodeBundle::default(), TextInputBundle::default()));
/// commands.spawn((NodeBundle::default(), TextInput));
/// }
/// ```
#[derive(Bundle, Default, Reflect)]
pub struct TextInputBundle {
/// A component containing the text input's settings.
pub settings: TextInputSettings,
/// A component containing the Bevy `TextStyle` that will be used when creating the text input's inner Bevy `TextBundle`.
pub text_style: TextInputTextStyle,
/// A component containing a value indicating whether the text input is active or not.
pub inactive: TextInputInactive,
/// A component that manages the cursor's blinking.
pub cursor_timer: TextInputCursorTimer,
/// A component containing the current text cursor position.
pub cursor_pos: TextInputCursorPos,
/// A component containing the current value of the text input.
pub value: TextInputValue,
/// A component containing the placeholder text that is displayed when the text input is empty and not focused.
pub placeholder: TextInputPlaceholder,
/// This component's value is managed by Bevy's UI systems and enables tracking of hovers and presses.
pub interaction: Interaction,
}

impl TextInputBundle {
/// Returns this [`TextInputBundle`] with a new [`TextInputValue`] containing the provided `String`.
///
/// This also sets [`TextInputCursorPos`] so that the cursor position is at the end of the provided `String`.
pub fn with_value(mut self, value: impl Into<String>) -> Self {
let owned = value.into();

self.cursor_pos = TextInputCursorPos(owned.len());
self.value = TextInputValue(owned);

self
}

/// Returns this [`TextInputBundle`] with a new [`TextInputPlaceholder`] containing the provided `String`.
pub fn with_placeholder(
mut self,
placeholder: impl Into<String>,
text_style: Option<TextStyle>,
) -> Self {
self.placeholder = TextInputPlaceholder {
value: placeholder.into(),
text_style,
};
self
}

/// Returns this [`TextInputBundle`] with a new [`TextInputTextStyle`] containing the provided Bevy `TextStyle`.
pub fn with_text_style(mut self, text_style: TextStyle) -> Self {
self.text_style = TextInputTextStyle(text_style);
self
}

/// Returns this [`TextInputBundle`] with a new [`TextInputInactive`] containing the provided `bool`.
pub fn with_inactive(mut self, inactive: bool) -> Self {
self.inactive = TextInputInactive(inactive);
self
}

/// Returns this [`TextInputBundle`] with a new [`TextInputSettings`].
pub fn with_settings(mut self, settings: TextInputSettings) -> Self {
self.settings = settings;
self
}
}
#[derive(Component)]
#[require(
TextInputSettings,
TextInputTextStyle,
TextInputInactive,
TextInputCursorTimer,
TextInputValue,
TextInputPlaceholder,
Interaction
)]
pub struct TextInput;

/// The Bevy `TextStyle` that will be used when creating the text input's inner Bevy `TextBundle`.
#[derive(Component, Default, Reflect)]
Expand Down Expand Up @@ -603,17 +551,27 @@ fn create(
trigger: Trigger<OnAdd, TextInputValue>,
mut commands: Commands,
query: Query<(
Entity,
&TextInputTextStyle,
&TextInputValue,
&TextInputCursorPos,
Option<&TextInputCursorPos>,
&TextInputInactive,
&TextInputSettings,
&TextInputPlaceholder,
)>,
) {
if let Ok((style, text_input, cursor_pos, inactive, settings, placeholder)) =
if let Ok((entity, style, text_input, maybe_cursor_pos, inactive, settings, placeholder)) =
&query.get(trigger.entity())
{
let cursor_pos = match maybe_cursor_pos {
None => {
let len = text_input.0.len();
commands.entity(*entity).insert(TextInputCursorPos(len));
len
}
Some(cursor_pos) => cursor_pos.0,
};

let mut sections = vec![
// Pre-cursor
TextSection {
Expand Down Expand Up @@ -642,7 +600,7 @@ fn create(

set_section_values(
&masked_value(&text_input.0, settings.mask_character),
cursor_pos.0,
cursor_pos,
&mut sections,
);

Expand Down

0 comments on commit 68e8564

Please sign in to comment.