-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTHREE.GUI.VerticalList.js
178 lines (147 loc) · 3.83 KB
/
THREE.GUI.VerticalList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
~function () {
THREE = THREE || {};
THREE.GUI = THREE.GUI || {};
var _style = {
margin: 10
};
/**
* Create a new VerticalList.
*/
THREE.GUI.VerticalList = function (props) {
this._list = [];
this._current = 0;
this._needUpdate = true;
this._style = Object.create(_style);
this.style(props);
THREE.EventDispatcher.call(this);
THREE.Object3D.call(this);
}
/**
* Inherite from Object3d.
*/
THREE.GUI.VerticalList.prototype =
Object.create(THREE.Object3D.prototype);
/**
* Set list style.
*/
THREE.GUI.VerticalList.prototype.style = function (props) {
for (var key in props)
this._style[key] = props[key];
}
/**
* Append an object to the list.
*/
THREE.GUI.VerticalList.prototype.append = function (obj) {
this.add(obj);
this._list.push(obj);
_updatePositions(this);
}
/**
* Prepend an object to the list.
*/
THREE.GUI.VerticalList.prototype.prepend = function (obj) {
this.add(obj);
this._list.unshift(obj);
_updatePositions(this);
}
/**
* Remove an object from the list.
*/
THREE.GUI.VerticalList.prototype.removeAt = function (index) {
this.remove(this._list[index]);
this._list.splice(index, 1);
_updatePositions(this);
}
/**
* Call this method at each frame to update list behaviour.
*/
THREE.GUI.VerticalList.prototype.update = function (event) {
// If has event listener sumbit, then update keyboard events.
if (this._listeners["submit"] !== undefined) {
if (THREE.Input.isKeyRepeat("upArrow", 0.2, event.deltaTime))
_goPrevious(this);
if (THREE.Input.isKeyRepeat("downArrow", 0.2, event.deltaTime))
_goNext(this);
if (THREE.Input.isKeyDown("enter"))
this.dispatchEvent({type: 'submit', choice: this._current});
if (this._needUpdate) {
this._needUpdate = false;
_updateMove(this);
}
}
// If has event listener click, then update click events.
if (this._listeners["click"] !== undefined) {
if (THREE.Input.getMouseButton() === 'left')
this._leftButtonClicked = true;
else if (THREE.Input.getMouseButton() !== 'none')
this._leftButtonClicked = false;
for (var i = 0; i < this._list.length; ++i) {
if (
this._list[i].hitMouse() &&
THREE.Input.isMouseUp() &&
this._leftButtonClicked
) {
this.dispatchEvent({type: 'click', choice: i});
}
this._list[i].update(event);
}
}
}
/**
* Compute vertical offset to translate for each element.
*/
var _verticalOffset = function (self, index) {
var res = 0;
for (var i = index; i < self._list.length; ++i)
res += self._list[i].geometry.height + self._style.margin;
return res;
}
/**
* Place all elements according list center.
*/
var _updatePositions = function (self) {
if (self._list.length < 2)
return ;
// Compute offset.
var offset = self._style.margin; // remove bottom margin.
for (var i = 0; i < self._list.length; ++i)
offset += self._list[i].geometry.height + self._style.margin;
offset += self._list[0].geometry.height;
offset /= 2;
// Place list items.
for (var i = 0; i < self._list.length; ++i) {
self._list[i].position.set(0, -offset, 0);
self._list[i].translateY(_verticalOffset(self, i));
}
}
/**
* Update style and event according current position in list.
*/
var _updateMove = function (self) {
for (var i = 0; i < self._list.length; ++i) {
if (i === self._current)
self._list[i].select();
else
self._list[i].unselect();
}
self.dispatchEvent({type: 'change', choice: this._current});
}
/**
* Go to the previous element.
*/
var _goPrevious = function (self) {
--self._current;
if (self._current < 0)
self._current = self._list.length - 1;
_updateMove(self);
}
/**
* Go to the next element.
*/
var _goNext = function (self) {
++self._current;
if (self._current >= self._list.length)
self._current = 0;
_updateMove(self);
}
}();