From da6986f759860fa619a1d7b9a98ef0d8e60050aa Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 7 Jul 2015 21:06:12 +0200 Subject: [PATCH 01/12] Planky refactorings and basic planky settings --- examples/example/games/planky.js | 282 +++++++++++++++++++++--------- examples/html/plankySettings.html | 112 ++++++++++++ 2 files changed, 312 insertions(+), 82 deletions(-) create mode 100644 examples/html/plankySettings.html diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 00a4e7f61d..cd4a7295be 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -12,23 +12,28 @@ // HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; -const NUM_LAYERS = 16; -const BASE_DIMENSION = { x: 7, y: 2, z: 7 }; -const BLOCKS_PER_LAYER = 3; -const BLOCK_SIZE = {x: 0.2, y: 0.1, z: 0.8}; -const BLOCK_SPACING = BLOCK_SIZE.x / 3; +const DEFAULT_NUM_LAYERS = 16; +const DEFAULT_BASE_DIMENSION = { x: 7, y: 2, z: 7 }; +const DEFAULT_BLOCKS_PER_LAYER = 3; +const DEFAULT_BLOCK_SIZE = {x: 0.2, y: 0.1, z: 0.8}; +const DEFAULT_BLOCK_SPACING = DEFAULT_BLOCK_SIZE.x / DEFAULT_BLOCKS_PER_LAYER; // BLOCK_HEIGHT_VARIATION removes a random percentages of the default block height per block. (for example 0.001 %) -const BLOCK_HEIGHT_VARIATION = 0.001; -const GRAVITY = {x: 0, y: -2.8, z: 0}; -const DENSITY = 2000; -const DAMPING_FACTOR = 0.98; -const ANGULAR_DAMPING_FACTOR = 0.8; -const FRICTION = 0.99; -const RESTITUTION = 0.0; -const SPAWN_DISTANCE = 3; -const BLOCK_YAW_OFFSET = 45; +const DEFAULT_BLOCK_HEIGHT_VARIATION = 0.001; +const DEFAULT_GRAVITY = {x: 0, y: -2.8, z: 0}; +const DEFAULT_DENSITY = 2000; +const DEFAULT_DAMPING_FACTOR = 0.98; +const DEFAULT_ANGULAR_DAMPING_FACTOR = 0.8; +const DEFAULT_FRICTION = 0.99; +const DEFAULT_RESTITUTION = 0.0; +const DEFAULT_SPAWN_DISTANCE = 3; +const DEFAULT_BLOCK_YAW_OFFSET = 45; + +var editMode = false; + const BUTTON_DIMENSIONS = {width: 49, height: 49}; const MAXIMUM_PERCENTAGE = 100.0; +const NO_ANGLE = 0; +const RIGHT_ANGLE = 90; var windowWidth = Window.innerWidth; var size; @@ -36,6 +41,163 @@ var pieces = []; var ground = false; var layerRotated = false; +SettingsWindow = function() { + this.webWindow = null; + this.init = function() { + this.webWindow = new WebWindow('Planky', Script.resolvePath('../../html/plankySettings.html'), 255, 500, true); + this.webWindow.setVisible(false); + }; +}; + +PlankyOptions = function() { + var _this = this; + this.save = function() { + //TODO: save Planky Options here. + }; + this.load = function() { + //TODO: load Planky Options here. + }; + this.setDefaults = function() { + _this.numLayers = DEFAULT_NUM_LAYERS; + _this.baseDimension = DEFAULT_BASE_DIMENSION; + _this.blocksPerLayer = DEFAULT_BLOCKS_PER_LAYER; + _this.blockSize = DEFAULT_BLOCK_SIZE; + _this.blockSpacing = DEFAULT_BLOCK_SPACING; + _this.blockHeightVariation = DEFAULT_BLOCK_HEIGHT_VARIATION; + _this.gravity = DEFAULT_GRAVITY; + _this.density = DEFAULT_DENSITY; + _this.dampingFactor = DEFAULT_DAMPING_FACTOR; + _this.angularDampingFactor = DEFAULT_ANGULAR_DAMPING_FACTOR; + _this.friction = DEFAULT_FRICTION; + _this.restitution = DEFAULT_RESTITUTION; + _this.spawnDistance = DEFAULT_SPAWN_DISTANCE; + _this.blockYawOffset = DEFAULT_BLOCK_YAW_OFFSET; + }; + this.setDefaults(); +}; + +// The PlankyStack exists out of rows and layers +PlankyStack = function() { + var _this = this; + this.planks = []; + this.ground = false; + this.options = new PlankyOptions(); + this.deRez = function() { + _this.planks.forEach(function(plank) { + Entities.deleteEntity(plank.entity); + }); + _this.planks = []; + if (_this.ground) { + Entities.deleteEntity(_this.ground); + } + _this.ground = false; + }; + this.rez = function() { + if (_this.planks.length > 0) { + _this.deRez(); + } + _this.baseRotation = Quat.fromPitchYawRollDegrees(0.0, MyAvatar.bodyYaw, 0.0); + var basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(_this.options.spawnDistance, Quat.getFront(_this.baseRotation))); + basePosition.y = grabLowestJointY(); + _this.basePosition = basePosition; + _this.refresh(); + }; + + //private function + var refreshGround = function() { + if (!_this.ground) { + _this.ground = Entities.addEntity({ + type: 'Model', + modelURL: HIFI_PUBLIC_BUCKET + 'eric/models/woodFloor.fbx', + dimensions: _this.options.baseDimension, + position: Vec3.sum(_this.basePosition, {y: -(_this.options.baseDimension.y / 2)}), + rotation: _this.baseRotation, + shapeType: 'box' + }); + return; + } + // move ground to rez position/rotation + Entities.editEntity(_this.ground, {dimensions: _this.options.baseDimension, position: Vec3.sum(_this.basePosition, {y: -(_this.options.baseDimension.y / 2)}), rotation: _this.baseRotation}); + } + var trimDimension = function(dimension, maxIndex) { + _this.planks.forEach(function(plank, index, object) { + if (plank[dimension] > maxIndex) { + Entities.deleteEntity(plank.entity); + object.splice(index, 1); + } + }); + }; + var createOrUpdate = function(layer, row) { + var found = false; + var layerRotated = layer % 2 === 0; + var offset = -(_this.options.blockSpacing); + var layerRotation = Quat.fromPitchYawRollDegrees(0, layerRotated ? NO_ANGLE : RIGHT_ANGLE, 0.0); + var blockPositionXZ = (_this.options.blockSize.x * row) - (((_this.options.blockSize.x * _this.options.blocksPerLayer) / 2) - (_this.options.blockSize.x / 2)); + var localTransform = Vec3.multiplyQbyV(_this.offsetRot, { + x: (layerRotated ? blockPositionXZ + offset: 0), + y: (_this.options.blockSize.y / 2) + (_this.options.blockSize.y * layer), + z: (layerRotated ? 0 : blockPositionXZ + offset) + }); + var newProperties = { + type: 'Model', + modelURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/block.fbx', + shapeType: 'box', + name: 'PlankyBlock' + layer + '-' + row, + dimensions: Vec3.sum(_this.options.blockSize, {x: 0, y: -((_this.options.blockSize.y * (_this.options.blockHeightVariation / MAXIMUM_PERCENTAGE)) * Math.random()), z: 0}), + position: Vec3.sum(_this.basePosition, localTransform), + rotation: Quat.multiply(layerRotation, _this.offsetRot), + //collisionsWillMove: false,//!editMode,//false,//!editMode, + damping: _this.options.dampingFactor, + restitution: _this.options.restitution, + friction: _this.options.friction, + angularDamping: _this.options.angularDampingFactor, + gravity: _this.options.gravity, + density: _this.options.density, + velocity: {x: 0, y: 0, z: 0}, + angularVelocity: Quat.fromPitchYawRollDegrees(0, 0, 0), + ignoreForCollisions: true + }; + _this.planks.forEach(function(plank, index, object) { + if (plank.layer === layer && plank.row === row) { + Entities.editEntity(plank.entity, newProperties); + found = true; + // break loop: + return false; + } + }); + if (!found) { + _this.planks.push({layer: layer, row: row, entity: Entities.addEntity(newProperties)}) + } + }; + this.refresh = function() { + refreshGround(); + trimDimension('layer', _this.options.numLayers); + trimDimension('row', _this.options.blocksPerLayer); + _this.offsetRot = Quat.multiply(_this.baseRotation, Quat.fromPitchYawRollDegrees(0.0, _this.options.blockYawOffset, 0.0)); + for (var layer = 0; layer < _this.options.numLayers; layer++) { + for (var row = 0; row < _this.options.blocksPerLayer; row++) { + createOrUpdate(layer, row); + } + } + if (!editMode) { + _this.planks.forEach(function(plank, index, object) { + print(index + " , " + plank); + Entities.editEntity(plank.entity, {ignoreForCollisions: false, collisionsWillMove: true}); + // Entities.editEntity(plank.entity, {collisionsWillMove: true}); + }); + } + }; + this.isFound = function() { + //TODO: identify entities here until one is found + return _this.planks.length > 0; + }; +}; + +var settingsWindow = new SettingsWindow(); +settingsWindow.init(); + +var plankyStack = new PlankyStack(); + function grabLowestJointY() { var jointNames = MyAvatar.getJointNames(); var floorY = MyAvatar.position.y; @@ -51,6 +213,10 @@ function getButtonPosX() { return windowWidth - ((BUTTON_DIMENSIONS.width / 2) + BUTTON_DIMENSIONS.width); } +function getCogButtonPosX() { + return getButtonPosX() - (BUTTON_DIMENSIONS.width * 1.1); +} + var button = Overlays.addOverlay('image', { x: getButtonPosX(), y: 10, @@ -60,73 +226,26 @@ var button = Overlays.addOverlay('image', { alpha: 0.8 }); - -function resetBlocks() { - pieces.forEach(function(piece) { - Entities.deleteEntity(piece); - }); - pieces = []; - var avatarRot = Quat.fromPitchYawRollDegrees(0.0, MyAvatar.bodyYaw, 0.0); - basePosition = Vec3.sum(MyAvatar.position, Vec3.multiply(SPAWN_DISTANCE, Quat.getFront(avatarRot))); - basePosition.y = grabLowestJointY() - (BASE_DIMENSION.y / 2); - if (!ground) { - ground = Entities.addEntity({ - type: 'Model', - modelURL: HIFI_PUBLIC_BUCKET + 'eric/models/woodFloor.fbx', - dimensions: BASE_DIMENSION, - position: basePosition, - rotation: avatarRot, - shapeType: 'box' - }); - } else { - Entities.editEntity(ground, {position: basePosition, rotation: avatarRot}); - } - var offsetRot = Quat.multiply(avatarRot, Quat.fromPitchYawRollDegrees(0.0, BLOCK_YAW_OFFSET, 0.0)); - basePosition.y += (BASE_DIMENSION.y / 2); - for (var layerIndex = 0; layerIndex < NUM_LAYERS; layerIndex++) { - var layerRotated = layerIndex % 2 === 0; - var offset = -(BLOCK_SPACING); - var layerRotation = Quat.fromPitchYawRollDegrees(0, layerRotated ? 0 : 90, 0.0); - for (var blockIndex = 0; blockIndex < BLOCKS_PER_LAYER; blockIndex++) { - var blockPositionXZ = BLOCK_SIZE.x * blockIndex - (BLOCK_SIZE.x * 3 / 2 - BLOCK_SIZE.x / 2); - var localTransform = Vec3.multiplyQbyV(offsetRot, { - x: (layerRotated ? blockPositionXZ + offset: 0), - y: (BLOCK_SIZE.y / 2) + (BLOCK_SIZE.y * layerIndex), - z: (layerRotated ? 0 : blockPositionXZ + offset) - }); - pieces.push(Entities.addEntity({ - type: 'Model', - modelURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/block.fbx', - shapeType: 'box', - name: 'PlankyBlock' + ((layerIndex * BLOCKS_PER_LAYER) + blockIndex), - dimensions: { - x: BLOCK_SIZE.x, - y: BLOCK_SIZE.y - ((BLOCK_SIZE.y * (BLOCK_HEIGHT_VARIATION / MAXIMUM_PERCENTAGE)) * Math.random()), - z: BLOCK_SIZE.z - }, - position: { - x: basePosition.x + localTransform.x, - y: basePosition.y + localTransform.y, - z: basePosition.z + localTransform.z - }, - rotation: Quat.multiply(layerRotation, offsetRot), - collisionsWillMove: true, - damping: DAMPING_FACTOR, - restitution: RESTITUTION, - friction: FRICTION, - angularDamping: ANGULAR_DAMPING_FACTOR, - gravity: GRAVITY, - density: DENSITY - })); - offset += BLOCK_SPACING; - } - } -} +var cogButton = Overlays.addOverlay('image', { + x: getCogButtonPosX(), + y: 10, + width: BUTTON_DIMENSIONS.width, + height: BUTTON_DIMENSIONS.height, + imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', + alpha: 0.8 +}); function mousePressEvent(event) { var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); if (clickedOverlay === button) { - resetBlocks(); + if (!plankyStack.isFound()) { + plankyStack.rez(); + return; + } + editMode = !editMode; + plankyStack.refresh(); + } else if (clickedOverlay === cogButton) { + settingsWindow.webWindow.setVisible(true); } } @@ -134,19 +253,18 @@ Controller.mousePressEvent.connect(mousePressEvent); function cleanup() { Overlays.deleteOverlay(button); + Overlays.deleteOverlay(cogButton); if (ground) { Entities.deleteEntity(ground); } - pieces.forEach(function(piece) { - Entities.deleteEntity(piece); - }); - pieces = []; + plankyStack.deRez(); } function onUpdate() { - if (windowWidth != Window.innerWidth) { + if (windowWidth !== Window.innerWidth) { windowWidth = Window.innerWidth; Overlays.editOverlay(button, {x: getButtonPosX()}); + Overlays.editOverlay(cogButton, {x: getCogButtonPosX()}); } } diff --git a/examples/html/plankySettings.html b/examples/html/plankySettings.html new file mode 100644 index 0000000000..9e2a08c650 --- /dev/null +++ b/examples/html/plankySettings.html @@ -0,0 +1,112 @@ + + + + + + + + +
+ + \ No newline at end of file From 90284b8bb46825aca0aee3bc13969b413af0609b Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 7 Jul 2015 21:33:28 +0200 Subject: [PATCH 02/12] fixes mysterious button display bug --- examples/example/games/planky.js | 41 +++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index cd4a7295be..383e1d57d5 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -40,6 +40,8 @@ var size; var pieces = []; var ground = false; var layerRotated = false; +var button; +var cogButton; SettingsWindow = function() { this.webWindow = null; @@ -217,25 +219,38 @@ function getCogButtonPosX() { return getButtonPosX() - (BUTTON_DIMENSIONS.width * 1.1); } -var button = Overlays.addOverlay('image', { +print(JSON.stringify({ x: getButtonPosX(), y: 10, width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height, imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/planky_button.svg', alpha: 0.8 -}); +})); -var cogButton = Overlays.addOverlay('image', { - x: getCogButtonPosX(), - y: 10, - width: BUTTON_DIMENSIONS.width, - height: BUTTON_DIMENSIONS.height, - imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', - alpha: 0.8 -}); +function createButtons() { + button = Overlays.addOverlay('image', { + x: getButtonPosX(), + y: 10, + width: BUTTON_DIMENSIONS.width, + height: BUTTON_DIMENSIONS.height, + imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/planky_button.svg', + alpha: 0.8 + }); -function mousePressEvent(event) { + cogButton = Overlays.addOverlay('image', { + x: getCogButtonPosX(), + y: 10, + width: BUTTON_DIMENSIONS.width, + height: BUTTON_DIMENSIONS.height, + imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', + alpha: 0.8 + }); +} +// Fixes bug of not showing buttons on startup +Script.setTimeout(createButtons, 1000); + +Controller.mousePressEvent.connect(function(event) { var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); if (clickedOverlay === button) { if (!plankyStack.isFound()) { @@ -247,9 +262,7 @@ function mousePressEvent(event) { } else if (clickedOverlay === cogButton) { settingsWindow.webWindow.setVisible(true); } -} - -Controller.mousePressEvent.connect(mousePressEvent); +}); function cleanup() { Overlays.deleteOverlay(button); From c6b3801d0b2b3774678e1b1e2640597f84545f37 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Thu, 9 Jul 2015 20:53:09 +0200 Subject: [PATCH 03/12] proper block offset calculations --- examples/example/games/planky.js | 42 ++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 383e1d57d5..82ae4af9e0 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -83,6 +83,7 @@ PlankyStack = function() { var _this = this; this.planks = []; this.ground = false; + this.editLines = []; this.options = new PlankyOptions(); this.deRez = function() { _this.planks.forEach(function(plank) { @@ -92,7 +93,14 @@ PlankyStack = function() { if (_this.ground) { Entities.deleteEntity(_this.ground); } + this.editLines.forEach(function(line) { + Entities.deleteEntity(line); + }) + if (_this.centerLine) { + Entities.deleteEntity(_this.centerLine); + } _this.ground = false; + _this.centerLine = false; }; this.rez = function() { if (_this.planks.length > 0) { @@ -121,6 +129,20 @@ PlankyStack = function() { // move ground to rez position/rotation Entities.editEntity(_this.ground, {dimensions: _this.options.baseDimension, position: Vec3.sum(_this.basePosition, {y: -(_this.options.baseDimension.y / 2)}), rotation: _this.baseRotation}); } + + var refreshLines = function() { + if (_this.editLines.length === 0) { + _this.editLines.push(Entities.addEntity({ + type: 'Line', + dimensions: {x: 5, y: 21, z: 5}, + position: Vec3.sum(_this.basePosition, {y: -(_this.options.baseDimension.y / 2)}), + lineWidth: 7, + color: {red: 214, green: 91, blue: 67}, + linePoints: [{x: 0, y: 0, z: 0}, {x: 0, y: 10, z: 0}] + })); + return; + } + } var trimDimension = function(dimension, maxIndex) { _this.planks.forEach(function(plank, index, object) { if (plank[dimension] > maxIndex) { @@ -132,13 +154,12 @@ PlankyStack = function() { var createOrUpdate = function(layer, row) { var found = false; var layerRotated = layer % 2 === 0; - var offset = -(_this.options.blockSpacing); - var layerRotation = Quat.fromPitchYawRollDegrees(0, layerRotated ? NO_ANGLE : RIGHT_ANGLE, 0.0); - var blockPositionXZ = (_this.options.blockSize.x * row) - (((_this.options.blockSize.x * _this.options.blocksPerLayer) / 2) - (_this.options.blockSize.x / 2)); + var layerRotation = Quat.fromPitchYawRollDegrees(0, layerRotated ? NO_ANGLE : RIGHT_ANGLE, 0.0); + var blockPositionXZ = ((row - (_this.options.blocksPerLayer / 2) + 0.5) * (_this.options.blockSpacing + _this.options.blockSize.x)); var localTransform = Vec3.multiplyQbyV(_this.offsetRot, { - x: (layerRotated ? blockPositionXZ + offset: 0), + x: (layerRotated ? blockPositionXZ : 0), y: (_this.options.blockSize.y / 2) + (_this.options.blockSize.y * layer), - z: (layerRotated ? 0 : blockPositionXZ + offset) + z: (layerRotated ? 0 : blockPositionXZ) }); var newProperties = { type: 'Model', @@ -173,6 +194,7 @@ PlankyStack = function() { }; this.refresh = function() { refreshGround(); + refreshLines(); trimDimension('layer', _this.options.numLayers); trimDimension('row', _this.options.blocksPerLayer); _this.offsetRot = Quat.multiply(_this.baseRotation, Quat.fromPitchYawRollDegrees(0.0, _this.options.blockYawOffset, 0.0)); @@ -183,7 +205,6 @@ PlankyStack = function() { } if (!editMode) { _this.planks.forEach(function(plank, index, object) { - print(index + " , " + plank); Entities.editEntity(plank.entity, {ignoreForCollisions: false, collisionsWillMove: true}); // Entities.editEntity(plank.entity, {collisionsWillMove: true}); }); @@ -219,15 +240,6 @@ function getCogButtonPosX() { return getButtonPosX() - (BUTTON_DIMENSIONS.width * 1.1); } -print(JSON.stringify({ - x: getButtonPosX(), - y: 10, - width: BUTTON_DIMENSIONS.width, - height: BUTTON_DIMENSIONS.height, - imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/planky_button.svg', - alpha: 0.8 -})); - function createButtons() { button = Overlays.addOverlay('image', { x: getButtonPosX(), From 1f453e07e33e20ccb7cf39bbbbdbe1969466d679 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 02:18:17 +0200 Subject: [PATCH 04/12] connection between planky script and web-window -load settings -include toolbar --- examples/example/games/planky.js | 110 +++++++++------ examples/html/plankySettings.html | 214 ++++++++++++++++-------------- 2 files changed, 183 insertions(+), 141 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 82ae4af9e0..60c3b3ad3a 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -12,6 +12,8 @@ // HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; +Script.include("../../libraries/toolBars.js"); + const DEFAULT_NUM_LAYERS = 16; const DEFAULT_BASE_DIMENSION = { x: 7, y: 2, z: 7 }; const DEFAULT_BLOCKS_PER_LAYER = 3; @@ -30,7 +32,7 @@ const DEFAULT_BLOCK_YAW_OFFSET = 45; var editMode = false; -const BUTTON_DIMENSIONS = {width: 49, height: 49}; +const BUTTON_DIMENSIONS = {width: 50, height: 50}; const MAXIMUM_PERCENTAGE = 100.0; const NO_ANGLE = 0; const RIGHT_ANGLE = 90; @@ -42,12 +44,30 @@ var ground = false; var layerRotated = false; var button; var cogButton; +var toolBar; SettingsWindow = function() { + var _this = this; + this.plankyStack = null; this.webWindow = null; - this.init = function() { - this.webWindow = new WebWindow('Planky', Script.resolvePath('../../html/plankySettings.html'), 255, 500, true); - this.webWindow.setVisible(false); + this.init = function(plankyStack) { + _this.webWindow = new WebWindow('Planky', Script.resolvePath('../../html/plankySettings.html'), 255, 500, true); + _this.webWindow.setVisible(false); + _this.webWindow.eventBridge.webEventReceived.connect(_this.onWebEventReceived); + _this.plankyStack = plankyStack; + }; + this.sendData = function(data) { + _this.webWindow.eventBridge.emitScriptEvent(JSON.stringify(data)); + }; + this.onWebEventReceived = function(data) { + data = JSON.parse(data); + switch (data.action) { + case 'loaded': + _this.sendData({action: 'load', options: _this.plankyStack.options.getJSON()}) + break; + default: + Window.alert('unknown action ' + data.action); + } }; }; @@ -59,6 +79,24 @@ PlankyOptions = function() { this.load = function() { //TODO: load Planky Options here. }; + this.getJSON = function() { + return { + numLayers: _this.numLayers, + baseDimension: _this.baseDimension, + blocksPerLayer: _this.blocksPerLayer, + blockSize: _this.blockSize, + blockSpacing: _this.blockSpacing, + blockHeightVariation: _this.blockHeightVariation, + gravity: _this.gravity, + density: _this.density, + dampingFactor: _this.dampingFactor, + angularDampingFactor: _this.angularDampingFactor, + friction: _this.friction, + restitution: _this.restitution, + spawnDistance: _this.spawnDistance, + blockYawOffset: _this.blockYawOffset, + }; + } this.setDefaults = function() { _this.numLayers = DEFAULT_NUM_LAYERS; _this.baseDimension = DEFAULT_BASE_DIMENSION; @@ -74,6 +112,7 @@ PlankyOptions = function() { _this.restitution = DEFAULT_RESTITUTION; _this.spawnDistance = DEFAULT_SPAWN_DISTANCE; _this.blockYawOffset = DEFAULT_BLOCK_YAW_OFFSET; + }; this.setDefaults(); }; @@ -93,7 +132,7 @@ PlankyStack = function() { if (_this.ground) { Entities.deleteEntity(_this.ground); } - this.editLines.forEach(function(line) { + _this.editLines.forEach(function(line) { Entities.deleteEntity(line); }) if (_this.centerLine) { @@ -155,7 +194,7 @@ PlankyStack = function() { var found = false; var layerRotated = layer % 2 === 0; var layerRotation = Quat.fromPitchYawRollDegrees(0, layerRotated ? NO_ANGLE : RIGHT_ANGLE, 0.0); - var blockPositionXZ = ((row - (_this.options.blocksPerLayer / 2) + 0.5) * (_this.options.blockSpacing + _this.options.blockSize.x)); + var blockPositionXZ = (row - (_this.options.blocksPerLayer / 2) + 0.5) * (_this.options.blockSpacing + _this.options.blockSize.x); var localTransform = Vec3.multiplyQbyV(_this.offsetRot, { x: (layerRotated ? blockPositionXZ : 0), y: (_this.options.blockSize.y / 2) + (_this.options.blockSize.y * layer), @@ -217,9 +256,8 @@ PlankyStack = function() { }; var settingsWindow = new SettingsWindow(); -settingsWindow.init(); - var plankyStack = new PlankyStack(); +settingsWindow.init(plankyStack); function grabLowestJointY() { var jointNames = MyAvatar.getJointNames(); @@ -232,66 +270,58 @@ function grabLowestJointY() { return floorY; } -function getButtonPosX() { - return windowWidth - ((BUTTON_DIMENSIONS.width / 2) + BUTTON_DIMENSIONS.width); -} - -function getCogButtonPosX() { - return getButtonPosX() - (BUTTON_DIMENSIONS.width * 1.1); -} - function createButtons() { - button = Overlays.addOverlay('image', { - x: getButtonPosX(), - y: 10, + toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.games.planky", function (windowDimensions, toolbar) { + return { + x: windowDimensions.x - (toolbar.width * 1.1), + y: toolbar.height / 2 + }; + }); + button = toolBar.addTool({ width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height, imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/planky_button.svg', - alpha: 0.8 + alpha: 0.8, + visible: true }); - cogButton = Overlays.addOverlay('image', { - x: getCogButtonPosX(), - y: 10, + cogButton = toolBar.addTool({ width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height, imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', - alpha: 0.8 + alpha: 0.8, + visible: true }); } // Fixes bug of not showing buttons on startup -Script.setTimeout(createButtons, 1000); +Script.setTimeout(createButtons, 2000); Controller.mousePressEvent.connect(function(event) { var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); - if (clickedOverlay === button) { + if (toolBar.clicked(clickedOverlay) === button) { if (!plankyStack.isFound()) { plankyStack.rez(); return; } editMode = !editMode; plankyStack.refresh(); - } else if (clickedOverlay === cogButton) { + } else if (toolBar.clicked(clickedOverlay) === cogButton) { settingsWindow.webWindow.setVisible(true); } }); - -function cleanup() { - Overlays.deleteOverlay(button); - Overlays.deleteOverlay(cogButton); - if (ground) { - Entities.deleteEntity(ground); - } - plankyStack.deRez(); -} -function onUpdate() { +Script.update.connect(function() { if (windowWidth !== Window.innerWidth) { windowWidth = Window.innerWidth; Overlays.editOverlay(button, {x: getButtonPosX()}); Overlays.editOverlay(cogButton, {x: getCogButtonPosX()}); } -} +}) -Script.update.connect(onUpdate) -Script.scriptEnding.connect(cleanup); +Script.scriptEnding.connect(function() { + toolBar.cleanup(); + if (ground) { + Entities.deleteEntity(ground); + } + plankyStack.deRez(); +}); diff --git a/examples/html/plankySettings.html b/examples/html/plankySettings.html index 9e2a08c650..d5f6f78a71 100644 --- a/examples/html/plankySettings.html +++ b/examples/html/plankySettings.html @@ -1,110 +1,122 @@ - - - + + } + EventBridge.emitWebEvent(JSON.stringify({action: 'loaded'})); + +}); +
From 6d1df036174046d2139702bdf5dc64e882791191 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 03:19:23 +0200 Subject: [PATCH 05/12] planky: load / save settings --- examples/example/games/planky.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 60c3b3ad3a..abf1e63412 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -74,10 +74,18 @@ SettingsWindow = function() { PlankyOptions = function() { var _this = this; this.save = function() { - //TODO: save Planky Options here. + Settings.setValue('plankyOptions', JSON.stringify(_this.getJSON())); }; this.load = function() { - //TODO: load Planky Options here. + _this.setDefaults(); + var plankyOptions = Settings.getValue('plankyOptions') + if (plankyOptions === null || plankyOptions === '') { + return; + } + var options = JSON.parse(plankyOptions); + options.forEach(function(value, option, object) { + _this[option] = value; + }); }; this.getJSON = function() { return { @@ -112,9 +120,8 @@ PlankyOptions = function() { _this.restitution = DEFAULT_RESTITUTION; _this.spawnDistance = DEFAULT_SPAWN_DISTANCE; _this.blockYawOffset = DEFAULT_BLOCK_YAW_OFFSET; - }; - this.setDefaults(); + this.load(); }; // The PlankyStack exists out of rows and layers From 222234cf1d43576649526cafce4551804f174c26 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 03:40:58 +0200 Subject: [PATCH 06/12] wild planky changes on value change --- examples/example/games/planky.js | 7 +++++++ examples/html/plankySettings.html | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index abf1e63412..0849481a61 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -65,6 +65,9 @@ SettingsWindow = function() { case 'loaded': _this.sendData({action: 'load', options: _this.plankyStack.options.getJSON()}) break; + case 'value_change': + _this.plankyStack.onValueChanged(data.option, data.value); + break; default: Window.alert('unknown action ' + data.action); } @@ -238,6 +241,10 @@ PlankyStack = function() { _this.planks.push({layer: layer, row: row, entity: Entities.addEntity(newProperties)}) } }; + this.onValueChanged = function(option, value) { + _this.options[option] = value; + _this.refresh(); + }; this.refresh = function() { refreshGround(); refreshLines(); diff --git a/examples/html/plankySettings.html b/examples/html/plankySettings.html index d5f6f78a71..a5ad2f2939 100644 --- a/examples/html/plankySettings.html +++ b/examples/html/plankySettings.html @@ -38,13 +38,21 @@ PropertyInput = function(key, label, value, attributes) { this.construct(); }; +var valueChangeHandler = function() { + EventBridge.emitWebEvent(JSON.stringify({ + action: 'value_change', + option: $(this).attr('name'), + value: properties[$(this).attr('name')].getValue() + })); +}; + NumberInput = function(key, label, value, attributes) { PropertyInput.call(this, key, label, value, attributes); }; NumberInput.prototype = Object.create(PropertyInput.prototype); NumberInput.prototype.constructor = NumberInput; NumberInput.prototype.createValue = function() { - this.input = $('').attr('name', this.key).attr('type', 'number').val(this.value); + this.input = $('').attr('name', this.key).attr('type', 'number').val(this.value).on('change', valueChangeHandler); if (this.attributes !== undefined) { this.input.attr(this.attributes); } @@ -114,7 +122,6 @@ $(function() { }); } EventBridge.emitWebEvent(JSON.stringify({action: 'loaded'})); - }); From c8453bec671cbec04f7245e6b2878d08900423d2 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 03:50:06 +0200 Subject: [PATCH 07/12] planky: fix layer setting, restraints on values --- examples/html/plankySettings.html | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/examples/html/plankySettings.html b/examples/html/plankySettings.html index a5ad2f2939..b357e83792 100644 --- a/examples/html/plankySettings.html +++ b/examples/html/plankySettings.html @@ -91,23 +91,23 @@ function addHeader(label) { $(function() { addHeader('Stack Settings'); - properties['numLayers'] = new NumberInput('layers', 'Layers', 17, {'min': 0, 'max': 300, 'step': 1}); + properties['numLayers'] = new NumberInput('numLayers', 'Layers', 17, {'min': 0, 'max': 300, 'step': 1}); properties['baseDimension'] = new CoordinateInput('baseDimension', 'Base dimension', {x: 7, y: 2, z: 7}); - properties['blocksPerLayer'] = new NumberInput('blocksPerLayer', 'Blocks per layer', 4); + properties['blocksPerLayer'] = new NumberInput('blocksPerLayer', 'Blocks per layer', 4, {'min': 1, 'max': 100, 'step': 1}); properties['blockSize'] = new CoordinateInput('blockSize', 'Block size', {x: 0.2, y: 0.1, z: 0.8}); properties['blockSpacing'] = new NumberInput('blockSpacing', 'Block spacing', properties['blockSize'].getValue().x / properties['blocksPerLayer'].getValue()); properties['blockSpacing'].addButton('btn-recalculate-spacing', 'Recalculate spacing'); $('#btn-recalculate-spacing').on('click', function() { properties['blockSpacing'].setValue(properties['blockSize'].getValue().x / properties['blocksPerLayer'].getValue()); }); - properties['blockHeightVariation'] = new NumberInput('blockHeightVariation', 'Block height variation (%)', 0.1); + properties['blockHeightVariation'] = new NumberInput('blockHeightVariation', 'Block height variation (%)', 0.1, {'min': 0, 'max': 1, 'step': 0.01}); addHeader('Physics Settings'); properties['gravity'] = new CoordinateInput('gravity', 'Gravity', {x: 0, y: -2.8, z: 0}); - properties['density'] = new NumberInput('density', 'Density', 4000); - properties['dampingFactor'] = new NumberInput('dampingFactor', 'Damping factor', 0.98); - properties['angularDampingFactor'] = new NumberInput('angularDampingFactor', 'Angular damping factor', 0.8); - properties['friction'] = new NumberInput('friction', 'Friction', 0.99); - properties['restitution'] = new NumberInput('restitution', 'Restitution', 0.0); + properties['density'] = new NumberInput('density', 'Density', 4000, {'min': 0, 'max': 4000, 'step': 1}); + properties['dampingFactor'] = new NumberInput('dampingFactor', 'Damping factor', 0.98, {'min': 0, 'max': 1, 'step': 0.01}); + properties['angularDampingFactor'] = new NumberInput('angularDampingFactor', 'Angular damping factor', 0.8, {'min': 0, 'max': 1, 'step': 0.01}); + properties['friction'] = new NumberInput('friction', 'Friction', 0.99, {'min': 0, 'max': 1, 'step': 0.01}); + properties['restitution'] = new NumberInput('restitution', 'Restitution', 0.0, {'min': 0, 'max': 1, 'step': 0.01}); addHeader('Spawn Settings'); properties['spawnDistance'] = new NumberInput('spawnDistance', 'Spawn distance (meters)', 3); properties['blockYawOffset'] = new NumberInput('blockYawOffset', 'Block yaw offset (degrees)', 45, {'min': 0, 'max': 360, 'step': 1}); From 95948851f1427767e2a8372153a70064aa4d3dda Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 04:36:29 +0200 Subject: [PATCH 08/12] save, clear and reset button --- examples/html/plankySettings.html | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/html/plankySettings.html b/examples/html/plankySettings.html index b357e83792..0a79df6c7b 100644 --- a/examples/html/plankySettings.html +++ b/examples/html/plankySettings.html @@ -5,6 +5,9 @@ From b7110227963d187aca08cfe06de26501c0243546 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 12:28:49 +0200 Subject: [PATCH 09/12] planky: - removed workaround for delayed overlay loading - make buttons functional (reset, cleanup, save-default) - only show live changes for the visual planky properties: blocksize , numLayers etc. (no physical properties) --- examples/example/games/planky.js | 74 +++++++++++++++++++------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 0849481a61..5232201449 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -68,14 +68,28 @@ SettingsWindow = function() { case 'value_change': _this.plankyStack.onValueChanged(data.option, data.value); break; + case 'factory-reset': + _this.plankyStack.options.factoryReset(); + _this.sendData({action: 'load', options: _this.plankyStack.options.getJSON()}) + break; + case 'save-default': + _this.plankyStack.options.save(); + break; + case 'cleanup': + _this.plankyStack.deRez(); + break; default: - Window.alert('unknown action ' + data.action); + Window.alert('[planky] unknown action ' + data.action); } }; }; PlankyOptions = function() { var _this = this; + this.factoryReset = function() { + _this.setDefaults(); + Settings.setValue('plankyOptions', ''); + }; this.save = function() { Settings.setValue('plankyOptions', JSON.stringify(_this.getJSON())); }; @@ -86,9 +100,9 @@ PlankyOptions = function() { return; } var options = JSON.parse(plankyOptions); - options.forEach(function(value, option, object) { - _this[option] = value; - }); + for (option in options) { + _this[option] = options[option]; + } }; this.getJSON = function() { return { @@ -147,9 +161,10 @@ PlankyStack = function() { }) if (_this.centerLine) { Entities.deleteEntity(_this.centerLine); - } + } _this.ground = false; _this.centerLine = false; + _this.editLines = []; }; this.rez = function() { if (_this.planks.length > 0) { @@ -243,7 +258,9 @@ PlankyStack = function() { }; this.onValueChanged = function(option, value) { _this.options[option] = value; - _this.refresh(); + if (['numLayers', 'blocksPerLayer', 'blockSize', 'blockSpacing', 'blockHeightVariation'].indexOf(option) !== -1) { + _this.refresh(); + } }; this.refresh = function() { refreshGround(); @@ -284,31 +301,28 @@ function grabLowestJointY() { return floorY; } -function createButtons() { - toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.games.planky", function (windowDimensions, toolbar) { - return { - x: windowDimensions.x - (toolbar.width * 1.1), - y: toolbar.height / 2 - }; - }); - button = toolBar.addTool({ - width: BUTTON_DIMENSIONS.width, - height: BUTTON_DIMENSIONS.height, - imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/planky_button.svg', - alpha: 0.8, - visible: true - }); +toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.games.planky", function (windowDimensions, toolbar) { + return { + x: windowDimensions.x - (toolbar.width * 1.1), + y: toolbar.height / 2 + }; +}); - cogButton = toolBar.addTool({ - width: BUTTON_DIMENSIONS.width, - height: BUTTON_DIMENSIONS.height, - imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', - alpha: 0.8, - visible: true - }); -} -// Fixes bug of not showing buttons on startup -Script.setTimeout(createButtons, 2000); +button = toolBar.addTool({ + width: BUTTON_DIMENSIONS.width, + height: BUTTON_DIMENSIONS.height, + imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/planky_button.svg', + alpha: 0.8, + visible: true +}); + +cogButton = toolBar.addTool({ + width: BUTTON_DIMENSIONS.width, + height: BUTTON_DIMENSIONS.height, + imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', + alpha: 0.8, + visible: true +}); Controller.mousePressEvent.connect(function(event) { var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); From 6926ae9aa3bcdb474360a89dff4f01ba24dbd5cd Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 14 Jul 2015 20:27:51 +0200 Subject: [PATCH 10/12] small planky improvements --- examples/example/games/planky.js | 19 +++++++++++++------ examples/html/plankySettings.html | 21 +++++++++++---------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 5232201449..6733749317 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -65,7 +65,7 @@ SettingsWindow = function() { case 'loaded': _this.sendData({action: 'load', options: _this.plankyStack.options.getJSON()}) break; - case 'value_change': + case 'value-change': _this.plankyStack.onValueChanged(data.option, data.value); break; case 'factory-reset': @@ -148,6 +148,7 @@ PlankyStack = function() { this.ground = false; this.editLines = []; this.options = new PlankyOptions(); + this.deRez = function() { _this.planks.forEach(function(plank) { Entities.deleteEntity(plank.entity); @@ -159,13 +160,14 @@ PlankyStack = function() { _this.editLines.forEach(function(line) { Entities.deleteEntity(line); }) + _this.editLines = []; if (_this.centerLine) { Entities.deleteEntity(_this.centerLine); } _this.ground = false; _this.centerLine = false; - _this.editLines = []; }; + this.rez = function() { if (_this.planks.length > 0) { _this.deRez(); @@ -192,7 +194,7 @@ PlankyStack = function() { } // move ground to rez position/rotation Entities.editEntity(_this.ground, {dimensions: _this.options.baseDimension, position: Vec3.sum(_this.basePosition, {y: -(_this.options.baseDimension.y / 2)}), rotation: _this.baseRotation}); - } + }; var refreshLines = function() { if (_this.editLines.length === 0) { @@ -206,7 +208,8 @@ PlankyStack = function() { })); return; } - } + }; + var trimDimension = function(dimension, maxIndex) { _this.planks.forEach(function(plank, index, object) { if (plank[dimension] > maxIndex) { @@ -215,6 +218,7 @@ PlankyStack = function() { } }); }; + var createOrUpdate = function(layer, row) { var found = false; var layerRotated = layer % 2 === 0; @@ -256,17 +260,19 @@ PlankyStack = function() { _this.planks.push({layer: layer, row: row, entity: Entities.addEntity(newProperties)}) } }; + this.onValueChanged = function(option, value) { _this.options[option] = value; if (['numLayers', 'blocksPerLayer', 'blockSize', 'blockSpacing', 'blockHeightVariation'].indexOf(option) !== -1) { _this.refresh(); } }; + this.refresh = function() { refreshGround(); refreshLines(); - trimDimension('layer', _this.options.numLayers); - trimDimension('row', _this.options.blocksPerLayer); + trimDimension('layer', _this.options.numLayers - 1); + trimDimension('row', _this.options.blocksPerLayer - 1); _this.offsetRot = Quat.multiply(_this.baseRotation, Quat.fromPitchYawRollDegrees(0.0, _this.options.blockYawOffset, 0.0)); for (var layer = 0; layer < _this.options.numLayers; layer++) { for (var row = 0; row < _this.options.blocksPerLayer; row++) { @@ -280,6 +286,7 @@ PlankyStack = function() { }); } }; + this.isFound = function() { //TODO: identify entities here until one is found return _this.planks.length > 0; diff --git a/examples/html/plankySettings.html b/examples/html/plankySettings.html index 0a79df6c7b..0eea4948ad 100644 --- a/examples/html/plankySettings.html +++ b/examples/html/plankySettings.html @@ -42,10 +42,11 @@ PropertyInput = function(key, label, value, attributes) { }; var valueChangeHandler = function() { + sendWebEvent({ - action: 'value_change', - option: $(this).attr('name'), - value: properties[$(this).attr('name')].getValue() + action: 'value-change', + option: $(this).data('var-name'), + value: properties[$(this).data('var-name')].getValue() }); }; @@ -55,14 +56,14 @@ NumberInput = function(key, label, value, attributes) { NumberInput.prototype = Object.create(PropertyInput.prototype); NumberInput.prototype.constructor = NumberInput; NumberInput.prototype.createValue = function() { - this.input = $('').attr('name', this.key).attr('type', 'number').val(this.value).on('change', valueChangeHandler); + this.input = $('').data('var-name', this.key).attr('name', this.key).attr('type', 'number').val(this.value).on('change', valueChangeHandler); if (this.attributes !== undefined) { this.input.attr(this.attributes); } return this.input; }; NumberInput.prototype.getValue = function() { - return this.input.val(); + return parseFloat(this.input.val()); }; CoordinateInput = function(key, label, value, attributes) { @@ -71,9 +72,9 @@ CoordinateInput = function(key, label, value, attributes) { CoordinateInput.prototype = Object.create(PropertyInput.prototype); CoordinateInput.prototype.constructor = CoordinateInput; CoordinateInput.prototype.createValue = function() { - this.inputX = $('').attr('name', this.key + '-x').attr('type', 'number').addClass('coord').val(this.value.x); - this.inputY = $('').attr('name', this.key + '-y').attr('type', 'number').addClass('coord').val(this.value.y); - this.inputZ = $('').attr('name', this.key + '-z').attr('type', 'number').addClass('coord').val(this.value.z); + this.inputX = $('').data('var-name', this.key).attr('name', this.key + '-x').attr('type', 'number').addClass('coord').val(this.value.x).on('change', valueChangeHandler); + this.inputY = $('').data('var-name', this.key).attr('name', this.key + '-y').attr('type', 'number').addClass('coord').val(this.value.y).on('change', valueChangeHandler); + this.inputZ = $('').data('var-name', this.key).attr('name', this.key + '-z').attr('type', 'number').addClass('coord').val(this.value.z).on('change', valueChangeHandler); if (this.attributes !== undefined) { this.inputX.attr(this.attributes); this.inputY.attr(this.attributes); @@ -82,7 +83,7 @@ CoordinateInput.prototype.createValue = function() { return [encapsulateInput(this.inputX, 'X'), encapsulateInput(this.inputY, 'Y'), encapsulateInput(this.inputZ, 'Z')]; }; CoordinateInput.prototype.getValue = function() { - return {x: this.inputX.val(), y: this.inputY.val(), z: this.inputZ.val()}; + return {x: parseFloat(this.inputX.val()), y: parseFloat(this.inputY.val()), z: parseFloat(this.inputZ.val())}; }; function encapsulateInput(input, label) { return $('
').addClass('input-area').append(label + ' ').append(input); @@ -95,7 +96,6 @@ function addHeader(label) { $(function() { addHeader('Stack Settings'); properties['numLayers'] = new NumberInput('numLayers', 'Layers', 17, {'min': 0, 'max': 300, 'step': 1}); - properties['baseDimension'] = new CoordinateInput('baseDimension', 'Base dimension', {x: 7, y: 2, z: 7}, {'min': 0.5, 'max': 200, 'step': 0.1}); properties['blocksPerLayer'] = new NumberInput('blocksPerLayer', 'Blocks per layer', 4, {'min': 1, 'max': 100, 'step': 1}); properties['blockSize'] = new CoordinateInput('blockSize', 'Block size', {x: 0.2, y: 0.1, z: 0.8}, {'min': 0.05, 'max': 20, 'step': 0.1}); properties['blockSpacing'] = new NumberInput('blockSpacing', 'Block spacing', properties['blockSize'].getValue().x / properties['blocksPerLayer'].getValue(), {'min': 0, 'max': 20, 'step': 0.01}); @@ -114,6 +114,7 @@ $(function() { addHeader('Spawn Settings'); properties['spawnDistance'] = new NumberInput('spawnDistance', 'Spawn distance (meters)', 3); properties['blockYawOffset'] = new NumberInput('blockYawOffset', 'Block yaw offset (degrees)', 45, {'min': 0, 'max': 360, 'step': 1}); + properties['baseDimension'] = new CoordinateInput('baseDimension', 'Base dimension', {x: 7, y: 2, z: 7}, {'min': 0.5, 'max': 200, 'step': 0.1}); addHeader('Actions'); $('#properties-list') .append($('').val('factory reset').attr('type', 'button').on('click', function() { sendWebEvent({action: 'factory-reset'}); })) From 0d0f12164a0c529ebd2cee6e42d6319722b28c27 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Tue, 21 Jul 2015 16:57:02 +0200 Subject: [PATCH 11/12] planky, enabling properties button --- examples/example/games/planky.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 6733749317..4181eb541e 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -32,7 +32,7 @@ const DEFAULT_BLOCK_YAW_OFFSET = 45; var editMode = false; -const BUTTON_DIMENSIONS = {width: 50, height: 50}; +const BUTTON_DIMENSIONS = {width: 49, height: 49}; const MAXIMUM_PERCENTAGE = 100.0; const NO_ANGLE = 0; const RIGHT_ANGLE = 90; @@ -326,10 +326,11 @@ button = toolBar.addTool({ cogButton = toolBar.addTool({ width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height, - imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', + imageURL: "https://dl.dropboxusercontent.com/u/14997455/hifi/planky/cog.svg", //HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', + subImage: { x: 0, y: BUTTON_DIMENSIONS.height, width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height }, alpha: 0.8, visible: true -}); +}, true, false); Controller.mousePressEvent.connect(function(event) { var clickedOverlay = Overlays.getOverlayAtPoint({x: event.x, y: event.y}); @@ -341,6 +342,7 @@ Controller.mousePressEvent.connect(function(event) { editMode = !editMode; plankyStack.refresh(); } else if (toolBar.clicked(clickedOverlay) === cogButton) { + toolBar.selectTool(cogButton, true); settingsWindow.webWindow.setVisible(true); } }); From de116c4e3b35b26f8014641074d84d6f2796d431 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 22 Jul 2015 10:21:13 +0200 Subject: [PATCH 12/12] editMode on and off switch by clicking the COG, proper removal of planks when rows/columns removed --- examples/example/games/planky.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/example/games/planky.js b/examples/example/games/planky.js index 4181eb541e..8abc697353 100644 --- a/examples/example/games/planky.js +++ b/examples/example/games/planky.js @@ -203,20 +203,29 @@ PlankyStack = function() { dimensions: {x: 5, y: 21, z: 5}, position: Vec3.sum(_this.basePosition, {y: -(_this.options.baseDimension.y / 2)}), lineWidth: 7, - color: {red: 214, green: 91, blue: 67}, - linePoints: [{x: 0, y: 0, z: 0}, {x: 0, y: 10, z: 0}] + color: {red: 20, green: 20, blue: 20}, + linePoints: [{x: 0, y: 0, z: 0}, {x: 0, y: 10, z: 0}], + visible: editMode })); return; } + _this.editLines.forEach(function(line) { + Entities.editEntity(line, {visible: editMode}); + }) }; var trimDimension = function(dimension, maxIndex) { + var removingPlanks = []; _this.planks.forEach(function(plank, index, object) { if (plank[dimension] > maxIndex) { - Entities.deleteEntity(plank.entity); - object.splice(index, 1); + removingPlanks.push(index); } }); + removingPlanks.reverse(); + for (var i = 0; i < removingPlanks.length; i++) { + Entities.deleteEntity(_this.planks[removingPlanks[i]].entity); + _this.planks.splice(removingPlanks[i], 1); + } }; var createOrUpdate = function(layer, row) { @@ -237,7 +246,6 @@ PlankyStack = function() { dimensions: Vec3.sum(_this.options.blockSize, {x: 0, y: -((_this.options.blockSize.y * (_this.options.blockHeightVariation / MAXIMUM_PERCENTAGE)) * Math.random()), z: 0}), position: Vec3.sum(_this.basePosition, localTransform), rotation: Quat.multiply(layerRotation, _this.offsetRot), - //collisionsWillMove: false,//!editMode,//false,//!editMode, damping: _this.options.dampingFactor, restitution: _this.options.restitution, friction: _this.options.friction, @@ -282,7 +290,6 @@ PlankyStack = function() { if (!editMode) { _this.planks.forEach(function(plank, index, object) { Entities.editEntity(plank.entity, {ignoreForCollisions: false, collisionsWillMove: true}); - // Entities.editEntity(plank.entity, {collisionsWillMove: true}); }); } }; @@ -326,7 +333,7 @@ button = toolBar.addTool({ cogButton = toolBar.addTool({ width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height, - imageURL: "https://dl.dropboxusercontent.com/u/14997455/hifi/planky/cog.svg", //HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', + imageURL: HIFI_PUBLIC_BUCKET + 'marketplace/hificontent/Games/blocks/cog.svg', subImage: { x: 0, y: BUTTON_DIMENSIONS.height, width: BUTTON_DIMENSIONS.width, height: BUTTON_DIMENSIONS.height }, alpha: 0.8, visible: true @@ -339,11 +346,14 @@ Controller.mousePressEvent.connect(function(event) { plankyStack.rez(); return; } - editMode = !editMode; plankyStack.refresh(); } else if (toolBar.clicked(clickedOverlay) === cogButton) { - toolBar.selectTool(cogButton, true); - settingsWindow.webWindow.setVisible(true); + editMode = !editMode; + toolBar.selectTool(cogButton, editMode); + settingsWindow.webWindow.setVisible(editMode); + if(plankyStack.planks.length) { + plankyStack.refresh(); + } } });