Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Bevy Opacity Tree
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Libraries
Bevy Opacity Tree
Commits
478c1b69
Verified
Commit
478c1b69
authored
1 month ago
by
Louis
Browse files
Options
Downloads
Patches
Plain Diff
Update README.md
parent
edd12683
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
Cargo.toml
+2
-0
2 additions, 0 deletions
Cargo.toml
README.md
+86
-0
86 additions, 0 deletions
README.md
src/opacity_tree.rs
+13
-0
13 additions, 0 deletions
src/opacity_tree.rs
with
101 additions
and
0 deletions
Cargo.toml
+
2
−
0
View file @
478c1b69
...
@@ -2,6 +2,8 @@
...
@@ -2,6 +2,8 @@
name
=
"weirdboi_opacity_tree"
name
=
"weirdboi_opacity_tree"
version
=
"0.1.0"
version
=
"0.1.0"
edition
=
"2024"
edition
=
"2024"
license
=
"Apache-2.0"
description
=
"Adds push down opacity hierarchy to Bevy entities"
[features]
[features]
default
=
[
"reflect"
,
"ui"
,
"sprite"
]
default
=
[
"reflect"
,
"ui"
,
"sprite"
]
...
...
This diff is collapsed.
Click to expand it.
README.md
0 → 100644
+
86
−
0
View file @
478c1b69
# Opacity Tree
Adds hierarchical inheritance of opacity to Bevy entities.
## Usage
1.
Add weirdboi_opacity_tree to you Cargo.toml
2.
Include the plugin in your app
3.
Add the
`TreeOpacity`
component to any supported node whose children should inherit its opacity
4.
In any system, set the value of the
`TreeOpacity`
component
```
toml
# There is no current crates release
weirdboi_opacity_tree
=
{
git
=
"https://weirdboi.dev/libraries/weirdboi-opacity-tree.git"
}
```
```
rust
//! Entry point for your game, main.rs
use
weirdboi_opacity_tree
::
TreeOpacityPlugin
;
fn
main
()
{
// Create your app, then..
app
.add_plugins
(
TreeOpacityPlugin
);
}
```
```
rust
//! Some gameplay code, where a window has to fade out
pub
fn
spawn_entity
(
mut
commands
:
Commands
)
{
commands
.spawn
((
Node
::
default
(),
ImageNode
::
default
(),
TreeOpacity
::
default
(),
// Default value is 1, full opacity
))
.with_children
(|
children
|
{
children
.spawn
(
Text
::
new
(
"Some value"
));
// The opacity of the text will sync with the parent node
});
}
pub
fn
alter_entity
(
mut
query
:
Query
<&
mut
TreeOpacity
>
)
{
for
mut
root
in
&
mut
query
{
root
.set_opacity
(
0.0
);
// Hide the entire tree of elements
}
}
```
## Supported Components
This library add component support for built in Bevy components by default, although this can be disabled by turning off
default features when adding to you Cargo.toml. The following feature flags will enable different sets of components:
-
`ui`
: Enables support for
`TextColor`
,
`TextShadow`
,
`ImageNode`
,
`BackgroundColor`
, and
`BorderColor`
-
`sprite`
: Enables support for
`Sprite`
## Ignoring Changes
If a particular component
`C`
should not have its opacity modified as part of a tree, you can add the
`IgnoreOpacity<C>`
component to its entity. This is typically desirable for things like Text nodes, where the required components will add
elements that you don't want to change.
This use case is also supported by the
`presets`
module, which exports utility functions for common bundles of ignored
changes - e.g.
`presets::text_node_opacity_preset()`
will return a bundle that ignores changes for Background and Border
color. Without these
`IgnoreOpacity`
components, the background of a text node would become black when setting the opacity
of a parent to 1
## Custom Components
Any
**mutable**
component can be included in the opacity tree calculations by implementing
`HandlesTreeOpacity`
and then registering
it with your app. Immutable components cannot be registered for handling by the opacity tree.
```
rust
#[derive(Component)]
struct
MyCustomRenderable
{
alpha
:
f32
,
}
impl
HandlesTreeOpacity
for
MyCustomRenderable
{
fn
on_change_opacity
(
&
mut
self
,
opacity
:
f32
)
{
self
.alpha
=
opacity
;
}
}
pub
fn
my_plugin
(
app
:
&
mut
App
)
{
app
.register_opacity_type
::
<
MyCustomRenderable
>
();
}
```
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/opacity_tree.rs
+
13
−
0
View file @
478c1b69
...
@@ -17,6 +17,19 @@ impl Default for TreeOpacity {
...
@@ -17,6 +17,19 @@ impl Default for TreeOpacity {
Self
(
1.0
)
Self
(
1.0
)
}
}
}
}
impl
From
<
f32
>
for
TreeOpacity
{
fn
from
(
value
:
f32
)
->
Self
{
Self
(
value
)
}
}
impl
TreeOpacity
{
pub
fn
opacity
(
&
self
)
->
f32
{
self
.0
}
pub
fn
set_opacity
(
&
mut
self
,
opacity
:
f32
)
{
self
.0
=
opacity
;
}
}
pub
trait
HandlesTreeOpacity
:
Component
<
Mutability
=
Mutable
>
{
pub
trait
HandlesTreeOpacity
:
Component
<
Mutability
=
Mutable
>
{
fn
on_change_opacity
(
&
mut
self
,
opacity
:
f32
);
fn
on_change_opacity
(
&
mut
self
,
opacity
:
f32
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment