Number controllers not perfect, but dragging is infinitely more intuitive.

This commit is contained in:
George Michael Brower 2011-04-18 14:57:32 -07:00
parent 9c5cd9a919
commit 9959a24161
5 changed files with 59 additions and 28 deletions

View File

@ -119,7 +119,9 @@
<p><strong>dat.gui</strong> is a lightweight controller library for JavaScript. It allows you to easily manipulate variables and fire functions on the fly.</p> <p><strong>dat.gui</strong> is a lightweight controller library for JavaScript. It allows you to easily manipulate variables and fire functions on the fly.</p>
<ul> <ul>
<li><a href="https://github.com/jonobr1/dat.gui/raw/versions/gui.min.js"><strong>Download the minified source</strong></a> <small>[11kb]</small></li> <li><a href="https://github.com/jonobr1/dat.gui/raw/versions/gui.min
.js"><strong>Download the minified source</strong></a> <small
id="buildsize">[11kb]</small></li>
<li><a href="http://github.com/jonobr1/dat.gui">Contribute on GitHub!</a></li> <li><a href="http://github.com/jonobr1/dat.gui">Contribute on GitHub!</a></li>
</ul> </ul>

View File

@ -12,6 +12,9 @@ DAT.GUI.ControllerNumber = function() {
var clickedNumberField = false; var clickedNumberField = false;
var draggingHorizontal = false;
var draggingVertical = false;
var y = 0, py = 0; var y = 0, py = 0;
var min = arguments[3]; var min = arguments[3];
@ -45,8 +48,7 @@ DAT.GUI.ControllerNumber = function() {
numberField.addEventListener('blur', function() { numberField.addEventListener('blur', function() {
var val = parseFloat(this.value); var val = parseFloat(this.value);
if (slider) { if (slider) {
DAT.GUI.removeClass(slider.domElement, 'active'); DAT.GUI.removeClass(_this.domElement, 'active');
console.log(slider.domElement.className);
} }
if (!isNaN(val)) { if (!isNaN(val)) {
_this.setValue(val); _this.setValue(val);
@ -64,7 +66,8 @@ DAT.GUI.ControllerNumber = function() {
py = y = e.pageY; py = y = e.pageY;
clickedNumberField = true; clickedNumberField = true;
if (slider) { if (slider) {
DAT.GUI.addClass(slider.domElement, 'active'); DAT.GUI.addClass(_this.domElement, 'active');
console.log(_this.domElement.className);
} }
document.addEventListener('mousemove', dragNumberField, false); document.addEventListener('mousemove', dragNumberField, false);
document.addEventListener('mouseup', mouseup, false); document.addEventListener('mouseup', mouseup, false);
@ -91,7 +94,8 @@ DAT.GUI.ControllerNumber = function() {
var mouseup = function(e) { var mouseup = function(e) {
document.removeEventListener('mousemove', dragNumberField, false); document.removeEventListener('mousemove', dragNumberField, false);
DAT.GUI.makeSelectable(_this.parent.domElement);
DAT.GUI.makeSelectable(numberField);
if (clickedNumberField && !draggedNumberField) { if (clickedNumberField && !draggedNumberField) {
numberField.focus(); numberField.focus();
numberField.select(); numberField.select();
@ -101,23 +105,37 @@ DAT.GUI.ControllerNumber = function() {
if (_this.finishChangeFunction != null) { if (_this.finishChangeFunction != null) {
_this.finishChangeFunction.call(this, _this.getValue()); _this.finishChangeFunction.call(this, _this.getValue());
} }
draggingHorizontal = false;
draggingVertical = false;
document.removeEventListener('mouseup', mouseup, false); document.removeEventListener('mouseup', mouseup, false);
}; };
var dragNumberField = function(e) { var dragNumberField = function(e) {
draggedNumberField = true;
e.preventDefault();
// We don't want to be highlighting this field as we scroll.
// Or any other fields in this gui for that matter ...
// TODO: Make makeUselectable go through each element and child element.
DAT.GUI.makeUnselectable(_this.parent.domElement);
py = y; py = y;
y = e.pageY; y = e.pageY;
var dy = py - y; var dy = py - y;
if (!draggingHorizontal && !draggingVertical) {
if (dy == 0) {
draggingHorizontal = true;
} else {
draggingVertical = true;
}
}
if (draggingHorizontal) {
return true;
}
DAT.GUI.makeUnselectable(_this.parent.domElement);
DAT.GUI.makeUnselectable(numberField);
draggedNumberField = true;
e.preventDefault();
var newVal = _this.getValue() + dy * step; var newVal = _this.getValue() + dy * step;
_this.setValue(newVal); _this.setValue(newVal);
return false; return false;

View File

@ -25,17 +25,13 @@ DAT.GUI.Slider = function(numberController, min, max, step, initValue) {
this.domElement.addEventListener('mousedown', function(e) { this.domElement.addEventListener('mousedown', function(e) {
clicked = true; clicked = true;
x = px = e.pageX; x = px = e.pageX;
_this.domElement.className += ' active'; DAT.GUI.addClass(numberController.domElement, 'active');
_this.fg.className += ' active';
numberController.domElement.className += ' active';
onDrag(e); onDrag(e);
document.addEventListener('mouseup', mouseup, false); document.addEventListener('mouseup', mouseup, false);
}, false); }, false);
var mouseup = function(e) { var mouseup = function(e) {
_this.domElement.className = _this.domElement.className.replace(' active', ''); DAT.GUI.removeClass(numberController.domElement, 'active');
_this.fg.className = _this.fg.className.replace(' active', '');
numberController.domElement.className = numberController.domElement.className.replace(' active', '');
clicked = false; clicked = false;
if (numberController.finishChangeFunction != null) { if (numberController.finishChangeFunction != null) {
numberController.finishChangeFunction.call(this, numberController.getValue()); numberController.finishChangeFunction.call(this, numberController.getValue());

View File

@ -80,13 +80,14 @@ a.guidat-toggle:hover {
float: right; float: right;
} }
.guidat-controller input:hover .guidat-controller input:hover {
.guidat-controller.number.active {
background-color: #444; background-color: #444;
} }
.guidat-controller input:focus { .guidat-controller input:focus,
.guidat-controller.active input {
background-color: #555; background-color: #555;
color: #fff;
} }
.guidat-controller.number { .guidat-controller.number {
@ -139,13 +140,13 @@ a.guidat-toggle:hover {
display: inline-block; display: inline-block;
} }
.guidat-slider-bg:hover, .guidat-controller .guidat-slider-bg:hover,
.guidat-slider-bg.active { .guidat-controller.active .guidat-slider-bg {
background-color: #444; background-color: #444;
} }
.guidat-slider-bg:hover .guidat-slider-fg, .guidat-controller .guidat-slider-bg .guidat-slider-fg:hover,
.guidat-slider-bg.active .guidat-slider-fg { .guidat-controller.active .guidat-slider-bg .guidat-slider-fg {
background-color: #52c8ff; background-color: #52c8ff;
} }
@ -161,5 +162,5 @@ a.guidat-toggle:hover {
.guidat-slider-fg { .guidat-slider-fg {
cursor: ew-resize; cursor: ew-resize;
background-color: #00aeff; background-color: #00aeff;
height: 20px; height: 21px;
} }

View File

@ -605,20 +605,34 @@ DAT.GUI.showSaveString = function() {
// Util functions // Util functions
DAT.GUI.makeUnselectable = function(elem) { DAT.GUI.makeUnselectable = function(elem) {
if (elem == undefined || elem.style == undefined) return;
elem.onselectstart = function() { elem.onselectstart = function() {
return false; return false;
}; };
elem.style.MozUserSelect = 'none'; elem.style.MozUserSelect = 'none';
elem.style.KhtmlUserSelect = 'none'; elem.style.KhtmlUserSelect = 'none';
elem.unselectable = 'on'; elem.unselectable = 'on';
var kids = elem.childNodes;
for (var i = 0; i < kids.length; i++) {
DAT.GUI.makeUnselectable(kids[i]);
}
}; };
DAT.GUI.makeSelectable = function(elem) { DAT.GUI.makeSelectable = function(elem) {
if (elem == undefined || elem.style == undefined) return;
elem.onselectstart = function() { elem.onselectstart = function() {
}; };
elem.style.MozUserSelect = 'auto'; elem.style.MozUserSelect = 'auto';
elem.style.KhtmlUserSelect = 'auto'; elem.style.KhtmlUserSelect = 'auto';
elem.unselectable = 'off'; elem.unselectable = 'off';
var kids = elem.childNodes;
for (var i = 0; i < kids.length; i++) {
DAT.GUI.makeSelectable(kids[i]);
}
}; };
DAT.GUI.map = function(v, i1, i2, o1, o2) { DAT.GUI.map = function(v, i1, i2, o1, o2) {