https://github.com/dekirisu/mevy
Bevy macros - e.g. CSS-like syntax for bevy_ui & simple child spawning
https://github.com/dekirisu/mevy
bevy bevy-engine bevy-ui
Last synced: 9 months ago
JSON representation
Bevy macros - e.g. CSS-like syntax for bevy_ui & simple child spawning
- Host: GitHub
- URL: https://github.com/dekirisu/mevy
- Owner: dekirisu
- License: apache-2.0
- Created: 2025-01-01T11:58:31.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-10T00:09:26.000Z (10 months ago)
- Last Synced: 2025-03-30T06:04:30.770Z (9 months ago)
- Topics: bevy, bevy-engine, bevy-ui
- Language: Rust
- Homepage:
- Size: 114 KB
- Stars: 70
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
[!IMPORTANT]
> This crate is meant to provide macros only - no additional bevy plugins, resources, components or systems
## Simpler Hierarchy Spawning
Spawn children just by stating `[]` - the 'names' are just variables containing their `Entity`
- those variables can be used anywhere in the macro - even 'before'
- [read more](crates/ecs/README.md) or see [this example](examples/ecs_simple_spawn.rs).
```rust
spawn!{
// component/bundle;
// .method(..);
SpecificChild(optional_child_name);
[optional_child_name][
// component;
// .method(..);
]
}
```
## CSS-like notation for bevy_ui
Using `ui!((..))` (inner round braces) will return a tuple of **mentioned components** only.
- read about **available fields**, custom fields & notation in [this readme](crates/ui/README.md)
- see [this example](examples/ui_bundle.rs).
```rust
c.spawn(ui!((
size: 100px 100px;
border: 5px #ff0000;
box_shadow: 10% 10% 3px 8px #ffaa44;
background: #ffffff;
border_radius: 6px;
neat_outline;
)?));
//^ optional ? (or any token): hovering shows the returned tuple (if LSP used)
/// function as custom fields or p refabs
fn neat_outline() -> Outline {ui!((
outline: 3px 1px #00ff00;
))}
```
## Code Replacement Macro
Using the `code!{}` macro simplifies constructing:
- `Color` by writing `#rgb`/`#rgba`/`#rrggbb`/`#rrggbbaa`
- `Val` by writing `0px`/`0%`/`0vw`/`0vh`/`0vmin`/`0vmax`/`@`(auto)
- `UiRect` by writing `[>0px]`/`[>0px 0px]`/`[>0px 0px 0px]`/`[>0px 0px 0px 0px]` (css-like)
So you can do fun things like:
```rust
let shadow = code!{BoxShadow{
// use #... is replaced with Color, meaning you can e.g. use methods
color: #FF1265.mix(F93ECA,0.4).with_alpha(0.2),
x_offset: 100px,
y_offset: 50%,
spread_radius: 3.1vh,
blur_radius: 40.23vmax,
}}};
let color = code!{#FF0000};
// or multiple things in the macro
code!{
let color2 = #00FF00;
let color3 = #6600AA;
}
println!{"{color2:?}"}
```
## Version
Just to mention the obvious:
- Macros are token-based, meaning they aren't hard-bound to a specific bevy version
- **However:** These are mainly designed for **bevy 0.15** and onwards
- The closer your bevy version is to **0.15**, the more things will work
## Design
Crates are separated into:
- `crate/*/syntax`: token handling, meant to be reusable
- `crate/*`: actual macros, based on that 'syntax'
> [!NOTE]
> **Only relevant if you dig deeper into this crate:** The versions of those are not hard linked, since the macros can keep (or gain) features, even if the the syntax api has changed. So if one of those is `0.2.x` and the other `0.5.x` at some point, don't worry.