From dc7d20ff86a75a6c9228d4dee4646e9645ce8278 Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Sun, 30 Aug 2015 23:49:47 -0700 Subject: [PATCH] Working on ui --- examples/toys/magBalls/ballController.js | 33 +++++++++++- examples/toys/magBalls/handController.js | 32 +++++++++++- examples/toys/magBalls/magBalls.js | 12 +++-- examples/toys/magBalls/magBallsMain.js | 1 - examples/toys/magBalls/menuController.js | 66 ++++++++++++++++++++++++ 5 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 examples/toys/magBalls/menuController.js diff --git a/examples/toys/magBalls/ballController.js b/examples/toys/magBalls/ballController.js index 62e2e0a4d0..0f178b2804 100644 --- a/examples/toys/magBalls/ballController.js +++ b/examples/toys/magBalls/ballController.js @@ -7,8 +7,6 @@ BallController = function(side, magBalls) { this.highlighter = new Highlighter(); this.highlighter.setSize(BALL_SIZE); this.ghostEdges = {}; - this.lastUpdate = 0; - this.updateInterval = 0.05; } BallController.prototype = Object.create( HandController.prototype ); @@ -72,3 +70,34 @@ BallController.prototype.onCleanup = function() { HandController.prototype.onCleanup.call(this); this.clearGhostEdges(); } + +BallController.prototype.onAltClick = function() { + return; + var target = this.magBalls.findNearestNode(this.tipPosition, BALL_SELECTION_RADIUS); + if (!target) { + logDebug(target); + return; + } + + // FIXME move to delete shape + var toDelete = {}; + var deleteQueue = [ target ]; + while (deleteQueue.length) { + var curNode = deleteQueue.shift(); + if (toDelete[curNode]) { + continue; + } + toDelete[curNode] = true; + for (var nodeId in this.magBalls.getConnectedNodes(curNode)) { + deleteQueue.push(nodeId); + } + } + for (var nodeId in toDelete) { + this.magBalls.destroyNode(nodeId); + } +} + + + +BallController.prototype.onAltRelease = function() { +} diff --git a/examples/toys/magBalls/handController.js b/examples/toys/magBalls/handController.js index 3e54c7ed1b..998d22c6f8 100644 --- a/examples/toys/magBalls/handController.js +++ b/examples/toys/magBalls/handController.js @@ -14,6 +14,7 @@ HandController = function(side) { this.palm = 2 * side; this.tip = 2 * side + 1; this.action = findAction(side ? "ACTION2" : "ACTION1"); + this.altAction = findAction(side ? "ACTION1" : "ACTION2"); this.active = false; this.tipScale = 1.4; this.pointer = Overlays.addOverlay("sphere", { @@ -41,6 +42,10 @@ HandController = function(side) { } HandController.prototype.onActionEvent = function(action, state) { + var spatialControlCount = Controller.getNumberOfSpatialControls(); + // If only 2 spacial controls, then we only have one controller active, so use either button + // otherwise, only use the specified action + if (action == this.action) { if (state) { this.onClick(); @@ -48,6 +53,14 @@ HandController.prototype.onActionEvent = function(action, state) { this.onRelease(); } } + + if (action == this.altAction) { + if (state) { + this.onAltClick(); + } else { + this.onAltRelease(); + } + } } HandController.prototype.setActive = function(active) { @@ -65,11 +78,18 @@ HandController.prototype.setActive = function(active) { } HandController.prototype.updateControllerState = function() { + // FIXME this returns data if either the left or right controller is not on the base this.palmPos = Controller.getSpatialControlPosition(this.palm); var tipPos = Controller.getSpatialControlPosition(this.tip); this.tipPosition = scaleLine(this.palmPos, tipPos, this.tipScale); - // When on the base hydras report a position of 0 + // When on the base, hydras report a position of 0 this.setActive(Vec3.length(this.palmPos) > 0.001); + + //logDebug(Controller.getTriggerValue(0) + " " + Controller.getTriggerValue(1)); + + //if (this.active) { + // logDebug("#ctrls " + Controller.getNumberOfSpatialControls() + " Side: " + this.side + " Palm: " + this.palm + " " + vec3toStr(this.palmPos)) + //} } HandController.prototype.onCleanup = function() { @@ -95,3 +115,13 @@ HandController.prototype.onClick = function() { HandController.prototype.onRelease = function() { logDebug("Base hand controller does nothing on release"); } + +HandController.prototype.onAltClick = function() { + logDebug("Base hand controller does nothing on alt click"); +} + +HandController.prototype.onAltRelease = function() { + logDebug("Base hand controller does nothing on alt click"); +} + + diff --git a/examples/toys/magBalls/magBalls.js b/examples/toys/magBalls/magBalls.js index 9e0cbb4982..187c550073 100644 --- a/examples/toys/magBalls/magBalls.js +++ b/examples/toys/magBalls/magBalls.js @@ -15,8 +15,10 @@ Script.include("edgeSpring.js"); MagBalls = function() { Graph.call(this); + this.MAX_ADJUST_ITERATIONS = 100; this.lastUpdateAge = 0; this.stable = false; + this.adjustIterations = 0; this.selectedNodes = {}; this.edgeObjects = {}; @@ -39,6 +41,7 @@ MagBalls.prototype.onUpdate = function(deltaTime) { if (this.lastUpdateAge > UPDATE_INTERVAL) { this.lastUpdateAge = 0; if (!this.stable) { + this.adjustIterations += 1; // logDebug("Update"); var adjusted = false; var nodeAdjustResults = {}; @@ -68,9 +71,10 @@ MagBalls.prototype.onUpdate = function(deltaTime) { } }, ((UPDATE_INTERVAL * 1000) / 2)); - if (!adjusted) { + if (!adjusted || this.adjustIterations > this.MAX_ADJUST_ITERATIONS) { + this.adjustIterations = 0; this.stable = true; - } + } } } } @@ -129,6 +133,7 @@ MagBalls.prototype.grabBall = function(position, maxDist) { selected = this.createNode({ position: position }); } if (selected) { + this.stable = true; this.breakEdges(selected); this.selectedNodes[selected] = true; } @@ -159,12 +164,11 @@ MagBalls.prototype.releaseBall = function(releasedBall) { var targets = this.findPotentialEdges(releasedBall); if (!targets || !Object.keys(targets).length) { - this.destroyNode(releasedBall); +// this.destroyNode(releasedBall); } for (var otherBallId in targets) { this.createEdge(otherBallId, releasedBall); } -// this.clean(); this.validate(); } diff --git a/examples/toys/magBalls/magBallsMain.js b/examples/toys/magBalls/magBallsMain.js index 1c6bd2b159..e54b818e4a 100644 --- a/examples/toys/magBalls/magBallsMain.js +++ b/examples/toys/magBalls/magBallsMain.js @@ -21,6 +21,5 @@ MenuController = function(side) { HandController.call(this, side); } - // FIXME resolve some of the issues with dual controllers before allowing both controllers active var handControllers = [new BallController(LEFT_CONTROLLER, magBalls)]; //, new HandController(RIGHT) ]; diff --git a/examples/toys/magBalls/menuController.js b/examples/toys/magBalls/menuController.js new file mode 100644 index 0000000000..0a076d1ff8 --- /dev/null +++ b/examples/toys/magBalls/menuController.js @@ -0,0 +1,66 @@ +Script.include("handController.js"); + +MenuController = function(side, magBalls) { + HandController.call(this, side); +} + +MenuController.prototype = Object.create( HandController.prototype ); + +MenuController.prototype.onUpdate = function(deltaTime) { + HandController.prototype.onUpdate.call(this, deltaTime); + if (!this.selected) { + // Find the highlight target and set it. + var target = this.magBalls.findNearestNode(this.tipPosition, BALL_SELECTION_RADIUS); + this.highlighter.highlight(target); + return; + } + this.highlighter.highlight(null); + Entities.editEntity(this.selected, { position: this.tipPosition }); + var targetBalls = this.magBalls.findPotentialEdges(this.selected); + for (var ballId in targetBalls) { + if (!this.ghostEdges[ballId]) { + // create the ovleray + this.ghostEdges[ballId] = Overlays.addOverlay("line3d", { + start: this.magBalls.getNodePosition(ballId), + end: this.tipPosition, + color: COLORS.RED, + alpha: 1, + lineWidth: 5, + visible: true, + }); + } else { + Overlays.editOverlay(this.ghostEdges[ballId], { + end: this.tipPosition, + }); + } + } + for (var ballId in this.ghostEdges) { + if (!targetBalls[ballId]) { + Overlays.deleteOverlay(this.ghostEdges[ballId]); + delete this.ghostEdges[ballId]; + } + } +} + +MenuController.prototype.onClick = function() { + this.selected = this.magBalls.grabBall(this.tipPosition, BALL_SELECTION_RADIUS); + this.highlighter.highlight(null); +} + +MenuController.prototype.onRelease = function() { + this.clearGhostEdges(); + this.magBalls.releaseBall(this.selected); + this.selected = null; +} + +MenuController.prototype.clearGhostEdges = function() { + for(var ballId in this.ghostEdges) { + Overlays.deleteOverlay(this.ghostEdges[ballId]); + delete this.ghostEdges[ballId]; + } +} + +MenuController.prototype.onCleanup = function() { + HandController.prototype.onCleanup.call(this); + this.clearGhostEdges(); +}