Skip to content

Commit 542259a

Browse files
committed
Add Selection component
1 parent 9963750 commit 542259a

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

src/game/card/deck.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::ui::prelude::*;
1313
use crate::util::prelude::*;
1414

1515
pub(super) fn plugin(app: &mut App) {
16-
app.configure::<(Deck, DeckDisplay)>();
16+
app.configure::<(Deck, IsDeckDisplay)>();
1717
}
1818

1919
#[derive(Component, Reflect, Serialize, Deserialize, Default, Clone)]
@@ -95,11 +95,11 @@ fn advance_deck(
9595

9696
#[derive(Component, Reflect)]
9797
#[reflect(Component)]
98-
struct DeckDisplay {
98+
struct IsDeckDisplay {
9999
target: Entity,
100100
}
101101

102-
impl Configure for DeckDisplay {
102+
impl Configure for IsDeckDisplay {
103103
fn configure(app: &mut App) {
104104
app.register_type::<Self>();
105105
app.add_systems(
@@ -112,48 +112,43 @@ impl Configure for DeckDisplay {
112112
}
113113
}
114114

115-
impl Default for DeckDisplay {
115+
impl Default for IsDeckDisplay {
116116
fn default() -> Self {
117117
Self {
118118
target: Entity::PLACEHOLDER,
119119
}
120120
}
121121
}
122122

123-
// Clear deck display if its target or its target's deck has changed.
123+
/// Clear deck display on any change.
124124
fn clear_deck_display(
125125
mut commands: Commands,
126-
deck_display_query: Query<(Entity, &DeckDisplay)>,
127-
target_changed_query: Query<(), Changed<DeckDisplay>>,
126+
deck_display_query: Query<(Entity, &Selection), With<IsDeckDisplay>>,
127+
target_changed_query: Query<(), Changed<Selection>>,
128128
deck_changed_query: Query<(), Changed<Deck>>,
129129
) {
130-
// Clear display if target entity has not changed, but its deck has.
131-
for (entity, deck_display) in &deck_display_query {
132-
if !target_changed_query.contains(entity)
133-
&& !deck_changed_query.contains(deck_display.target)
134-
{
130+
for (entity, selection) in &deck_display_query {
131+
if !target_changed_query.contains(entity) && !deck_changed_query.contains(selection.0) {
135132
continue;
136133
}
137134

138135
commands.entity(entity).despawn_descendants();
139136
}
140137
}
141138

142-
// Populate deck display if its target or its target's deck has changed.
139+
/// Populate deck display on any change.
143140
fn populate_deck_display(
144141
mut commands: Commands,
145-
deck_display_query: Query<(Entity, &DeckDisplay)>,
142+
deck_display_query: Query<(Entity, &Selection), With<IsDeckDisplay>>,
146143
deck_query: Query<&Deck>,
147-
target_changed_query: Query<(), Changed<DeckDisplay>>,
144+
target_changed_query: Query<(), Changed<IsDeckDisplay>>,
148145
deck_changed_query: Query<(), Changed<Deck>>,
149146
) {
150-
for (entity, deck_display) in &deck_display_query {
151-
if !target_changed_query.contains(entity)
152-
&& !deck_changed_query.contains(deck_display.target)
153-
{
147+
for (entity, selection) in &deck_display_query {
148+
if !target_changed_query.contains(entity) && !deck_changed_query.contains(selection.0) {
154149
continue;
155150
}
156-
let deck = deck_query.get(deck_display.target).unwrap();
151+
let deck = deck_query.get(selection.0).unwrap();
157152

158153
commands.entity(entity).with_children(|children| {
159154
for (i, card_key) in deck.card_keys.iter().enumerate() {
@@ -176,7 +171,7 @@ pub fn deck_display(player: Entity) -> impl EntityCommand {
176171
},
177172
..default()
178173
},
179-
DeckDisplay { target: player },
174+
IsDeckDisplay { target: player },
180175
));
181176
}
182177
}

src/ui.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44

55
pub mod font;
66
pub mod interaction;
7+
pub mod selection;
78
pub mod tooltip;
89
pub mod widget;
910

1011
pub mod prelude {
1112
pub use bevy::ui::Val::*;
1213

13-
pub use super::font::*;
14-
pub use super::interaction::*;
14+
pub use super::font::parse_rich;
15+
pub use super::font::parse_rich_custom;
16+
pub use super::font::DynamicFontSize;
17+
pub use super::font::BOLD_FONT_HANDLE;
18+
pub use super::font::FONT_HANDLE;
19+
pub use super::font::THICK_FONT_HANDLE;
20+
pub use super::interaction::InteractionTable;
21+
pub use super::interaction::IsDisabled;
22+
pub use super::selection::Selection;
1523
pub use super::widget;
1624
pub use super::UiRoot;
1725
pub use crate::core::theme::ThemeColor;
@@ -28,7 +36,12 @@ use crate::util::prelude::*;
2836
pub(super) fn plugin(app: &mut App) {
2937
app.configure::<UiRoot>();
3038

31-
app.add_plugins((font::plugin, interaction::plugin, tooltip::plugin));
39+
app.add_plugins((
40+
font::plugin,
41+
interaction::plugin,
42+
selection::plugin,
43+
tooltip::plugin,
44+
));
3245
}
3346

3447
#[derive(Resource, Reflect)]

src/ui/font.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl DynamicFontSize {
7373
}
7474
}
7575

76-
pub fn apply_dynamic_font_size(
76+
fn apply_dynamic_font_size(
7777
window_root: Res<WindowRoot>,
7878
window_query: Query<&Window>,
7979
mut text_query: Query<(&DynamicFontSize, &Node, &mut Text)>,

src/ui/selection.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use bevy::prelude::*;
2+
3+
use crate::util::prelude::*;
4+
5+
pub(super) fn plugin(app: &mut App) {
6+
app.configure::<Selection>();
7+
}
8+
9+
/// The entity that a UI display will pull values from.
10+
#[derive(Component, Reflect)]
11+
#[reflect(Component)]
12+
pub struct Selection(pub Entity);
13+
14+
impl Configure for Selection {
15+
fn configure(app: &mut App) {
16+
app.register_type::<Self>();
17+
}
18+
}
19+
20+
impl Default for Selection {
21+
fn default() -> Self {
22+
Self(Entity::PLACEHOLDER)
23+
}
24+
}

0 commit comments

Comments
 (0)