-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: master
Are you sure you want to change the base?
Customcontroller #232
Changes from 4 commits
bff12b8
fc465bf
ced2ce7
3f46a30
c711fc7
9c86835
8f8b062
271cba9
880a802
e56ba62
55b4013
f976ef0
462c59d
cbcd3d5
cfa3631
3d52787
7f5abea
7a68784
3bf8a33
292a826
70d70b7
55adefe
e091554
624d0ae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Large diffs are not rendered by default.
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 ); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -455,6 +456,8 @@ const GUI = function(pars) { | |
} | ||
}; | ||
|
||
GUI.CustomController = CustomController; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's not assign this to the import { GUI, controllers } from 'dat.gui';
class FooController extends controllers.CustomController {} See https://github.com/dataarts/dat.gui/blob/master/src/dat/index.js. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I don't understand you. I can not include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you aren't using ES modules, it would just be: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have removed GUI.CustomController. See Remove GUI.CustomController. |
||
|
||
GUI.toggleHide = function() { | ||
hide = !hide; | ||
common.each(hideableGuis, function(gui) { | ||
|
@@ -520,6 +523,7 @@ common.extend( | |
object, | ||
property, | ||
{ | ||
custom: object instanceof CustomController, | ||
factoryArgs: Array.prototype.slice.call(arguments, 2) | ||
} | ||
); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep this simple, maybe let's only support There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See #232 (comment) – I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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); | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
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.