]> git.lizzy.rs Git - dragonfireclient.git/blob - src/client/camera.h
Add nametag background setting and object property (#10937)
[dragonfireclient.git] / src / client / camera.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include "irrlichttypes_extrabloated.h"
23 #include "inventory.h"
24 #include "client/tile.h"
25 #include <ICameraSceneNode.h>
26 #include <ISceneNode.h>
27 #include <list>
28 #include "util/Optional.h"
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class Client;
33 class WieldMeshSceneNode;
34
35 struct Nametag
36 {
37         scene::ISceneNode *parent_node;
38         std::string text;
39         video::SColor textcolor;
40         Optional<video::SColor> bgcolor;
41         v3f pos;
42
43         Nametag(scene::ISceneNode *a_parent_node,
44                         const std::string &text,
45                         const video::SColor &textcolor,
46                         const Optional<video::SColor> &bgcolor,
47                         const v3f &pos):
48                 parent_node(a_parent_node),
49                 text(text),
50                 textcolor(textcolor),
51                 bgcolor(bgcolor),
52                 pos(pos)
53         {
54         }
55
56         video::SColor getBgColor(bool use_fallback) const
57         {
58                 if (bgcolor)
59                         return bgcolor.value();
60                 else if (!use_fallback)
61                         return video::SColor(0, 0, 0, 0);
62                 else if (textcolor.getLuminance() > 186)
63                         // Dark background for light text
64                         return video::SColor(50, 50, 50, 50);
65                 else
66                         // Light background for dark text
67                         return video::SColor(50, 255, 255, 255);
68         }
69 };
70
71 enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
72
73 /*
74         Client camera class, manages the player and camera scene nodes, the viewing distance
75         and performs view bobbing etc. It also displays the wielded tool in front of the
76         first-person camera.
77 */
78 class Camera
79 {
80 public:
81         Camera(MapDrawControl &draw_control, Client *client);
82         ~Camera();
83
84         // Get camera scene node.
85         // It has the eye transformation, pitch and view bobbing applied.
86         inline scene::ICameraSceneNode* getCameraNode() const
87         {
88                 return m_cameranode;
89         }
90
91         // Get the camera position (in absolute scene coordinates).
92         // This has view bobbing applied.
93         inline v3f getPosition() const
94         {
95                 return m_camera_position;
96         }
97
98         // Returns the absolute position of the head SceneNode in the world
99         inline v3f getHeadPosition() const
100         {
101                 return m_headnode->getAbsolutePosition();
102         }
103
104         // Get the camera direction (in absolute camera coordinates).
105         // This has view bobbing applied.
106         inline v3f getDirection() const
107         {
108                 return m_camera_direction;
109         }
110
111         // Get the camera offset
112         inline v3s16 getOffset() const
113         {
114                 return m_camera_offset;
115         }
116
117         // Horizontal field of view
118         inline f32 getFovX() const
119         {
120                 return m_fov_x;
121         }
122
123         // Vertical field of view
124         inline f32 getFovY() const
125         {
126                 return m_fov_y;
127         }
128
129         // Get maximum of getFovX() and getFovY()
130         inline f32 getFovMax() const
131         {
132                 return MYMAX(m_fov_x, m_fov_y);
133         }
134
135         // Notify about new server-sent FOV and initialize smooth FOV transition
136         void notifyFovChange();
137
138         // Checks if the constructor was able to create the scene nodes
139         bool successfullyCreated(std::string &error_message);
140
141         // Step the camera: updates the viewing range and view bobbing.
142         void step(f32 dtime);
143
144         // Update the camera from the local player's position.
145         // busytime is used to adjust the viewing range.
146         void update(LocalPlayer* player, f32 frametime, f32 busytime,
147                         f32 tool_reload_ratio);
148
149         // Update render distance
150         void updateViewingRange();
151
152         // Start digging animation
153         // Pass 0 for left click, 1 for right click
154         void setDigging(s32 button);
155
156         // Replace the wielded item mesh
157         void wield(const ItemStack &item);
158
159         // Draw the wielded tool.
160         // This has to happen *after* the main scene is drawn.
161         // Warning: This clears the Z buffer.
162         void drawWieldedTool(irr::core::matrix4* translation=NULL);
163
164         // Toggle the current camera mode
165         void toggleCameraMode() {
166                 if (m_camera_mode == CAMERA_MODE_FIRST)
167                         m_camera_mode = CAMERA_MODE_THIRD;
168                 else if (m_camera_mode == CAMERA_MODE_THIRD)
169                         m_camera_mode = CAMERA_MODE_THIRD_FRONT;
170                 else
171                         m_camera_mode = CAMERA_MODE_FIRST;
172         }
173
174         // Set the current camera mode
175         inline void setCameraMode(CameraMode mode)
176         {
177                 m_camera_mode = mode;
178         }
179
180         //read the current camera mode
181         inline CameraMode getCameraMode()
182         {
183                 return m_camera_mode;
184         }
185
186         Nametag *addNametag(scene::ISceneNode *parent_node,
187                 const std::string &text, video::SColor textcolor,
188                 Optional<video::SColor> bgcolor, const v3f &pos);
189
190         void removeNametag(Nametag *nametag);
191
192         void drawNametags();
193
194         inline void addArmInertia(f32 player_yaw);
195
196 private:
197         // Nodes
198         scene::ISceneNode *m_playernode = nullptr;
199         scene::ISceneNode *m_headnode = nullptr;
200         scene::ICameraSceneNode *m_cameranode = nullptr;
201
202         scene::ISceneManager *m_wieldmgr = nullptr;
203         WieldMeshSceneNode *m_wieldnode = nullptr;
204
205         // draw control
206         MapDrawControl& m_draw_control;
207
208         Client *m_client;
209
210         // Default Client FOV (as defined by the "fov" setting)
211         f32 m_cache_fov;
212
213         // Absolute camera position
214         v3f m_camera_position;
215         // Absolute camera direction
216         v3f m_camera_direction;
217         // Camera offset
218         v3s16 m_camera_offset;
219
220         // Server-sent FOV variables
221         bool m_server_sent_fov = false;
222         f32 m_curr_fov_degrees, m_old_fov_degrees, m_target_fov_degrees;
223
224         // FOV transition variables
225         bool m_fov_transition_active = false;
226         f32 m_fov_diff, m_transition_time;
227
228         v2f m_wieldmesh_offset = v2f(55.0f, -35.0f);
229         v2f m_arm_dir;
230         v2f m_cam_vel;
231         v2f m_cam_vel_old;
232         v2f m_last_cam_pos;
233
234         // Field of view and aspect ratio stuff
235         f32 m_aspect = 1.0f;
236         f32 m_fov_x = 1.0f;
237         f32 m_fov_y = 1.0f;
238
239         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
240         f32 m_view_bobbing_anim = 0.0f;
241         // If 0, view bobbing is off (e.g. player is standing).
242         // If 1, view bobbing is on (player is walking).
243         // If 2, view bobbing is getting switched off.
244         s32 m_view_bobbing_state = 0;
245         // Speed of view bobbing animation
246         f32 m_view_bobbing_speed = 0.0f;
247         // Fall view bobbing
248         f32 m_view_bobbing_fall = 0.0f;
249
250         // Digging animation frame (0 <= m_digging_anim < 1)
251         f32 m_digging_anim = 0.0f;
252         // If -1, no digging animation
253         // If 0, left-click digging animation
254         // If 1, right-click digging animation
255         s32 m_digging_button = -1;
256
257         // Animation when changing wielded item
258         f32 m_wield_change_timer = 0.125f;
259         ItemStack m_wield_item_next;
260
261         CameraMode m_camera_mode = CAMERA_MODE_FIRST;
262
263         f32 m_cache_fall_bobbing_amount;
264         f32 m_cache_view_bobbing_amount;
265         bool m_arm_inertia;
266
267         std::list<Nametag *> m_nametags;
268         bool m_show_nametag_backgrounds;
269 };