Skip to content

Customcontroller #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
522 changes: 273 additions & 249 deletions build/dat.gui.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/dat.gui.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/dat.gui.min.js

Large diffs are not rendered by default.

522 changes: 273 additions & 249 deletions build/dat.gui.module.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/dat.gui.module.js.map

Large diffs are not rendered by default.

2,010 changes: 1,005 additions & 1,005 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions src/dat/controllers/CustomController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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
*/
import Controller from './Controller';

/**
* @class Represents a custom controller.
* @param {Object} object
* @param {string} property
*/
class CustomController extends Controller{
constructor(object, property) {
super(object, property);

object.constructor( this );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this line is doing?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My current version of the CustomController class:

/**
 * @class Represents a custom controller.
 * @param {init} callback function for adding of elements into this.domElement
 */
class CustomController extends Controller{
    constructor(init) {
    super({});

    init( this );
    this.custom = true;
  }
}

I have created my own ES module for testing and use it in my Example . The code below is example of user's custom controller class.

import { GUI, controllers } from '../../dat.gui';

export class KnobController extends controllers.CustomController {
	constructor( a, b ) {
		super(function (controller) {

			var button = document.createElement('span');
			button.innerHTML = 'Knob Controller';
			button.title = 'Please press knob';
			button.style.cursor = 'pointer';
			button.style.margin = '0px 2px';
			button.onclick = function (value) {

				alert('Knob Controller ' + ( knobController.a + knobController.b ));

			}
			controller.domElement.appendChild(button);

		});
		this.a = a;
		this.b = b;
		var knobController = this;

	}
}

}
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your example

var controllerPlayRate1 = folder1.add( new dat.controllers.CustomController( function ( controller ) {

  controllerPlay( controller, buttons1 );

} ),
{

  playRate: 1,

}, 'playRate', 1, 25, 1 ).onChange( function ( value ) {

  onChangePlayRate( value );

} );

I'd rather have an API where custom controllers can be used more similarly to the builtin controller types. Users would not instantiate CustomController directly (i.e. it's an "abstract" class) but would instantiate subtypes. For example, they should do this:

class KnobController extends CustomController {
  constructor ( object, name, ...opts ) {
    super( object, name );
    // ... set up options if needed
  }
}

const api = {
  color: '#ffffff',
  value: 0.5
};

const gui = new dat.GUI();
gui.add( api, 'color' );
gui.add( new KnobController( api, 'value' ) );

I believe that can be implemented without requiring an init callback, and without needing a addCustomController method (since add works just as well).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see my new version of the CustomController.js as you asked me. I have rewired my own ES module for testing and use it in my example.

export default CustomController;
47 changes: 40 additions & 7 deletions src/dat/gui/GUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import FunctionController from '../controllers/FunctionController';
import NumberControllerBox from '../controllers/NumberControllerBox';
import NumberControllerSlider from '../controllers/NumberControllerSlider';
import ColorController from '../controllers/ColorController';
import CustomController from '../controllers/CustomController';
import requestAnimationFrame from '../utils/requestAnimationFrame';
import CenteredDiv from '../dom/CenteredDiv';
import dom from '../dom/dom';
Expand Down Expand Up @@ -455,6 +456,8 @@ const GUI = function(pars) {
}
};

GUI.CustomController = CustomController;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not assign this to the GUI object. Instead we can export CustomController from the module, so it can be used as:

import { GUI, controllers } from 'dat.gui';

class FooController extends controllers.CustomController {}

See https://github.com/dataarts/dat.gui/blob/master/src/dat/index.js.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand you. I can not include
import { GUI, controllers } from 'dat.gui';
line into webgl_custom_controller.html file.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you aren't using ES modules, it would just be: dat.controllers.CustomController in this case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Now I want to continue researching of your replies and want to rewrite my code

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed GUI.CustomController. See Remove GUI.CustomController.
I want to continue researching of your replies and want to rewrite my code


