Pioneer
SystemView.h
Go to the documentation of this file.
1 // Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _SYSTEMVIEW_H
5 #define _SYSTEMVIEW_H
6 
7 #include "Color.h"
8 #include "DeleteEmitter.h"
9 #include "Frame.h"
10 #include "Input.h"
11 #include "TransferPlanner.h"
12 #include "enum_table.h"
13 #include "graphics/Drawables.h"
14 #include "matrix4x4.h"
15 #include "pigui/PiGuiView.h"
16 #include "vector3.h"
17 #include "ConnectionTicket.h"
18 
19 class StarSystem;
20 class SystemBody;
21 class Orbit;
22 class Ship;
23 class Game;
24 class Body;
25 
29  OFF
30 };
31 
32 enum class GridDrawing {
33  GRID,
35  OFF
36 };
37 
41  LAG_OFF
42 };
43 
44 struct Projectable {
45  enum types { // <enum name=ProjectableTypes scope='Projectable' public>
46  NONE = 0, // empty projectable, don't try to get members
47  OBJECT = 1, // clickable space object, may be without phys.body (other starsystem)
48  L4 = 2,
49  L5 = 3,
50  APOAPSIS = 4,
51  PERIAPSIS = 5
52  } type;
53  enum bases { // <enum name=ProjectableBases scope='Projectable' public>
54  SYSTEMBODY = 0, // ref class SystemBody, may not have a physical body
55  BODY = 1, // generic body
56  SHIP = 2,
57  PLAYER = 3, // player's ship
58  PLANNER = 4 // player's ship planned by transfer planner, refers to player's object
59  } base;
60  union {
61  const Body *body;
62  const SystemBody *sbody;
63  } ref;
64  vector3d screenpos; // x,y - screen coordinate, z - in NDC
66  float screensize = 0.f; // approximate size in screen pixels
67 
68  Projectable(const types t, const bases b, const Body *obj) :
69  type(t), base(b)
70  {
71  ref.body = obj;
72  }
73  Projectable(const types t, const bases b, const SystemBody *obj) :
74  type(t), base(b)
75  {
76  ref.sbody = obj;
77  }
79  type(NONE) {}
80 };
81 
84  float radius;
85  bool isVertical;
86  bool isBinary;
89 
90  std::vector<AtlasBodyLayout> children;
91 };
92 
93 class SystemView : public PiGuiView, public DeleteEmitter {
94 public:
95  enum class Mode { // <enum name=SystemViewMode scope='SystemView::Mode' public>
96  Orrery = 0,
97  Atlas = 1
98  };
99 
100  SystemView(Game *game);
101  ~SystemView() override;
102  void Update() override;
103  void Draw3D() override;
104  void OnSwitchFrom() override;
105 
109  void ClearSelectedObject();
110  void ViewSelectedObject();
111  void ResetViewpoint();
112 
114 
115  TransferPlanner *GetTransferPlanner() const { return m_planner; }
116  double GetOrbitPlannerStartTime() const { return m_planner->GetStartTime(); }
117  double GetOrbitPlannerTime() const { return m_time; }
118  void AccelerateTime(float step);
119  void SetRealTime();
120  std::vector<Projectable> GetProjected() const { return m_projected; }
121  void SetVisibility(std::string param);
122  void SetZoomMode(bool enable);
123  void SetRotateMode(bool enable);
124  double ProjectedSize(double size, vector3d pos);
125  float AtlasViewPlanetGap(float planetRadius) { return std::max(planetRadius * 0.6, 1.33); }
126  float AtlasViewPixelPerUnit();
127 
128  float GetZoom() const;
129 
130  Mode GetDisplayMode() { return m_displayMode; }
131  void SetDisplayMode(Mode displayMode) { m_displayMode = displayMode; }
132 
133  // all used colors. defined in system-view-ui.lua
134  enum ColorIndex { // <enum name=SystemViewColorIndex scope='SystemView' public>
135  GRID = 0,
136  GRID_LEG = 1,
142  SHIP_ORBIT = 7
143  };
144 
146  void SetColor(ColorIndex color_index, Color *color_value) { svColor[color_index] = *color_value; }
147 
148 private:
149  struct InputBindings : public Input::InputFrame {
150  using InputFrame::InputFrame;
151  void RegisterBindings() override;
152 
153  Axis *mapViewPitch;
154  Axis *mapViewYaw;
155  Axis *mapViewZoom;
156  } m_input;
157 
158  void DrawOrreryView();
159  void DrawAtlasView();
160 
161  void LayoutSystemBody(SystemBody *body, AtlasBodyLayout &layout);
162  void RenderAtlasBody(const AtlasBodyLayout &layout, vector3f pos, const matrix4x4f &cameraTrans);
163 
164  template <typename RefType>
165  void PutOrbit(Projectable::bases base, RefType *ref, const Orbit *orb, const vector3d &offset, const Color &color, const double planetRadius = 0.0, const bool showLagrange = false);
166  void PutBody(const SystemBody *b, const vector3d &offset, const matrix4x4f &trans);
167  void GetTransformTo(const SystemBody *b, vector3d &pos);
168  void GetTransformTo(Projectable &p, vector3d &pos);
169  void MouseWheel(bool up);
170  void RefreshShips(void);
171  void DrawShips(const double t, const vector3d &offset);
172 
173  // draw a grid with `radius` * 2 gridlines on an evenly spaced 1-AU grid
174  void DrawGrid(uint32_t radius);
175 
176  // Project a position in the current renderer project to screenspace and add it to the list of projected objects
177  template <typename T>
178  void AddProjected(Projectable::types type, Projectable::bases base, T *ref, const vector3d &worldpos, float screensize = 0.f);
179  void CalculateShipPositionAtTime(const Ship *s, Orbit o, double t, vector3d &pos);
180  void CalculateFramePositionAtTime(FrameId frameId, double t, vector3d &pos);
181  double GetOrbitTime(double t, const SystemBody *b);
182  double GetOrbitTime(double t, const Body *b);
183 
184  Game *m_game;
185 
186  RefCountedPtr<StarSystem> m_system;
187  Projectable m_selectedObject;
188  Projectable m_viewedObject;
189  std::vector<Projectable> m_projected;
190  std::vector<SystemBody *> m_displayed_sbody;
191  bool m_unexplored;
192  bool m_viewingCurrentSystem;
193  std::list<std::pair<Ship *, Orbit>> m_contacts;
194 
195  AtlasBodyLayout m_atlasLayout = {};
196 
197  Mode m_displayMode;
198  ShowLagrange m_showL4L5;
199  TransferPlanner *m_planner;
200  ShipDrawing m_shipDrawing;
201  GridDrawing m_gridDrawing;
202 
203  bool m_rotateWithMouseButton = false;
204  bool m_rotateView = false;
205  bool m_zoomView = false;
206  const float CAMERA_FOV = 50.f;
207  const float CAMERA_FOV_RADIANS = CAMERA_FOV / 57.295779f;
208  matrix4x4f m_cameraSpace;
209 
210  float m_rot_x, m_rot_y;
211  float m_rot_x_to, m_rot_y_to;
212  float m_zoom, m_zoomTo;
213  float m_atlasZoom, m_atlasZoomTo, m_atlasZoomDefault;
214  vector2f m_atlasPos, m_atlasPosTo, m_atlasPosDefault;
215  float m_atlasViewW, m_atlasViewH;
216  int m_animateTransition;
217  vector3d m_trans;
218  vector3d m_transTo;
219  double m_time;
220  bool m_realtime;
221  double m_timeStep;
222 
223  ConnectionTicket m_onMouseWheelCon;
224 
225  std::unique_ptr<Graphics::Drawables::Disk> m_bodyIcon;
226  std::unique_ptr<Graphics::Material> m_bodyMat;
227  std::unique_ptr<Graphics::Material> m_atlasMat;
228  std::unique_ptr<Graphics::Material> m_lineMat;
229  std::unique_ptr<Graphics::Material> m_gridMat;
231  Graphics::Drawables::Lines m_selectBox;
232 
233  std::unique_ptr<vector3f[]> m_orbitVts;
234  std::unique_ptr<Color[]> m_orbitColors;
235 
236  std::unique_ptr<Graphics::VertexArray> m_lineVerts;
238 };
239 
240 #endif /* _SYSTEMVIEW_H */
ShowLagrange
Definition: SystemView.h:38
@ LAG_ICONTEXT
Definition: SystemView.h:40
@ LAG_ICON
Definition: SystemView.h:39
@ LAG_OFF
Definition: SystemView.h:41
ShipDrawing
Definition: SystemView.h:26
@ ORBITS
Definition: SystemView.h:28
@ BOXES
Definition: SystemView.h:27
@ OFF
Definition: SystemView.h:29
GridDrawing
Definition: SystemView.h:32
Definition: Body.h:57
Definition: DeleteEmitter.h:16
Definition: Game.h:38
Definition: Drawables.h:80
Definition: Orbit.h:11
Definition: PiGuiView.h:11
Definition: Ship.h:64
Definition: StarSystem.h:27
Definition: SystemBody.h:19
Definition: SystemView.h:93
void ViewSelectedObject()
Definition: SystemView.cpp:964
Projectable * GetSelectedObject()
Definition: SystemView.cpp:940
void SetDisplayMode(Mode displayMode)
Definition: SystemView.h:131
void Draw3D() override
Definition: SystemView.cpp:368
void ClearSelectedObject()
Definition: SystemView.cpp:959
RefCountedPtr< StarSystem > GetCurrentSystem()
Definition: SystemView.cpp:153
Color svColor[8]
Definition: SystemView.h:145
~SystemView() override
Definition: SystemView.cpp:105
Mode GetDisplayMode()
Definition: SystemView.h:130
void SetSelectedObject(Projectable::types type, Projectable::bases base, SystemBody *sb)
Definition: SystemView.cpp:945
double ProjectedSize(double size, vector3d pos)
Definition: SystemView.cpp:974
void SetRotateMode(bool enable)
Definition: SystemView.cpp:931
TransferPlanner * GetTransferPlanner() const
Definition: SystemView.h:115
void OnSwitchFrom() override
Definition: SystemView.cpp:989
float AtlasViewPixelPerUnit()
Definition: SystemView.cpp:990
double GetOrbitPlannerTime() const
Definition: SystemView.h:117
double GetOrbitPlannerStartTime() const
Definition: SystemView.h:116
void Update() override
Definition: SystemView.cpp:705
void AccelerateTime(float step)
Definition: SystemView.cpp:110
void ResetViewpoint()
Definition: SystemView.cpp:121
Mode
Definition: SystemView.h:95
float GetZoom() const
Definition: SystemView.cpp:982
void SetRealTime()
Definition: SystemView.cpp:116
std::vector< Projectable > GetProjected() const
Definition: SystemView.h:120
void SetVisibility(std::string param)
Definition: SystemView.cpp:891
SystemView(Game *game)
Definition: SystemView.cpp:60
void SetZoomMode(bool enable)
Definition: SystemView.cpp:922
void SetColor(ColorIndex color_index, Color *color_value)
Definition: SystemView.h:146
float AtlasViewPlanetGap(float planetRadius)
Definition: SystemView.h:125
ColorIndex
Definition: SystemView.h:134
@ GRID_LEG
Definition: SystemView.h:136
@ GRID
Definition: SystemView.h:135
@ SELECTED_SHIP_ORBIT
Definition: SystemView.h:141
@ SYSTEMBODY_ORBIT
Definition: SystemView.h:138
@ PLAYER_ORBIT
Definition: SystemView.h:139
@ PLANNER_ORBIT
Definition: SystemView.h:140
@ SYSTEMBODY
Definition: SystemView.h:137
@ SHIP_ORBIT
Definition: SystemView.h:142
Definition: TransferPlanner.h:8
double GetStartTime() const
Definition: TransferPlanner.cpp:71
Definition: InputBindings.h:14
Definition: SystemView.h:82
float radius
Definition: SystemView.h:84
SystemBody * body
Definition: SystemView.h:83
vector2f size
Definition: SystemView.h:88
bool isVertical
Definition: SystemView.h:85
vector2f offset
Definition: SystemView.h:87
bool isBinary
Definition: SystemView.h:86
std::vector< AtlasBodyLayout > children
Definition: SystemView.h:90
Definition: Color.h:66
Definition: ConnectionTicket.h:12
Definition: FrameId.h:9
Definition: Input.h:46
Definition: SystemView.h:44
enum Projectable::bases base
Projectable(const types t, const bases b, const Body *obj)
Definition: SystemView.h:68
vector3d screenpos
Definition: SystemView.h:64
const SystemBody * sbody
Definition: SystemView.h:62
const Body * body
Definition: SystemView.h:61
bases
Definition: SystemView.h:53
@ BODY
Definition: SystemView.h:55
@ SHIP
Definition: SystemView.h:56
@ PLAYER
Definition: SystemView.h:57
@ PLANNER
Definition: SystemView.h:58
@ SYSTEMBODY
Definition: SystemView.h:54
Projectable(const types t, const bases b, const SystemBody *obj)
Definition: SystemView.h:73
union Projectable::@10 ref
float screensize
Definition: SystemView.h:66
Projectable()
Definition: SystemView.h:78
types
Definition: SystemView.h:45
@ L5
Definition: SystemView.h:49
@ L4
Definition: SystemView.h:48
@ PERIAPSIS
Definition: SystemView.h:51
@ OBJECT
Definition: SystemView.h:47
@ APOAPSIS
Definition: SystemView.h:50
@ NONE
Definition: SystemView.h:46
vector3d worldpos
Definition: SystemView.h:65
enum Projectable::types type