Compare commits
7 Commits
ed2e1e75ef
...
submission
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c042f9937 | |||
| ea9055e46c | |||
| be9cb3fe69 | |||
| 6fb3539329 | |||
| fb8a27325e | |||
| 4a80b4f4ad | |||
| f470687494 |
@@ -16,16 +16,22 @@ pub mod machines {
|
|||||||
|
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::game::consumables::Fuel;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct CuttingMachine;
|
pub struct CuttingMachine;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct RotatingMachine;
|
pub struct RotatingMachine;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct FlippingMachine;
|
pub struct FlippingMachine;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
#[require(Fuel)]
|
||||||
pub struct TransposingMachine;
|
pub struct TransposingMachine;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,8 +39,13 @@ pub mod consumables {
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
pub struct Fuel(u32);
|
pub struct Fuel(pub u32);
|
||||||
|
|
||||||
|
impl Default for Fuel {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self(5)
|
||||||
|
}
|
||||||
|
}
|
||||||
#[derive(Event)]
|
#[derive(Event)]
|
||||||
pub struct FuelChanged;
|
pub struct FuelChanged;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ fn dummy_machines(mut commands: Commands) {
|
|||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
RotatingMachine,
|
||||||
Sprite::from_color(GREEN, Vec2::splat(40.0)),
|
Sprite::from_color(GREEN, Vec2::splat(40.0)),
|
||||||
Transform::from_translation(Vec3::new(40.0, 40.0, 0.0)),
|
Transform::from_translation(Vec3::new(40.0, 40.0, 0.0)),
|
||||||
Pickable::default(),
|
Pickable::default(),
|
||||||
@@ -64,6 +65,7 @@ fn dummy_machines(mut commands: Commands) {
|
|||||||
// TODO: The other observers, once they have a spawner struct & impl.
|
// TODO: The other observers, once they have a spawner struct & impl.
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
FlippingMachine,
|
||||||
Sprite::from_color(PURPLE, Vec2::splat(40.)),
|
Sprite::from_color(PURPLE, Vec2::splat(40.)),
|
||||||
Transform::from_translation(Vec3::new(-40.0, -40.0, 0.0)),
|
Transform::from_translation(Vec3::new(-40.0, -40.0, 0.0)),
|
||||||
Pickable::default(),
|
Pickable::default(),
|
||||||
@@ -72,6 +74,7 @@ fn dummy_machines(mut commands: Commands) {
|
|||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
|
TransposingMachine,
|
||||||
Sprite::from_color(DARK_ORANGE, Vec2::splat(40.)),
|
Sprite::from_color(DARK_ORANGE, Vec2::splat(40.)),
|
||||||
Transform::from_translation(Vec3::new(40.0, -40.0, 0.0)),
|
Transform::from_translation(Vec3::new(40.0, -40.0, 0.0)),
|
||||||
Pickable::default(),
|
Pickable::default(),
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
use bevy::{color::palettes::css::*, prelude::*, ui::Val::*};
|
use bevy::{
|
||||||
|
color::palettes::css::*,
|
||||||
|
ecs::{component::HookContext, world::DeferredWorld},
|
||||||
|
prelude::*,
|
||||||
|
ui::Val::*,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::game::consumables::{Fuel, FuelChanged};
|
use crate::game::consumables::{Fuel, FuelChanged};
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
#[require(Label)]
|
#[require(Label)]
|
||||||
|
#[component(on_add = FuelGauge::refresh_label)]
|
||||||
pub struct FuelGauge(Entity);
|
pub struct FuelGauge(Entity);
|
||||||
|
|
||||||
impl FuelGauge {
|
impl FuelGauge {
|
||||||
@@ -41,4 +47,23 @@ impl FuelGauge {
|
|||||||
);
|
);
|
||||||
label.0 = format!("Fuel: {}", fuel.0);
|
label.0 = format!("Fuel: {}", fuel.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn refresh_label(mut world: DeferredWorld, ctx: HookContext) {
|
||||||
|
// Get the machine ID from the FuelGauge component.
|
||||||
|
let Some(machine_id) = world.get::<FuelGauge>(ctx.entity).map(|fg| fg.0) else {
|
||||||
|
panic!("Couldn't get a FuelGauge during it's on-add hook run!");
|
||||||
|
};
|
||||||
|
// and fuel level
|
||||||
|
let Some(fuel) = world.get::<Fuel>(machine_id).map(|lvl| lvl.0) else {
|
||||||
|
// TODO: Maybe don't panic and just emit a warning.
|
||||||
|
panic!("Couldn't get a Fuel for a machine during a FuelGauge on-add hook.");
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get an excl ref to the Text component so we can redraw its text
|
||||||
|
let Some(mut label) = world.get_mut::<Text>(ctx.entity) else {
|
||||||
|
panic!()
|
||||||
|
};
|
||||||
|
|
||||||
|
label.0 = format!("Fuel: {}", fuel);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ impl SpawnUi for CuttingMachine {
|
|||||||
commands.entity(base_entity).with_children(|commands| {
|
commands.entity(base_entity).with_children(|commands| {
|
||||||
// Left panel. For fuel or machine stats or whatever.
|
// Left panel. For fuel or machine stats or whatever.
|
||||||
// TODO: Pass along target machine, not the UI's root entity.
|
// TODO: Pass along target machine, not the UI's root entity.
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder for the Card view)
|
// Center panel (placeholder for the Card view)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -52,7 +52,7 @@ impl SpawnUi for RotatingMachine {
|
|||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
|
let base_entity = machine_ui_base(&mut commands, "Rotating Machine", &theme);
|
||||||
commands.entity(base_entity).with_children(|commands| {
|
commands.entity(base_entity).with_children(|commands| {
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder for input-output rotation)
|
// Center panel (placeholder for input-output rotation)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -87,7 +87,7 @@ impl SpawnUi for FlippingMachine {
|
|||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
|
let base_entity = machine_ui_base(&mut commands, "Flipping Machine", &theme);
|
||||||
commands.entity(base_entity).with_children(|commands| {
|
commands.entity(base_entity).with_children(|commands| {
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder)
|
// Center panel (placeholder)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -122,7 +122,7 @@ impl SpawnUi for TransposingMachine {
|
|||||||
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
fn spawn_ui(mut commands: Commands, theme: Res<UiTheme>, machine_id: Entity) {
|
||||||
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
|
let base_entity = machine_ui_base(&mut commands, "Transposing Machine", &theme);
|
||||||
commands.entity(base_entity).with_children(|commands| {
|
commands.entity(base_entity).with_children(|commands| {
|
||||||
commands.spawn(FuelGauge::bundle(Entity::PLACEHOLDER));
|
commands.spawn(FuelGauge::bundle(machine_id));
|
||||||
|
|
||||||
// Center panel (placeholder)
|
// Center panel (placeholder)
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ impl Plugin for GameUiPlugin {
|
|||||||
app
|
app
|
||||||
.init_resource::<UiTheme>()
|
.init_resource::<UiTheme>()
|
||||||
.register_type::<UiTheme>()
|
.register_type::<UiTheme>()
|
||||||
|
.add_observer(HoverBackgroundcolor::hover_start)
|
||||||
|
.add_observer(HoverBackgroundcolor::hover_stop)
|
||||||
.add_observer(CloseButton::hover_start)
|
.add_observer(CloseButton::hover_start)
|
||||||
.add_observer(CloseButton::hover_stop)
|
.add_observer(CloseButton::hover_stop)
|
||||||
.add_observer(CloseButton::press_start)
|
.add_observer(CloseButton::press_start)
|
||||||
@@ -216,6 +218,7 @@ impl CloseButton {
|
|||||||
BackgroundColor(GRAY.into()),
|
BackgroundColor(GRAY.into()),
|
||||||
BorderColor(DARK_GRAY.into()),
|
BorderColor(DARK_GRAY.into()),
|
||||||
BorderRadius::all(Px(2.0)),
|
BorderRadius::all(Px(2.0)),
|
||||||
|
HoverBackgroundcolor::new(GREEN.into()),
|
||||||
children![(
|
children![(
|
||||||
// TODO: Replace with an icon/sprite
|
// TODO: Replace with an icon/sprite
|
||||||
Text::new("X"),
|
Text::new("X"),
|
||||||
@@ -233,7 +236,7 @@ impl CloseButton {
|
|||||||
) {
|
) {
|
||||||
// Get the components for only the Trigger's target entity
|
// Get the components for only the Trigger's target entity
|
||||||
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
|
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
|
||||||
bg.0 = ui_theme.quiet_hover_bg;
|
// bg.0 = ui_theme.quiet_hover_bg;
|
||||||
border.0 = ui_theme.quiet_hover_border;
|
border.0 = ui_theme.quiet_hover_border;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -244,7 +247,7 @@ impl CloseButton {
|
|||||||
ui_theme: Res<UiTheme>,
|
ui_theme: Res<UiTheme>,
|
||||||
) {
|
) {
|
||||||
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
|
if let Ok((mut bg, mut border)) = button_colors.get_mut(event.target()) {
|
||||||
bg.0 = ui_theme.quiet_bg;
|
// bg.0 = ui_theme.quiet_bg;
|
||||||
border.0 = ui_theme.quiet_border;
|
border.0 = ui_theme.quiet_border;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -273,3 +276,48 @@ impl CloseButton {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Background color to use for an item being hovered over.
|
||||||
|
///
|
||||||
|
/// Adding this to an entity both sets the color and implicitly enables the
|
||||||
|
/// on-hover behavior. Without it, the system(s) don't find the entity and so
|
||||||
|
/// no color changing is exhibited.
|
||||||
|
#[derive(Component)]
|
||||||
|
struct HoverBackgroundcolor {
|
||||||
|
pub color: Color,
|
||||||
|
// hold onto the original so it can be put back
|
||||||
|
orig_bg: Option<Color>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HoverBackgroundcolor {
|
||||||
|
fn new(color: Color) -> Self {
|
||||||
|
Self {
|
||||||
|
color,
|
||||||
|
orig_bg: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hover_start(
|
||||||
|
event: Trigger<Pointer<Over>>,
|
||||||
|
// Get button background and border colors so we can change them.
|
||||||
|
// Filter for *changed* interactions, and only entities with a [`Button`]
|
||||||
|
mut colors: Query<(&mut HoverBackgroundcolor, &mut BackgroundColor)>,
|
||||||
|
) {
|
||||||
|
// Get the components for only the Trigger's target entity
|
||||||
|
if let Ok((mut hover, mut bg)) = colors.get_mut(event.target()) {
|
||||||
|
hover.orig_bg = Some(bg.0); // store the original
|
||||||
|
bg.0 = hover.color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hover_stop(
|
||||||
|
event: Trigger<Pointer<Out>>,
|
||||||
|
mut colors: Query<(&HoverBackgroundcolor, &mut BackgroundColor)>,
|
||||||
|
) {
|
||||||
|
if let Ok((hover, mut bg)) = colors.get_mut(event.target()) {
|
||||||
|
bg.0 = hover
|
||||||
|
.orig_bg
|
||||||
|
.expect("Tried to restore an original color but there was none!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user