Browse Source

Convert transform, animation, and transition to string.

Michael 7 năm trước cách đây
mục cha
commit
9bb9c01bf9

+ 2 - 0
Include/Rocket/Core/Transform.h

@@ -77,6 +77,8 @@ public:
 	Primitives& GetPrimitives() noexcept { return primitives; }
 	const Primitives& GetPrimitives() const noexcept { return primitives; }
 
+	String ToString() const;
+
 private:
 	Primitives primitives;
 };

+ 58 - 0
Include/Rocket/Core/TransformPrimitive.h

@@ -58,6 +58,8 @@ struct NumericValue
 	/// Defined for: {Number, Deg, %} -> Rad
 	float ResolveAbsoluteUnit(Property::Unit base_unit) const noexcept;
 
+	String ToString() const noexcept;
+
 	float number;
 	Property::Unit unit;
 };
@@ -89,6 +91,8 @@ struct ResolvedPrimitive
 	ResolvedPrimitive(std::array<float, N> values) noexcept : values(values) { }
 	
 	std::array<float, N> values;
+
+	String ToString(String unit, bool rad_to_deg = false, bool only_unit_on_last_value = false) const noexcept;
 };
 
 template< size_t N >
@@ -102,6 +106,8 @@ struct UnresolvedPrimitive
 	UnresolvedPrimitive(std::array<NumericValue, N> values) noexcept : values(values) { }
 
 	std::array<NumericValue, N> values;
+
+	String ToString() const noexcept;
 };
 
 
