!I (1670414, 1670415, 1670416, 1670424, 1670431):
[CRYENGINE.git] / Code / Sandbox / Plugins / VehicleEditor / VehicleSeat.cpp
blobda68e01153b231ca52e42af4a71e1b66e8a69ef7
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
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)
21 namespace
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);
36 return pWeapon;
40 //////////////////////////////////////////////////////////////////////////
41 CVehicleSeat::CVehicleSeat()
43 m_pVehicle = 0;
44 m_pPart = 0;
45 m_pVar = 0;
48 //////////////////////////////////////////////////////////////////////////
49 bool CVehicleSeat::Init(CBaseObject* prev, const string& file)
51 bool res = CBaseObject::Init(prev, file);
52 return res;
55 //////////////////////////////////////////////////////////////////////////
56 void CVehicleSeat::Display(DisplayContext& dc)
58 return;
60 COLORREF color = GetColor();
62 if (IsSelected())
64 dc.SetSelectedColor(0.6f);
67 //if (dc.flags & DISPLAY_2D)
69 AABB box;
70 GetLocalBounds(box);
71 dc.SetColor(color, 0.8f);
72 dc.PushMatrix(GetWorldTM());
73 dc.SetLineWidth(2);
74 dc.DrawWireBox(box.min, box.max);
75 dc.SetLineWidth(0);
76 dc.PopMatrix();
79 DrawDefault(dc);
82 //////////////////////////////////////////////////////////////////////////
83 bool CVehicleSeat::HitTest(HitContext& hc)
85 return false;
88 //////////////////////////////////////////////////////////////////////////
89 void CVehicleSeat::GetBoundBox(AABB& box)
91 box.Reset();
92 // Transform local bounding box into world space.
93 GetLocalBounds(box);
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()
107 if (!m_pVar)
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"))
123 // update obj name
124 string name;
125 pName->Get(name);
126 SetName(name);
129 if (IVariable* pPart = GetChildVar(m_pVar, "part"))
131 string val;
132 pPart->Get(val);
134 DetachThis();
135 CBaseObject* pPartObj = GetIEditor()->GetObjectManager()->FindObject(val);
137 if (val == "" || !pPartObj || !pPartObj->IsKindOf(RUNTIME_CLASS(CVehiclePart)))
139 m_pVehicle->AttachChild(this);
141 else
143 pPartObj->AttachChild(this);
149 //////////////////////////////////////////////////////////////////////////
150 void CVehicleSeat::SetVariable(IVariable* pVar)
152 m_pVar = pVar;
153 if (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));
181 CBaseObject::Done();
184 //////////////////////////////////////////////////////////////////////////
185 void CVehicleSeat::UpdateVarFromObject()
187 assert(m_pVar);
188 if (!m_pVar)
189 return;
191 IEntity* pEnt = m_pVehicle->GetCEntity()->GetIEntity();
192 if (!pEnt)
194 CryWarning(VALIDATOR_MODULE_EDITOR, VALIDATOR_WARNING, "[CVehicleSeat::UpdateVariable] pEnt is null, returning");
195 return;
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");
204 if (pParent && pVar)
206 if (pParent == m_pVehicle)
207 pVar->Set("");
208 else
209 pVar->Set(pParent->GetName());
213 //////////////////////////////////////////////////////////////////////////
214 void CVehicleSeat::SetVehicle(CVehiclePrototype* pProt)
216 m_pVehicle = pProt;
217 if (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
231 if (!pWeaponVar)
233 const char* category = (weaponType == WEAPON_PRIMARY) ? "Primary" : "Secondary";
235 IVariable* pWeapons = GetChildVar(m_pVar, category, true);
236 if (pWeapons)
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());
253 // delete variable
254 if (IVeedObject* pVO = IVeedObject::GetVeedObject(node))
256 if (pVO->DeleteVar())
258 if (m_pVar)
259 m_pVar->DeleteVariable(pVO->GetVariable(), true);
260 pVO->SetVariable(0);
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);