]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiScene.cpp
Translated using Weblate (Malay)
[minetest.git] / src / gui / guiScene.cpp
1 /*
2 Minetest
3 Copyright (C) 2020 Jean-Patrick Guerrero <jeanpatrick.guerrero@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 #include "guiScene.h"
21
22 #include <SViewFrustum.h>
23 #include <IAnimatedMeshSceneNode.h>
24 #include "porting.h"
25
26 GUIScene::GUIScene(gui::IGUIEnvironment *env, scene::ISceneManager *smgr,
27                    gui::IGUIElement *parent, core::recti rect, s32 id)
28         : IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rect)
29 {
30         m_driver = env->getVideoDriver();
31         m_smgr = smgr->createNewSceneManager(false);
32
33         m_cam = m_smgr->addCameraSceneNode(0, v3f(0.f, 0.f, -100.f), v3f(0.f));
34         m_cam->setFOV(30.f * core::DEGTORAD);
35
36         m_smgr->getParameters()->setAttribute(scene::ALLOW_ZWRITE_ON_TRANSPARENT, true);
37 }
38
39 GUIScene::~GUIScene()
40 {
41         setMesh(nullptr);
42
43         m_smgr->drop();
44 }
45
46 scene::IAnimatedMeshSceneNode *GUIScene::setMesh(scene::IAnimatedMesh *mesh)
47 {
48         if (m_mesh) {
49                 m_mesh->remove();
50                 m_mesh = nullptr;
51         }
52
53         if (!mesh)
54                 return nullptr;
55
56         m_mesh = m_smgr->addAnimatedMeshSceneNode(mesh);
57         m_mesh->setPosition(-m_mesh->getBoundingBox().getCenter());
58         m_mesh->animateJoints();
59
60         return m_mesh;
61 }
62
63 void GUIScene::setTexture(u32 idx, video::ITexture *texture)
64 {
65         video::SMaterial &material = m_mesh->getMaterial(idx);
66         material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
67         material.MaterialTypeParam = 0.5f;
68         material.TextureLayer[0].Texture = texture;
69         material.setFlag(video::EMF_LIGHTING, false);
70         material.setFlag(video::EMF_FOG_ENABLE, true);
71         material.setFlag(video::EMF_BILINEAR_FILTER, false);
72         material.setFlag(video::EMF_BACK_FACE_CULLING, false);
73         material.setFlag(video::EMF_ZWRITE_ENABLE, true);
74 }
75
76 void GUIScene::draw()
77 {
78         m_driver->clearBuffers(video::ECBF_DEPTH);
79
80         // Control rotation speed based on time
81         u64 new_time = porting::getTimeMs();
82         u64 dtime_ms = 0;
83         if (m_last_time != 0)
84                 dtime_ms = porting::getDeltaMs(m_last_time, new_time);
85         m_last_time = new_time;
86
87         core::rect<s32> oldViewPort = m_driver->getViewPort();
88         m_driver->setViewPort(getAbsoluteClippingRect());
89         core::recti borderRect = Environment->getRootGUIElement()->getAbsoluteClippingRect();
90
91         if (m_bgcolor != 0) {
92                 Environment->getSkin()->draw3DSunkenPane(
93                         this, m_bgcolor, false, true, borderRect, 0);
94         }
95
96         core::dimension2d<s32> size = getAbsoluteClippingRect().getSize();
97         m_smgr->getActiveCamera()->setAspectRatio((f32)size.Width / (f32)size.Height);
98
99         if (!m_target) {
100                 updateCamera(m_smgr->addEmptySceneNode());
101                 rotateCamera(v3f(0.f));
102                 m_cam->bindTargetAndRotation(true);
103         }
104
105         cameraLoop();
106
107         // Continuous rotation
108         if (m_inf_rot)
109                 rotateCamera(v3f(0.f, -0.03f * (float)dtime_ms, 0.f));
110
111         m_smgr->drawAll();
112
113         if (m_initial_rotation && m_mesh) {
114                 rotateCamera(v3f(m_custom_rot.X, m_custom_rot.Y, 0.f));
115                 calcOptimalDistance();
116
117                 m_initial_rotation = false;
118         }
119
120         m_driver->setViewPort(oldViewPort);
121 }
122
123 bool GUIScene::OnEvent(const SEvent &event)
124 {
125         if (m_mouse_ctrl && event.EventType == EET_MOUSE_INPUT_EVENT) {
126                 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
127                         m_last_pos = v2f((f32)event.MouseInput.X, (f32)event.MouseInput.Y);
128                         return true;
129                 } else if (event.MouseInput.Event == EMIE_MOUSE_MOVED) {
130                         if (event.MouseInput.isLeftPressed()) {
131                                 m_curr_pos = v2f((f32)event.MouseInput.X, (f32)event.MouseInput.Y);
132
133                                 rotateCamera(v3f(
134                                         m_last_pos.Y - m_curr_pos.Y,
135                                         m_curr_pos.X - m_last_pos.X, 0.f));
136
137                                 m_last_pos = m_curr_pos;
138                                 return true;
139                         }
140                 }
141         }
142
143         return gui::IGUIElement::OnEvent(event);
144 }
145
146 void GUIScene::setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES> &styles)
147 {
148         StyleSpec::State state = StyleSpec::STATE_DEFAULT;
149         StyleSpec style = StyleSpec::getStyleFromStatePropagation(styles, state);
150
151         setNotClipped(style.getBool(StyleSpec::NOCLIP, false));
152         setBackgroundColor(style.getColor(StyleSpec::BGCOLOR, m_bgcolor));
153 }
154
155 /**
156  * Sets the frame loop range for the mesh
157  */
158 void GUIScene::setFrameLoop(s32 begin, s32 end)
159 {
160         if (m_mesh->getStartFrame() != begin || m_mesh->getEndFrame() != end)
161                 m_mesh->setFrameLoop(begin, end);
162 }
163
164 /**
165  * Sets the animation speed (FPS) for the mesh
166  */
167 void GUIScene::setAnimationSpeed(f32 speed)
168 {
169         m_mesh->setAnimationSpeed(speed);
170 }
171
172 /* Camera control functions */
173
174 inline void GUIScene::calcOptimalDistance()
175 {
176         core::aabbox3df box = m_mesh->getBoundingBox();
177         f32 width  = box.MaxEdge.X - box.MinEdge.X;
178         f32 height = box.MaxEdge.Y - box.MinEdge.Y;
179         f32 depth  = box.MaxEdge.Z - box.MinEdge.Z;
180         f32 max_width = width > depth ? width : depth;
181
182         const scene::SViewFrustum *f = m_cam->getViewFrustum();
183         f32 cam_far = m_cam->getFarValue();
184         f32 far_width = core::line3df(f->getFarLeftUp(), f->getFarRightUp()).getLength();
185         f32 far_height = core::line3df(f->getFarLeftUp(), f->getFarLeftDown()).getLength();
186
187         core::recti rect = getAbsolutePosition();
188         f32 zoomX = rect.getWidth() / max_width;
189         f32 zoomY = rect.getHeight() / height;
190         f32 dist;
191
192         if (zoomX < zoomY)
193                 dist = (max_width / (far_width / cam_far)) + (0.5f * max_width);
194         else
195                 dist = (height / (far_height / cam_far)) + (0.5f * max_width);
196
197         m_cam_distance = dist;
198         m_update_cam = true;
199 }
200
201 void GUIScene::updateCamera(scene::ISceneNode *target)
202 {
203         m_target = target;
204         updateTargetPos();
205
206         m_last_target_pos = m_target_pos;
207         updateCameraPos();
208
209         m_update_cam = true;
210 }
211
212 void GUIScene::updateTargetPos()
213 {
214         m_last_target_pos = m_target_pos;
215         m_target->updateAbsolutePosition();
216         m_target_pos = m_target->getAbsolutePosition();
217 }
218
219 void GUIScene::setCameraRotation(v3f rot)
220 {
221         correctBounds(rot);
222
223         core::matrix4 mat;
224         mat.setRotationDegrees(rot);
225
226         m_cam_pos = v3f(0.f, 0.f, m_cam_distance);
227         mat.rotateVect(m_cam_pos);
228
229         m_cam_pos += m_target_pos;
230         m_cam->setPosition(m_cam_pos);
231         m_update_cam = false;
232 }
233
234 bool GUIScene::correctBounds(v3f &rot)
235 {
236         const float ROTATION_MAX_1 = 60.0f;
237         const float ROTATION_MAX_2 = 300.0f;
238
239         // Limit and correct the rotation when needed
240         if (rot.X < 90.f) {
241                 if (rot.X > ROTATION_MAX_1) {
242                         rot.X = ROTATION_MAX_1;
243                         return true;
244                 }
245         } else if (rot.X < ROTATION_MAX_2) {
246                 rot.X = ROTATION_MAX_2;
247                 return true;
248         }
249
250         // Not modified
251         return false;
252 }
253
254 void GUIScene::cameraLoop()
255 {
256         updateCameraPos();
257         updateTargetPos();
258
259         if (m_target_pos != m_last_target_pos)
260                 m_update_cam = true;
261
262         if (m_update_cam) {
263                 m_cam_pos = m_target_pos + (m_cam_pos - m_target_pos).normalize() * m_cam_distance;
264
265                 v3f rot = getCameraRotation();
266                 if (correctBounds(rot))
267                         setCameraRotation(rot);
268
269                 m_cam->setPosition(m_cam_pos);
270                 m_cam->setTarget(m_target_pos);
271
272                 m_update_cam = false;
273         }
274 }