Implement full getters & setters for Font roblox datatype

This commit is contained in:
Filip Tibell 2023-03-17 17:18:00 +01:00
parent 9722f5b862
commit b7d0481a83
No known key found for this signature in database
2 changed files with 62 additions and 2 deletions

View file

@ -34,6 +34,14 @@ impl EnumItem {
}) })
} }
pub(crate) fn from_enum_name_and_name(
enum_name: impl AsRef<str>,
name: impl AsRef<str>,
) -> Option<Self> {
let parent = Enum::from_name(enum_name)?;
Self::from_enum_and_name(&parent, name)
}
/** /**
Converts an instance property into an [`EnumItem`] datatype, if the property is known. Converts an instance property into an [`EnumItem`] datatype, if the property is known.

View file

@ -1,4 +1,5 @@
use core::fmt; use core::fmt;
use std::str::FromStr;
use mlua::prelude::*; use mlua::prelude::*;
use rbx_dom_weak::types::{ use rbx_dom_weak::types::{
@ -60,9 +61,60 @@ impl Font {
impl LuaUserData for Font { impl LuaUserData for Font {
fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) { fn add_fields<'lua, F: LuaUserDataFields<'lua, Self>>(fields: &mut F) {
// Getters
fields.add_field_method_get("Family", |_, this| Ok(this.family.clone())); fields.add_field_method_get("Family", |_, this| Ok(this.family.clone()));
// TODO: Getters & setters for weight, style fields.add_field_method_get("Weight", |_, this| {
Ok(EnumItem::from_enum_name_and_name(
"FontWeight",
this.weight.to_string(),
))
});
fields.add_field_method_get("Style", |_, this| {
Ok(EnumItem::from_enum_name_and_name(
"FontStyle",
this.style.to_string(),
))
});
fields.add_field_method_get("Bold", |_, this| Ok(this.weight.clone().as_u16() >= 600)); fields.add_field_method_get("Bold", |_, this| Ok(this.weight.clone().as_u16() >= 600));
// Setters
fields.add_field_method_set("Weight", |_, this, value: EnumItem| {
if value.parent.desc.name == "FontWeight" {
match FontWeight::from_str(&value.name) {
Ok(weight) => {
this.weight = weight;
Ok(())
}
Err(e) => Err(LuaError::RuntimeError(format!(
"Failed to set value to FontWeight '{}' - {}",
value.name, e
))),
}
} else {
Err(LuaError::RuntimeError(format!(
"Expected value to be a FontWeight, got {}",
value.parent.desc.name
)))
}
});
fields.add_field_method_set("Style", |_, this, value: EnumItem| {
if value.parent.desc.name == "FontStyle" {
match FontStyle::from_str(&value.name) {
Ok(style) => {
this.style = style;
Ok(())
}
Err(e) => Err(LuaError::RuntimeError(format!(
"Failed to set value to FontStyle '{}' - {}",
value.name, e
))),
}
} else {
Err(LuaError::RuntimeError(format!(
"Expected value to be a FontStyle, got {}",
value.parent.desc.name
)))
}
});
fields.add_field_method_set("Bold", |_, this, value: bool| { fields.add_field_method_set("Bold", |_, this, value: bool| {
if value { if value {
this.weight = FontWeight::Bold; this.weight = FontWeight::Bold;
@ -81,7 +133,7 @@ impl LuaUserData for Font {
impl fmt::Display for Font { impl fmt::Display for Font {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Font({}, {}, {})", self.family, self.weight, self.style) write!(f, "{}, {}, {}", self.family, self.weight, self.style)
} }
} }