105 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2015-12-16 01:21:22 -08:00
//
// slider.js
//
2015-12-16 01:31:24 -08:00
// Created by James Pollack @imgntn on 12/15/2015
2015-12-16 01:21:22 -08:00
// Copyright 2015 High Fidelity, Inc.
//
// Entity script that sends a scaled value to a light based on its distance from the start of its constraint axis.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
2015-12-16 01:31:24 -08:00
//
2015-12-16 11:57:54 -08:00
2015-12-12 16:17:00 -08:00
(function() {
var AXIS_SCALE = 1;
var COLOR_MAX = 255;
2015-12-15 18:20:12 -08:00
var INTENSITY_MAX = 0.05;
var CUTOFF_MAX = 360;
var EXPONENT_MAX = 1;
2015-12-15 18:20:12 -08:00
function Slider() {
2015-12-12 17:14:43 -08:00
return this;
2015-12-12 16:17:00 -08:00
}
2015-12-15 18:20:12 -08:00
Slider.prototype = {
2015-12-12 16:17:00 -08:00
preload: function(entityID) {
this.entityID = entityID;
2015-12-14 18:42:00 -08:00
var entityProperties = Entities.getEntityProperties(this.entityID, "userData");
var parsedUserData = JSON.parse(entityProperties.userData);
2015-12-15 15:55:03 -08:00
this.userData = parsedUserData.lightModifierKey;
2015-12-12 16:17:00 -08:00
},
2015-12-12 17:14:43 -08:00
startNearGrab: function() {
this.setInitialProperties();
2015-12-12 16:17:00 -08:00
},
2015-12-12 17:14:43 -08:00
startDistantGrab: function() {
this.setInitialProperties();
2015-12-12 16:17:00 -08:00
},
2015-12-12 17:14:43 -08:00
setInitialProperties: function() {
this.initialProperties = Entities.getEntityProperties(this.entityID);
},
2015-12-15 18:20:12 -08:00
continueNearGrab: function() {
// this.continueDistantGrab();
2015-12-12 17:14:43 -08:00
},
continueDistantGrab: function() {
2015-12-15 18:20:12 -08:00
this.setSliderValueBasedOnDistance();
},
setSliderValueBasedOnDistance: function() {
2015-12-15 15:55:03 -08:00
var currentPosition = Entities.getEntityProperties(this.entityID, "position").position;
2015-12-14 16:51:12 -08:00
2015-12-15 18:20:12 -08:00
var distance = Vec3.distance(this.userData.axisStart, currentPosition);
2015-12-15 15:55:03 -08:00
if (this.userData.sliderType === 'color_red' || this.userData.sliderType === 'color_green' || this.userData.sliderType === 'color_blue') {
2015-12-20 16:40:41 -08:00
this.sliderValue = this.scaleValueBasedOnDistanceFromStart(distance, 0, COLOR_MAX);
2015-12-14 16:51:12 -08:00
}
2015-12-15 15:55:03 -08:00
if (this.userData.sliderType === 'intensity') {
2015-12-20 16:40:41 -08:00
this.sliderValue = this.scaleValueBasedOnDistanceFromStart(distance, 0, INTENSITY_MAX);
2015-12-14 16:51:12 -08:00
}
2015-12-15 15:55:03 -08:00
if (this.userData.sliderType === 'cutoff') {
2015-12-20 16:40:41 -08:00
this.sliderValue = this.scaleValueBasedOnDistanceFromStart(distance, 0, CUTOFF_MAX);
2015-12-14 16:51:12 -08:00
}
2015-12-15 15:55:03 -08:00
if (this.userData.sliderType === 'exponent') {
2015-12-20 16:40:41 -08:00
this.sliderValue = this.scaleValueBasedOnDistanceFromStart(distance, 0, EXPONENT_MAX);
2015-12-14 18:42:00 -08:00
};
2015-12-12 17:22:57 -08:00
2015-12-15 18:20:12 -08:00
this.sendValueToSlider();
2015-12-12 17:14:43 -08:00
},
releaseGrab: function() {
Entities.editEntity(this.entityID, {
velocity: {
x: 0,
y: 0,
z: 0
2015-12-15 15:55:03 -08:00
},
2015-12-15 18:20:12 -08:00
angularVelocity: {
x: 0,
y: 0,
z: 0
}
2015-12-12 17:14:43 -08:00
})
2015-12-12 17:22:57 -08:00
2015-12-12 17:14:43 -08:00
this.sendValueToSlider();
},
2015-12-20 16:40:41 -08:00
scaleValueBasedOnDistanceFromStart: function(value, min2, max2) {
2015-12-12 17:14:43 -08:00
var min1 = 0;
2015-12-14 17:27:01 -08:00
var max1 = AXIS_SCALE;
2015-12-20 16:40:41 -08:00
var min2 = min2;
var max2 = max2;
2015-12-12 17:14:43 -08:00
return min2 + (max2 - min2) * ((value - min1) / (max1 - min1));
},
sendValueToSlider: function() {
2015-12-15 15:55:03 -08:00
var _t = this;
2015-12-12 17:14:43 -08:00
var message = {
2015-12-15 15:55:03 -08:00
lightID: _t.userData.lightID,
sliderType: _t.userData.sliderType,
sliderValue: _t.sliderValue
2015-12-12 17:14:43 -08:00
}
Messages.sendMessage('Hifi-Slider-Value-Reciever', JSON.stringify(message));
if (_t.userData.sliderType === 'cutoff') {
Messages.sendMessage('entityToolUpdates', 'callUpdate');
}
2015-12-14 18:42:00 -08:00
}
2015-12-12 16:17:00 -08:00
};
2015-12-15 18:20:12 -08:00
return new Slider();
2015-12-12 16:17:00 -08:00
});