Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
Godot: Support vertical sprite flip (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
porkbrain authored May 17, 2024
1 parent 341b284 commit 5b1284e
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 7 deletions.
2 changes: 2 additions & 0 deletions main_game_lib/src/rscn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub struct SpriteTexture {
pub animation: Option<SpriteFrames>,
/// If the texture should be flipped horizontally.
pub flip_horizontally: bool,
/// If the texture should be flipped vertically.
pub flip_vertically: bool,
}

/// Atlas animation.
Expand Down
2 changes: 2 additions & 0 deletions main_game_lib/src/rscn/intermediate_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ pub(crate) enum SectionKey {
Visibility(bool),
/// A texture should be flipped horizontally.
FlipHorizontally(bool),
/// A texture should be flipped vertically.
FlipVertically(bool),
/// RGBa
SelfModulateColor(Number, Number, Number, Number),
}
Expand Down
2 changes: 2 additions & 0 deletions main_game_lib/src/rscn/spawner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ fn node_to_entity<T: TscnSpawner>(
visible,
color,
flip_horizontally,
flip_vertically,
}) = texture
{
let texture = spawner.load_texture(&path);
cmd.entity(entity).insert(texture).insert(Sprite {
color: color.unwrap_or(Color::WHITE),
flip_x: flip_horizontally,
flip_y: flip_vertically,
..default()
});

Expand Down
20 changes: 13 additions & 7 deletions main_game_lib/src/rscn/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,18 @@ fn parse_with_state(

Expecting::HeadingOrSectionKey
}
Expecting::SectionKey(SectionKeyBuilder::FlipHorizontally) => {
Expecting::SectionKey(
dir @ (SectionKeyBuilder::FlipHorizontally
| SectionKeyBuilder::FlipVertically),
) => {
let visible = matches!(token, TscnToken::True);
state
.nodes
.last_mut()
.unwrap()
.section_keys
.push(SectionKey::FlipHorizontally(visible));
state.nodes.last_mut().unwrap().section_keys.push(
if matches!(dir, SectionKeyBuilder::FlipHorizontally) {
SectionKey::FlipHorizontally(visible)
} else {
SectionKey::FlipVertically(visible)
},
);

Expecting::HeadingOrSectionKey
}
Expand Down Expand Up @@ -371,6 +375,8 @@ enum SectionKeyBuilder {
Visibility,
/// true or false
FlipHorizontally,
/// true or false
FlipVertically,
/// e.g. `self_modulate = Color(1, 1, 1, 0.823529)`
SelfModulate(ColorExpecting),
}
Expand Down
3 changes: 3 additions & 0 deletions main_game_lib/src/rscn/token/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub(super) fn parse(
"flip_h" => {
Expecting::SectionKey(SectionKeyBuilder::FlipHorizontally)
}
"flip_v" => {
Expecting::SectionKey(SectionKeyBuilder::FlipVertically)
}
s if s.starts_with("metadata/") => {
Expecting::SectionKey(SectionKeyBuilder::StringMetadata(
s["metadata/".len()..].to_ascii_lowercase(),
Expand Down
9 changes: 9 additions & 0 deletions main_game_lib/src/rscn/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Properties {
visible: bool,
color: Option<Color>,
flip_horizontally: bool,
flip_vertically: bool,
}

pub(crate) fn from_state(
Expand Down Expand Up @@ -88,6 +89,7 @@ pub(crate) fn from_state(
visible,
color,
flip_horizontally,
flip_vertically,
} = properties;

let in_2d = match parsed_node.kind {
Expand All @@ -103,6 +105,7 @@ pub(crate) fn from_state(
animation
},
flip_horizontally,
flip_vertically,
}),
}),
ParsedNodeKind::Sprite2D => Some(In2D {
Expand All @@ -117,6 +120,7 @@ pub(crate) fn from_state(
None
},
flip_horizontally,
flip_vertically,
}),
}),
ParsedNodeKind::Node2D => Some(In2D {
Expand Down Expand Up @@ -184,6 +188,7 @@ fn apply_section_key(
visible,
color,
flip_horizontally,
flip_vertically,
}: &mut Properties,
) {
use intermediate_repr::{Number, SectionKey, X};
Expand Down Expand Up @@ -213,6 +218,9 @@ fn apply_section_key(
SectionKey::FlipHorizontally(flip) => {
*flip_horizontally = flip;
}
SectionKey::FlipVertically(flip) => {
*flip_vertically = flip;
}
SectionKey::Visibility(visibility) => {
*visible = visibility;
}
Expand Down Expand Up @@ -379,6 +387,7 @@ impl Default for Properties {
visible: true,
color: None,
flip_horizontally: false,
flip_vertically: false,
}
}
}
1 change: 1 addition & 0 deletions wiki/src/devtools_godot.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ This hierarchical arrangement allows for precise control over the rendering orde
- Visibility / Self Modulate color
- Ordering / Z Index
- Offset / Flip H
- Offset / Flip V

## Components

Expand Down

0 comments on commit 5b1284e

Please sign in to comment.