@@ -111,36 +117,42 @@ struct UnresolvedPrimitive
 struct Matrix2D : public ResolvedPrimitive< 6 >
 {
 	Matrix2D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
+	String ToString() const noexcept { return "matrix" + ResolvedPrimitive< 6 >::ToString(""); }
 };
 
 struct Matrix3D : public ResolvedPrimitive< 16 >
 {
 	Matrix3D(const Matrix4f& matrix) noexcept : ResolvedPrimitive(matrix.data()) { }
 	Matrix3D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
+	String ToString() const noexcept { return "matrix3d" + ResolvedPrimitive< 16 >::ToString(""); }
 };
 
 struct TranslateX : public UnresolvedPrimitive< 1 >
 {
 	TranslateX(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
 	TranslateX(float x, Property::Unit unit = Property::PX) noexcept : UnresolvedPrimitive({ NumericValue{x, unit} }) { }
+	String ToString() const noexcept { return "translateX" + UnresolvedPrimitive< 1 >::ToString(); }
 };
 
 struct TranslateY : public UnresolvedPrimitive< 1 >
 {
 	TranslateY(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
 	TranslateY(float y, Property::Unit unit = Property::PX) noexcept : UnresolvedPrimitive({ NumericValue(y, unit) }) { }
+	String ToString() const noexcept { return "translateY" + UnresolvedPrimitive< 1 >::ToString(); }
 };
 
 struct TranslateZ : public UnresolvedPrimitive< 1 >
 {
 	TranslateZ(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
 	TranslateZ(float z, Property::Unit unit = Property::PX) noexcept : UnresolvedPrimitive({ NumericValue(z, unit) }) { }
+	String ToString() const noexcept { return "translateZ" + UnresolvedPrimitive< 1 >::ToString(); }
 };
 
 struct Translate2D : public UnresolvedPrimitive< 2 >
 {
 	Translate2D(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
 	Translate2D(float x, float y, Property::Unit units = Property::PX) noexcept : UnresolvedPrimitive({ NumericValue(x, units), NumericValue(y, units) }) { }
+	String ToString() const noexcept { return "translate" + UnresolvedPrimitive< 2 >::ToString(); }
 };
 
 struct Translate3D : public UnresolvedPrimitive< 3 >
@@ -148,24 +160,28 @@ struct Translate3D : public UnresolvedPrimitive< 3 >
 	Translate3D(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
 	Translate3D(NumericValue x, NumericValue y, NumericValue z) noexcept : UnresolvedPrimitive({ x, y, z }) { }
 	Translate3D(float x, float y, float z, Property::Unit units = Property::PX) noexcept : UnresolvedPrimitive({ NumericValue(x, units), NumericValue(y, units), NumericValue(z, units) }) { }
+	String ToString() const noexcept { return "translate3d" + UnresolvedPrimitive< 3 >::ToString(); }
 };
 
 struct ScaleX : public ResolvedPrimitive< 1 >
 {
 	ScaleX(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
 	ScaleX(float value) noexcept : ResolvedPrimitive({ value }) { }
+	String ToString() const noexcept { return "scaleX" + ResolvedPrimitive< 1 >::ToString(""); }
 };
 
 struct ScaleY : public ResolvedPrimitive< 1 >
 {
 	ScaleY(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
 	ScaleY(float value) noexcept : ResolvedPrimitive({ value }) { }
+	String ToString() const noexcept { return "scaleY" + ResolvedPrimitive< 1 >::ToString(""); }
 };
 
 struct ScaleZ : public ResolvedPrimitive< 1 >
 {
 	ScaleZ(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
 	ScaleZ(float value) noexcept : ResolvedPrimitive({ value }) { }
+	String ToString() const noexcept { return "scaleZ" + ResolvedPrimitive< 1 >::ToString(""); }
 };
 
 struct Scale2D : public ResolvedPrimitive< 2 >
@@ -173,6 +189,7 @@ struct Scale2D : public ResolvedPrimitive< 2 >
 	Scale2D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
 	Scale2D(float xy) noexcept : ResolvedPrimitive({ xy, xy }) { }
 	Scale2D(float x, float y) noexcept : ResolvedPrimitive({ x, y }) { }
+	String ToString() const noexcept { return "scale" + ResolvedPrimitive< 2 >::ToString(""); }
 };
 
 struct Scale3D : public ResolvedPrimitive< 3 >
@@ -180,30 +197,35 @@ struct Scale3D : public ResolvedPrimitive< 3 >
 	Scale3D(const NumericValue* values) noexcept : ResolvedPrimitive(values) { }
 	Scale3D(float xyz) noexcept : ResolvedPrimitive({ xyz, xyz, xyz }) { }
 	Scale3D(float x, float y, float z) noexcept : ResolvedPrimitive({ x, y, z }) { }
+	String ToString() const noexcept { return "scale3d" + ResolvedPrimitive< 3 >::ToString(""); }
 };
 
 struct RotateX : public ResolvedPrimitive< 1 >
 {
 	RotateX(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
 	RotateX(float angle, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
+	String ToString() const noexcept { return "rotateX" + ResolvedPrimitive< 1 >::ToString("deg", true); }
 };
 
 struct RotateY : public ResolvedPrimitive< 1 >
 {
 	RotateY(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) {}
 	RotateY(float angle, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
+	String ToString() const noexcept { return "rotateY" + ResolvedPrimitive< 1 >::ToString("deg", true); }
 };
 
 struct RotateZ : public ResolvedPrimitive< 1 >
 {
 	RotateZ(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
 	RotateZ(float angle, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
+	String ToString() const noexcept { return "rotateZ" + ResolvedPrimitive< 1 >::ToString("deg", true); }
 };
 
 struct Rotate2D : public ResolvedPrimitive< 1 >
 {
 	Rotate2D(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
 	Rotate2D(float angle, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
+	String ToString() const noexcept { return "rotate" + ResolvedPrimitive< 1 >::ToString("deg", true); }
 };
 
 struct Rotate3D : public ResolvedPrimitive< 4 >
@@ -212,29 +234,34 @@ struct Rotate3D : public ResolvedPrimitive< 4 >
 	Rotate3D(float x, float y, float z, float angle, Property::Unit angle_unit = Property::DEG) noexcept
 		: ResolvedPrimitive({ NumericValue{x, Property::NUMBER}, NumericValue{y, Property::NUMBER}, NumericValue{z, Property::NUMBER}, NumericValue{angle, angle_unit} },
 			{ Property::NUMBER, Property::NUMBER, Property::NUMBER, Property::RAD }) { }
+	String ToString() const noexcept { return "rotate3d" + ResolvedPrimitive< 4 >::ToString("deg", true, true); }
 };
 
 struct SkewX : public ResolvedPrimitive< 1 >
 {
 	SkewX(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
 	SkewX(float angle, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
+	String ToString() const noexcept { return "skewX" + ResolvedPrimitive< 1 >::ToString("deg", true); }
 };
 
 struct SkewY : public ResolvedPrimitive< 1 >
 {
 	SkewY(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD }) { }
 	SkewY(float angle, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({ NumericValue{ angle, unit } }, { Property::RAD }) { }
+	String ToString() const noexcept { return "skewY" + ResolvedPrimitive< 1 >::ToString("deg", true); }
 };
 
 struct Skew2D : public ResolvedPrimitive< 2 >
 {
 	Skew2D(const NumericValue* values) noexcept : ResolvedPrimitive(values, { Property::RAD, Property::RAD }) { }
 	Skew2D(float x, float y, Property::Unit unit = Property::DEG) noexcept : ResolvedPrimitive({NumericValue{ x, unit }, { NumericValue{ y, unit }}}, {Property::RAD, Property::RAD}) { }
+	String ToString() const noexcept { return "skew" + ResolvedPrimitive< 2 >::ToString("deg", true); }
 };
 
 struct Perspective : public UnresolvedPrimitive< 1 >
 {
 	Perspective(const NumericValue* values) noexcept : UnresolvedPrimitive(values) { }
+	String ToString() const noexcept { return "perspective" + UnresolvedPrimitive< 1 >::ToString(); }
 };
 
 struct DecomposedMatrix4
@@ -246,6 +273,7 @@ struct DecomposedMatrix4
 	Vector3f skew;
 
 	bool Decompose(const Matrix4f& m);
+	String ToString() const noexcept { return "decomposedMatrix3d"; }
 };
 
 
@@ -293,9 +321,39 @@ struct Primitive
 	// Primitives must be of same type and PrepareForInterpolation() must previously have been called on both.
 	bool InterpolateWith(const Primitive& other, float alpha) noexcept;
 
+	String ToString() const noexcept;
+
 };
 
 
+template<size_t N>
+inline String ResolvedPrimitive<N>::ToString(String unit, bool rad_to_deg, bool only_unit_on_last_value) const noexcept {
+	float multiplier = 1.0f;
+	if (rad_to_deg) multiplier = 180.f / Math::ROCKET_PI;
+	String tmp;
+	String result = "(";
+	for (size_t i = 0; i < N; i++) {
+		if (TypeConverter<float, String>::Convert(values[i] * multiplier, tmp))
+			result += tmp;
+		if (!unit.Empty() && (!only_unit_on_last_value || (i == N - 1)))
+			result += unit;
+		if (i != N - 1) result += ", ";
+	}
+	result += ")";
+	return result;
+}
+
+template<size_t N>
+inline String UnresolvedPrimitive<N>::ToString() const noexcept {
+	String result = "(";
+	for (size_t i = 0; i < N; i++) {
+		result += values[i].ToString();
+		if (i != N - 1) result += ", ";
+	}
+	result += ")";
+	return result;
+}
+
 }
 }
 }

+ 33 - 1
Include/Rocket/Core/Tween.h

@@ -28,13 +28,15 @@
 #ifndef ROCKETCORETWEEN_H
 #define ROCKETCORETWEEN_H
 
+#include <array>
+
 namespace Rocket {
 namespace Core {
 
 
 class Tween {
 public:
-	enum Type { None, Back, Bounce, Circular, Cubic, Elastic, Exponential, Linear, Quadratic, Quartic, Quintic, Sine, Callback };
+	enum Type { None, Back, Bounce, Circular, Cubic, Elastic, Exponential, Linear, Quadratic, Quartic, Quintic, Sine, Callback, Count };
 	enum Direction { In = 1, Out = 2, InOut = 3 };
 	typedef float(*CallbackFnc)(float);
 
@@ -131,6 +133,36 @@ public:
 	bool operator==(const Tween& other) const { return type_in == other.type_in && type_out == other.type_out && callback == other.callback; }
 	bool operator!=(const Tween& other) const { return !(*this == other); }
 
+	String to_string() const
+	{
+		const std::array<String, size_t(Count)> type_str = { "none", "back", "bounce", "circular", "cubic", "elastic", "exponential", "linear", "quadratic", "quartic", "quintic", "sine", "callback" };
+
+		if (size_t(type_in) < type_str.size() && size_t(type_out) < type_str.size())
+		{
+			if (type_in == None && type_out == None)
+			{
+				return "none";
+			}
+			else if (type_in == type_out)
+			{
+				return type_str[size_t(type_in)] + String("-in-out");
+			}
+			else if (type_in == None)
+			{
+				return type_str[size_t(type_out)] + String("-out");
+			}
+			else if (type_out == None)
+			{
+				return type_str[size_t(type_in)] + String("-in");
+			}
+			else if (type_in != type_out)
+			{
+				return type_str[size_t(type_in)] + String("-in-") + type_str[size_t(type_out)] + String("-out");
+			}
+		}
+		return "unknown";
+	}
+
 private:
 
 

+ 2 - 2
Include/Rocket/Core/TypeConverter.h

@@ -56,9 +56,9 @@ public:
 	static bool Convert(const SourceType& src, DestType& dest);
 };
 
-#include "TypeConverter.inl"
-
 }
 }
 
+#include "TypeConverter.inl"
+
 #endif

+ 67 - 0
Include/Rocket/Core/TypeConverter.inl

@@ -25,6 +25,8 @@
  *
  */
 
+namespace Rocket::Core {
+
 template <typename SourceType, typename DestType>
 bool TypeConverter<SourceType, DestType>::Convert(const SourceType& /*src*/, DestType& /*dest*/)
 {
@@ -350,6 +352,69 @@ public:
 	}
 };
 
+template<>
+class TypeConverter< TransformRef, String >
+{
+public:
+	static bool Convert(const TransformRef& src, String& dest)
+	{
+		if (src) dest = src->ToString();
+		else dest = "none";
+		return true;
+	}
+};
+
+template<>
+class TypeConverter< TransitionList, String >
+{
+public:
+	static bool Convert(const TransitionList& src, String& dest)
+	{
+		if (src.none)
+		{
+			dest = "none";
+			return true;
+		}
+		String tmp;
+		for (size_t i = 0; i < src.transitions.size(); i++)
+		{
+			const Transition& t = src.transitions[i];
+			dest += t.name + " ";
+			dest += t.tween.to_string() + " ";
+			if (TypeConverter< float, String >::Convert(t.duration, tmp)) dest += tmp + "s ";
+			if (t.delay > 0.0f && TypeConverter< float, String >::Convert(t.delay, tmp)) dest += tmp + "s ";
+			if (t.reverse_adjustment_factor > 0.0f && TypeConverter< float, String >::Convert(t.delay, tmp)) dest += tmp;
+			if (dest.Length() > 0) dest.Resize(dest.Length() - 1);
+			if (i != src.transitions.size() - 1) dest += ", ";
+		}
+		return true;
+	}
+};
+
+template<>
+class TypeConverter< AnimationList, String >
+{
+public:
+	static bool Convert(const AnimationList& src, String& dest)
+	{
+		String tmp;
+		for (size_t i = 0; i < src.size(); i++)
+		{
+			const Animation& a = src[i];
+			if (TypeConverter< float, String >::Convert(a.duration, tmp)) dest += tmp + "s ";
+			dest += a.tween.to_string() + " ";
+			if (a.delay > 0.0f && TypeConverter< float, String >::Convert(a.delay, tmp)) dest += tmp + "s ";
+			if (a.alternate) dest += "alternate ";
+			if (a.paused) dest += "paused ";
+			if (a.num_iterations == -1) dest += "infinite ";
+			else if(TypeConverter< int, String >::Convert(a.num_iterations, tmp)) dest += tmp + " ";
+			dest += a.name;
+			if (i != src.size() - 1) dest += ", ";
+		}
+		return true;
+	}
+};
+
 template< typename SourceType, typename InternalType, int count >
 class TypeConverterVectorString
 {
@@ -395,3 +460,5 @@ VECTOR_STRING_CONVERTER(Colourb, byte, 4);
 #undef BASIC_CONVERTER_BOOL
 #undef STRING_VECTOR_CONVERTER
 #undef VECTOR_STRING_CONVERTER
+
+}

+ 1 - 1
Samples/basic/animation/src/main.cpp

@@ -56,7 +56,7 @@ public:
 				document->GetElementById("title")->SetInnerRML(title);
 				document->SetProperty("left", Property(position.x, Property::PX));
 				document->SetProperty("top", Property(position.y, Property::PX));
-				//document->Animate("opacity", Property(1.0f, Property::NUMBER), 0.8f, Tween{Tween::Quadratic, Tween::Out}, 1, false, 0.0f);
+				//document->Animate("opacity", Property(1.0f, Property::NUMBER), 1.5f, Tween{Tween::Quadratic, Tween::Out}, 1, true, 0.0f);
 			}
 
 			// Button fun

+ 11 - 0
Source/Core/Transform.cpp

@@ -72,5 +72,16 @@ const Transforms::Primitive & Transform::GetPrimitive(int i) const noexcept
 	return primitives[i];
 }
 
+String Transform::ToString() const
+{
+	String result;
+	for (size_t i = 0; i < primitives.size(); i++)
+	{
+		result += primitives[i].ToString();
+		if (i != primitives.size() - 1) result += " ";
+	}
+	return result;
+}
+
 }
 }

+ 22 - 0
Source/Core/TransformPrimitive.cpp

@@ -133,6 +133,14 @@ float NumericValue::ResolveAbsoluteUnit(Property::Unit base_unit) const noexcept
 	return number;
 }
 
+String NumericValue::ToString() const noexcept
+{
+	Property prop;
+	prop.value = Variant(number);
+	prop.unit = unit;
+	return prop.ToString();
+}
+
 
 
 
@@ -579,6 +587,20 @@ bool Primitive::InterpolateWith(const Primitive & other, float alpha) noexcept
 
 
 
+struct ToStringVisitor
+{
+	template <typename T>
+	String operator()(T& p0)
+	{
+		return p0.ToString();
+	}
+};
+
+String Primitive::ToString() const noexcept
+{
+	String result = std::visit(ToStringVisitor{}, primitive);
+	return result;
+}
 
 
 bool DecomposedMatrix4::Decompose(const Matrix4f & m)