!I (1670414, 1670415, 1670416, 1670424, 1670431):
[CRYENGINE.git] / Code / Sandbox / Plugins / VehicleEditor / VehicleHelperObject.cpp
blob56f33b09223e3c4e66fb11c24b76934c93ef469a
1 // Copyright 2001-2018 Crytek GmbH / Crytek Group. All rights reserved.
3 #include "StdAfx.h"
4 #include "VehicleHelperObject.h"
5 #include "Viewport.h"
7 #include "VehicleData.h"
8 #include "VehiclePrototype.h"
9 #include "VehiclePart.h"
11 REGISTER_CLASS_DESC(CVehicleHelperClassDesc);
13 //////////////////////////////////////////////////////////////////////////
14 // CBase implementation.
15 //////////////////////////////////////////////////////////////////////////
16 IMPLEMENT_DYNCREATE(CVehicleHelper, CBaseObject)
18 #define RADIUS 0.05f
20 #define VEHICLE_HELPER_COLOR (RGB(255, 255, 120))
21 #define VEHICLE_ASSET_HELPER_COLOR (RGB(120, 255, 120))
23 //////////////////////////////////////////////////////////////////////////
24 CVehicleHelper::CVehicleHelper()
26 m_pVar = 0;
27 m_fromGeometry = false;
28 m_pVehicle = 0;
30 ChangeColor(VEHICLE_HELPER_COLOR);
32 InitOnTransformCallback(this);
35 CVehicleHelper::~CVehicleHelper()
39 //////////////////////////////////////////////////////////////////////////
40 void CVehicleHelper::SetVariable(IVariable* pVar)
42 m_pVar = pVar;
44 DisableUpdateObjectOnVarChange("position");
45 DisableUpdateObjectOnVarChange("direction");
47 if (IVariable* pVar = GetChildVar(m_pVar, "position"))
49 pVar->SetLimits(-10, 10);
52 EnableUpdateObjectOnVarChange("position");
53 EnableUpdateObjectOnVarChange("direction");
56 //////////////////////////////////////////////////////////////////////////
57 void CVehicleHelper::UpdateVarFromObject()
59 if (IsFromGeometry())
61 assert(!m_pVar);
62 return;
65 assert(m_pVar);
66 if (!m_pVar || !m_pVehicle)
67 return;
69 IEntity* pEnt = m_pVehicle->GetCEntity()->GetIEntity();
70 if (!pEnt)
72 CryWarning(VALIDATOR_MODULE_EDITOR, VALIDATOR_WARNING, "[CVehicleHelper::UpdateVariable] pEnt is null, returning");
73 return;
76 if (IVariable* pVar = GetChildVar(m_pVar, "name"))
77 pVar->Set(GetName());
79 if (IVariable* pVar = GetChildVar(m_pVar, "position"))
81 Vec3 pos = pEnt->GetWorldTM().GetInvertedFast().TransformPoint(GetWorldPos());
82 pVar->Set(pos);
85 IVariable* pVar = GetChildVar(m_pVar, "direction");
86 if (!pVar)
88 pVar = new CVariable<Vec3>;
89 pVar->SetName("direction");
90 m_pVar->AddVariable(pVar);
93 IVariable* pPartVar = GetChildVar(m_pVar, "part");
94 if (!pPartVar)
96 pPartVar = new CVariable<string>;
97 pPartVar->SetName("part");
98 m_pVar->AddVariable(pPartVar);
101 Matrix33 relTM = Matrix33(pEnt->GetWorldTM().GetInvertedFast()) * Matrix33(GetWorldTM());
102 Vec3 dir = relTM.TransformVector(FORWARD_DIRECTION);
103 pVar->Set(dir);
107 //////////////////////////////////////////////////////////////////////////
108 void CVehicleHelper::UpdateObjectFromVar()
110 if (!m_pVar || !m_pVehicle)
111 return;
113 if (IVariable* pVar = GetChildVar(m_pVar, "position"))
115 Vec3 local(ZERO);
116 pVar->Get(local);
117 Matrix34 tm = GetWorldTM();
118 tm.SetTranslation(m_pVehicle->GetCEntity()->GetIEntity()->GetWorldTM().TransformPoint(local));
119 SetWorldTM(tm);
123 //////////////////////////////////////////////////////////////////////////
124 void CVehicleHelper::Done()
126 VeedLog("[CVehicleHelper:Done] <%s>", GetName());
127 CBaseObject::Done();
130 //////////////////////////////////////////////////////////////////////////
131 bool CVehicleHelper::Init(CBaseObject* prev, const string& file)
133 SetColor(RGB(255, 255, 0));
134 bool res = CBaseObject::Init(prev, file);
136 return res;
139 //////////////////////////////////////////////////////////////////////////
140 void CVehicleHelper::PostClone(CBaseObject* pFromObject, CObjectCloneContext& ctx)
142 CBaseObject* pFromParent = pFromObject->GetParent();
143 if (pFromParent)
145 CBaseObject* pChildParent = ctx.FindClone(pFromParent);
146 if (pChildParent)
147 pChildParent->AttachChild(this, false);
148 else
150 // // helper was cloned and attached to same parent
151 // if (pFromParent->IsKindOf(RUNTIME_CLASS(CVehiclePart)))
152 // ((CVehiclePart*)pFromParent)->AddHelper( this, 0 );
153 // else
154 // pFromParent->AttachChild(this, false);
159 //////////////////////////////////////////////////////////////////////////
160 bool CVehicleHelper::HitTest(HitContext& hc)
162 Vec3 origin = GetWorldPos();
163 float radius = RADIUS;
165 Vec3 w = origin - hc.raySrc;
166 w = hc.rayDir.Cross(w);
167 float d = w.GetLength();
169 if (d < radius + hc.distanceTolerance)
171 Vec3 i0;
172 if (Intersect::Ray_SphereFirst(Ray(hc.raySrc, hc.rayDir), Sphere(origin, radius), i0))
174 hc.dist = hc.raySrc.GetDistance(i0);
175 return true;
177 hc.dist = hc.raySrc.GetDistance(origin);
178 return true;
180 return false;
183 //////////////////////////////////////////////////////////////////////////
184 void CVehicleHelper::Display(DisplayContext& dc)
186 COLORREF color = GetColor();
187 float radius = RADIUS;
189 //dc.SetColor( color, 0.5f );
190 //dc.DrawBall( GetPos(), radius );
192 if (IsSelected())
194 dc.SetSelectedColor(0.6f);
197 AABB box;
198 GetLocalBounds(box);
199 dc.PushMatrix(GetWorldTM());
201 if (!IsHighlighted())
203 dc.SetColor(color, 0.8f);
204 dc.SetLineWidth(2);
205 dc.DrawWireBox(box.min, box.max);
208 if (!IsSelected())
210 // direction vector
211 Vec3 dirEndPos(0, 4 * box.max.y, 0);
212 dc.DrawArrow(Vec3(0, box.max.y, 0), dirEndPos, 0.15f);
215 dc.PopMatrix();
217 // draw label
218 if (dc.flags & DISPLAY_HIDENAMES)
220 Vec3 p(GetWorldPos());
221 DrawLabel(dc, p, RGB(255, 255, 255));
224 DrawDefault(dc);
227 //////////////////////////////////////////////////////////////////////////
228 void CVehicleHelper::GetBoundBox(AABB& box)
230 // Transform local bounding box into world space.
231 GetLocalBounds(box);
232 box.SetTransformedAABB(GetWorldTM(), box);
235 //////////////////////////////////////////////////////////////////////////
236 void CVehicleHelper::GetLocalBounds(AABB& box)
238 // return local bounds
239 float r = RADIUS;
240 box.min = -Vec3(r, r, r);
241 box.max = Vec3(r, r, r);
244 //////////////////////////////////////////////////////////////////////////
245 void CVehicleHelper::GetBoundSphere(Vec3& pos, float& radius)
247 pos = GetPos();
248 radius = RADIUS;
251 //////////////////////////////////////////////////////////////////////////
252 void CVehicleHelper::UpdateScale(float scale)
254 if (IVariable* pPos = GetChildVar(m_pVar, "position"))
256 Vec3 pos;
257 pPos->Get(pos);
258 pPos->Set(pos *= scale);
259 UpdateObjectFromVar();
263 //////////////////////////////////////////////////////////////////////////
264 void CVehicleHelper::SetIsFromGeometry(bool b)
266 m_fromGeometry = b;
267 if (m_fromGeometry)
269 ChangeColor(VEHICLE_ASSET_HELPER_COLOR);
270 SetFrozen(true);
272 else
274 ChangeColor(VEHICLE_HELPER_COLOR);
275 SetFrozen(false);
279 //////////////////////////////////////////////////////////////////////////
280 void CVehicleHelper::OnTransform()
282 UpdateVarFromObject();