This commit is contained in:
Andrej 2019-04-24 10:29:13 +07:00
parent c2edd82e39
commit bff12b89ef

View File

@ -20,6 +20,7 @@ import FunctionController from '../controllers/FunctionController';
import NumberControllerBox from '../controllers/NumberControllerBox'; import NumberControllerBox from '../controllers/NumberControllerBox';
import NumberControllerSlider from '../controllers/NumberControllerSlider'; import NumberControllerSlider from '../controllers/NumberControllerSlider';
import ColorController from '../controllers/ColorController'; import ColorController from '../controllers/ColorController';
import CustomController from '../controllers/CustomController';
import requestAnimationFrame from '../utils/requestAnimationFrame'; import requestAnimationFrame from '../utils/requestAnimationFrame';
import CenteredDiv from '../dom/CenteredDiv'; import CenteredDiv from '../dom/CenteredDiv';
import dom from '../dom/dom'; import dom from '../dom/dom';
@ -556,6 +557,27 @@ common.extend(
); );
}, },
/**
* Adds a new custom controller to the GUI.
*
* @param object
* @param property
* @returns {Controller} The controller that was added to the GUI.
* @instance
*
*/
addCustomController: function(object, property) {
return add(
this,
object,
property,
{
custom: true,
factoryArgs: Array.prototype.slice.call(arguments, 2)
}
);
},
/** /**
* Removes the given controller from the GUI. * Removes the given controller from the GUI.
* @param {Controller} controller * @param {Controller} controller
@ -1131,7 +1153,7 @@ function recallSavedValue(gui, controller) {
} }
function add(gui, object, property, params) { function add(gui, object, property, params) {
if (object[property] === undefined) { if (!params.custom && (object[property] === undefined)) {
throw new Error(`Object "${object}" has no property "${property}"`); throw new Error(`Object "${object}" has no property "${property}"`);
} }
@ -1139,6 +1161,8 @@ function add(gui, object, property, params) {
if (params.color) { if (params.color) {
controller = new ColorController(object, property); controller = new ColorController(object, property);
} else if (params.custom && (object[property] === undefined)) {
controller = new CustomController(object, property);
} else { } else {
const factoryArgs = [object, property].concat(params.factoryArgs); const factoryArgs = [object, property].concat(params.factoryArgs);
controller = ControllerFactory.apply(gui, factoryArgs); controller = ControllerFactory.apply(gui, factoryArgs);
@ -1152,12 +1176,14 @@ function add(gui, object, property, params) {
dom.addClass(controller.domElement, 'c'); dom.addClass(controller.domElement, 'c');
const name = document.createElement('span');
dom.addClass(name, 'property-name');
name.innerHTML = controller.property;
const container = document.createElement('div'); const container = document.createElement('div');
const name = params.custom && ( controller instanceof CustomController === false ) ? new CustomController(object).domElement : document.createElement('span');
if (!params.custom)
name.innerHTML = controller.property;
dom.addClass(name, 'property-name');
container.appendChild(name); container.appendChild(name);
container.appendChild(controller.domElement); container.appendChild(controller.domElement);
const li = addRow(gui, container, params.before); const li = addRow(gui, container, params.before);