dat.gui/src/dat/controllers/NumberController.js

139 lines
3.7 KiB
JavaScript
Raw Normal View History

2011-11-07 21:29:37 +00:00
/**
* dat-gui JavaScript Controller Library
* http://code.google.com/p/dat-gui
*
* Copyright 2011 Data Arts Team, Google Creative Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
2015-08-14 15:28:30 +00:00
import Controller from './Controller';
import common from '../utils/common';
2015-08-14 13:24:30 +00:00
/**
* @class Represents a given property of an object that is a number.
*
* @extends dat.controllers.Controller
*
* @param {Object} object The object to be manipulated
* @param {string} property The name of the property to be manipulated
* @param {Object} [params] Optional parameters
* @param {Number} [params.min] Minimum allowed value
* @param {Number} [params.max] Maximum allowed value
* @param {Number} [params.step] Increment by which to change value
*
* @member dat.controllers
*/
var NumberController = function(object, property, params) {
2011-11-07 21:29:37 +00:00
NumberController.superclass.call(this, object, property);
params = params || {};
this.__min = params.min;
this.__max = params.max;
this.__step = params.step;
if (common.isUndefined(this.__step)) {
2015-08-14 13:24:30 +00:00
if (this.initialValue == 0) {
this.__impliedStep = 1; // What are we, psychics?
} else {
// Hey Doug, check this out.
this.__impliedStep = Math.pow(10, Math.floor(Math.log(Math.abs(this.initialValue))/Math.LN10))/10;
}
2011-11-07 21:29:37 +00:00
} else {
2015-08-14 13:24:30 +00:00
this.__impliedStep = this.__step;
2011-11-07 21:29:37 +00:00
}
this.__precision = numDecimals(this.__impliedStep);
2015-08-14 13:24:30 +00:00
};
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
NumberController.superclass = Controller;
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
common.extend(
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
NumberController.prototype,
Controller.prototype,
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
/** @lends dat.controllers.NumberController.prototype */
{
2011-11-07 21:29:37 +00:00
setValue: function(v) {
2015-08-14 13:24:30 +00:00
if (this.__min !== undefined && v < this.__min) {
v = this.__min;
} else if (this.__max !== undefined && v > this.__max) {
v = this.__max;
}
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
if (this.__step !== undefined && v % this.__step != 0) {
v = Math.round(v / this.__step) * this.__step;
}
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
return NumberController.superclass.prototype.setValue.call(this, v);
2011-11-07 21:29:37 +00:00
},
/**
* Specify a minimum value for <code>object[property]</code>.
*
* @param {Number} minValue The minimum value for
* <code>object[property]</code>
* @returns {dat.controllers.NumberController} this
*/
min: function(v) {
2015-08-14 13:24:30 +00:00
this.__min = v;
return this;
2011-11-07 21:29:37 +00:00
},
/**
* Specify a maximum value for <code>object[property]</code>.
*
* @param {Number} maxValue The maximum value for
* <code>object[property]</code>
* @returns {dat.controllers.NumberController} this
*/
max: function(v) {
2015-08-14 13:24:30 +00:00
this.__max = v;
return this;
2011-11-07 21:29:37 +00:00
},
/**
* Specify a step value that dat.controllers.NumberController
* increments by.
*
* @param {Number} stepValue The step value for
* dat.controllers.NumberController
* @default if minimum and maximum specified increment is 1% of the
* difference otherwise stepValue is 1
* @returns {dat.controllers.NumberController} this
*/
step: function(v) {
2015-08-14 13:24:30 +00:00
this.__step = v;
this.__impliedStep = v;
this.__precision = numDecimals(v);
return this;
2011-11-07 21:29:37 +00:00
}
2015-08-14 13:24:30 +00:00
}
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
);
2011-11-07 21:29:37 +00:00
2015-08-14 13:24:30 +00:00
function numDecimals(x) {
2011-11-07 21:29:37 +00:00
x = x.toString();
if (x.indexOf('.') > -1) {
2015-08-14 13:24:30 +00:00
return x.length - x.indexOf('.') - 1;
2011-11-07 21:29:37 +00:00
} else {
2015-08-14 13:24:30 +00:00
return 0;
2011-11-07 21:29:37 +00:00
}
2015-08-14 13:24:30 +00:00
}
2011-11-07 21:29:37 +00:00
2015-08-14 15:28:30 +00:00
export default NumberController;