GUI.toggleHide = function() {
hide = !hide;
common.each(hideableGuis, function(gui) {
Expand Down Expand Up @@ -520,6 +523,7 @@ common.extend(
object,
property,
{
custom: object instanceof CustomController,
factoryArgs: Array.prototype.slice.call(arguments, 2)
}
);
Expand Down Expand Up @@ -557,6 +561,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep this simple, maybe let's only support gui.add( controller ) for now?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The addCustomController function is the easiest way of using of custom controller. It is ready and I do not want to remove it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #232 (comment) – I think that add can be just as easy, but if not could you share examples showing how to use them both? It doesn't seem to me like we need both, if add can be simplified.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed addCustomController from my code

return add(
this,
object,
property,
{
custom: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the custom: true parameter, if we can already check that the object is an instance of CustomController?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have removed it

factoryArgs: Array.prototype.slice.call(arguments, 2)
}
);
},

/**
* Removes the given controller from the GUI.
* @param {Controller} controller
* @instance
Expand Down Expand Up @@ -1131,16 +1156,21 @@ function recallSavedValue(gui, controller) {
}

function add(gui, object, property, params) {
if (object[property] === undefined) {
if (!object instanceof CustomController && !params.custom && (object[property] === undefined)) {
throw new Error(`Object "${object}" has no property "${property}"`);
}

let controller;

if (params.color) {
controller = new ColorController(object, property);
} else {
const factoryArgs = [object, property].concat(params.factoryArgs);
} else if(object instanceof CustomController && ( property === undefined )){
controller = object;
} else if (!(object instanceof CustomController) && params.custom && (object[property] === undefined)) {
controller = new CustomController(object, property);
}else {
const factoryArgs = object instanceof CustomController ?
[property].concat(params.factoryArgs) : [object, property].concat(params.factoryArgs);
controller = ControllerFactory.apply(gui, factoryArgs);
}

Expand All @@ -1152,12 +1182,15 @@ function add(gui, object, property, params) {

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 name = params.custom && ( controller instanceof CustomController === false ) ?
( object instanceof CustomController ? object.domElement : new CustomController(object).domElement ) : document.createElement('span');
if (!params.custom)
name.innerHTML = controller.property;
dom.addClass(name, 'property-name');
container.appendChild(name);

container.appendChild(controller.domElement);

const li = addRow(gui, container, params.before);
Expand Down
4 changes: 3 additions & 1 deletion src/dat/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import NumberControllerBox from './controllers/NumberControllerBox';
import NumberControllerSlider from './controllers/NumberControllerSlider';
import FunctionController from './controllers/FunctionController';
import ColorController from './controllers/ColorController';
import CustomController from './controllers/CustomController';

import domImport from './dom/dom';
import GUIImport from './gui/GUI';
Expand All @@ -43,7 +44,8 @@ export const controllers = {
NumberControllerBox: NumberControllerBox,
NumberControllerSlider: NumberControllerSlider,
FunctionController: FunctionController,
ColorController: ColorController
ColorController: ColorController,
CustomController: CustomController
};

export const dom = { dom: domImport };
Expand Down
17 changes: 17 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
var NumberControllerSlider = dat.controllers.NumberControllerSlider;
var FunctionController = dat.controllers.FunctionController;
var ColorController = dat.controllers.ColorController;
var CustomController = dat.controllers.CustomController;
var GUI = dat.gui.GUI;


Expand Down Expand Up @@ -863,6 +864,22 @@
document.body.removeChild(c1.domElement);
});
*/
module("CustomController");
test("Get Custom", function () {
var value = 'value',
c1 = new CustomController({
constructor: function (controller) {
var input = document.createElement('input');
input.value = value;
controller.domElement.appendChild(input);
}
});
document.body.appendChild(c1.domElement);
var input = c1.domElement.getElementsByTagName("input")[0];
equal(input.value, value , "Input value is the same as the " + value);
document.body.removeChild(c1.domElement);
});

module("Controller Events");

test("onChange", function() {
Expand Down