1 /*
2 Copyright (c) 2017-2022 Timur Gafarov
3 
4 Boost Software License - Version 1.0 - August 17th, 2003
5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 
12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 */
27 
28 module dagon.ui.firstpersonview;
29 
30 import std.math;
31 
32 import dlib.math.vector;
33 import dlib.math.matrix;
34 import dlib.math.quaternion;
35 import dlib.math.transformation;
36 import dlib.math.utils;
37 
38 import dagon.core.event;
39 import dagon.core.keycodes;
40 import dagon.core.time;
41 import dagon.graphics.entity;
42 
43 class FirstPersonViewComponent: EntityComponent
44 {
45     protected bool _active = true;
46     protected bool _useRelativeMouseMode = true;
47     
48     bool mouseActive = true;
49     
50     float mouseSensitivity = 0.2f;
51     float axisSensitivity = 20.0f;
52     
53     float pitchLimitMax = 60.0f;
54     float pitchLimitMin = -60.0f;
55     
56     int prevMouseX = 0;
57     int prevMouseY = 0;
58     
59     float pitch = 0.0f;
60     float turn = 0.0f;
61     
62     Quaternionf baseOrientation = Quaternionf.identity;
63     
64     this(EventManager em, Entity e)
65     {
66         super(em, e);
67         active = true;
68         useRelativeMouseMode = true;
69         reset();
70     }
71     
72     void active(bool mode) @property
73     {
74         _active = mode;
75         prevMouseX = eventManager.mouseX;
76         prevMouseY = eventManager.mouseY;
77         eventManager.setRelativeMouseMode(_useRelativeMouseMode && _active);
78         if (!_active)
79         {
80             eventManager.setMouseToCenter();
81         }
82     }
83     
84     bool active() @property
85     {
86         return _active;
87     }
88     
89     void useRelativeMouseMode(bool mode) @property
90     {
91         _useRelativeMouseMode = mode;
92         eventManager.setRelativeMouseMode(_useRelativeMouseMode && _active);
93     }
94     
95     bool useRelativeMouseMode() @property
96     {
97         return _useRelativeMouseMode;
98     }
99     
100     void reset()
101     {
102         pitch = 0.0f;
103         turn = 0.0f;
104         if (!useRelativeMouseMode)
105             eventManager.setMouseToCenter();
106         prevMouseX = eventManager.mouseX;
107         prevMouseY = eventManager.mouseY;
108     }
109     
110     override void update(Time time)
111     {
112         processEvents();
113         
114         if (_active & mouseActive)
115         {
116             float mouseRelH, mouseRelV;
117             if (useRelativeMouseMode)
118             {
119                 mouseRelH = eventManager.mouseRelX * mouseSensitivity;
120                 mouseRelV = eventManager.mouseRelY * mouseSensitivity;
121             }
122             else
123             {
124                 mouseRelH =  (eventManager.mouseX - prevMouseX) * mouseSensitivity;
125                 mouseRelV = (eventManager.mouseY - prevMouseY) * mouseSensitivity;
126             }
127             
128             float axisV = inputManager.getAxis("vertical") * axisSensitivity * mouseSensitivity;
129             float axisH = inputManager.getAxis("horizontal") * axisSensitivity * mouseSensitivity;
130             
131             pitch -= mouseRelV + axisV;
132             turn -= mouseRelH + axisH;
133             
134             if (pitch > pitchLimitMax)
135             {
136                 pitch = pitchLimitMax;
137             }
138             else if (pitch < pitchLimitMin)
139             {
140                 pitch = pitchLimitMin;
141             }
142             
143             if (!useRelativeMouseMode)
144                 eventManager.setMouseToCenter();
145             
146             prevMouseX = eventManager.mouseX;
147             prevMouseY = eventManager.mouseY;
148         }
149         
150         auto rotPitch = rotationQuaternion(Vector3f(1.0f,0.0f,0.0f), degtorad(pitch));
151         auto rotTurn = rotationQuaternion(Vector3f(0.0f,1.0f,0.0f), degtorad(turn));
152         
153         Quaternionf orientation = baseOrientation * rotTurn * rotPitch;
154         
155         entity.transformation =
156             (translationMatrix(entity.position) *
157             orientation.toMatrix4x4 *
158             scaleMatrix(entity.scaling));
159         
160         entity.invTransformation = entity.transformation.inverse;
161         
162         if (entity.parent)
163         {
164             entity.absoluteTransformation = entity.parent.absoluteTransformation * entity.transformation;
165             entity.invAbsoluteTransformation = entity.invTransformation * entity.parent.invAbsoluteTransformation;
166             entity.prevAbsoluteTransformation = entity.parent.prevAbsoluteTransformation * entity.prevTransformation;
167         }
168         else
169         {
170             entity.absoluteTransformation = entity.transformation;
171             entity.invAbsoluteTransformation = entity.invTransformation;
172             entity.prevAbsoluteTransformation = entity.prevTransformation;
173         }
174     }
175     
176     override void onFocusGain()
177     {
178         mouseActive = true;
179     }
180     
181     override void onFocusLoss()
182     {
183         mouseActive = false;
184     }
185     
186     void moveForward(float speed)
187     {
188         Vector3f forward = entity.transformation.forward;
189         entity.position -= forward.normalized * speed;
190     }
191     
192     void moveBack(float speed)
193     {
194         Vector3f forward = entity.transformation.forward;
195         entity.position += forward.normalized * speed;
196     }
197     
198     void strafeRight(float speed)
199     {
200         Vector3f right = entity.transformation.right;
201         entity.position += right.normalized * speed;
202     }
203     
204     void strafeLeft(float speed)
205     {
206         Vector3f right = entity.transformation.right;
207         entity.position -= right.normalized * speed;
208     }
209 }