Skip to content

Commit

Permalink
[#36] Fix hairline
Browse files Browse the repository at this point in the history
  • Loading branch information
Orchaldir committed Sep 11, 2023
1 parent fea1b4b commit 60e46a0
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 78 deletions.
51 changes: 25 additions & 26 deletions rpg_tools_core/src/model/character/appearance/hair/hairline.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,46 @@
use crate::model::size::Size;
use crate::ui::parser::get_enum;
use crate::ui::parser::UiParser;
use crate::ui::{UiVisitor, UI};
use macro_convert::Convert;
use macro_ui::ui;
use serde::{Deserialize, Serialize};

/// What type of hairline? It is not visible by some hair styles.
///
/// The [`size`](Size) defines the y position of the hairline.
#[derive(ui, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum Hairline {
Round {
size: Size,
},
Straight {
size: Size,
},
Triangle {
size: Size,
},
/// /// What style of hairline? It is not visible by some hair styles.
#[derive(Convert, ui, Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub enum HairlineStyle {
#[default]
Round,
Straight,
Triangle,
/// ```svgbob
/// +----* *----+
/// / \/ \
/// | |
/// ```
WidowsPeak {
size: Size,
},
WidowsPeak,
}

/// How does the hairline look like?

#[derive(ui, Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Hairline {
pub style: HairlineStyle,
/// Defines the y position of the hairline.
pub size: Size,
}

