Skip to content

Commit a7d175f

Browse files
mgermeriegchoqueux
authored andcommitted
refacto(StateControls): simplify setFromOptions method
1 parent d7c2ffa commit a7d175f

File tree

2 files changed

+82
-55
lines changed

2 files changed

+82
-55
lines changed

src/Controls/StateControl.js

+75-48
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,54 @@ const CONTROL_KEYS = {
1212
};
1313

1414

15+
const DEFAULT_STATES = {
16+
ORBIT: {
17+
enable: true,
18+
mouseButton: THREE.MOUSE.LEFT,
19+
double: false,
20+
keyboard: CONTROL_KEYS.CTRL,
21+
finger: 2,
22+
},
23+
MOVE_GLOBE: {
24+
enable: true,
25+
mouseButton: THREE.MOUSE.LEFT,
26+
double: false,
27+
finger: 1,
28+
},
29+
DOLLY: {
30+
enable: true,
31+
mouseButton: THREE.MOUSE.MIDDLE,
32+
double: false,
33+
finger: 2,
34+
},
35+
PAN: {
36+
enable: true,
37+
mouseButton: THREE.MOUSE.RIGHT,
38+
double: false,
39+
finger: 3,
40+
up: CONTROL_KEYS.UP,
41+
bottom: CONTROL_KEYS.BOTTOM,
42+
left: CONTROL_KEYS.LEFT,
43+
right: CONTROL_KEYS.RIGHT,
44+
},
45+
PANORAMIC: {
46+
enable: true,
47+
mouseButton: THREE.MOUSE.LEFT,
48+
double: false,
49+
keyboard: CONTROL_KEYS.SHIFT,
50+
},
51+
TRAVEL_IN: {
52+
enable: true,
53+
mouseButton: THREE.MOUSE.LEFT,
54+
double: true,
55+
},
56+
TRAVEL_OUT: {
57+
enable: false,
58+
double: false,
59+
},
60+
};
61+
62+
1563
function stateToTrigger(state) {
1664
if (!state) {
1765
return undefined;
@@ -106,7 +154,7 @@ class StateControl extends THREE.EventDispatcher {
106154
if (state.enable
107155
&& state.mouseButton === mouseButton
108156
&& state.keyboard === keyboard
109-
&& state.double === double
157+
&& (!double || state.double === double)
110158
) {
111159
return state;
112160
}
@@ -133,67 +181,46 @@ class StateControl extends THREE.EventDispatcher {
133181
/**
134182
* Set the current StateControl {@link State} properties to given values.
135183
* @param {Object} options Object containing the `State` values to set current `StateControl` properties to.
184+
* The `enable` property do not necessarily need to be specified. In that case, the
185+
* previous value of this property will be kept for the new {@link State}.
136186
*
137187
* @example
138-
* // Switch bindings for PAN and MOVE_GLOBE actions :
188+
* // Switch bindings for PAN and MOVE_GLOBE actions, and disabling PANORAMIC movement :
139189
* view.controls.states.setFromOptions({
140190
* PAN: {
141-
* enable: true,
142-
* mouseButton: itowns.THREE.MOUSE.LEFT,
191+
* mouseButton: itowns.THREE.MOUSE.LEFT,
143192
* },
144193
* MOVE_GLOBE: {
145-
* enable: true,
146194
* mouseButton: itowns.THREE.MOUSE.RIGHT,
147195
* },
196+
* PANORAMIC: {
197+
* enable: false,
198+
* },
148199
* };
149200
*/
150201
setFromOptions(options) {
151-
this.ORBIT = options.ORBIT || this.ORBIT || {
152-
mouseButton: THREE.MOUSE.LEFT,
153-
keyboard: CONTROL_KEYS.CTRL,
154-
enable: true,
155-
finger: 2,
156-
};
157-
this.DOLLY = options.DOLLY || this.DOLLY || {
158-
mouseButton: THREE.MOUSE.MIDDLE,
159-
enable: true,
160-
};
161-
this.PAN = options.PAN || this.PAN || {
162-
mouseButton: THREE.MOUSE.RIGHT,
163-
up: CONTROL_KEYS.UP,
164-
bottom: CONTROL_KEYS.BOTTOM,
165-
left: CONTROL_KEYS.LEFT,
166-
right: CONTROL_KEYS.RIGHT,
167-
enable: true,
168-
finger: 3,
169-
};
170-
this.MOVE_GLOBE = options.MOVE_GLOBE || this.MOVE_GLOBE || {
171-
mouseButton: THREE.MOUSE.LEFT,
172-
enable: true,
173-
finger: 1,
174-
};
175-
this.PANORAMIC = options.PANORAMIC || this.PANORAMIC || {
176-
mouseButton: THREE.MOUSE.LEFT,
177-
keyboard: CONTROL_KEYS.SHIFT,
178-
enable: true,
179-
};
202+
this._domElement.removeEventListener(stateToTrigger(this.TRAVEL_IN), this._handleTravelInEvent, false);
203+
this._domElement.removeEventListener(stateToTrigger(this.TRAVEL_OUT), this._handleTravelOutEvent, false);
180204

181-
const newTravelIn = options.TRAVEL_IN || this.TRAVEL_IN || {
182-
enable: true,
183-
mouseButton: THREE.MOUSE.LEFT,
184-
double: true,
185-
};
205+
for (const state in DEFAULT_STATES) {
206+
if ({}.hasOwnProperty.call(DEFAULT_STATES, state)) {
207+
let newState = {};
208+
newState = options[state] || this[state] || Object.assign(newState, DEFAULT_STATES[state]);
186209

187-
this._domElement.removeEventListener(stateToTrigger(this.TRAVEL_IN), this._handleTravelInEvent, false);
188-
this._domElement.addEventListener(stateToTrigger(newTravelIn), this._handleTravelInEvent, false);
189-
this.TRAVEL_IN = newTravelIn;
210+
// Copy the previous value of `enable` property if not defined in options
211+
if (options[state] && options[state].enable === undefined) {
212+
newState.enable = this[state].enable;
213+
}
214+
// If no value is provided for the `double` property,
215+
// defaults it to `false` instead of leaving it undefined
216+
newState.double = !!newState.double;
190217

191-
const newTravelOut = options.TRAVEL_OUT || this.TRAVEL_OUT || {
192-
enable: false,
193-
};
194-
this._domElement.removeEventListener(stateToTrigger(this.TRAVEL_OUT), this._handleTravelOutEvent, false);
195-
this._domElement.addEventListener(stateToTrigger(newTravelOut), this._handleTravelOutEvent, false);
196-
this.TRAVEL_OUT = newTravelOut;
218+
this[state] = newState;
219+
}
220+
}
221+
222+
this._domElement.addEventListener(stateToTrigger(this.TRAVEL_IN), this._handleTravelInEvent, false);
223+
this._domElement.addEventListener(stateToTrigger(this.TRAVEL_OUT), this._handleTravelOutEvent, false);
197224
}
198225

199226
/**

test/unit/statecontrol.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ describe('StateControl', function () {
4545

4646
it('setFromOptions should set states according to given options', function () {
4747
const options = {
48-
PAN: { enable: false },
49-
MOVE_GLOBE: { enable: true, mouseButton: MOUSE.LEFT },
50-
ORBIT: { enable: true, mouseButton: MOUSE.MIDDLE },
51-
DOLLY: { enable: true, mouseButton: MOUSE.RIGHT },
52-
PANORAMIC: { enable: true, mouseButton: MOUSE.LEFT, keyboard: 17 },
53-
TRAVEL_IN: { enable: true, mouseButton: MOUSE.LEFT, double: true },
54-
TRAVEL_OUT: { enable: true, mouseButton: MOUSE.RIGHT, double: true },
48+
PAN: { enable: false, double: false },
49+
MOVE_GLOBE: { enable: true, double: false, mouseButton: MOUSE.LEFT },
50+
ORBIT: { enable: true, double: false, mouseButton: MOUSE.MIDDLE },
51+
DOLLY: { enable: true, double: false, mouseButton: MOUSE.RIGHT },
52+
PANORAMIC: { enable: true, double: false, mouseButton: MOUSE.LEFT, keyboard: 17 },
53+
TRAVEL_IN: { enable: true, double: true, mouseButton: MOUSE.LEFT },
54+
TRAVEL_OUT: { enable: true, double: true, mouseButton: MOUSE.RIGHT },
5555
};
5656
states.setFromOptions(options);
5757

0 commit comments

Comments
 (0)