Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A little bit QoL cleaning up... #84

Merged
merged 6 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inox2d/src/formats.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod enums;
pub mod inp;
mod json;
mod payload;
Expand Down
121 changes: 121 additions & 0 deletions inox2d/src/formats/enums.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
use crate::node::data::{BlendMode, InterpolateMode, MaskMode, ParamMapMode};
use crate::puppet::{PuppetAllowedModification, PuppetAllowedRedistribution, PuppetAllowedUsers};

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown param map mode {0:?}")]
pub struct UnknownParamMapModeError(String);

impl TryFrom<&str> for ParamMapMode {
type Error = UnknownBlendModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"AngleLength" => Ok(ParamMapMode::AngleLength),
"XY" => Ok(ParamMapMode::XY),
a => todo!("Param map mode {} unimplemented", a),
}
}
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown interpolate mode {0:?}")]
pub struct UnknownInterpolateModeError(String);

impl TryFrom<&str> for InterpolateMode {
type Error = UnknownInterpolateModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Linear" => Ok(InterpolateMode::Linear),
unknown => Err(UnknownInterpolateModeError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown blend mode {0:?}")]
pub struct UnknownBlendModeError(String);

impl TryFrom<&str> for BlendMode {
type Error = UnknownBlendModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Normal" => Ok(BlendMode::Normal),
"Multiply" => Ok(BlendMode::Multiply),
"ColorDodge" => Ok(BlendMode::ColorDodge),
"LinearDodge" => Ok(BlendMode::LinearDodge),
"Screen" => Ok(BlendMode::Screen),
"ClipToLower" => Ok(BlendMode::ClipToLower),
"SliceFromLower" => Ok(BlendMode::SliceFromLower),
unknown => Err(UnknownBlendModeError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown mask mode {0:?}")]
pub struct UnknownMaskModeError(String);

impl TryFrom<&str> for MaskMode {
type Error = UnknownMaskModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Mask" => Ok(MaskMode::Mask),
"DodgeMask" => Ok(MaskMode::Dodge),
unknown => Err(UnknownMaskModeError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown allowed users {0:?}")]
pub struct UnknownPuppetAllowedUsersError(String);

impl TryFrom<&str> for PuppetAllowedUsers {
type Error = UnknownPuppetAllowedUsersError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"OnlyAuthor" => Ok(PuppetAllowedUsers::OnlyAuthor),
"OnlyLicensee" => Ok(PuppetAllowedUsers::OnlyLicensee),
"Everyone" => Ok(PuppetAllowedUsers::Everyone),
unknown => Err(UnknownPuppetAllowedUsersError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown allowed redistribution {0:?}")]
pub struct UnknownPuppetAllowedRedistributionError(String);

impl TryFrom<&str> for PuppetAllowedRedistribution {
type Error = UnknownPuppetAllowedRedistributionError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Prohibited" => Ok(PuppetAllowedRedistribution::Prohibited),
"ViralLicense" => Ok(PuppetAllowedRedistribution::ViralLicense),
"CopyleftLicense" => Ok(PuppetAllowedRedistribution::CopyleftLicense),
unknown => Err(UnknownPuppetAllowedRedistributionError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown allowed users {0:?}")]
pub struct UnknownPuppetAllowedModificationError(String);

impl TryFrom<&str> for PuppetAllowedModification {
type Error = UnknownPuppetAllowedModificationError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Prohibited" => Ok(PuppetAllowedModification::Prohibited),
"AllowPersonal" => Ok(PuppetAllowedModification::AllowPersonal),
"AllowRedistribute" => Ok(PuppetAllowedModification::AllowRedistribute),
unknown => Err(UnknownPuppetAllowedModificationError(unknown.to_owned())),
}
}
}
37 changes: 15 additions & 22 deletions inox2d/src/formats/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ use glam::{vec2, vec3, Vec2};
use indextree::Arena;
use json::JsonValue;

use crate::math::interp::{InterpolateMode, UnknownInterpolateModeError};
use super::enums::*;

use crate::math::matrix::{Matrix2d, Matrix2dFromSliceVecsError};
use crate::math::transform::TransformOffset;
use crate::mesh::{f32s_as_vec2s, Mesh};
use crate::node::data::{
BlendMode, Composite, Drawable, InoxData, Mask, MaskMode, ParamMapMode, Part, PhysicsModel, PhysicsProps,
SimplePhysics, UnknownBlendModeError, UnknownMaskModeError,
BlendMode, Composite, Drawable, InoxData, InterpolateMode, Mask, MaskMode, ParamMapMode, Part, PhysicsModel,
PhysicsProps, SimplePhysics, TransformOffset,
};
use crate::node::tree::InoxNodeTree;
use crate::node::{InoxNode, InoxNodeUuid};
use crate::params::{AxisPoints, Binding, BindingValues, Param, ParamUuid};
use crate::physics::runge_kutta::PhysicsState;
use crate::puppet::{
Puppet, PuppetAllowedModification, PuppetAllowedRedistribution, PuppetAllowedUsers, PuppetMeta, PuppetPhysics,
PuppetUsageRights, UnknownPuppetAllowedModificationError, UnknownPuppetAllowedRedistributionError,
UnknownPuppetAllowedUsersError,
PuppetUsageRights,
};
use crate::texture::TextureId;

Expand All @@ -40,6 +39,8 @@ pub enum InoxParseError {
#[error(transparent)]
InvalidMatrix2dData(#[from] Matrix2dFromSliceVecsError),
#[error(transparent)]
UnknownParamMapMode(#[from] UnknownParamMapModeError),
#[error(transparent)]
UnknownBlendMode(#[from] UnknownBlendModeError),
#[error(transparent)]
UnknownMaskMode(#[from] UnknownMaskModeError),
Expand Down Expand Up @@ -160,11 +161,7 @@ fn deserialize_simple_physics(obj: &JsonObject) -> InoxParseResult<SimplePhysics
a => todo!("{}", a),
};

let map_mode = match obj.get_str("map_mode")? {
"AngleLength" => ParamMapMode::AngleLength,
"XY" => ParamMapMode::XY,
a => todo!("{}", a),
};
let map_mode = ParamMapMode::try_from(obj.get_str("map_mode")?)?;

let local_only = obj.get_bool("local_only").unwrap_or_default();

Expand Down Expand Up @@ -198,7 +195,7 @@ fn deserialize_simple_physics(obj: &JsonObject) -> InoxParseResult<SimplePhysics

fn deserialize_drawable(obj: &JsonObject) -> InoxParseResult<Drawable> {
Ok(Drawable {
blend_mode: BlendMode::try_from(obj.get_str("blend_mode").unwrap_or_default()).unwrap_or_default(),
blend_mode: BlendMode::try_from(obj.get_str("blend_mode")?).unwrap_or_default(),
tint: obj.get_vec3("tint").unwrap_or(vec3(1.0, 1.0, 1.0)),
screen_tint: obj.get_vec3("screenTint").unwrap_or(vec3(0.0, 0.0, 0.0)),
mask_threshold: obj.get_f32("mask_threshold").unwrap_or(0.5),
Expand Down Expand Up @@ -237,16 +234,12 @@ fn deserialize_mask(obj: &JsonObject) -> InoxParseResult<Mask> {
}

fn deserialize_transform(obj: &JsonObject) -> InoxParseResult<TransformOffset> {
let translation = obj.get_vec3("trans")?;
let rotation = obj.get_vec3("rot")?;
let scale = obj.get_vec2("scale")?;
let pixel_snap = obj.get_bool("pixel_snap").unwrap_or_default();

Ok(TransformOffset::new()
.with_translation(translation)
.with_rotation(rotation)
.with_scale(scale)
.with_pixel_snap(pixel_snap))
Ok(TransformOffset {
translation: obj.get_vec3("trans")?,
rotation: obj.get_vec3("rot")?,
scale: obj.get_vec2("scale")?,
pixel_snap: obj.get_bool("pixel_snap").unwrap_or_default(),
})
}

fn deserialize_f32s(val: &[json::JsonValue]) -> Vec<f32> {
Expand Down
24 changes: 1 addition & 23 deletions inox2d/src/math/interp.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
use glam::Vec2;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterpolateMode {
/// Round to nearest
Nearest,
/// Linear interpolation
Linear,
// there's more but I'm not adding them for now.
}
Speykious marked this conversation as resolved.
Show resolved Hide resolved

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown interpolate mode {0:?}")]
pub struct UnknownInterpolateModeError(String);

impl TryFrom<&str> for InterpolateMode {
type Error = UnknownInterpolateModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Linear" => Ok(InterpolateMode::Linear),
unknown => Err(UnknownInterpolateModeError(unknown.to_owned())),
}
}
}
use crate::node::data::InterpolateMode;

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct InterpRange<T> {
Expand Down
49 changes: 2 additions & 47 deletions inox2d/src/math/transform.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,8 @@
use glam::{EulerRot, Mat4, Quat, Vec2, Vec3};
use glam::{EulerRot, Mat4, Quat, Vec3};

#[derive(Debug, Clone, Copy)]
pub struct TransformOffset {
/// X Y Z
pub translation: Vec3,
/// Euler angles
pub rotation: Vec3,
/// X Y zoom
pub scale: Vec2,
/// Whether the transform should snap to pixels
pub pixel_snap: bool,
}

impl Default for TransformOffset {
fn default() -> Self {
Self {
translation: Vec3::ZERO,
rotation: Vec3::ZERO,
scale: Vec2::ONE,
pixel_snap: false,
}
}
}
use crate::node::data::TransformOffset;
Speykious marked this conversation as resolved.
Show resolved Hide resolved

impl TransformOffset {
pub fn new() -> Self {
Self::default()
}

pub fn with_translation(mut self, translation: Vec3) -> Self {
self.translation = translation;
self
}

pub fn with_rotation(mut self, rotation: Vec3) -> Self {
self.rotation = rotation;
self
}

pub fn with_scale(mut self, scale: Vec2) -> Self {
self.scale = scale;
self
}

pub fn with_pixel_snap(mut self, pixel_snap: bool) -> Self {
self.pixel_snap = pixel_snap;
self
}

pub fn to_matrix(&self) -> Mat4 {
Mat4::from_translation(self.translation)
* Mat4::from_quat(Quat::from_euler(
Expand Down
4 changes: 1 addition & 3 deletions inox2d/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ pub mod tree;

use std::fmt::Debug;

use crate::math::transform::TransformOffset;

use data::InoxData;
use data::{InoxData, TransformOffset};

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
Expand Down
69 changes: 32 additions & 37 deletions inox2d/src/node/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ use crate::texture::TextureId;

use super::InoxNodeUuid;

#[derive(Debug, Clone, Copy)]
pub struct TransformOffset {
/// X Y Z
pub translation: Vec3,
/// Euler angles
pub rotation: Vec3,
/// X Y zoom
pub scale: Vec2,
/// Whether the transform should snap to pixels
pub pixel_snap: bool,
}

impl Default for TransformOffset {
fn default() -> Self {
Self {
translation: Vec3::ZERO,
rotation: Vec3::ZERO,
scale: Vec2::ONE,
pixel_snap: false,
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InterpolateMode {
/// Round to nearest
Nearest,
/// Linear interpolation
Linear,
// there's more but I'm not adding them for now.
}

/// Blending mode.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlendMode {
Expand Down Expand Up @@ -46,27 +78,6 @@ impl BlendMode {
];
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown blend mode {0:?}")]
pub struct UnknownBlendModeError(String);

impl TryFrom<&str> for BlendMode {
type Error = UnknownBlendModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Normal" => Ok(BlendMode::Normal),
"Multiply" => Ok(BlendMode::Multiply),
"ColorDodge" => Ok(BlendMode::ColorDodge),
"LinearDodge" => Ok(BlendMode::LinearDodge),
"Screen" => Ok(BlendMode::Screen),
"ClipToLower" => Ok(BlendMode::ClipToLower),
"SliceFromLower" => Ok(BlendMode::SliceFromLower),
unknown => Err(UnknownBlendModeError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MaskMode {
/// The part should be masked by the drawables specified.
Expand All @@ -75,22 +86,6 @@ pub enum MaskMode {
Dodge,
}

#[derive(Debug, Clone, thiserror::Error)]
#[error("Unknown mask mode {0:?}")]
pub struct UnknownMaskModeError(String);

impl TryFrom<&str> for MaskMode {
type Error = UnknownMaskModeError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Mask" => Ok(MaskMode::Mask),
"DodgeMask" => Ok(MaskMode::Dodge),
unknown => Err(UnknownMaskModeError(unknown.to_owned())),
}
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct Mask {
pub source: InoxNodeUuid,
Expand Down
Loading