Pioneer
Loading...
Searching...
No Matches
SystemView.h
Go to the documentation of this file.
1// Copyright © 2008-2024 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#pragma once
5
6#ifndef _SYSTEMVIEW_H
7#define _SYSTEMVIEW_H
8
9#include "Color.h"
10#include "DeleteEmitter.h"
11#include "Frame.h"
12#include "Input.h"
13#include "Orbit.h"
14#include "TransferPlanner.h"
15#include "enum_table.h"
16#include "graphics/Drawables.h"
17#include "graphics/Graphics.h"
18#include "matrix4x4.h"
19#include "pigui/PiGuiView.h"
20#include "vector3.h"
21
22#include <sigc++/signal.h>
23
24class GuiApplication;
25class StarSystem;
26class SystemBody;
27class Orbit;
28class Ship;
29class Game;
30class Body;
31
32namespace Background {
33 class Container;
34}
35
39 OFF
40};
41
42enum class GridDrawing {
43 GRID,
45 OFF
46};
47
52};
53
58};
59
79 enum types { // <enum name=ProjectableTypes scope='Projectable' public>
80 NONE = 0, // empty projectable, don't try to get members
81 OBJECT = 1, // clickable space object, may be without phys.body (other starsystem)
82 L4 = 2,
83 L5 = 3,
86 ORBIT = 6, // <enum skip>
87 _MAX = 7 // <enum skip>
89 enum bases { // <enum name=ProjectableBases scope='Projectable' public>
90 SYSTEMBODY = 0, // ref class SystemBody, may not have a physical body
91 BODY = 1, // generic body
92 SHIP = 2,
93 PLAYER = 3, // player's ship
94 PLANNER = 4 // player's ship planned by transfer planner, refers to player's object
96 union {
97 const Body *body;
99 } ref;
100 vector3f screenpos; // x,y - screen coordinate, z - in NDC
101 float screensize = 0.f; // approximate size in screen pixels
103 int orbitIdx = -1;
104
105 Projectable(const types t, const bases b, const Body *obj, const vector3d &pos = vector3d()) :
106 type(t), base(b), worldpos(pos)
107 {
108 ref.body = obj;
109 }
110 Projectable(const types t, const bases b, const SystemBody *obj, const vector3d &pos = vector3d()) :
111 type(t), base(b), worldpos(pos)
112 {
113 ref.sbody = obj;
114 }
116 type(NONE), worldpos() {}
117
118 void *getRef() const { return base == SYSTEMBODY ? (void *)ref.sbody : (void *)ref.body; }
119
120 bool operator==(const Projectable &rhs) const
121 {
122 return (type == rhs.type && base == rhs.base && getRef() == rhs.getRef());
123 }
124
125 struct GroupInfo {
127 type(type),
129 specials(0),
130 tracks({ trackIdx })
131 {
132 }
133
136 uint32_t specials;
137 std::vector<int> tracks;
138
139 void setSpecial(int index) { specials |= (1 << (index & 0x1F)); }
140 bool hasSpecial(int index) const { return specials & (1 << (index & 0x1F)); }
141 };
142};
143
146 float radius;
151
152 std::vector<AtlasBodyLayout> children;
153};
154
156
164class SystemView : public PiGuiView, public DeleteEmitter {
165public:
166 enum class Mode { // <enum name=SystemViewMode scope='SystemView::Mode' public>
167 Orrery = 0,
168 Atlas = 1
169 };
170
171 SystemView(Game *game);
172 ~SystemView() override;
173 void Update() override;
174 void Draw3D() override;
175 void OnSwitchFrom() override;
176
177 Mode GetDisplayMode() { return m_displayMode; }
178 void SetDisplayMode(Mode displayMode) { m_displayMode = displayMode; }
179
180 TransferPlanner *GetTransferPlanner() const { return m_planner; }
181 double GetOrbitPlannerStartTime() const { return m_planner->GetStartTime(); }
182
183 SystemMapViewport *GetMap() { return m_map.get(); }
184
185private:
186 void RefreshShips(void);
187 void AddShipTracks(double atTime);
188
189 void CalculateShipPositionAtTime(const Ship *s, Orbit o, double t, vector3d &pos);
190 void CalculateFramePositionAtTime(FrameId frameId, double t, vector3d &pos);
191
192 double CalculateStarportHeight(const SystemBody *body);
193
194private:
195 Game *m_game;
196
197 std::unique_ptr<SystemMapViewport> m_map;
198 TransferPlanner *m_planner;
199 std::list<std::pair<Ship *, Orbit>> m_contacts;
200
201 Mode m_displayMode;
202 bool m_viewingCurrentSystem;
203 bool m_unexplored;
204};
205
231public:
234
235 void Update(float deltaTime);
236 void HandleInput(float deltaTime);
237 void Draw3D();
238
239 Projectable *GetSelectedObject() { return &m_selectedObject; }
240 void SetSelectedObject(Projectable p) { m_selectedObject = p; }
241 void ClearSelectedObject();
242
243 void ViewSelectedObject() { SetViewedObject(m_selectedObject); }
245 void ResetViewpoint();
246
247 Projectable *GetViewedObject() { return &m_viewedObject; }
248
249 ShowLagrange GetShowLagrange() { return m_showL4L5; }
250 ShipDrawing GetShipDrawing() { return m_shipDrawing; }
251 GridDrawing GetGridDrawing() { return m_gridDrawing; }
252
253 // Generate a sorted list of non-overlapping projectable icon groups
254 // Up to 32 objects can be treated as "special" and will be noted in the
255 // GroupInfo bitvector of any group they appear in
256 std::vector<Projectable::GroupInfo> GroupProjectables(vector2f groupThreshold, const std::vector<Projectable> &specialObjects);
257
258 // Push a tracked object / sensor contact for display on the map.
259 // Object tracks are cleared every frame.
261
262 // Push an orbital conic for display on the map
263 // Expects the orbit's center to be provide as p.worldpos
264 // Orbit tracks are cleared every frame.
265 void AddOrbitTrack(Projectable p, const Orbit *orbit, Color color, double planetRadius);
266
269
270 void AccelerateTime(float step);
271 void SetRealTime();
272 void SetReferenceTime(double time) { m_refTime = time; }
273 double GetTime() { return m_time; }
274 const std::vector<Projectable> &GetProjected() const { return m_projected; }
275 void SetVisibility(std::string param);
276 void SetZoomMode(bool enable);
277 void SetRotateMode(bool enable);
278 void SetDisplayMode(SystemView::Mode displayMode) { m_displayMode = displayMode; }
279 void SetBackground(Background::Container *bg) { m_background = bg; }
280 void SetShowGravpoints(bool enabled) { m_showGravpoints = enabled; }
281
282 double ProjectedSize(double size, vector3d pos);
283 float AtlasViewPlanetGap(float planetRadius) { return std::max(planetRadius * 0.6, 1.33); }
284 float AtlasViewPixelPerUnit();
285
286 float GetZoom() const;
287
288 // all used colors. defined in system-view-ui.lua
289 enum ColorIndex { // <enum name=SystemViewColorIndex scope='SystemMapViewport' public>
290 GRID = 0,
297 SHIP_ORBIT = 7
298 };
299
301 void SetColor(ColorIndex color_index, Color *color_value) { svColor[color_index] = *color_value; }
302
303 sigc::slot<double(const SystemBody *)> GetStarportHeightAboveTerrain;
304
305private:
306 struct InputBindings : public Input::InputFrame {
307 using InputFrame::InputFrame;
308 void RegisterBindings() override;
309
310 Axis *mapViewPitch;
311 Axis *mapViewYaw;
312 Axis *mapViewZoom;
313 } m_input;
314
315 void DrawOrreryView();
316 void DrawAtlasView();
317
318 // Push the system body tree as object tracks
319 void AddBodyTrack(const SystemBody *b, const vector3d &offset = vector3d());
320
321 void LayoutSystemBody(SystemBody *body, AtlasBodyLayout &layout);
322 void RenderAtlasBody(const AtlasBodyLayout &layout, vector3f pos, const matrix4x4f &cameraTrans);
323
324 // Project a track to screenspace with the current renderer state and add it to the list of projected objects
325 void AddProjected(Projectable p, Projectable::types type, const vector3d &transformedPos, float screensize = 0.f);
326 void RenderBody(const SystemBody *b, const vector3d &pos, const matrix4x4f &trans);
327 void RenderOrbit(Projectable p, const ProjectedOrbit *orbitData, const vector3d &transformedPos);
328
329 // draw a grid with `radius` * 2 gridlines on an evenly spaced 1-AU grid
330 void DrawGrid(uint32_t radius);
331
332private:
333 GuiApplication *m_app;
334 Graphics::Renderer *m_renderer;
335 Background::Container *m_background;
336
338 Projectable m_selectedObject;
339 Projectable m_viewedObject;
340
341 std::vector<Projectable> m_objectTracks;
342 std::vector<ProjectedOrbit> m_orbitTracks;
343
344 std::vector<Projectable> m_projected;
345
346 SystemView::Mode m_displayMode; // FIXME: separate Atlas from SystemMapViewport
347 Graphics::ViewportExtents m_viewportSize;
348
349 AtlasBodyLayout m_atlasLayout = {};
350 float m_atlasZoom, m_atlasZoomTo, m_atlasZoomDefault;
351 vector2f m_atlasPos, m_atlasPosTo, m_atlasPosDefault;
352 float m_atlasViewW, m_atlasViewH;
353
354 bool m_showGravpoints;
355 ShowLagrange m_showL4L5;
356 ShipDrawing m_shipDrawing;
357 GridDrawing m_gridDrawing;
358
359 bool m_rotateWithMouseButton = false;
360 bool m_rotateView = false;
361 bool m_zoomView = false;
362 const float CAMERA_FOV = 50.f;
363 const float CAMERA_FOV_RADIANS = CAMERA_FOV / 57.295779f;
364 matrix4x4f m_cameraSpace;
365
366 float m_rot_x, m_rot_y;
367 float m_rot_x_to, m_rot_y_to;
368 float m_zoom, m_zoomTo;
369 int m_animateTransition;
370 vector3d m_trans;
371 vector3d m_transTo;
372 double m_time;
373 double m_refTime;
374 bool m_realtime;
375 double m_timeStep;
376
377 std::unique_ptr<Graphics::Drawables::Disk> m_bodyIcon;
378 std::unique_ptr<Graphics::Material> m_bodyMat;
379 std::unique_ptr<Graphics::Material> m_atlasMat;
380 std::unique_ptr<Graphics::Material> m_lineMat;
381 std::unique_ptr<Graphics::Material> m_gridMat;
383
384 std::unique_ptr<vector3f[]> m_orbitVts;
385 std::unique_ptr<Color[]> m_orbitColors;
386
387 std::unique_ptr<Graphics::VertexArray> m_lineVerts;
389};
390
391#endif /* _SYSTEMVIEW_H */
ShowLagrange
Definition: SystemView.h:48
@ LAG_ICONTEXT
Definition: SystemView.h:50
@ LAG_ICON
Definition: SystemView.h:49
@ LAG_OFF
Definition: SystemView.h:51
ShipDrawing
Definition: SystemView.h:36
@ ORBITS
Definition: SystemView.h:38
@ BOXES
Definition: SystemView.h:37
@ OFF
Definition: SystemView.h:39
GridDrawing
Definition: SystemView.h:42
Definition: Background.h:90
Definition: Body.h:65
Definition: DeleteEmitter.h:16
Definition: Game.h:38
Definition: Drawables.h:83
Definition: Renderer.h:45
Definition: GuiApplication.h:27
Definition: Orbit.h:13
Definition: PiGuiView.h:11
Definition: RefCounted.h:36
Definition: Ship.h:64
Definition: StarSystem.h:28
Definition: SystemBody.h:137
Definition: SystemView.h:230
ShowLagrange GetShowLagrange()
Definition: SystemView.h:249
ShipDrawing GetShipDrawing()
Definition: SystemView.h:250
RefCountedPtr< StarSystem > GetCurrentSystem()
Definition: SystemView.cpp:370
void SetColor(ColorIndex color_index, Color *color_value)
Definition: SystemView.h:301
void SetShowGravpoints(bool enabled)
Definition: SystemView.h:280
std::vector< Projectable::GroupInfo > GroupProjectables(vector2f groupThreshold, const std::vector< Projectable > &specialObjects)
Definition: SystemView.cpp:965
void ClearSelectedObject()
Definition: SystemView.cpp:1125
sigc::slot< double(const SystemBody *)> GetStarportHeightAboveTerrain
Definition: SystemView.h:303
~SystemMapViewport()
Definition: SystemView.cpp:321
void SetCurrentSystem(RefCountedPtr< StarSystem > system)
Definition: SystemView.cpp:375
void AddOrbitTrack(Projectable p, const Orbit *orbit, Color color, double planetRadius)
Definition: SystemView.cpp:1055
void HandleInput(float deltaTime)
Definition: SystemView.cpp:820
void SetSelectedObject(Projectable p)
Definition: SystemView.h:240
void SetRotateMode(bool enable)
Definition: SystemView.cpp:1116
void ViewSelectedObject()
Definition: SystemView.h:243
float GetZoom() const
Definition: SystemView.cpp:1147
void SetVisibility(std::string param)
Definition: SystemView.cpp:1076
void AccelerateTime(float step)
Definition: SystemView.cpp:325
void SetDisplayMode(SystemView::Mode displayMode)
Definition: SystemView.h:278
Projectable * GetViewedObject()
Definition: SystemView.h:247
double ProjectedSize(double size, vector3d pos)
Definition: SystemView.cpp:1139
void Draw3D()
Definition: SystemView.cpp:532
ColorIndex
Definition: SystemView.h:289
@ SYSTEMBODY_ORBIT
Definition: SystemView.h:293
@ PLANNER_ORBIT
Definition: SystemView.h:295
@ SELECTED_SHIP_ORBIT
Definition: SystemView.h:296
@ GRID
Definition: SystemView.h:290
@ GRID_LEG
Definition: SystemView.h:291
@ SHIP_ORBIT
Definition: SystemView.h:297
@ SYSTEMBODY
Definition: SystemView.h:292
@ PLAYER_ORBIT
Definition: SystemView.h:294
void ResetViewpoint()
Definition: SystemView.cpp:336
void AddObjectTrack(Projectable p)
Definition: SystemView.cpp:1040
GridDrawing GetGridDrawing()
Definition: SystemView.h:251
Projectable * GetSelectedObject()
Definition: SystemView.h:239
const std::vector< Projectable > & GetProjected() const
Definition: SystemView.h:274
void Update(float deltaTime)
Definition: SystemView.cpp:880
float AtlasViewPixelPerUnit()
Definition: SystemView.cpp:1152
void SetViewedObject(Projectable p)
Definition: SystemView.cpp:1130
void SetRealTime()
Definition: SystemView.cpp:331
float AtlasViewPlanetGap(float planetRadius)
Definition: SystemView.h:283
void SetZoomMode(bool enable)
Definition: SystemView.cpp:1107
double GetTime()
Definition: SystemView.h:273
void SetBackground(Background::Container *bg)
Definition: SystemView.h:279
void SetReferenceTime(double time)
Definition: SystemView.h:272
Color svColor[8]
Definition: SystemView.h:300
Definition: SystemView.h:164
void SetDisplayMode(Mode displayMode)
Definition: SystemView.h:178
void Draw3D() override
Definition: SystemView.cpp:247
~SystemView() override
Definition: SystemView.cpp:79
Mode GetDisplayMode()
Definition: SystemView.h:177
void OnSwitchFrom() override
Definition: SystemView.cpp:266
double GetOrbitPlannerStartTime() const
Definition: SystemView.h:181
void Update() override
Definition: SystemView.cpp:165
SystemMapViewport * GetMap()
Definition: SystemView.h:183
Mode
Definition: SystemView.h:166
TransferPlanner * GetTransferPlanner() const
Definition: SystemView.h:180
Definition: TransferPlanner.h:8
double GetStartTime() const
Definition: TransferPlanner.cpp:71
Definition: Background.cpp:62
Definition: InputBindings.h:16
Definition: SystemView.h:144
float radius
Definition: SystemView.h:146
SystemBody * body
Definition: SystemView.h:145
vector2f size
Definition: SystemView.h:150
bool isVertical
Definition: SystemView.h:147
vector2f offset
Definition: SystemView.h:149
bool isBinary
Definition: SystemView.h:148
std::vector< AtlasBodyLayout > children
Definition: SystemView.h:152
Definition: Color.h:66
Definition: FrameId.h:9
Definition: Graphics.h:59
Definition: Input.h:48
InputBindings::Axis Axis
Definition: Input.h:49
Definition: SystemView.h:125
types type
Definition: SystemView.h:134
bool hasSpecial(int index) const
Definition: SystemView.h:140
uint32_t specials
Definition: SystemView.h:136
GroupInfo(int trackIdx, vector3f screenpos, types type=OBJECT)
Definition: SystemView.h:126
void setSpecial(int index)
Definition: SystemView.h:139
vector3f screenpos
Definition: SystemView.h:135
std::vector< int > tracks
Definition: SystemView.h:137
Definition: SystemView.h:78
enum Projectable::bases base
Projectable(const types t, const bases b, const SystemBody *obj, const vector3d &pos=vector3d())
Definition: SystemView.h:110
const SystemBody * sbody
Definition: SystemView.h:98
const Body * body
Definition: SystemView.h:97
bases
Definition: SystemView.h:89
@ BODY
Definition: SystemView.h:91
@ SHIP
Definition: SystemView.h:92
@ PLAYER
Definition: SystemView.h:93
@ PLANNER
Definition: SystemView.h:94
@ SYSTEMBODY
Definition: SystemView.h:90
bool operator==(const Projectable &rhs) const
Definition: SystemView.h:120
union Projectable::@10 ref
int orbitIdx
Definition: SystemView.h:103
float screensize
Definition: SystemView.h:101
vector3f screenpos
Definition: SystemView.h:100
void * getRef() const
Definition: SystemView.h:118
Projectable()
Definition: SystemView.h:115
types
Definition: SystemView.h:79
@ L5
Definition: SystemView.h:83
@ L4
Definition: SystemView.h:82
@ ORBIT
Definition: SystemView.h:86
@ PERIAPSIS
Definition: SystemView.h:85
@ OBJECT
Definition: SystemView.h:81
@ APOAPSIS
Definition: SystemView.h:84
@ _MAX
Definition: SystemView.h:87
@ NONE
Definition: SystemView.h:80
vector3d worldpos
Definition: SystemView.h:102
Projectable(const types t, const bases b, const Body *obj, const vector3d &pos=vector3d())
Definition: SystemView.h:105
enum Projectable::types type
Definition: SystemView.h:54
double planetRadius
Definition: SystemView.h:57
Orbit orbit
Definition: SystemView.h:55
Color color
Definition: SystemView.h:56
vector3< double > vector3d
Definition: vector3.h:329