1 // Copyright © 2008-2021 Pioneer Developers. See AUTHORS.txt for details
2 // Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3 
4 #ifndef _WORLDVIEW_H
5 #define _WORLDVIEW_H
6 
7 #include "gui/GuiWidget.h"
8 #include "pigui/PiGuiView.h"
9 #include "ship/ShipViewController.h"
10 
11 class Body;
12 class Camera;
13 class SpeedLines;
14 class NavTunnelWidget;
15 class Game;
16 
17 enum VelIconType {
18 	V_PROGRADE,
19 	V_RETROGRADE,
20 	V_BURN
21 };
22 
23 enum PlaneType {
24 	NONE,
25 	ROTATIONAL,
26 	PARENT
27 };
28 
29 namespace Gui {
30 	class TexturedQuad;
31 }
32 
33 class WorldView : public PiGuiView {
34 public:
35 	static void RegisterInputBindings();
36 	friend class NavTunnelWidget;
37 	WorldView(Game *game);
38 	WorldView(const Json &jsonObj, Game *game);
39 	virtual ~WorldView();
40 
41 	void Update() override;
42 	void Draw3D() override;
43 	void Draw() override;
44 	void SaveToJson(Json &jsonObj) override;
45 
GetCameraContext()46 	RefCountedPtr<CameraContext> GetCameraContext() const { return m_cameraContext; }
47 
GetViewController()48 	ViewController *GetViewController() const { return m_viewController; }
49 	void SetViewController(ViewController *newView);
50 
51 	std::unique_ptr<ShipViewController> shipView;
52 
53 	int GetActiveWeapon() const;
54 
55 	std::tuple<double, double, double> CalculateHeadingPitchRoll(enum PlaneType);
56 
57 	vector3d WorldSpaceToScreenSpace(const Body *body) const;
58 	vector3d WorldSpaceToScreenSpace(const vector3d &position) const;
59 	vector3d WorldDirToScreenSpace(const vector3d &direction) const;
60 	vector3d GetTargetIndicatorScreenPosition(const Body *body) const;
61 	vector3d CameraSpaceToScreenSpace(const vector3d &pos) const;
62 
BeginCameraFrame()63 	void BeginCameraFrame() { m_cameraContext->BeginFrame(); };
EndCameraFrame()64 	void EndCameraFrame() { m_cameraContext->EndFrame(); };
65 
ShouldShowLabels()66 	bool ShouldShowLabels() { return m_labelsOn; }
67 
68 protected:
69 	void OnSwitchTo() override;
70 	void OnSwitchFrom() override;
71 
72 private:
73 	void InitObject();
74 
75 	enum IndicatorSide {
76 		INDICATOR_HIDDEN,
77 		INDICATOR_ONSCREEN,
78 		INDICATOR_LEFT,
79 		INDICATOR_RIGHT,
80 		INDICATOR_TOP,
81 		INDICATOR_BOTTOM
82 	};
83 
84 	struct Indicator {
85 		vector2f pos;
86 		vector2f realpos;
87 		IndicatorSide side;
IndicatorIndicator88 		Indicator() :
89 			pos(0.0f, 0.0f),
90 			realpos(0.0f, 0.0f),
91 			side(INDICATOR_HIDDEN)
92 		{}
93 	};
94 
95 	void UpdateProjectedObjects();
96 	void UpdateIndicator(Indicator &indicator, const vector3d &direction);
97 	void HideIndicator(Indicator &indicator);
98 
99 	void OnToggleLabels();
100 
101 	void DrawCombatTargetIndicator(const Indicator &target, const Indicator &lead, const Color &c);
102 	void DrawEdgeMarker(const Indicator &marker, const Color &c);
103 
104 	/// Handler for "requestTimeAccelerationInc" event
105 	void OnRequestTimeAccelInc();
106 	/// Handler for "requestTimeAccelerationDec" event
107 	void OnRequestTimeAccelDec();
108 	void SelectBody(Body *, bool reselectIsDeselect);
109 
110 	Game *m_game;
111 	ViewController *m_viewController;
112 
113 	std::unique_ptr<SpeedLines> m_speedLines;
114 
115 	bool m_labelsOn;
116 
117 	sigc::connection m_onToggleHudModeCon;
118 	sigc::connection m_onIncTimeAccelCon;
119 	sigc::connection m_onDecTimeAccelCon;
120 
121 	RefCountedPtr<CameraContext> m_cameraContext;
122 	std::unique_ptr<Camera> m_camera;
123 
124 	Indicator m_combatTargetIndicator;
125 	Indicator m_targetLeadIndicator;
126 
127 	Graphics::RenderState *m_blendState;
128 
129 	Graphics::Drawables::Line3D m_edgeMarker;
130 	Graphics::Drawables::Lines m_indicator;
131 
132 	struct InputBinding : public Input::InputFrame {
133 		using InputFrame::InputFrame;
134 
135 		Action *toggleHudMode;
136 		Action *increaseTimeAcceleration;
137 		Action *decreaseTimeAcceleration;
138 
139 		void RegisterBindings() override;
140 	} InputBindings;
141 };
142 
143 class NavTunnelWidget : public Gui::Widget {
144 public:
145 	NavTunnelWidget(WorldView *worldView, Graphics::RenderState *);
146 	virtual void Draw();
147 	virtual void GetSizeRequested(float size[2]);
148 	void DrawTargetGuideSquare(const vector2f &pos, const float size, const Color &c);
149 
150 private:
151 	void CreateVertexBuffer(const Uint32 size);
152 
153 	WorldView *m_worldView;
154 	Graphics::RenderState *m_renderState;
155 	RefCountedPtr<Graphics::Material> m_material;
156 	std::unique_ptr<Graphics::VertexBuffer> m_vbuffer;
157 };
158 
159 #endif /* _WORLDVIEW_H */
160