You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Code/Editor/Objects/EntityObject.h

443 lines
16 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#ifndef CRYINCLUDE_EDITOR_OBJECTS_ENTITYOBJECT_H
#define CRYINCLUDE_EDITOR_OBJECTS_ENTITYOBJECT_H
#pragma once
#if !defined(Q_MOC_RUN)
#include "BaseObject.h"
#include "IMovieSystem.h"
#include "IEntityObjectListener.h"
#include "Gizmo.h"
#include "CryListenerSet.h"
#include "StatObjBus.h"
#include <QObject>
#endif
#define CLASS_LIGHT "Light"
#define CLASS_DESTROYABLE_LIGHT "DestroyableLight"
#define CLASS_RIGIDBODY_LIGHT "RigidBodyLight"
#define CLASS_ENVIRONMENT_LIGHT "EnvironmentLight"
class CEntityObject;
class QMenu;
/*!
* CEntityEventTarget is an Entity event target and type.
*/
struct CEntityEventTarget
{
CBaseObject* target; //! Target object.
_smart_ptr<CGizmo> pLineGizmo;
QString event;
QString sourceEvent;
};
//////////////////////////////////////////////////////////////////////////
// Named link from entity to entity.
//////////////////////////////////////////////////////////////////////////
struct CEntityLink
{
GUID targetId; // Target entity id.
CEntityObject* target; // Target entity.
QString name; // Name of the link.
_smart_ptr<CGizmo> pLineGizmo;
};
struct IPickEntitesOwner
{
virtual void AddEntity(CBaseObject* pEntity) = 0;
virtual CBaseObject* GetEntity(int nIdx) = 0;
virtual int GetEntityCount() = 0;
virtual void RemoveEntity(int nIdx) = 0;
};
AZ_PUSH_DISABLE_DLL_EXPORT_BASECLASS_WARNING
AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
/*!
* CEntity is an static object on terrain.
*
*/
class CRYEDIT_API CEntityObject
: public CBaseObject
, public CBaseObject::EventListener
{
AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
AZ_POP_DISABLE_DLL_EXPORT_BASECLASS_WARNING
Q_OBJECT
public:
~CEntityObject();
//////////////////////////////////////////////////////////////////////////
// Overrides from CBaseObject.
//////////////////////////////////////////////////////////////////////////
//! Return type name of Entity.
QString GetTypeDescription() const { return GetEntityClass(); };
//////////////////////////////////////////////////////////////////////////
bool IsSameClass(CBaseObject* obj);
virtual bool Init(IEditor* ie, CBaseObject* prev, const QString& file);
virtual void InitVariables();
virtual void Done();
void DrawExtraLightInfo (DisplayContext& disp);
bool GetEntityPropertyBool(const char* name) const;
int GetEntityPropertyInteger(const char* name) const;
float GetEntityPropertyFloat(const char* name) const;
QString GetEntityPropertyString(const char* name) const;
void SetEntityPropertyBool(const char* name, bool value);
void SetEntityPropertyInteger(const char* name, int value);
void SetEntityPropertyFloat(const char* name, float value);
void SetEntityPropertyString(const char* name, const QString& value);
virtual int MouseCreateCallback(CViewport* view, EMouseEvent event, QPoint& point, int flags);
virtual void OnContextMenu(QMenu* menu);
void SetName(const QString& name);
void SetSelected(bool bSelect);
virtual void GetLocalBounds(AABB& box);
virtual bool HitTest(HitContext& hc);
virtual bool HitHelperTest(HitContext& hc);
virtual bool HitTestRect(HitContext& hc);
void UpdateVisibility(bool bVisible);
bool ConvertFromObject(CBaseObject* object);
virtual void Serialize(CObjectArchive& ar);
virtual void PostLoad(CObjectArchive& ar);
XmlNodeRef Export(const QString& levelPath, XmlNodeRef& xmlNode);
//////////////////////////////////////////////////////////////////////////
void OnEvent(ObjectEvent event);
virtual void SetTransformDelegate(ITransformDelegate* pTransformDelegate) override;
// Set attach flags and target
enum EAttachmentType
{
eAT_Pivot,
eAT_GeomCacheNode,
eAT_CharacterBone,
};
void SetAttachType(const EAttachmentType attachmentType) { m_attachmentType = attachmentType; }
void SetAttachTarget(const char* target) { m_attachmentTarget = target; }
EAttachmentType GetAttachType() const { return m_attachmentType; }
QString GetAttachTarget() const { return m_attachmentTarget; }
virtual void SetHelperScale(float scale);
virtual float GetHelperScale();
virtual void GatherUsedResources(CUsedResources& resources);
virtual bool IsSimilarObject(CBaseObject* pObject);
virtual bool HasMeasurementAxis() const { return false; }
virtual bool IsIsolated() const { return false; }
//////////////////////////////////////////////////////////////////////////
// END CBaseObject
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// CEntity interface.
//////////////////////////////////////////////////////////////////////////
virtual void DeleteEntity() {}
QString GetEntityClass() const { return m_entityClass; };
int GetEntityId() const { return m_entityId; };
//////////////////////////////////////////////////////////////////////////
//! Return number of event targets of Script.
int GetEventTargetCount() const { return m_eventTargets.size(); };
CEntityEventTarget& GetEventTarget(int index) { return m_eventTargets[index]; };
//! Add new event target, returns index of created event target.
//! Event targets are Always entities.
int AddEventTarget(CBaseObject* target, const QString& event, const QString& sourceEvent, bool bUpdateScript = true);
//! Remove existing event target by index.
void RemoveEventTarget(int index, bool bUpdateScript = true);
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Entity Links.
//////////////////////////////////////////////////////////////////////////
//! Return number of event targets of Script.
int GetEntityLinkCount() const { return m_links.size(); };
CEntityLink& GetEntityLink(int index) { return m_links[index]; };
virtual int AddEntityLink(const QString& name, GUID targetEntityId);
virtual bool EntityLinkExists(const QString& name, GUID targetEntityId);
void RenameEntityLink(int index, const QString& newName);
void RemoveEntityLink(int index);
void RemoveAllEntityLinks();
virtual void EntityLinked([[maybe_unused]] const QString& name, [[maybe_unused]] GUID targetEntityId){}
virtual void EntityUnlinked([[maybe_unused]] const QString& name, [[maybe_unused]] GUID targetEntityId) {}
void LoadLink(XmlNodeRef xmlNode, CObjectArchive* pArchive = NULL);
void SaveLink(XmlNodeRef xmlNode);
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
int GetCastShadowMinSpec() const { return mv_castShadowMinSpec; }
float GetRatioLod() const { return static_cast<float>(mv_ratioLOD); };
float GetViewDistanceMultiplier() const { return mv_viewDistanceMultiplier; }
CVarBlock* GetProperties() const { return m_pProperties; };
CVarBlock* GetProperties2() const { return m_pProperties2; };
bool IsLight() const { return m_bLight; }
void Validate(IErrorReport* report) override;
// Find CEntity from AZ::EntityId, which can also handle legacy game Ids stored as AZ::EntityIds
static CEntityObject* FindFromEntityId(const AZ::EntityId& id);
// Get the name of the light animation node assigned to this, if any.
QString GetLightAnimation() const;
IVariable* GetLightVariable(const char* name) const;
void PreInitLightProperty();
void UpdateLightProperty();
void EnableReload(bool bEnable)
{
m_bEnableReload = bEnable;
}
static void StoreUndoEntityLink(CSelectionGroup* pGroup);
void RegisterListener(IEntityObjectListener* pListener);
void UnregisterListener(IEntityObjectListener* pListener);
protected:
template <typename T>
void SetEntityProperty(const char* name, T value);
template <typename T>
T GetEntityProperty(const char* name, T defaultvalue) const;
//////////////////////////////////////////////////////////////////////////
//! Must be called after cloning the object on clone of object.
//! This will make sure object references are cloned correctly.
virtual void PostClone(CBaseObject* pFromObject, CObjectCloneContext& ctx);
//! Draw default object items.
void DrawProjectorPyramid(DisplayContext& dc, float dist);
void DrawProjectorFrustum(DisplayContext& dc, Vec2 size, float dist);
void OnLoadFailed();
CVarBlock* CloneProperties(CVarBlock* srcProperties);
//////////////////////////////////////////////////////////////////////////
//! Callback called when one of entity properties have been modified.
void OnPropertyChange(IVariable* var);
//////////////////////////////////////////////////////////////////////////
void OnObjectEvent(CBaseObject* target, int event) override;
void ResolveEventTarget(CBaseObject* object, unsigned int index);
void ReleaseEventTargets();
public:
CEntityObject();
static const GUID& GetClassID()
{
// {C80F8AEA-90EF-471f-82C7-D14FA80B9203}
static const GUID guid = {
0xc80f8aea, 0x90ef, 0x471f, { 0x82, 0xc7, 0xd1, 0x4f, 0xa8, 0xb, 0x92, 0x3 }
};
return guid;
}
protected:
void DeleteThis() { delete this; };
//////////////////////////////////////////////////////////////////////////
// Radius callbacks.
//////////////////////////////////////////////////////////////////////////
void OnRadiusChange(IVariable* var);
void OnInnerRadiusChange(IVariable* var);
void OnOuterRadiusChange(IVariable* var);
void OnBoxSizeXChange(IVariable* var);
void OnBoxSizeYChange(IVariable* var);
void OnBoxSizeZChange(IVariable* var);
void OnProjectorFOVChange(IVariable* var);
void OnProjectorTextureChange(IVariable* var);
void OnProjectInAllDirsChange(IVariable* var);
//////////////////////////////////////////////////////////////////////////
// Area light callbacks.
//////////////////////////////////////////////////////////////////////////
void OnAreaLightChange(IVariable* var);
void OnAreaWidthChange(IVariable* var);
void OnAreaHeightChange(IVariable* var);
void OnAreaFOVChange(IVariable* var);
void OnAreaLightSizeChange(IVariable* var);
void OnColorChange(IVariable* var);
//////////////////////////////////////////////////////////////////////////
// Box projection callbacks.
//////////////////////////////////////////////////////////////////////////
void OnBoxProjectionChange(IVariable* var);
void OnBoxWidthChange(IVariable* var);
void OnBoxHeightChange(IVariable* var);
void OnBoxLengthChange(IVariable* var);
//////////////////////////////////////////////////////////////////////////
void FreeGameData();
void AdjustLightProperties(CVarBlockPtr& properties, const char* pSubBlock);
IVariable* FindVariableInSubBlock(CVarBlockPtr& properties, IVariable* pSubBlockVar, const char* pVarName);
unsigned int m_bLoadFailed : 1;
unsigned int m_bCalcPhysics : 1;
unsigned int m_bDisplayBBox : 1;
unsigned int m_bDisplaySolidBBox : 1;
unsigned int m_bDisplayAbsoluteRadius : 1;
unsigned int m_bDisplayArrow : 1;
unsigned int m_bIconOnTop : 1;
unsigned int m_bVisible : 1;
unsigned int m_bLight : 1;
unsigned int m_bAreaLight : 1;
unsigned int m_bProjectorHasTexture : 1;
unsigned int m_bProjectInAllDirs : 1;
unsigned int m_bBoxProjectedCM : 1;
unsigned int m_bBBoxSelection : 1;
AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
Vec3 m_lightColor;
//! Entity class.
QString m_entityClass;
//! Id of spawned entity.
int m_entityId;
// Used for light entities
float m_projectorFOV;
IStatObj* m_visualObject;
AABB m_box;
//////////////////////////////////////////////////////////////////////////
// Main entity parameters.
//////////////////////////////////////////////////////////////////////////
CVariable<bool> mv_outdoor;
CVariable<bool> mv_castShadow; // Legacy, required for backwards compatibility
CSmartVariableEnum<int> mv_castShadowMinSpec;
CVariable<int> mv_ratioLOD;
CVariable<float> mv_viewDistanceMultiplier;
CVariable<bool> mv_hiddenInGame; // Entity is hidden in game (on start).
CVariable<bool> mv_recvWind;
CVariable<bool> mv_renderNearest;
CVariable<bool> mv_noDecals;
CVariable<bool> mv_createdThroughPool;
CVariable<float> mv_obstructionMultiplier;
//////////////////////////////////////////////////////////////////////////
// Temp variables (Not serializable) just to display radii from properties.
//////////////////////////////////////////////////////////////////////////
// Used for proximity entities.
float m_proximityRadius;
float m_innerRadius;
float m_outerRadius;
// Used for probes
float m_boxSizeX;
float m_boxSizeY;
float m_boxSizeZ;
// Used for area lights
float m_fAreaWidth;
float m_fAreaHeight;
float m_fAreaLightSize;
// Used for box projected cubemaps
float m_fBoxWidth;
float m_fBoxHeight;
float m_fBoxLength;
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// Event Targets.
//////////////////////////////////////////////////////////////////////////
//! Array of event targets of this Entity.
typedef std::vector<CEntityEventTarget> EventTargets;
EventTargets m_eventTargets;
//////////////////////////////////////////////////////////////////////////
// Links
typedef std::vector<CEntityLink> Links;
Links m_links;
//! Entity properties variables.
CVarBlockPtr m_pProperties;
//! Per instance entity properties variables
CVarBlockPtr m_pProperties2;
// Physics state, as a string.
XmlNodeRef m_physicsState;
AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
static float m_helperScale;
EAttachmentType m_attachmentType;
bool m_bEnableReload;
QString m_attachmentTarget;
private:
struct VariableCallbackIndex
{
enum : unsigned char
{
OnAreaHeightChange = 0,
OnAreaLightChange,
OnAreaLightSizeChange,
OnAreaWidthChange,
OnBoxHeightChange,
OnBoxLengthChange,
OnBoxProjectionChange,
OnBoxSizeXChange,
OnBoxSizeYChange,
OnBoxSizeZChange,
OnBoxWidthChange,
OnColorChange,
OnInnerRadiusChange,
OnOuterRadiusChange,
OnProjectInAllDirsChange,
OnProjectorFOVChange,
OnProjectorTextureChange,
OnPropertyChange,
OnRadiusChange,
// must be at the end
Count,
};
};
void ResetCallbacks();
void SetVariableCallback(IVariable* pVar, IVariable::OnSetCallback* func);
void ClearCallbacks();
void ForceVariableUpdate();
AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
CListenerSet<IEntityObjectListener*> m_listeners;
std::vector< std::pair<IVariable*, IVariable::OnSetCallback*> > m_callbacks;
AZStd::fixed_vector< IVariable::OnSetCallback, VariableCallbackIndex::Count > m_onSetCallbacksCache;
AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
};
#endif // CRYINCLUDE_EDITOR_OBJECTS_ENTITYOBJECT_H