leocad/common/object.h

237 lines
4.4 KiB
C
Raw Normal View History

2011-09-07 21:06:51 +00:00
#ifndef _OBJECT_H_
#define _OBJECT_H_
2012-08-16 23:50:40 +00:00
#include "lc_math.h"
#include "lc_array.h"
2012-08-16 23:50:40 +00:00
2014-07-06 06:04:09 +00:00
typedef lcuint32 lcStep;
#define LC_STEP_MAX 0xffffffff
enum lcObjectType
2011-09-07 21:06:51 +00:00
{
2012-08-16 23:50:40 +00:00
LC_OBJECT_PIECE,
LC_OBJECT_CAMERA,
LC_OBJECT_LIGHT
2012-08-16 23:50:40 +00:00
};
2011-09-07 21:06:51 +00:00
2014-08-31 00:53:12 +00:00
template<typename T>
struct lcObjectKey
2011-09-07 21:06:51 +00:00
{
2014-07-06 06:04:09 +00:00
lcStep Step;
2014-08-31 00:53:12 +00:00
T Value;
2012-08-16 23:50:40 +00:00
};
2011-09-07 21:06:51 +00:00
struct lcObjectSection
{
2014-05-01 18:42:11 +00:00
lcObject* Object;
lcuint32 Section;
};
struct lcObjectRayTest
2011-09-07 21:06:51 +00:00
{
2014-05-03 16:59:57 +00:00
lcCamera* ViewCamera;
bool PiecesOnly;
2012-08-16 23:50:40 +00:00
lcVector3 Start;
lcVector3 End;
float Distance;
lcObjectSection ObjectSection;
};
struct lcObjectBoxTest
{
2014-05-03 16:59:57 +00:00
lcCamera* ViewCamera;
lcVector4 Planes[6];
2014-11-29 02:55:58 +00:00
lcArray<lcObject*> Objects;
};
2014-05-01 18:42:11 +00:00
class lcObject
2011-09-07 21:06:51 +00:00
{
public:
2014-05-01 18:42:11 +00:00
lcObject(lcObjectType ObjectType);
virtual ~lcObject();
2011-09-07 21:06:51 +00:00
public:
2014-01-30 23:26:55 +00:00
bool IsPiece() const
{
return mObjectType == LC_OBJECT_PIECE;
2014-01-30 23:26:55 +00:00
}
2011-09-07 21:06:51 +00:00
2014-01-30 23:26:55 +00:00
bool IsCamera() const
{
return mObjectType == LC_OBJECT_CAMERA;
2014-01-30 23:26:55 +00:00
}
2011-09-07 21:06:51 +00:00
2014-01-30 23:26:55 +00:00
bool IsLight() const
{
return mObjectType == LC_OBJECT_LIGHT;
2014-01-30 23:26:55 +00:00
}
2011-09-07 21:06:51 +00:00
lcObjectType GetType() const
2014-01-30 23:26:55 +00:00
{
return mObjectType;
2014-01-30 23:26:55 +00:00
}
2011-09-07 21:06:51 +00:00
virtual bool IsSelected() const = 0;
virtual bool IsSelected(lcuint32 Section) const = 0;
virtual void SetSelected(bool Selected) = 0;
virtual void SetSelected(lcuint32 Section, bool Selected) = 0;
virtual bool IsFocused() const = 0;
virtual bool IsFocused(lcuint32 Section) const = 0;
virtual void SetFocused(lcuint32 Section, bool Focused) = 0;
virtual lcuint32 GetFocusSection() const = 0;
virtual lcVector3 GetSectionPosition(lcuint32 Section) const = 0;
virtual void RayTest(lcObjectRayTest& ObjectRayTest) const = 0;
virtual void BoxTest(lcObjectBoxTest& ObjectBoxTest) const = 0;
2015-05-16 23:04:35 +00:00
virtual void DrawInterface(lcContext* Context) const = 0;
2014-01-30 23:26:55 +00:00
virtual const char* GetName() const = 0;
2011-09-07 21:06:51 +00:00
2014-01-30 23:26:55 +00:00
protected:
2014-09-05 00:24:28 +00:00
template<typename T>
void SaveKeysLDraw(QTextStream& Stream, const lcArray<lcObjectKey<T>>& Keys, const char* KeyName) const
{
const int Count = sizeof(T) / sizeof(float);
for (int KeyIdx = 0; KeyIdx < Keys.GetSize(); KeyIdx++)
{
lcObjectKey<T>& Key = Keys[KeyIdx];
Stream << QLatin1String("0 !LEOCAD ") << KeyName << Key.Step << ' ';
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
Stream << ((float*)&Key.Value)[ValueIdx] << ' ';
2014-09-08 19:42:20 +00:00
Stream << QLatin1String("\r\n");
2014-09-05 00:24:28 +00:00
}
}
template<typename T>
2014-09-05 00:24:28 +00:00
void LoadKeysLDraw(QTextStream& Stream, lcArray<lcObjectKey<T>>& Keys)
{
2014-09-03 14:34:53 +00:00
QString Token;
Stream >> Token;
int Step = Token.toInt();
T Value;
const int Count = sizeof(T) / sizeof(float);
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
Stream >> ((float*)&Value)[ValueIdx];
ChangeKey(Keys, Value, Step, true);
}
2014-08-31 00:53:12 +00:00
template<typename T>
const T& CalculateKey(const lcArray<lcObjectKey<T>>& Keys, lcStep Step)
2014-01-30 23:26:55 +00:00
{
2014-08-31 00:53:12 +00:00
lcObjectKey<T>* PreviousKey = &Keys[0];
for (int KeyIdx = 0; KeyIdx < Keys.GetSize(); KeyIdx++)
{
if (Keys[KeyIdx].Step > Step)
break;
PreviousKey = &Keys[KeyIdx];
}
return PreviousKey->Value;
2014-01-30 23:26:55 +00:00
}
2014-08-31 00:53:12 +00:00
template<typename T>
void ChangeKey(lcArray<lcObjectKey<T>>& Keys, const T& Value, lcStep Step, bool AddKey)
2014-01-30 23:26:55 +00:00
{
2014-08-31 00:53:12 +00:00
lcObjectKey<T>* Key;
for (int KeyIdx = 0; KeyIdx < Keys.GetSize(); KeyIdx++)
{
Key = &Keys[KeyIdx];
if (Key->Step == Step)
{
Key->Value = Value;
return;
}
if (Key->Step > Step)
{
if (AddKey)
{
Key = &Keys.InsertAt(KeyIdx);
Key->Step = Step;
}
else if (KeyIdx)
Key = &Keys[KeyIdx - 1];
Key->Value = Value;
return;
}
}
if (AddKey || Keys.GetSize() == 0)
{
Key = &Keys.Add();
Key->Step = Step;
Key->Value = Value;
}
else
{
Key = &Keys[Keys.GetSize() - 1];
Key->Value = Value;
}
2014-01-30 23:26:55 +00:00
}
2014-08-31 00:53:12 +00:00
template<typename T>
void InsertTime(lcArray<lcObjectKey<T>>& Keys, lcStep Start, lcStep Time)
2014-01-30 23:26:55 +00:00
{
2014-08-31 00:53:12 +00:00
bool EndKey = false;
for (int KeyIdx = 0; KeyIdx < Keys.GetSize(); KeyIdx++)
{
lcObjectKey<T>& Key = Keys[KeyIdx];
if ((Key.Step < Start) || (Key.Step == 1))
continue;
if (EndKey)
{
Keys.RemoveIndex(KeyIdx);
KeyIdx--;
continue;
}
if (Key.Step >= LC_STEP_MAX - Time)
{
Key.Step = LC_STEP_MAX;
EndKey = true;
}
else
Key.Step += Time;
}
2014-01-30 23:26:55 +00:00
}
2014-08-31 00:53:12 +00:00
template<typename T>
void RemoveTime(lcArray<lcObjectKey<T>>& Keys, lcStep Start, lcStep Time)
{
for (int KeyIdx = 0; KeyIdx < Keys.GetSize(); KeyIdx++)
{
lcObjectKey<T>& Key = Keys[KeyIdx];
if ((Key.Step < Start) || (Key.Step == 1))
continue;
if (Key.Step < Start + Time)
{
Keys.RemoveIndex(KeyIdx);
KeyIdx--;
continue;
}
Key.Step -= Time;
}
}
2011-09-07 21:06:51 +00:00
2012-08-16 23:50:40 +00:00
private:
lcObjectType mObjectType;
2011-09-07 21:06:51 +00:00
};
#endif