bevy-toolbox
A simple crate that provides macros for simplifying some common Bevy tasks.
Table of Contents:
spawn!
This macro is used to simplify the entity creation of the bevy engine.
Spawner
Spawner is the object that have spawn
method which takes a bevy bundle and returns
EntityCommands
as the result.
Top level
Top level means the part of the macro thats been directly quoted by the macro itself.
spawn!
Entity definition
An entity definition is a tuple of components that will be spawned as an entity.
spawn!
Top level can accept multiple entity definitions.
spawn!
Order
The order of any bit in the macro matters. The execution order is strictly follow the macro input.
// entity `a` will always being spawned before `b`
spawn!
Naming
An entity can be named for later reference. The variable will hold the Entity
of the corresponding
entity, NOT THE EntityCommands
.
spawn!
Parenting
A top level entities can have explicit parent. Parenting is done by using >
operator.
spawn!
Insertion
Insertion is a way to add some components to an existing entity. The entity must be named and spawned in advanced in order to be referenced.
spawn!
Code block injection
Since the entities inside the macro is enclosed within a generated scope to prevent the namespace pollution, code block injection makes it possible to execute code without leaving the macro.
spawn!
Extension
An entity can be extended with any number of:
- Method Call
- Code Block
All extensions are started with .
after the entity definition.
Method Call
Method call is a call to a method of EntityCommands
. The auto completion is supported for the
method name and the arguments.
spawn!
Since observe
is most likely to be used, a shortcut is provided to omit the method name.
spawn!
To reference the current entity, you can use this
for Entity
and entity
for EntityCommands
.
spawn!
Code Block
Code block is a block of code that will be executed in the context of the entity. As previously
mentioned, the code block can also access this
and entity
variables.
spawn!
Children Group
Children group is a group of entities quoted by []
after the .
. The entities in the group will
be spawned as children of the parent entity. One entity can have multiple children groups, but all
of them have to be after the extensions. This is because the spawner
ownership will be temporarily
taken for method calls and code blocks, to prevent this from happening, the children group is forced
to be the last part of the entity definition. Within the same group, the entities can reference
each other, but entities in 2 different groups under same parent can't.
spawn!
v!
This macro is used to simplify the creation of the bevy's Val
enum.
Syntax
There are 7 variants of the Val
enum, so 7 corresponding syntaxes are provided.
+
in between tokens means there can be no space between them.
Val::Auto
-auto
,@
Val::Percent
-number '%'
space is optional (e.g.10%
)Val::Px
-number + 'px'
(e.g.10px
)Val::Vw
-number + 'vw'
(e.g.10vw
)Val::Vh
-number + 'vh'
(e.g.10vh
)Val::Vmin
-number + 'vmin'
(e.g.10vmin
)Val::Vmax
-number + 'vmax'
(e.g.10vmax
)
v!;
v!;
v!;
v!;
v!; // space not allowed, error will be thrown
c!
This macro is used to simplify the creation of the bevy's Color
enum. The syntax is mimicking
the CSS color syntax. The macro fully supports the auto completion for the color names and the
color spaces.
Syntax
Hex
Hex colors are color codes that starts with #
followed by 3, 4, 6, or 8 hex characters. The digits
are not case sensitive, so #fff
is equivalent to #FFF
. The color will be converted to srgb
color space.
- 3 hex digits -
#rgb
(equivalent to#rrggbb
) - 4 hex digits -
#rgba
(equivalent to#rrggbbaa
) - 6 hex digits -
#rrggbb
- 8 hex digits -
#rrggbbaa
c!;
c!;
Functional Notation
All functional notations can have 3 or 4 arguments. The 4th argument is the alpha channel, if not
provided, it will be set to 1.0
.
srgb
- Standard RGB color spacelinear
- Linear RGB color spacehsl
- Hue, Saturation, Lightnesshsv
- Hue, Saturation, Valuehwb
- Hue, Whiteness, Blacknesslab
- L*a*b* color spacelch
- Luminance, Chroma, Hueoklab
- Oklab color spaceoklch
- Oklch color spacexyz
- CIE 1931 XYZ color space
With the function selected, follow it with the arguments in the parentheses.
c!;
c!;
CSS Named Colors
There are 149 named colors in CSS. The auto completion is supported for all of them. The color will
be converted to srgb
color space.
c!;
c!;
No wrap
The c!
macro by default will wrap the color with Color
enum, but sometimes you might just want
the inner value that color represents. To do this, simply adding !
before the color.
c!; // Color::Srgba(Srgba::new(1.0, 1.0, 1.0, 1.0))
c!; // Srgba::new(0.0, 0.0, 0.0, 1.0)