Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "MessageHandler.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "graphics/CinemaManager.h"
24 : #include "graphics/GameView.h"
25 : #include "lib/debug.h"
26 : #include "maths/Fixed.h"
27 : #include "maths/FixedVector3D.h"
28 : #include "maths/MathUtil.h"
29 : #include "maths/Matrix3D.h"
30 : #include "maths/NUSpline.h"
31 : #include "maths/Quaternion.h"
32 : #include "maths/Vector2D.h"
33 : #include "maths/Vector3D.h"
34 : #include "ps/CStr.h"
35 : #include "ps/Game.h"
36 : #include "simulation2/components/ICmpCinemaManager.h"
37 : #include "simulation2/helpers/CinemaPath.h"
38 : #include "simulation2/system/Component.h"
39 : #include "simulation2/system/Entity.h"
40 : #include "tools/atlas/GameInterface/CommandProc.h"
41 : #include "tools/atlas/GameInterface/GameLoop.h"
42 : #include "tools/atlas/GameInterface/Messages.h"
43 : #include "tools/atlas/GameInterface/Shareable.h"
44 : #include "tools/atlas/GameInterface/SharedTypes.h"
45 : #include "tools/atlas/GameInterface/View.h"
46 :
47 : #include <cstddef>
48 : #include <map>
49 : #include <string>
50 : #include <utility>
51 : #include <vector>
52 :
53 : namespace AtlasMessage { struct cAddPathNode; }
54 : namespace AtlasMessage { struct cDeletePathNode; }
55 : namespace AtlasMessage { struct cMovePathNode; }
56 :
57 : namespace AtlasMessage
58 : {
59 :
60 : const float MINIMAL_SCREEN_DISTANCE = 5.f;
61 :
62 0 : sCinemaPath ConstructCinemaPath(const CCinemaPath* source)
63 : {
64 0 : sCinemaPath path;
65 0 : const CCinemaData* data = source->GetData();
66 : //path.mode = data->m_Mode;
67 : //path.style = data->m_Style;
68 0 : path.growth = data->m_Growth;
69 0 : path.timescale = data->m_Timescale.ToFloat();
70 0 : path.change = data->m_Switch;
71 :
72 0 : return path;
73 0 : }
74 0 : CCinemaData ConstructCinemaData(const sCinemaPath& path)
75 : {
76 0 : CCinemaData data;
77 0 : data.m_Growth = data.m_GrowthCount = path.growth;
78 0 : data.m_Switch = path.change;
79 : //data.m_Mode = path.mode;
80 : //data.m_Style = path.style;
81 :
82 0 : return data;
83 : }
84 0 : sCinemaSplineNode ConstructCinemaNode(const SplineData& data)
85 : {
86 0 : sCinemaSplineNode node;
87 0 : node.px = data.Position.X.ToFloat();
88 0 : node.py = data.Position.Y.ToFloat();
89 0 : node.pz = data.Position.Z.ToFloat();
90 :
91 0 : node.rx = data.Rotation.X.ToFloat();
92 0 : node.ry = data.Rotation.Y.ToFloat();
93 0 : node.rz = data.Rotation.Z.ToFloat();
94 0 : node.t = data.Distance.ToFloat();
95 :
96 0 : return node;
97 : }
98 :
99 0 : std::vector<sCinemaPath> GetCurrentPaths()
100 : {
101 0 : std::vector<sCinemaPath> atlasPaths;
102 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
103 0 : if (!cmpCinemaManager)
104 : return atlasPaths;
105 0 : const std::map<CStrW, CCinemaPath>& paths = cmpCinemaManager->GetPaths();
106 :
107 0 : for ( std::map<CStrW, CCinemaPath>::const_iterator it=paths.begin(); it!=paths.end(); ++it )
108 : {
109 0 : sCinemaPath path = ConstructCinemaPath(&it->second);
110 0 : path.name = it->first;
111 :
112 0 : const std::vector<SplineData>& nodes = it->second.GetAllNodes();
113 0 : std::vector<sCinemaSplineNode> atlasNodes;
114 :
115 0 : for ( size_t i=0; i<nodes.size(); ++i )
116 0 : atlasNodes.push_back( ConstructCinemaNode(nodes[i]) );
117 :
118 0 : if ( !atlasNodes.empty() )
119 : {
120 0 : float back = atlasNodes.back().t;
121 0 : if ( atlasNodes.size() > 2 )
122 : {
123 0 : for ( size_t i=atlasNodes.size()-2; i>0; --i )
124 0 : atlasNodes[i].t = atlasNodes[i-1].t;
125 : }
126 0 : atlasNodes.back().t = atlasNodes.front().t;
127 0 : atlasNodes.front().t = back;
128 : }
129 0 : path.nodes = atlasNodes;
130 0 : atlasPaths.push_back(path);
131 0 : }
132 : return atlasPaths;
133 0 : }
134 :
135 0 : void SetCurrentPaths(const std::vector<sCinemaPath>& atlasPaths)
136 : {
137 0 : std::map<CStrW, CCinemaPath> paths;
138 :
139 0 : for ( std::vector<sCinemaPath>::const_iterator it=atlasPaths.begin(); it!=atlasPaths.end(); ++it )
140 : {
141 0 : CStrW pathName(*it->name);
142 0 : paths[pathName] = CCinemaPath();
143 0 : paths[pathName].SetTimescale(fixed::FromFloat(it->timescale));
144 :
145 0 : const sCinemaPath& atlasPath = *it;
146 0 : const std::vector<sCinemaSplineNode> nodes = *atlasPath.nodes;
147 0 : TNSpline spline;
148 0 : CCinemaData data = ConstructCinemaData(atlasPath);
149 :
150 0 : for ( size_t j=0; j<nodes.size(); ++j )
151 : {
152 0 : spline.AddNode(CFixedVector3D(fixed::FromFloat(nodes[j].px), fixed::FromFloat(nodes[j].py), fixed::FromFloat(nodes[j].pz)),
153 0 : CFixedVector3D(fixed::FromFloat(nodes[j].rx), fixed::FromFloat(nodes[j].ry), fixed::FromFloat(nodes[j].rz)), fixed::FromFloat(nodes[j].t));
154 : }
155 0 : paths[pathName] = CCinemaPath(data, spline, TNSpline());
156 0 : }
157 :
158 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
159 0 : if (cmpCinemaManager)
160 0 : cmpCinemaManager->SetPaths(paths);
161 0 : }
162 0 : QUERYHANDLER(GetCameraInfo)
163 : {
164 0 : sCameraInfo info;
165 0 : const CMatrix3D& cameraOrientation = g_Game->GetView()->GetCamera().GetOrientation();
166 :
167 0 : CQuaternion quatRot = cameraOrientation.GetRotation();
168 0 : quatRot.Normalize();
169 0 : CVector3D rotation = quatRot.ToEulerAngles();
170 0 : rotation.X = RADTODEG(rotation.X);
171 0 : rotation.Y = RADTODEG(rotation.Y);
172 0 : rotation.Z = RADTODEG(rotation.Z);
173 0 : CVector3D translation = cameraOrientation.GetTranslation();
174 :
175 0 : info.pX = translation.X;
176 0 : info.pY = translation.Y;
177 0 : info.pZ = translation.Z;
178 0 : info.rX = rotation.X;
179 0 : info.rY = rotation.Y;
180 0 : info.rZ = rotation.Z;
181 0 : msg->info = info;
182 0 : }
183 :
184 0 : MESSAGEHANDLER(CinemaEvent)
185 : {
186 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
187 0 : if (!cmpCinemaManager)
188 0 : return;
189 :
190 0 : if (msg->mode == eCinemaEventMode::SMOOTH)
191 : {
192 0 : cmpCinemaManager->PushPathToQueue(*msg->path);
193 : }
194 0 : else if ( msg->mode == eCinemaEventMode::RESET )
195 : {
196 : // g_Game->GetView()->ResetCamera();
197 : }
198 : else
199 0 : ENSURE(false);
200 : }
201 :
202 0 : BEGIN_COMMAND(AddCinemaPath)
203 : {
204 0 : void Do()
205 : {
206 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
207 0 : if (!cmpCinemaManager)
208 0 : return;
209 :
210 0 : CCinemaData pathData;
211 0 : pathData.m_Name = *msg->pathName;
212 0 : pathData.m_Timescale = fixed::FromInt(1);
213 0 : pathData.m_Orientation = L"target";
214 0 : pathData.m_Mode = L"ease_inout";
215 0 : pathData.m_Style = L"default";
216 :
217 0 : CVector3D focus = g_Game->GetView()->GetCamera().GetFocus();
218 0 : CFixedVector3D target(
219 : fixed::FromFloat(focus.X),
220 : fixed::FromFloat(focus.Y),
221 : fixed::FromFloat(focus.Z)
222 0 : );
223 :
224 0 : CVector3D camera = g_Game->GetView()->GetCamera().GetOrientation().GetTranslation();
225 0 : CFixedVector3D position(
226 : fixed::FromFloat(camera.X),
227 : fixed::FromFloat(camera.Y),
228 : fixed::FromFloat(camera.Z)
229 0 : );
230 :
231 0 : TNSpline positionSpline;
232 0 : positionSpline.AddNode(position, CFixedVector3D(), fixed::FromInt(0));
233 :
234 0 : TNSpline targetSpline;
235 0 : targetSpline.AddNode(target, CFixedVector3D(), fixed::FromInt(0));
236 :
237 0 : cmpCinemaManager->AddPath(CCinemaPath(pathData, positionSpline, targetSpline));
238 0 : }
239 0 : void Redo()
240 : {
241 0 : }
242 0 : void Undo()
243 : {
244 0 : }
245 : };
246 0 : END_COMMAND(AddCinemaPath)
247 :
248 0 : BEGIN_COMMAND(DeleteCinemaPath)
249 : {
250 0 : void Do()
251 : {
252 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
253 0 : if (!cmpCinemaManager)
254 0 : return;
255 :
256 0 : cmpCinemaManager->DeletePath(*msg->pathName);
257 : }
258 0 : void Redo()
259 : {
260 0 : }
261 0 : void Undo()
262 : {
263 0 : }
264 : };
265 0 : END_COMMAND(DeleteCinemaPath)
266 :
267 0 : BEGIN_COMMAND(SetCinemaPaths)
268 : {
269 : std::vector<sCinemaPath> m_oldPaths, m_newPaths;
270 0 : void Do()
271 : {
272 0 : m_oldPaths = GetCurrentPaths();
273 0 : m_newPaths = *msg->paths;
274 0 : Redo();
275 0 : }
276 0 : void Redo()
277 : {
278 0 : SetCurrentPaths(m_newPaths);
279 0 : }
280 0 : void Undo()
281 : {
282 0 : SetCurrentPaths(m_oldPaths);
283 0 : }
284 : };
285 0 : END_COMMAND(SetCinemaPaths)
286 :
287 0 : BEGIN_COMMAND(SetCinemaPathsDrawing)
288 : {
289 0 : void Do()
290 : {
291 0 : if (g_Game && g_Game->GetView() && g_Game->GetView()->GetCinema())
292 0 : g_Game->GetView()->GetCinema()->SetPathsDrawing(msg->drawPaths);
293 0 : }
294 :
295 0 : void Redo()
296 : {
297 0 : }
298 :
299 0 : void Undo()
300 : {
301 0 : }
302 : };
303 0 : END_COMMAND(SetCinemaPathsDrawing)
304 :
305 0 : static CVector3D GetNearestPointToScreenCoords(const CVector3D& base, const CVector3D& dir, const CVector2D& screen, float lower = -1e5, float upper = 1e5)
306 : {
307 : // It uses a ternary search, because an intersection of cylinders is the complex task
308 0 : for (int i = 0; i < 64; ++i)
309 : {
310 0 : float delta = (upper - lower) / 3.0;
311 0 : float middle1 = lower + delta, middle2 = lower + 2.0f * delta;
312 0 : CVector3D p1 = base + dir * middle1, p2 = base + dir * middle2;
313 0 : const CVector2D s1{g_Game->GetView()->GetCamera().GetScreenCoordinates(p1)};
314 0 : const CVector2D s2{g_Game->GetView()->GetCamera().GetScreenCoordinates(p2)};
315 0 : if ((s1 - screen).Length() < (s2 - screen).Length())
316 : upper = middle2;
317 : else
318 0 : lower = middle1;
319 : }
320 0 : return base + dir * upper;
321 : }
322 :
323 : #define GET_PATH_NODE_WITH_VALIDATION() \
324 : int index = msg->node->index; \
325 : if (index < 0) \
326 : return; \
327 : CStrW name = *msg->node->name; \
328 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY); \
329 : if (!cmpCinemaManager || !cmpCinemaManager->HasPath(name)) \
330 : return; \
331 : const CCinemaPath& path = cmpCinemaManager->GetPaths().find(name)->second; \
332 : if (!msg->node->targetNode) \
333 : { \
334 : if (index >= (int)path.GetAllNodes().size()) \
335 : return; \
336 : } \
337 : else \
338 : { \
339 : if (index >= (int)path.GetTargetSpline().GetAllNodes().size()) \
340 : return; \
341 : }
342 :
343 0 : BEGIN_COMMAND(AddPathNode)
344 : {
345 0 : void Do()
346 : {
347 0 : GET_PATH_NODE_WITH_VALIDATION();
348 :
349 0 : CCinemaData data = *path.GetData();
350 0 : TNSpline positionSpline = path;
351 0 : TNSpline targetSpline = path.GetTargetSpline();
352 0 : TNSpline& spline = msg->node->targetNode ? targetSpline : positionSpline;
353 :
354 0 : CVector3D focus = g_Game->GetView()->GetCamera().GetFocus();
355 0 : CFixedVector3D target(
356 : fixed::FromFloat(focus.X),
357 : fixed::FromFloat(focus.Y),
358 : fixed::FromFloat(focus.Z)
359 0 : );
360 0 : spline.InsertNode(index + 1, target, CFixedVector3D(), fixed::FromInt(1));
361 :
362 0 : spline.BuildSpline();
363 0 : cmpCinemaManager->DeletePath(name);
364 0 : cmpCinemaManager->AddPath(CCinemaPath(data, positionSpline, targetSpline));
365 0 : }
366 :
367 0 : void Redo()
368 : {
369 0 : }
370 :
371 0 : void Undo()
372 : {
373 0 : }
374 : };
375 0 : END_COMMAND(AddPathNode)
376 :
377 0 : BEGIN_COMMAND(DeletePathNode)
378 : {
379 0 : void Do()
380 : {
381 0 : GET_PATH_NODE_WITH_VALIDATION();
382 :
383 0 : CCinemaData data = *path.GetData();
384 0 : TNSpline positionSpline = path;
385 0 : TNSpline targetSpline = path.GetTargetSpline();
386 0 : TNSpline& spline = msg->node->targetNode ? targetSpline : positionSpline;
387 0 : if (spline.GetAllNodes().size() <= 1)
388 0 : return;
389 :
390 0 : spline.RemoveNode(index);
391 0 : spline.BuildSpline();
392 0 : cmpCinemaManager->DeletePath(name);
393 0 : cmpCinemaManager->AddPath(CCinemaPath(data, positionSpline, targetSpline));
394 :
395 0 : g_AtlasGameLoop->view->SetParam(L"movetool", false);
396 0 : }
397 :
398 0 : void Redo()
399 : {
400 0 : }
401 :
402 0 : void Undo()
403 : {
404 0 : }
405 : };
406 0 : END_COMMAND(DeletePathNode)
407 :
408 0 : BEGIN_COMMAND(MovePathNode)
409 : {
410 0 : void Do()
411 : {
412 0 : int axis = msg->axis;
413 0 : if (axis == AXIS_INVALID)
414 0 : return;
415 :
416 0 : GET_PATH_NODE_WITH_VALIDATION();
417 :
418 0 : CCinemaData data = *path.GetData();
419 0 : TNSpline positionSpline = path;
420 0 : TNSpline targetSpline = path.GetTargetSpline();
421 0 : TNSpline& spline = msg->node->targetNode ? targetSpline : positionSpline;
422 :
423 : // Get shift of the tool by the cursor movement
424 0 : CFixedVector3D pos = spline.GetAllNodes()[index].Position;
425 0 : CVector3D position(
426 : pos.X.ToFloat(),
427 : pos.Y.ToFloat(),
428 : pos.Z.ToFloat()
429 0 : );
430 0 : CVector3D axisDirection(axis & AXIS_X, axis & AXIS_Y, axis & AXIS_Z);
431 0 : CVector2D from, to;
432 0 : msg->from->GetScreenSpace(from.X, from.Y);
433 0 : msg->to->GetScreenSpace(to.X, to.Y);
434 0 : CVector3D shift(
435 0 : GetNearestPointToScreenCoords(position, axisDirection, to) -
436 0 : GetNearestPointToScreenCoords(position, axisDirection, from)
437 : );
438 :
439 : // Change, rebuild and update the path
440 0 : position += shift;
441 0 : pos += CFixedVector3D(
442 : fixed::FromFloat(shift.X),
443 : fixed::FromFloat(shift.Y),
444 : fixed::FromFloat(shift.Z)
445 0 : );
446 0 : spline.UpdateNodePos(index, pos);
447 0 : spline.BuildSpline();
448 0 : cmpCinemaManager->DeletePath(name);
449 0 : cmpCinemaManager->AddPath(CCinemaPath(data, positionSpline, targetSpline));
450 :
451 : // Update visual tool coordinates
452 0 : g_AtlasGameLoop->view->SetParam(L"movetool_x", position.X);
453 0 : g_AtlasGameLoop->view->SetParam(L"movetool_y", position.Y);
454 0 : g_AtlasGameLoop->view->SetParam(L"movetool_z", position.Z);
455 0 : }
456 :
457 0 : void Redo()
458 : {
459 0 : }
460 :
461 0 : void Undo()
462 : {
463 0 : }
464 : };
465 0 : END_COMMAND(MovePathNode)
466 :
467 0 : QUERYHANDLER(GetCinemaPaths)
468 : {
469 0 : msg->paths = GetCurrentPaths();
470 0 : }
471 :
472 0 : static bool isPathNodePicked(const TNSpline& spline, const CVector2D& cursor, AtlasMessage::sCinemaPathNode& node, bool targetNode)
473 : {
474 0 : for (size_t i = 0; i < spline.GetAllNodes().size(); ++i)
475 : {
476 0 : const SplineData& data = spline.GetAllNodes()[i];
477 0 : CVector3D position(
478 : data.Position.X.ToFloat(),
479 : data.Position.Y.ToFloat(),
480 : data.Position.Z.ToFloat()
481 0 : );
482 0 : const CVector2D screenPos{g_Game->GetView()->GetCamera().GetScreenCoordinates(position)};
483 0 : if ((screenPos - cursor).Length() < MINIMAL_SCREEN_DISTANCE)
484 : {
485 0 : node.index = i;
486 0 : node.targetNode = targetNode;
487 0 : g_AtlasGameLoop->view->SetParam(L"movetool", true);
488 0 : g_AtlasGameLoop->view->SetParam(L"movetool_x", position.X);
489 0 : g_AtlasGameLoop->view->SetParam(L"movetool_y", position.Y);
490 0 : g_AtlasGameLoop->view->SetParam(L"movetool_z", position.Z);
491 0 : return true;
492 : }
493 : }
494 : return false;
495 : }
496 :
497 0 : QUERYHANDLER(PickPathNode)
498 : {
499 0 : AtlasMessage::sCinemaPathNode node;
500 0 : CmpPtr<ICmpCinemaManager> cmpCinemaManager(*g_Game->GetSimulation2(), SYSTEM_ENTITY);
501 0 : if (!cmpCinemaManager)
502 : {
503 0 : msg->node = node;
504 0 : return;
505 : }
506 :
507 0 : CVector2D cursor;
508 0 : msg->pos->GetScreenSpace(cursor.X, cursor.Y);
509 :
510 0 : for (const std::pair<const CStrW, CCinemaPath>& p : cmpCinemaManager->GetPaths())
511 : {
512 0 : const CCinemaPath& path = p.second;
513 0 : if (isPathNodePicked(path, cursor, node, false) || isPathNodePicked(path.GetTargetSpline(), cursor, node, true))
514 : {
515 0 : node.name = path.GetName();
516 0 : msg->node = node;
517 0 : return;
518 : }
519 : }
520 0 : msg->node = node;
521 0 : g_AtlasGameLoop->view->SetParam(L"movetool", false);
522 0 : }
523 :
524 0 : static bool isAxisPicked(const CVector3D& base, const CVector3D& direction, float length, const CVector2D& cursor)
525 : {
526 0 : CVector3D position = GetNearestPointToScreenCoords(base, direction, cursor, 0, length);
527 0 : const CVector2D screenPos{g_Game->GetView()->GetCamera().GetScreenCoordinates(position)};
528 0 : return (cursor - screenPos).Length() < MINIMAL_SCREEN_DISTANCE;
529 : }
530 :
531 0 : QUERYHANDLER(PickAxis)
532 : {
533 0 : msg->axis = AXIS_INVALID;
534 :
535 0 : GET_PATH_NODE_WITH_VALIDATION();
536 :
537 0 : const TNSpline& spline = msg->node->targetNode ? path.GetTargetSpline() : path;
538 0 : CFixedVector3D pos = spline.GetAllNodes()[index].Position;
539 0 : CVector3D position(pos.X.ToFloat(), pos.Y.ToFloat(), pos.Z.ToFloat());
540 0 : CVector3D camera = g_Game->GetView()->GetCamera().GetOrientation().GetTranslation();
541 0 : float scale = (position - camera).Length() / 10.0;
542 :
543 0 : CVector2D cursor;
544 0 : msg->pos->GetScreenSpace(cursor.X, cursor.Y);
545 0 : if (isAxisPicked(position, CVector3D(1, 0, 0), scale, cursor))
546 0 : msg->axis = AXIS_X;
547 0 : else if (isAxisPicked(position, CVector3D(0, 1, 0), scale, cursor))
548 0 : msg->axis = AXIS_Y;
549 0 : else if (isAxisPicked(position, CVector3D(0, 0, 1), scale, cursor))
550 0 : msg->axis = AXIS_Z;
551 0 : }
552 :
553 0 : MESSAGEHANDLER(ClearPathNodePreview)
554 : {
555 0 : g_AtlasGameLoop->view->SetParam(L"movetool", false);
556 0 : }
557 :
558 : } // namespace AtlasMessage
|