2024-01-07 20:39:34 +01:00
|
|
|
#include "lc_global.h"
|
|
|
|
#include "lc_objectproperty.h"
|
|
|
|
#include "lc_math.h"
|
|
|
|
|
|
|
|
#define LC_OBJECT_PROPERTY(T) \
|
2024-01-16 01:48:09 +01:00
|
|
|
template void lcObjectProperty<T>::Update(lcStep Step); \
|
2024-02-19 23:54:45 +01:00
|
|
|
template bool lcObjectProperty<T>::ChangeKey(const T& Value, lcStep Step, bool AddKey); \
|
2024-01-07 20:39:34 +01:00
|
|
|
template void lcObjectProperty<T>::InsertTime(lcStep Start, lcStep Time); \
|
|
|
|
template void lcObjectProperty<T>::RemoveTime(lcStep Start, lcStep Time); \
|
2024-01-15 02:41:01 +01:00
|
|
|
template bool lcObjectProperty<T>::HasKeyFrame(lcStep Time) const; \
|
2024-01-21 21:53:18 +01:00
|
|
|
template bool lcObjectProperty<T>::SetKeyFrame(lcStep Time, bool KeyFrame); \
|
2024-01-16 02:11:03 +01:00
|
|
|
template void lcObjectProperty<T>::Save(QTextStream& Stream, const char* ObjectName, const char* VariableName, bool SaveEmpty) const; \
|
2024-01-07 20:39:34 +01:00
|
|
|
template bool lcObjectProperty<T>::Load(QTextStream& Stream, const QString& Token, const char* VariableName);
|
|
|
|
|
2024-01-15 02:41:01 +01:00
|
|
|
LC_OBJECT_PROPERTY(float)
|
2024-07-04 19:42:02 +02:00
|
|
|
LC_OBJECT_PROPERTY(int)
|
2024-01-15 02:41:01 +01:00
|
|
|
LC_OBJECT_PROPERTY(lcVector2i)
|
|
|
|
LC_OBJECT_PROPERTY(lcVector2)
|
|
|
|
LC_OBJECT_PROPERTY(lcVector3)
|
|
|
|
LC_OBJECT_PROPERTY(lcVector4)
|
|
|
|
LC_OBJECT_PROPERTY(lcMatrix33)
|
2024-01-07 20:39:34 +01:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void lcObjectPropertySaveValue(QTextStream& Stream, const T& Value)
|
|
|
|
{
|
|
|
|
constexpr int Count = sizeof(T) / sizeof(float);
|
|
|
|
|
|
|
|
for (int ValueIndex = 0; ValueIndex < Count; ValueIndex++)
|
|
|
|
Stream << ((const float*)&Value)[ValueIndex] << ' ';
|
|
|
|
}
|
|
|
|
|
2024-07-04 19:42:02 +02:00
|
|
|
template<>
|
|
|
|
void lcObjectPropertySaveValue(QTextStream& Stream, const int& Value)
|
|
|
|
{
|
|
|
|
constexpr int Count = sizeof(int) / sizeof(int);
|
|
|
|
|
|
|
|
for (int ValueIndex = 0; ValueIndex < Count; ValueIndex++)
|
|
|
|
Stream << ((const int*)&Value)[ValueIndex] << ' ';
|
|
|
|
}
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
template<>
|
|
|
|
void lcObjectPropertySaveValue(QTextStream& Stream, const lcVector2i& Value)
|
|
|
|
{
|
|
|
|
constexpr int Count = sizeof(lcVector2i) / sizeof(int);
|
|
|
|
|
|
|
|
for (int ValueIndex = 0; ValueIndex < Count; ValueIndex++)
|
|
|
|
Stream << ((const int*)&Value)[ValueIndex] << ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static void lcObjectPropertyLoadValue(QTextStream& Stream, T& Value)
|
|
|
|
{
|
|
|
|
constexpr int Count = sizeof(T) / sizeof(float);
|
|
|
|
|
|
|
|
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
|
|
|
|
Stream >> ((float*)&Value)[ValueIdx];
|
|
|
|
}
|
|
|
|
|
2024-07-04 19:42:02 +02:00
|
|
|
template<>
|
|
|
|
void lcObjectPropertyLoadValue(QTextStream& Stream, int& Value)
|
|
|
|
{
|
|
|
|
constexpr int Count = sizeof(int) / sizeof(int);
|
|
|
|
|
|
|
|
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
|
|
|
|
Stream >> ((int*)&Value)[ValueIdx];
|
|
|
|
}
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
template<>
|
|
|
|
void lcObjectPropertyLoadValue(QTextStream& Stream, lcVector2i& Value)
|
|
|
|
{
|
|
|
|
constexpr int Count = sizeof(lcVector2i) / sizeof(int);
|
|
|
|
|
|
|
|
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
|
|
|
|
Stream >> ((int*)&Value)[ValueIdx];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2024-01-16 01:48:09 +01:00
|
|
|
void lcObjectProperty<T>::Update(lcStep Step)
|
2024-01-07 20:39:34 +01:00
|
|
|
{
|
2024-01-16 01:48:09 +01:00
|
|
|
if (mKeys.empty())
|
|
|
|
return;
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
const lcObjectPropertyKey<T>* PreviousKey = &mKeys[0];
|
|
|
|
|
|
|
|
for (const lcObjectPropertyKey<T>& Key : mKeys)
|
|
|
|
{
|
|
|
|
if (Key.Step > Step)
|
|
|
|
break;
|
|
|
|
|
|
|
|
PreviousKey = &Key;
|
|
|
|
}
|
|
|
|
|
2024-01-16 01:48:09 +01:00
|
|
|
mValue = PreviousKey->Value;
|
2024-01-07 20:39:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2024-02-19 23:54:45 +01:00
|
|
|
bool lcObjectProperty<T>::ChangeKey(const T& Value, lcStep Step, bool AddKey)
|
2024-01-07 20:39:34 +01:00
|
|
|
{
|
2024-01-16 01:48:09 +01:00
|
|
|
if (!AddKey && mKeys.empty())
|
|
|
|
{
|
2024-02-19 23:54:45 +01:00
|
|
|
if (mValue == Value)
|
|
|
|
return false;
|
|
|
|
|
2024-01-16 01:48:09 +01:00
|
|
|
mValue = Value;
|
|
|
|
|
2024-02-19 23:54:45 +01:00
|
|
|
return true;
|
2024-01-16 01:48:09 +01:00
|
|
|
}
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
for (typename std::vector<lcObjectPropertyKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
|
|
|
|
{
|
|
|
|
if (KeyIt->Step < Step)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (KeyIt->Step == Step)
|
2024-02-19 23:54:45 +01:00
|
|
|
{
|
|
|
|
if (KeyIt->Value == Value)
|
|
|
|
return false;
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
KeyIt->Value = Value;
|
2024-02-19 23:54:45 +01:00
|
|
|
}
|
2024-01-07 20:39:34 +01:00
|
|
|
else if (AddKey)
|
|
|
|
mKeys.insert(KeyIt, lcObjectPropertyKey<T>{ Step, Value });
|
|
|
|
else if (KeyIt == mKeys.begin())
|
2024-02-19 23:54:45 +01:00
|
|
|
{
|
|
|
|
if (KeyIt->Value == Value)
|
|
|
|
return false;
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
KeyIt->Value = Value;
|
2024-02-19 23:54:45 +01:00
|
|
|
}
|
2024-01-07 20:39:34 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
KeyIt = KeyIt - 1;
|
2024-02-19 23:54:45 +01:00
|
|
|
|
|
|
|
if (KeyIt->Value == Value)
|
|
|
|
return false;
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
KeyIt->Value = Value;
|
|
|
|
}
|
|
|
|
|
2024-02-19 23:54:45 +01:00
|
|
|
return true;
|
2024-01-07 20:39:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AddKey || mKeys.empty())
|
|
|
|
mKeys.emplace_back(lcObjectPropertyKey<T>{ Step, Value });
|
|
|
|
else
|
|
|
|
mKeys.back().Value = Value;
|
2024-02-19 23:54:45 +01:00
|
|
|
|
|
|
|
return true;
|
2024-01-07 20:39:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void lcObjectProperty<T>::InsertTime(lcStep Start, lcStep Time)
|
|
|
|
{
|
|
|
|
bool EndKey = false;
|
|
|
|
|
|
|
|
for (typename std::vector<lcObjectPropertyKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
|
|
|
|
{
|
|
|
|
if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
|
|
|
|
{
|
|
|
|
KeyIt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EndKey)
|
|
|
|
{
|
|
|
|
KeyIt = mKeys.erase(KeyIt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KeyIt->Step >= LC_STEP_MAX - Time)
|
|
|
|
{
|
|
|
|
KeyIt->Step = LC_STEP_MAX;
|
|
|
|
EndKey = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
KeyIt->Step += Time;
|
|
|
|
|
|
|
|
KeyIt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void lcObjectProperty<T>::RemoveTime(lcStep Start, lcStep Time)
|
|
|
|
{
|
|
|
|
for (typename std::vector<lcObjectPropertyKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
|
|
|
|
{
|
|
|
|
if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
|
|
|
|
{
|
|
|
|
KeyIt++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (KeyIt->Step < Start + Time)
|
|
|
|
{
|
|
|
|
KeyIt = mKeys.erase(KeyIt);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyIt->Step -= Time;
|
|
|
|
KeyIt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 02:41:01 +01:00
|
|
|
template<typename T>
|
|
|
|
bool lcObjectProperty<T>::HasKeyFrame(lcStep Time) const
|
|
|
|
{
|
|
|
|
for (typename std::vector<lcObjectPropertyKey<T>>::const_iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
|
|
|
|
{
|
|
|
|
if (KeyIt->Step == Time)
|
|
|
|
return true;
|
|
|
|
else if (KeyIt->Step > Time)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-21 21:53:18 +01:00
|
|
|
template<typename T>
|
|
|
|
bool lcObjectProperty<T>::SetKeyFrame(lcStep Time, bool KeyFrame)
|
|
|
|
{
|
|
|
|
if (KeyFrame)
|
|
|
|
{
|
|
|
|
typename std::vector<lcObjectPropertyKey<T>>::const_iterator KeyIt;
|
|
|
|
|
|
|
|
for (KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
|
|
|
|
{
|
|
|
|
if (KeyIt->Step == Time)
|
|
|
|
return false;
|
|
|
|
else if (KeyIt->Step > Time)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mKeys.insert(KeyIt, lcObjectPropertyKey<T>{ Time, mValue });
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (typename std::vector<lcObjectPropertyKey<T>>::const_iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
|
|
|
|
{
|
|
|
|
if (KeyIt->Step == Time)
|
|
|
|
{
|
|
|
|
if (mKeys.size() == 1)
|
|
|
|
mValue = KeyIt->Value;
|
|
|
|
|
|
|
|
mKeys.erase(KeyIt);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (KeyIt->Step > Time)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-01-07 20:39:34 +01:00
|
|
|
template<typename T>
|
2024-01-16 02:11:03 +01:00
|
|
|
void lcObjectProperty<T>::Save(QTextStream& Stream, const char* ObjectName, const char* VariableName, bool SaveEmpty) const
|
2024-01-07 20:39:34 +01:00
|
|
|
{
|
2024-01-16 01:48:09 +01:00
|
|
|
if (mKeys.empty())
|
2024-01-07 20:39:34 +01:00
|
|
|
{
|
2024-01-16 02:11:03 +01:00
|
|
|
if (SaveEmpty)
|
|
|
|
{
|
|
|
|
Stream << QLatin1String("0 !LEOCAD ") << ObjectName << ' ' << VariableName << ' ';
|
2024-01-07 20:39:34 +01:00
|
|
|
|
2024-01-16 02:11:03 +01:00
|
|
|
lcObjectPropertySaveValue(Stream, mValue);
|
2024-01-07 20:39:34 +01:00
|
|
|
|
2024-01-16 02:11:03 +01:00
|
|
|
Stream << QLatin1String("\r\n");
|
|
|
|
}
|
2024-01-07 20:39:34 +01:00
|
|
|
}
|
|
|
|
else
|
2024-01-16 02:11:03 +01:00
|
|
|
{
|
|
|
|
for (const lcObjectPropertyKey<T>& Key : mKeys)
|
|
|
|
{
|
|
|
|
Stream << QLatin1String("0 !LEOCAD ") << ObjectName << ' ' << VariableName << "_KEY " << Key.Step << ' ';
|
|
|
|
|
|
|
|
lcObjectPropertySaveValue(Stream, Key.Value);
|
|
|
|
|
|
|
|
Stream << QLatin1String("\r\n");
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 20:39:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool lcObjectProperty<T>::Load(QTextStream& Stream, const QString& Token, const char* VariableName)
|
|
|
|
{
|
|
|
|
if (Token == VariableName)
|
|
|
|
{
|
|
|
|
lcObjectPropertyLoadValue(Stream, mValue);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-04-28 02:19:34 +02:00
|
|
|
if (Token.endsWith(QLatin1String("_KEY")) && Token.left(Token.size() - 4) == VariableName)
|
2024-01-07 20:39:34 +01:00
|
|
|
{
|
2024-01-16 02:11:03 +01:00
|
|
|
QString StepString;
|
|
|
|
Stream >> StepString;
|
|
|
|
|
|
|
|
const int Step = StepString.toInt();
|
|
|
|
T Value;
|
|
|
|
|
|
|
|
constexpr int Count = sizeof(T) / sizeof(float);
|
|
|
|
|
|
|
|
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
|
|
|
|
Stream >> ((float*)&Value)[ValueIdx];
|
|
|
|
|
|
|
|
ChangeKey(Value, Step, true);
|
2024-01-07 20:39:34 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|