Pioneer
SectorView.h
Go to the documentation of this file.
1 // Copyright © 2008-2022 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _SECTORVIEW_H
5 #define _SECTORVIEW_H
6 
7 #include "DeleteEmitter.h"
8 #include "Input.h"
9 #include "galaxy/Sector.h"
10 #include "galaxy/SystemPath.h"
11 #include "graphics/Drawables.h"
12 #include "imgui/imgui.h"
13 #include "imgui/imgui_internal.h"
14 #include "pigui/PiGuiView.h"
15 #include <set>
16 #include <string>
17 #include <vector>
18 
19 class Game;
20 class Galaxy;
21 
22 class SectorView : public PiGuiView, public DeleteEmitter {
23 public:
24  SectorView(Game *game);
25  SectorView(const Json &jsonObj, Game *game);
26  ~SectorView() override;
27 
28  void Update() override;
29  // void ShowAll() override;
30  void Draw3D() override;
31 
32  void DrawPiGui() override;
33 
34  vector3f GetPosition() const { return m_pos; }
35  SystemPath GetCurrent() const { return m_current; }
36  SystemPath GetSelected() const { return m_selected; }
37  void SwitchToPath(const SystemPath &path);
38  SystemPath GetHyperspaceTarget() const { return m_hyperspaceTarget; }
39  void SetHyperspaceTarget(const SystemPath &path);
40  void ResetHyperspaceTarget();
41  void GotoSector(const SystemPath &path);
42  void GotoSystem(const SystemPath &path);
43  void GotoCurrentSystem() { GotoSystem(m_current); }
44  void GotoSelectedSystem() { GotoSystem(m_selected); }
45  void GotoHyperspaceTarget() { GotoSystem(m_hyperspaceTarget); }
46  bool IsCenteredOn(const SystemPath &path);
47  void SaveToJson(Json &jsonObj) override;
48 
49  sigc::signal<void> onHyperspaceTargetChanged;
50 
51  double GetZoomLevel() const;
52  void ZoomIn();
53  void ZoomOut();
55  double GetCenterDistance();
56  void SetDrawUninhabitedLabels(bool value) { m_drawUninhabitedLabels = value; }
57  void SetDrawVerticalLines(bool value) { m_drawVerticalLines = value; }
58  void SetDrawOutRangeLabels(bool value) { m_drawOutRangeLabels = value; }
59  void SetAutomaticSystemSelection(bool value) { m_automaticSystemSelection = value; }
60  std::vector<SystemPath> GetNearbyStarSystemsByName(std::string pattern);
61  const std::set<const Faction *> &GetVisibleFactions() { return m_visibleFactions; }
62  const std::set<const Faction *> &GetHiddenFactions() { return m_hiddenFactions; }
63  void SetFactionVisible(const Faction *faction, bool visible);
64  void SetZoomMode(bool enable);
65  void SetRotateMode(bool enable);
66  void ResetView();
67  void SetLabelParams(std::string fontName, int fontSize, float gap, Color highlight, Color shade);
68  void DrawLabels();
69  void SetLabelsVisibility(bool hideLabels) { m_hideLabels = hideLabels; }
70 
71  // HyperJump Route Planner
72  bool MoveRouteItemUp(const std::vector<SystemPath>::size_type element);
73  bool MoveRouteItemDown(const std::vector<SystemPath>::size_type element);
74  void UpdateRouteItem(const std::vector<SystemPath>::size_type element, const SystemPath &path);
75  void AddToRoute(const SystemPath &path);
76  bool RemoveRouteItem(const std::vector<SystemPath>::size_type element);
77  void ClearRoute();
78  std::vector<SystemPath> GetRoute();
79  const std::string AutoRoute(const SystemPath &start, const SystemPath &target, std::vector<SystemPath> &outRoute) const;
80  void SetDrawRouteLines(bool value) { m_drawRouteLines = value; }
81 
82 protected:
83  void OnSwitchTo() override;
84  void OnSwitchFrom() override;
85 
86  struct InputBinding : public Input::InputFrame {
87  using InputFrame::InputFrame;
88 
93 
100 
101  void RegisterBindings() override;
103 
104 private:
105  void InitDefaults();
106  void InitObject();
107 
108  void DrawNearSectors(const matrix4x4f &modelview);
109  void DrawNearSector(const int sx, const int sy, const int sz, const matrix4x4f &trans);
110  void PutSystemLabels(RefCountedPtr<Sector> sec, const vector3f &origin, int drawRadius);
111  void PutSystemLabel(const Sector::System &sys);
112 
113  void DrawFarSectors(const matrix4x4f &modelview);
114  void BuildFarSector(RefCountedPtr<Sector> sec, const vector3f &origin, std::vector<vector3f> &points, std::vector<Color> &colors);
115  void PutFactionLabels(const vector3f &secPos);
116  void AddStarBillboard(const matrix4x4f &modelview, const vector3f &pos, const Color &col, float size);
117 
118  void OnClickSystem(const SystemPath &path);
119  const SystemPath &CheckPathInRoute(const SystemPath &path);
120 
121  RefCountedPtr<Sector> GetCached(const SystemPath &loc) { return m_sectorCache->GetCached(loc); }
122  void ShrinkCache();
123  void SetSelected(const SystemPath &path);
124 
125  void MouseWheel(bool up);
126 
127  RefCountedPtr<Galaxy> m_galaxy;
128 
129  bool m_inSystem;
130 
131  SystemPath m_current;
132  SystemPath m_selected;
133 
134  vector3f m_pos;
135  vector3f m_posMovingTo;
136 
137  float m_rotXDefault, m_rotZDefault, m_zoomDefault;
138 
139  float m_rotX, m_rotZ;
140  float m_rotXMovingTo, m_rotZMovingTo;
141 
142  float m_zoom;
143  float m_zoomClamped;
144  float m_zoomMovingTo;
145 
146  bool m_rotateWithMouseButton = false;
147  bool m_rotateView = false;
148  bool m_zoomView = false;
149  bool m_manualMove = false;
150 
151  SystemPath m_hyperspaceTarget;
152  bool m_automaticSystemSelection;
153 
154  bool m_drawUninhabitedLabels;
155  bool m_drawOutRangeLabels;
156  bool m_drawVerticalLines;
157 
158  //Gui::LabelSet *m_clickableLabels;
159 
160  std::set<const Faction *> m_visibleFactions;
161  std::set<const Faction *> m_hiddenFactions;
162 
163  Uint8 m_detailBoxVisible;
164 
165  sigc::connection m_onMouseWheelCon;
166  sigc::connection m_onToggleSelectionFollowView;
167  sigc::connection m_onWarpToCurrent;
168  sigc::connection m_onWarpToSelected;
169  sigc::connection m_onViewReset;
170 
171  RefCountedPtr<SectorCache::Slave> m_sectorCache;
172  std::string m_previousSearch;
173 
174  float m_playerHyperspaceRange;
175 
176  // HyperJump Route Planner Stuff
177  std::vector<SystemPath> m_route;
178 
179  bool m_drawRouteLines;
180  bool m_setupRouteLines;
181  void DrawRouteLines(const matrix4x4f &trans);
182  void SetupRouteLines(const vector3f &playerAbsPos);
183  void GetPlayerPosAndStarSize(vector3f &playerPosOut, float &currentStarSizeOut);
184 
185  std::vector<vector3f> m_farstars;
186  std::vector<Color> m_farstarsColor;
187 
188  vector3f m_secPosFar;
189  int m_radiusFar;
190  bool m_toggledFaction;
191 
192  int m_cacheXMin;
193  int m_cacheXMax;
194  int m_cacheYMin;
195  int m_cacheYMax;
196  int m_cacheZMin;
197  int m_cacheZMax;
198 
199  std::unique_ptr<ImDrawList> m_drawList;
200 
201  std::unique_ptr<Graphics::VertexArray> m_lineVerts;
202  std::unique_ptr<Graphics::VertexArray> m_secLineVerts;
203  std::unique_ptr<Graphics::VertexArray> m_starVerts;
204 
205  RefCountedPtr<Graphics::Material> m_starMaterial;
208  RefCountedPtr<Graphics::Material> m_farStarsMat;
209 
210  std::unique_ptr<Graphics::Drawables::Sphere3D> m_jumpSphere;
211 
213  Graphics::Drawables::Lines m_sectorlines;
214  Graphics::Drawables::Lines m_routeLines;
215  Graphics::Drawables::Points m_farstarsPoints;
216 
217  class Label;
218  class StarLabel;
219  class FactionLabel;
220  struct Labels {
221  // the constructor can only be defined after defining the Label class
222  Labels(SectorView &view);
223  // settings and globals for labels
224  // this is not hardcode, these are the defaults
225  std::string fontName = "orbiteer";
226  int fontSize = 15;
227  float gap = 2.f;
228  ImFont *starLabelFont = nullptr;
229  ImFont *factionHomeFont = nullptr;
230  ImFont *factionNameFont = nullptr;
231  ImRect starLabelHoverArea;
232  ImRect factionHomeHoverArea;
233  ImRect factionNameHoverArea;
234  ImU32 highlightColor = IM_COL32(255, 255, 255, 100);
235  ImU32 shadeColor = IM_COL32(25, 51, 82, 200);
236  // owning object
237  SectorView &view;
238  // array
239  std::vector<std::unique_ptr<Label>> array;
240  };
241  Labels m_labels;
242  bool m_hideLabels = false;
243 };
244 
245 #endif /* _SECTORVIEW_H */
nlohmann::json Json
Definition: Json.h:8
Definition: DeleteEmitter.h:16
Definition: Factions.h:21
Definition: Galaxy.h:18
Definition: Game.h:38
Definition: Drawables.h:80
Definition: Drawables.h:110
Definition: PiGuiView.h:11
Definition: RefCounted.h:36
Definition: SectorView.h:22
void SetHyperspaceTarget(const SystemPath &path)
Definition: SectorView.cpp:823
~SectorView() override
Definition: SectorView.cpp:625
SectorView::InputBinding InputBindings
void GotoSystem(const SystemPath &path)
Definition: SectorView.cpp:844
sigc::signal< void > onHyperspaceTargetChanged
Definition: SectorView.h:49
double GetZoomLevel() const
Definition: SectorView.cpp:1684
void SetDrawRouteLines(bool value)
Definition: SectorView.h:80
void SetLabelsVisibility(bool hideLabels)
Definition: SectorView.h:69
const std::string AutoRoute(const SystemPath &start, const SystemPath &target, std::vector< SystemPath > &outRoute) const
Definition: SectorView.cpp:1046
void ZoomIn()
Definition: SectorView.cpp:1689
void SwitchToPath(const SystemPath &path)
Definition: SectorView.cpp:874
vector3f GetCenterSector()
Definition: SectorView.cpp:1707
void DrawPiGui() override
Definition: SectorView.cpp:744
std::vector< SystemPath > GetRoute()
Definition: SectorView.cpp:1041
double GetCenterDistance()
Definition: SectorView.cpp:1712
SectorView(Game *game)
Definition: SectorView.cpp:474
bool MoveRouteItemDown(const std::vector< SystemPath >::size_type element)
Definition: SectorView.cpp:1002
void SetLabelParams(std::string fontName, int fontSize, float gap, Color highlight, Color shade)
Definition: SectorView.cpp:750
void OnSwitchFrom() override
Definition: SectorView.cpp:1510
const std::set< const Faction * > & GetVisibleFactions()
Definition: SectorView.h:61
void ResetHyperspaceTarget()
Definition: SectorView.cpp:829
void UpdateRouteItem(const std::vector< SystemPath >::size_type element, const SystemPath &path)
Definition: SectorView.cpp:1012
void ResetView()
Definition: SectorView.cpp:1779
void Update() override
Definition: SectorView.cpp:1515
void SaveToJson(Json &jsonObj) override
Definition: SectorView.cpp:634
void SetDrawVerticalLines(bool value)
Definition: SectorView.h:57
bool MoveRouteItemUp(const std::vector< SystemPath >::size_type element)
Definition: SectorView.cpp:992
SystemPath GetCurrent() const
Definition: SectorView.h:35
void AddToRoute(const SystemPath &path)
Definition: SectorView.cpp:1018
void SetAutomaticSystemSelection(bool value)
Definition: SectorView.h:59
vector3f GetPosition() const
Definition: SectorView.h:34
void GotoHyperspaceTarget()
Definition: SectorView.h:45
void OnSwitchTo() override
Definition: SectorView.cpp:1504
void GotoSelectedSystem()
Definition: SectorView.h:44
void GotoSector(const SystemPath &path)
Definition: SectorView.cpp:839
void GotoCurrentSystem()
Definition: SectorView.h:43
void SetDrawUninhabitedLabels(bool value)
Definition: SectorView.h:56
bool IsCenteredOn(const SystemPath &path)
Definition: SectorView.cpp:853
SystemPath GetSelected() const
Definition: SectorView.h:36
void ZoomOut()
Definition: SectorView.cpp:1698
std::vector< SystemPath > GetNearbyStarSystemsByName(std::string pattern)
Definition: SectorView.cpp:1722
void Draw3D() override
Definition: SectorView.cpp:664
const std::set< const Faction * > & GetHiddenFactions()
Definition: SectorView.h:62
void SetDrawOutRangeLabels(bool value)
Definition: SectorView.h:58
void SetFactionVisible(const Faction *faction, bool visible)
Definition: SectorView.cpp:1752
SystemPath GetHyperspaceTarget() const
Definition: SectorView.h:38
bool RemoveRouteItem(const std::vector< SystemPath >::size_type element)
Definition: SectorView.cpp:1024
void DrawLabels()
Definition: SectorView.cpp:760
void ClearRoute()
Definition: SectorView.cpp:1035
void SetRotateMode(bool enable)
Definition: SectorView.cpp:1770
void SetZoomMode(bool enable)
Definition: SectorView.cpp:1761
Definition: Sector.h:38
Definition: SystemPath.h:13
Definition: Color.h:66
Definition: InputBindings.h:142
Definition: InputBindings.h:168
Definition: Input.h:45
Definition: SectorView.h:86
void RegisterBindings() override
Definition: SectorView.cpp:458
Axis * mapViewYaw
Definition: SectorView.h:97
Action * mapViewReset
Definition: SectorView.h:92
Axis * mapViewMoveForward
Definition: SectorView.h:94
Axis * mapViewMoveLeft
Definition: SectorView.h:95
Axis * mapViewPitch
Definition: SectorView.h:98
Action * mapWarpToSelected
Definition: SectorView.h:91
Axis * mapViewMoveUp
Definition: SectorView.h:96
Action * mapToggleSelectionFollowView
Definition: SectorView.h:89
Action * mapWarpToCurrent
Definition: SectorView.h:90
Axis * mapViewZoom
Definition: SectorView.h:99