1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
4 #include "VehicleSeat.h"
6 #include "VehiclePrototype.h"
7 #include "VehicleData.h"
8 #include "VehiclePart.h"
9 #include "VehicleEditorDialog.h"
10 #include "VehiclePartsPanel.h"
11 #include "VehicleWeapon.h"
12 #include "Objects/DisplayContext.h"
14 REGISTER_CLASS_DESC(CVehicleSeatClassDesc
);
16 //////////////////////////////////////////////////////////////////////////
17 // CBase implementation.
18 //////////////////////////////////////////////////////////////////////////
19 IMPLEMENT_DYNCREATE(CVehicleSeat
, CBaseObject
)
23 IVariable
* CreateWeaponVar()
25 IVariable
* pWeapon
= new CVariableArray
;
26 pWeapon
->SetName("Weapon");
28 IVariable
* pClass
= new CVariable
<string
>;
29 pClass
->SetName("class");
30 pWeapon
->AddVariable(pClass
);
32 IVariable
* pHelpers
= new CVariableArray
;
33 pHelpers
->SetName("Helpers");
34 pWeapon
->AddVariable(pHelpers
);
40 //////////////////////////////////////////////////////////////////////////
41 CVehicleSeat::CVehicleSeat()
48 //////////////////////////////////////////////////////////////////////////
49 bool CVehicleSeat::Init(CBaseObject
* prev
, const string
& file
)
51 bool res
= CBaseObject::Init(prev
, file
);
55 //////////////////////////////////////////////////////////////////////////
56 void CVehicleSeat::Display(DisplayContext
& dc
)
60 COLORREF color
= GetColor();
64 dc
.SetSelectedColor(0.6f
);
67 //if (dc.flags & DISPLAY_2D)
71 dc
.SetColor(color
, 0.8f
);
72 dc
.PushMatrix(GetWorldTM());
74 dc
.DrawWireBox(box
.min
, box
.max
);
82 //////////////////////////////////////////////////////////////////////////
83 bool CVehicleSeat::HitTest(HitContext
& hc
)
88 //////////////////////////////////////////////////////////////////////////
89 void CVehicleSeat::GetBoundBox(AABB
& box
)
92 // Transform local bounding box into world space.
94 box
.SetTransformedAABB(GetWorldTM(), box
);
97 //////////////////////////////////////////////////////////////////////////
98 void CVehicleSeat::GetLocalBounds(AABB
& box
)
100 // return local bounds
101 // todo: take box around sit and eye helpers?
104 //////////////////////////////////////////////////////////////////////////
105 void CVehicleSeat::UpdateObjectFromVar()
109 // create default seat here
110 m_pVar
= CreateDefaultChildOf("Seats");
111 IVariable
* pSeats
= GetChildVar(m_pVehicle
->GetVariable(), "Seats");
112 if (pSeats
&& m_pVar
)
113 pSeats
->AddVariable(m_pVar
);
115 if (IVariable
* pPart
= GetChildVar(m_pVar
, "part"))
117 pPart
->AddOnSetCallback(functor(*this, &CVehicleSeat::OnSetPart
));
121 if (IVariable
* pName
= GetChildVar(m_pVar
, "name"))
129 if (IVariable
* pPart
= GetChildVar(m_pVar
, "part"))
135 CBaseObject
* pPartObj
= GetIEditor()->GetObjectManager()->FindObject(val
);
137 if (val
== "" || !pPartObj
|| !pPartObj
->IsKindOf(RUNTIME_CLASS(CVehiclePart
)))
139 m_pVehicle
->AttachChild(this);
143 pPartObj
->AttachChild(this);
149 //////////////////////////////////////////////////////////////////////////
150 void CVehicleSeat::SetVariable(IVariable
* pVar
)
154 UpdateObjectFromVar();
156 if (IVariable
* pPart
= GetChildVar(m_pVar
, "part"))
158 pPart
->AddOnSetCallback(functor(*this, &CVehicleSeat::OnSetPart
));
162 //////////////////////////////////////////////////////////////////////////
163 void CVehicleSeat::OnSetPart(IVariable
* pVar
)
165 UpdateObjectFromVar();
168 //////////////////////////////////////////////////////////////////////////
169 void CVehicleSeat::Done()
171 VeedLog("[CVehicleSeat:Done] <%s>", GetName());
173 int nChildCount
= GetChildCount();
174 for (int i
= nChildCount
- 1; i
>= 0; --i
)
176 CBaseObject
* pChild
= GetChild(i
);
177 VeedLog("[CVehicleSeat]: deleting %s", pChild
->GetName());
178 GetIEditor()->DeleteObject(GetChild(i
));
184 //////////////////////////////////////////////////////////////////////////
185 void CVehicleSeat::UpdateVarFromObject()
191 IEntity
* pEnt
= m_pVehicle
->GetCEntity()->GetIEntity();
194 CryWarning(VALIDATOR_MODULE_EDITOR
, VALIDATOR_WARNING
, "[CVehicleSeat::UpdateVariable] pEnt is null, returning");
198 if (IVariable
* pVar
= GetChildVar(m_pVar
, "name"))
199 pVar
->Set(GetName());
201 // update part variable
202 CBaseObject
* pParent
= GetParent();
203 IVariable
* pVar
= GetChildVar(m_pVar
, "part");
206 if (pParent
== m_pVehicle
)
209 pVar
->Set(pParent
->GetName());
213 //////////////////////////////////////////////////////////////////////////
214 void CVehicleSeat::SetVehicle(CVehiclePrototype
* pProt
)
218 SetWorldTM(pProt
->GetWorldTM());
222 //////////////////////////////////////////////////////////////////////////
223 void CVehicleSeat::AddWeapon(int weaponType
, CVehicleWeapon
* pWeap
, IVariable
* pVar
/*=0*/)
225 AttachChild(pWeap
, true);
226 pWeap
->SetVehicle(m_pVehicle
);
228 IVariable
* pWeaponVar
= pVar
;
230 // if no variable, create and add it
233 const char* category
= (weaponType
== WEAPON_PRIMARY
) ? "Primary" : "Secondary";
235 IVariable
* pWeapons
= GetChildVar(m_pVar
, category
, true);
238 pWeaponVar
= CreateWeaponVar();
239 pWeapons
->AddVariable(pWeaponVar
);
243 pWeap
->SetVariable(pWeaponVar
);
244 pWeap
->UpdateObjectFromVar();
247 //////////////////////////////////////////////////////////////////////////
248 void CVehicleSeat::OnObjectEvent(CBaseObject
* node
, int event
)
250 if (event
== OBJECT_ON_DELETE
)
252 VeedLog("[CVehicleSeat]: ON_DELETE for %s", node
->GetName());
254 if (IVeedObject
* pVO
= IVeedObject::GetVeedObject(node
))
256 if (pVO
->DeleteVar())
259 m_pVar
->DeleteVariable(pVO
->GetVariable(), true);
261 VeedLog("[CVehiclePart] deleting var for %s", node
->GetName());
267 //////////////////////////////////////////////////////////////////////////
268 void CVehicleSeat::AttachChild(CBaseObject
* child
, bool bKeepPos
, bool bInvalidateTM
)
270 child
->AddEventListener(functor(*this, &CVehicleSeat::OnObjectEvent
));
272 CBaseObject::AttachChild(child
, bKeepPos
, bInvalidateTM
);