Implement ColorSequence roblox datatype

This commit is contained in:
Filip Tibell 2023-03-15 16:14:17 +01:00
parent e9b79ea28f
commit 709426d8ef
No known key found for this signature in database
5 changed files with 184 additions and 15 deletions

View file

@ -120,9 +120,6 @@ impl<'lua> RbxVariantToLua<'lua> for LuaAnyUserData<'lua> {
// Not yet implemented datatypes
// Rbx::Axes(_) => todo!(),
// Rbx::CFrame(_) => todo!(),
// Rbx::Color3(_) => todo!(),
// Rbx::Color3uint8(_) => todo!(),
// Rbx::ColorSequence(_) => todo!(),
// Rbx::Enum(_) => todo!(),
// Rbx::Faces(_) => todo!(),
// Rbx::NumberRange(_) => todo!(),
@ -138,6 +135,7 @@ impl<'lua> RbxVariantToLua<'lua> for LuaAnyUserData<'lua> {
Rbx::Color3(value) => lua.create_userdata(Color3::from(value))?,
Rbx::Color3uint8(value) => lua.create_userdata(Color3::from(value))?,
Rbx::ColorSequence(value) => lua.create_userdata(ColorSequence::from(value))?,
Rbx::UDim(value) => lua.create_userdata(UDim::from(value))?,
Rbx::UDim2(value) => lua.create_userdata(UDim2::from(value))?,
@ -173,6 +171,7 @@ impl<'lua> LuaToRbxVariant<'lua> for LuaAnyUserData<'lua> {
RbxVariantType::Color3 => convert::<Color3, rbx::Color3>,
RbxVariantType::Color3uint8 => convert::<Color3, rbx::Color3uint8>,
RbxVariantType::ColorSequence => convert::<ColorSequence, rbx::ColorSequence>,
RbxVariantType::UDim => convert::<UDim, rbx::UDim>,
RbxVariantType::UDim2 => convert::<UDim2, rbx::UDim2>,

View file

@ -0,0 +1,104 @@
use core::fmt;
use mlua::prelude::*;
use rbx_dom_weak::types::{
ColorSequence as RbxColorSequence, ColorSequenceKeypoint as RbxColorSequenceKeypoint,
};
use super::{Color3, ColorSequenceKeypoint};
/**
An implementation of the [ColorSequence](https://create.roblox.com/docs/reference/engine/datatypes/ColorSequence) Roblox datatype.
This implements all documented properties, methods & constructors of the ColorSequence class as of March 2023.
*/
#[derive(Debug, Clone, PartialEq)]
pub struct ColorSequence {
pub(crate) keypoints: Vec<ColorSequenceKeypoint>,
}
impl ColorSequence {
pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> {
type ArgsColor = Color3;
type ArgsColors = (Color3, Color3);
type ArgsKeypoints = Vec<ColorSequenceKeypoint>;
datatype_table.set(
"new",
lua.create_function(|lua, args: LuaMultiValue| {
if let Ok(color) = ArgsColor::from_lua_multi(args.clone(), lua) {
Ok(ColorSequence {
keypoints: vec![
ColorSequenceKeypoint { time: 0.0, color },
ColorSequenceKeypoint { time: 1.0, color },
],
})
} else if let Ok((c0, c1)) = ArgsColors::from_lua_multi(args.clone(), lua) {
Ok(ColorSequence {
keypoints: vec![
ColorSequenceKeypoint {
time: 0.0,
color: c0,
},
ColorSequenceKeypoint {
time: 1.0,
color: c1,
},
],
})
} else if let Ok(keypoints) = ArgsKeypoints::from_lua_multi(args, lua) {
Ok(ColorSequence { keypoints })
} else {
// FUTURE: Better error message here using given arg types
Err(LuaError::RuntimeError(
"Invalid arguments to constructor".to_string(),
))
}
})?,
)
}
}
impl LuaUserData for ColorSequence {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("Keypoints", |_, this| Ok(this.keypoints.clone()));
}
}
impl fmt::Display for ColorSequence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, keypoint) in self.keypoints.iter().enumerate() {
if index < self.keypoints.len() - 1 {
write!(f, "{}, ", keypoint)?;
} else {
write!(f, "{}", keypoint)?;
}
}
Ok(())
}
}
impl From<RbxColorSequence> for ColorSequence {
fn from(v: RbxColorSequence) -> Self {
Self {
keypoints: v
.keypoints
.iter()
.cloned()
.map(ColorSequenceKeypoint::from)
.collect(),
}
}
}
impl From<ColorSequence> for RbxColorSequence {
fn from(v: ColorSequence) -> Self {
Self {
keypoints: v
.keypoints
.iter()
.cloned()
.map(RbxColorSequenceKeypoint::from)
.collect(),
}
}
}

View file

@ -0,0 +1,60 @@
use core::fmt;
use mlua::prelude::*;
use rbx_dom_weak::types::ColorSequenceKeypoint as RbxColorSequenceKeypoint;
use super::Color3;
/**
An implementation of the [ColorSequenceKeypoint](https://create.roblox.com/docs/reference/engine/datatypes/ColorSequenceKeypoint) Roblox datatype.
This implements all documented properties, methods & constructors of the ColorSequenceKeypoint class as of March 2023.
*/
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ColorSequenceKeypoint {
pub(crate) time: f32,
pub(crate) color: Color3,
}
impl ColorSequenceKeypoint {
pub(crate) fn make_table(lua: &Lua, datatype_table: &LuaTable) -> LuaResult<()> {
datatype_table.set(
"new",
lua.create_function(|_, (time, color): (f32, Color3)| {
Ok(ColorSequenceKeypoint { time, color })
})?,
)?;
Ok(())
}
}
impl LuaUserData for ColorSequenceKeypoint {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field_method_get("Time", |_, this| Ok(this.time));
fields.add_field_method_get("Value", |_, this| Ok(this.color));
}
}
impl fmt::Display for ColorSequenceKeypoint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} > {}", self.time, self.color)
}
}
impl From<RbxColorSequenceKeypoint> for ColorSequenceKeypoint {
fn from(v: RbxColorSequenceKeypoint) -> Self {
Self {
time: v.time,
color: v.color.into(),
}
}
}
impl From<ColorSequenceKeypoint> for RbxColorSequenceKeypoint {
fn from(v: ColorSequenceKeypoint) -> Self {
Self {
time: v.time,
color: v.color.into(),
}
}
}

View file

@ -1,5 +1,7 @@
mod brick_color;
mod color3;
mod color_sequence;
mod color_sequence_keypoint;
mod udim;
mod udim2;
mod vector2;
@ -9,6 +11,8 @@ mod vector3int16;
pub use brick_color::BrickColor;
pub use color3::Color3;
pub use color_sequence::ColorSequence;
pub use color_sequence_keypoint::ColorSequenceKeypoint;
pub use udim::UDim;
pub use udim2::UDim2;
pub use vector2::Vector2;

View file

@ -23,6 +23,8 @@ fn make_all_datatypes(lua: &Lua) -> LuaResult<Vec<(&'static str, LuaTable)>> {
Ok(vec![
("BrickColor", make_dt(lua, BrickColor::make_table)?),
("Color3", make_dt(lua, Color3::make_table)?),
("ColorSequence", make_dt(lua, ColorSequence::make_table)?),
("ColorSequenceKeypoint", make_dt(lua, ColorSequenceKeypoint::make_table)?),
("UDim", make_dt(lua, UDim::make_table)?),
("UDim2", make_dt(lua, UDim2::make_table)?),
("Vector2", make_dt(lua, Vector2::make_table)?),