impl Hairline {
pub fn get_y_position(&self) -> Size {
match self {
Hairline::Round { size } => *size,
Hairline::Straight { size } => *size,
Hairline::Triangle { size } => *size,
Hairline::WidowsPeak { size } => *size,
}
pub fn new(style: HairlineStyle, size: Size) -> Self {
Self { style, size }
}
}

impl Default for Hairline {
fn default() -> Self {
Self::Round { size: Size::Medium }
Self {
style: HairlineStyle::default(),
size: Size::Medium,
}
}
}
4 changes: 2 additions & 2 deletions rpg_tools_rendering/examples/beards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rpg_tools_core::model::character::appearance::ear::Ears;
use rpg_tools_core::model::character::appearance::eye::pupil::PupilShape;
use rpg_tools_core::model::character::appearance::eye::shape::EyeShape;
use rpg_tools_core::model::character::appearance::eye::{Eye, Eyes};
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::short::ShortHair;
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::{Head, HeadShape};
Expand Down Expand Up @@ -89,7 +89,7 @@ fn create_appearance(height: Length, beard: &Beard, face: &HeadShape) -> Appeara
},
hair: Hair::Short {
style: ShortHair::MiddlePart,
hairline: Hairline::Round { size: Medium },
hairline: Hairline::new(HairlineStyle::Round, Medium),
color: Color::SaddleBrown,
},
mouth: Mouth::Simple {
Expand Down
6 changes: 3 additions & 3 deletions rpg_tools_rendering/examples/beards_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rpg_tools_core::model::character::appearance::ear::Ears;
use rpg_tools_core::model::character::appearance::eye::pupil::PupilShape;
use rpg_tools_core::model::character::appearance::eye::shape::EyeShape;
use rpg_tools_core::model::character::appearance::eye::{Eye, Eyes};
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::short::ShortHair;
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::{Head, HeadShape};
Expand Down Expand Up @@ -55,7 +55,7 @@ fn create_appearance(height: Length, beard: &Beard, face: &HeadShape) -> Appeara
Body {
shape: BodyShape::Rectangle,
width: Width::Average,
skin: Skin::akin(SkinColor::Light),
skin: Skin::normal(SkinColor::Light),
},
Head {
ears: Ears::Normal {
Expand All @@ -74,7 +74,7 @@ fn create_appearance(height: Length, beard: &Beard, face: &HeadShape) -> Appeara
},
hair: Hair::Short {
style: ShortHair::MiddlePart,
hairline: Hairline::Round { size: Medium },
hairline: Hairline::new(HairlineStyle::Round, Medium),
color: Color::SaddleBrown,
},
mouth: Mouth::Simple {
Expand Down
4 changes: 2 additions & 2 deletions rpg_tools_rendering/examples/ears.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rpg_tools_core::model::character::appearance::ear::Ears;
use rpg_tools_core::model::character::appearance::eye::pupil::PupilShape;
use rpg_tools_core::model::character::appearance::eye::shape::EyeShape;
use rpg_tools_core::model::character::appearance::eye::{Eye, Eyes};
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::short::ShortHair;
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::{Head, HeadShape};
Expand Down Expand Up @@ -59,7 +59,7 @@ fn create_appearance(height: Length, ears: &Ears, face: &HeadShape) -> Appearanc
},
hair: Hair::Short {
style: ShortHair::SidePart { side: Left },
hairline: Hairline::Round { size: Medium },
hairline: Hairline::new(HairlineStyle::Round, Medium),
color: Color::Yellow,
},
mouth: Mouth::Simple {
Expand Down
6 changes: 3 additions & 3 deletions rpg_tools_rendering/examples/eyebrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rpg_tools_core::model::character::appearance::eye::brow::EyeBrows;
use rpg_tools_core::model::character::appearance::eye::pupil::PupilShape;
use rpg_tools_core::model::character::appearance::eye::shape::EyeShape;
use rpg_tools_core::model::character::appearance::eye::{Eye, Eyes};
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::short::ShortHair;
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::{Head, HeadShape};
Expand Down Expand Up @@ -85,7 +85,7 @@ fn create_appearance(
},
hair: Hair::Short {
style: ShortHair::MiddlePart,
hairline: Hairline::Round { size: Medium },
hairline: Hairline::new(HairlineStyle::Round, Medium),
color: Color::SaddleBrown,
},
mouth: Mouth::Simple {
Expand All @@ -95,7 +95,7 @@ fn create_appearance(
teeth_color: TeethColor::White,
},
shape: HeadShape::Round,
skin: Skin::akin(SkinColor::Light),
skin: Skin::normal(SkinColor::Light),
},
height,
)
Expand Down
4 changes: 2 additions & 2 deletions rpg_tools_rendering/examples/hair_bun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate rpg_tools_rendering;
use crate::utils::appearance::create_head_with_hair;
use crate::utils::render::render_2_sets;
use rpg_tools_core::model::character::appearance::hair::bun::BunStyle;
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::HeadShape;
use rpg_tools_core::model::color::Color;
Expand Down Expand Up @@ -34,7 +34,7 @@ fn create_bun(style: BunStyle, size: Size) -> Hair {
Hair::Bun {
style,
size,
hairline: Hairline::Straight { size: Size::Small },
hairline: Hairline::new(HairlineStyle::Straight, Size::Small),
color: Color::SaddleBrown,
}
}
4 changes: 2 additions & 2 deletions rpg_tools_rendering/examples/hair_long.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate rpg_tools_rendering;

use crate::utils::appearance::create_humanoid_with_hair;
use crate::utils::render::render_2_sets;
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::long::LongHairStyle;
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::HeadShape;
Expand Down Expand Up @@ -34,7 +34,7 @@ fn main() {
fn create_long(style: LongHairStyle, length: f32) -> Hair {
Hair::Long {
style,
hairline: Hairline::Straight { size: Size::Medium },
hairline: Hairline::new(HairlineStyle::Straight, Size::Medium),
length: Length::from_metre(length),
color: Color::SaddleBrown,
}
Expand Down
4 changes: 2 additions & 2 deletions rpg_tools_rendering/examples/hair_ponytail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate rpg_tools_rendering;

use crate::utils::appearance::create_humanoid_with_hair;
use crate::utils::render::render_2_sets;
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::ponytail::position::PonytailPosition;
use rpg_tools_core::model::character::appearance::hair::ponytail::style::PonytailStyle;
use rpg_tools_core::model::character::appearance::hair::ponytail::Ponytail;
Expand Down Expand Up @@ -38,7 +38,7 @@ fn create_ponytail(position: PonytailPosition, style: PonytailStyle) -> Hair {
position,
style,
length: Length::from_metre(1.0),
hairline: Hairline::Straight { size: Size::Medium },
hairline: Hairline::new(HairlineStyle::Straight, Size::Medium),
color: Color::SaddleBrown,
})
}
51 changes: 26 additions & 25 deletions rpg_tools_rendering/examples/hair_short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,30 @@ extern crate rpg_tools_rendering;

use crate::utils::appearance::create_head_with_hair;
use crate::utils::render::render_2_sets;
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::{Hairline, HairlineStyle};
use rpg_tools_core::model::character::appearance::hair::short::ShortHair;
use rpg_tools_core::model::character::appearance::hair::Hair;
use rpg_tools_core::model::character::appearance::head::HeadShape;
use rpg_tools_core::model::color::Color;
use rpg_tools_core::model::side::Side;
use rpg_tools_core::model::size::Size;
use rpg_tools_core::model::size::Size::{Large, Medium, Small};
use Hairline::{Round, Straight, Triangle, WidowsPeak};
use ShortHair::{BuzzCut, FlatTop, MiddlePart, SidePart};
use HairlineStyle::{Round, Straight, Triangle, WidowsPeak};
use ShortHair::{BuzzCut, MiddlePart};
use Side::{Left, Right};

pub mod utils;

fn main() {
let mut short_options = vec![
create_hair(ShortHair::flat_top(Small), Round(Medium)),
create_hair(ShortHair::flat_top(Medium), Straight(Medium)),
create_hair(ShortHair::flat_top(Large), WidowsPeak(Medium)),
create_hair(MiddlePart, Round(Small)),
create_hair(MiddlePart, Round(Medium)),
create_hair(MiddlePart, Round(Large)),
create_hair(ShortHair::side_part(Left), Round(Small)),
create_hair(ShortHair::side_part(Right), Round(Small)),
create_hair(ShortHair::flat_top(Small), Round, Medium),
create_hair(ShortHair::flat_top(Medium), Straight, Medium),
create_hair(ShortHair::flat_top(Large), WidowsPeak, Medium),
create_hair(MiddlePart, Round, Small),
create_hair(MiddlePart, Round, Medium),
create_hair(MiddlePart, Round, Large),
create_hair(ShortHair::side_part(Left), Round, Small),
create_hair(ShortHair::side_part(Right), Round, Small),
];
add_all_hairlines(&mut short_options, BuzzCut);

Expand All @@ -40,25 +41,25 @@ fn main() {

fn add_all_hairlines(short_options: &mut Vec<Hair>, style: ShortHair) {
short_options.append(&mut vec![
create_hair(style, Round(Small)),
create_hair(style, Round(Medium)),
create_hair(style, Round(Large)),
create_hair(style, Straight(Small)),
create_hair(style, Straight(Medium)),
create_hair(style, Straight(Large)),
create_hair(style, Triangle(Small)),
create_hair(style, Triangle(Medium)),
create_hair(style, Triangle(Large)),
create_hair(style, WidowsPeak(Small)),
create_hair(style, WidowsPeak(Medium)),
create_hair(style, WidowsPeak(Large)),
create_hair(style, Round, Small),
create_hair(style, Round, Medium),
create_hair(style, Round, Large),
create_hair(style, Straight, Small),
create_hair(style, Straight, Medium),
create_hair(style, Straight, Large),
create_hair(style, Triangle, Small),
create_hair(style, Triangle, Medium),
create_hair(style, Triangle, Large),
create_hair(style, WidowsPeak, Small),
create_hair(style, WidowsPeak, Medium),
create_hair(style, WidowsPeak, Large),
]);
}

fn create_hair(style: ShortHair, hairline: Hairline) -> Hair {
fn create_hair(style: ShortHair, hairline: HairlineStyle, size: Size) -> Hair {
Hair::Short {
style,
hairline,
hairline: Hairline::new(hairline, size),
color: Color::SaddleBrown,
}
}
13 changes: 7 additions & 6 deletions rpg_tools_rendering/src/rendering/hair/hairline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,37 @@ use crate::math::aabb2d::AABB;
use crate::math::point2d::Point2d;
use crate::rendering::config::RenderConfig;
use rpg_tools_core::model::character::appearance::hair::hairline::Hairline;
use rpg_tools_core::model::character::appearance::hair::hairline::HairlineStyle;

pub fn add_hairlines(
config: &RenderConfig,
aabb: &AABB,
hairline: Hairline,
corners: &mut Vec<Point2d>,
) {
let hairline_y = config.hair.hairline.y.convert(hairline.get_y_position());
let hairline_y = config.hair.hairline.y.convert(hairline.size);

match hairline {
Hairline::Round { .. } => {
match hairline.style {
HairlineStyle::Round { .. } => {
add_2_points(corners, aabb, hairline_y, config.hair.hairline.width_round);
}
Hairline::Straight { .. } => {
HairlineStyle::Straight { .. } => {
add_2_points(
corners,
aabb,
hairline_y,
config.hair.hairline.width_straight,
);
}
Hairline::Triangle { .. } => {
HairlineStyle::Triangle { .. } => {
add_2_points(
corners,
aabb,
hairline_y,
config.hair.hairline.width_triangle,
);
}
Hairline::WidowsPeak { .. } => {
HairlineStyle::WidowsPeak { .. } => {
let (left, right) =
aabb.get_mirrored_points(config.hair.hairline.width_widows_peak, hairline_y);
let center = aabb.get_point(0.5, hairline_y + config.hair.hairline.height_widows_peak);
Expand Down
6 changes: 1 addition & 5 deletions rpg_tools_rendering/src/rendering/hair/short.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ pub fn get_middle_part(
head_shape: HeadShape,
hairline: Hairline,
) -> Polygon2d {
let hairline_y = config
.hair
.short
.y_middle_part
.convert(hairline.get_y_position());
let hairline_y = config.hair.short.y_middle_part.convert(hairline.size);
let bottom_width = config.head.get_eye_width(head_shape);
let forehead_width = config.head.get_forehead_width(head_shape);

Expand Down

0 comments on commit 60e46a0

Please sign in to comment.