1
|
|
|
<?php |
2
|
|
|
/** @formatter:off |
3
|
|
|
* ****************************************************************** |
4
|
|
|
* Created by Marko Kungla on Jun 25, 2016 - 7:51:42 PM |
5
|
|
|
* Contact [email protected] |
6
|
|
|
* @copyright 2016 Marko Kungla - https://github.com/mkungla |
7
|
|
|
* @license The MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* @category AframeVR |
10
|
|
|
* @package aframe-php |
11
|
|
|
* |
12
|
|
|
* Lang PHP (php version >= 7) |
13
|
|
|
* Encoding UTF-8 |
14
|
|
|
* File RotationComponent.php |
15
|
|
|
* Code format PSR-2 and 12 |
16
|
|
|
* @link https://github.com/mkungla/aframe-php |
17
|
|
|
* @issues https://github.com/mkungla/aframe-php/issues |
18
|
|
|
* ******************************************************************** |
19
|
|
|
* Contributors: |
20
|
|
|
* @author Marko Kungla <[email protected]> |
21
|
|
|
* ******************************************************************** |
22
|
|
|
* Comments: |
23
|
|
|
* @formatter:on */ |
24
|
|
|
namespace AframeVR\Core\Components\Rotation; |
25
|
|
|
|
26
|
|
|
use \AframeVR\Interfaces\Core\Components\RotationCMPTIF; |
27
|
|
|
use \AframeVR\Core\Helpers\ComponentAbstract; |
28
|
|
|
use \AframeVR\Core\Helpers\ComponentHelper; |
29
|
|
|
|
30
|
|
View Code Duplication |
class RotationComponent extends ComponentAbstract implements RotationCMPTIF |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
use ComponentHelper; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize Rotation Component |
36
|
|
|
* |
37
|
|
|
* The rotation component defines the orientation of an entity. |
38
|
|
|
* It takes the |
39
|
|
|
* roll (x), |
40
|
|
|
* pitch (y), |
41
|
|
|
* and yaw (z) |
42
|
|
|
* as three space-delimited numbers indicating degrees of rotation. |
43
|
|
|
* |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
67 |
|
public function initializeComponent(): bool |
49
|
|
|
{ |
50
|
67 |
|
$this->setDomAttribute('rotation'); |
51
|
67 |
|
return true; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get Rotation |
56
|
|
|
* |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
1 |
|
public function getRotation(): string |
62
|
|
|
{ |
63
|
1 |
|
return $this->getDomAttributeString(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return DOM attribute contents |
68
|
|
|
* |
69
|
|
|
* Scale Components dom atribute contains roll, pitch, yaw |
70
|
|
|
* Ex: rotation="1 1 1" |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
6 |
|
public function getDomAttributeString(): string |
75
|
|
|
{ |
76
|
6 |
|
$attrs = $this->getDOMAttributesArray(); |
77
|
6 |
|
return $this->createCoordinateString($attrs['x'], $attrs['y'], $attrs['z']); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Roll, rotation about the X-axis. |
82
|
|
|
* |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
* |
85
|
|
|
* @param double $roll |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
15 |
|
public function roll(float $roll) |
89
|
|
|
{ |
90
|
15 |
|
$this->init(); |
91
|
15 |
|
$this->dom_attributes['x'] = $roll; |
92
|
15 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Pitch, rotation about the Y-axis. |
96
|
|
|
* |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
* |
99
|
|
|
* @param double $pitch |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
14 |
|
public function pitch(float $pitch) |
103
|
|
|
{ |
104
|
14 |
|
$this->init(); |
105
|
14 |
|
$this->dom_attributes['y'] = $pitch; |
106
|
14 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Yaw, rotation about the Z-axis. |
110
|
|
|
* |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @param double $yaw |
114
|
|
|
* @return void |
115
|
|
|
*/ |
116
|
14 |
|
public function yaw(float $yaw) |
117
|
|
|
{ |
118
|
14 |
|
$this->init(); |
119
|
14 |
|
$this->dom_attributes['z'] = $yaw; |
120
|
14 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* When any rotation component methods are called then init others |
124
|
|
|
* |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
* |
127
|
|
|
* @param array $dom_attributes |
|
|
|
|
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
15 |
|
private function init() |
131
|
|
|
{ |
132
|
15 |
|
$this->dom_attributes['x'] = $this->dom_attributes['x'] ?? 0; |
133
|
15 |
|
$this->dom_attributes['y'] = $this->dom_attributes['y'] ?? 0; |
134
|
15 |
|
$this->dom_attributes['z'] = $this->dom_attributes['z'] ?? 0; |
135
|
|
|
} |
136
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.