Skylicht Engine
Loading...
Searching...
No Matches
CInterpolateSerializable.h
1/*
2!@
3MIT License
4
5Copyright (c) 2025 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "CObjectSerializable.h"
28#include "CArraySerializable.h"
29#include "Utils/CInterpolator.h"
30
31namespace Skylicht
32{
33 template <class T>
34 class SKYLICHT_API CInterpolateSerializable : public CObjectSerializable
35 {
36 protected:
39
40 public:
41 CInterpolateSerializable(const char* name) :
42 CObjectSerializable(name),
43 m_valueX("valueX", this),
44 m_valueY("valueY", this)
45 {
46 m_objectType = ObjectInterpolate;
47 }
48
49 CInterpolateSerializable(const char* name, CObjectSerializable* parent) :
50 CObjectSerializable(name, parent),
51 m_valueX("valueX", this),
52 m_valueY("valueY", this)
53 {
54 m_objectType = ObjectInterpolate;
55 }
56
57 virtual ~CInterpolateSerializable()
58 {
59
60 }
61
62 inline int count()
63 {
64 return m_valueX.getElementCount();
65 }
66
67 virtual void clear()
68 {
69 m_valueX.clear();
70 m_valueY.clear();
71 }
72
73 virtual void setInterpolator(const CInterpolator* interpolator) = 0;
74
75 virtual void getInterpolator(CInterpolator* interpolator) = 0;
76 };
77
78
79 class SKYLICHT_API CInterpolateFloatSerializable : public CInterpolateSerializable<CFloatProperty>
80 {
81 protected:
86
87 public:
88 CInterpolateFloatSerializable(const char* name) :
89 CInterpolateSerializable<CFloatProperty>(name),
90 m_control("control", this),
91 m_controlL("controlLeft", this),
92 m_controlR("controlRight", this),
93 m_controlType("controlType", this)
94 {
95 }
96
97 CInterpolateFloatSerializable(const char* name, CObjectSerializable* parent) :
98 CInterpolateSerializable<CFloatProperty>(name, parent),
99 m_control("control", this),
100 m_controlL("controlLeft", this),
101 m_controlR("controlRight", this),
102 m_controlType("controlType", this)
103 {
104 }
105
106 virtual ~CInterpolateFloatSerializable()
107 {
108
109 }
110
111 virtual void clear()
112 {
113 m_valueX.clear();
114 m_valueY.clear();
115 m_control.clear();
116 m_controlL.clear();
117 m_controlR.clear();
118 m_controlType.clear();
119 }
120
121 inline void addEntry(float x, float y)
122 {
123 CFloatProperty* valueX = (CFloatProperty*)m_valueX.createElement();
124 valueX->set(x);
125
126 CFloatProperty* valueY = (CFloatProperty*)m_valueY.createElement();
127 valueY->set(y);
128 }
129
130 inline void addControl(const SControlPoint& p)
131 {
132 CVector2Property* control = (CVector2Property*)m_control.createElement();
133 control->set(p.Position);
134
135 CVector2Property* left = (CVector2Property*)m_controlL.createElement();
136 left->set(p.Left);
137
138 CVector2Property* right = (CVector2Property*)m_controlR.createElement();
139 right->set(p.Right);
140
141 CIntProperty* type = (CIntProperty*)m_controlType.createElement();
142 type->set(p.Type);
143 }
144
145 virtual void setInterpolator(const CInterpolator* interpolator)
146 {
147 const std::set<SInterpolatorEntry>& graph = interpolator->getGraph();
148 const std::vector<SControlPoint>& controls = interpolator->getControlPoints(0);
149
150 clear();
151 for (auto& i : graph)
152 {
153 addEntry(i.X, i.Value[0]);
154 }
155
156 for (auto& i : controls)
157 {
158 addControl(i);
159 }
160 }
161
162 virtual void getInterpolator(CInterpolator* interpolator)
163 {
164 interpolator->clearGraph();
165
166 for (int i = 0, n = m_valueX.getElementCount(); i < n; i++)
167 {
168 float x = m_valueX.getElementValue(i, CFloatProperty()).get();
169 float y = m_valueY.getElementValue(i, CFloatProperty()).get();
170
171 interpolator->addEntry(x, y);
172 }
173
174 for (int i = 0, n = m_control.getElementCount(); i < n; i++)
175 {
176 SControlPoint& control = interpolator->addControlPoint();
177
178 control.Position = m_control.getElementValue(i, CVector2Property()).get();
179 control.Left = m_controlL.getElementValue(i, CVector2Property()).get();
180 control.Right = m_controlR.getElementValue(i, CVector2Property()).get();
181 control.Type = (SControlPoint::EControlType)m_controlType.getElementValue(i, CIntProperty()).get();
182 }
183
184 interpolator->setType(CInterpolator::Float);
185 }
186
187 virtual CObjectSerializable* clone()
188 {
189 CInterpolateFloatSerializable* object = new CInterpolateFloatSerializable(Name.c_str());
190 copyTo(object);
191 return object;
192 }
193 };
194}
Definition CArraySerializable.h:83
Definition CValuePropertyTemplate.h:197
Definition CValuePropertyTemplate.h:65
Keyframe interpolator for scalar, vector, and color values.
Definition CInterpolator.h:146
std::vector< SControlPoint > & getControlPoints(int layer)
Get mutable Bezier control points for a value layer.
Definition CInterpolator.h:278
@ Float
One float channel.
Definition CInterpolator.h:154
SControlPoint & addControlPoint(int layer=0)
Add a control point to a value layer.
void setType(EInterpolatorType type)
Set the value layout.
Definition CInterpolator.h:194
std::set< SInterpolatorEntry > & getGraph()
Get mutable access to graph entries.
Definition CInterpolator.h:259
bool addEntry(const SInterpolatorEntry &entry)
Add a raw graph entry.
Definition CInterpolator.h:305
void clearGraph()
Remove all graph entries and control points.
Definition CInterpolator.h:390
Definition CObjectSerializable.h:36
Definition CValuePropertyTemplate.h:779
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
Bezier control point used to generate interpolation graph entries.
Definition CInterpolator.h:99
core::vector2df Left
Left tangent offset.
Definition CInterpolator.h:118
EControlType Type
Control point tangent type.
Definition CInterpolator.h:122
core::vector2df Position
Point position.
Definition CInterpolator.h:116
EControlType
Tangent behavior for a control point.
Definition CInterpolator.h:104
core::vector2df Right
Right tangent offset.
Definition CInterpolator.h:120