From 3346d77c448e29abe1c100d83015e48243308b9c Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Tue, 10 Nov 2015 17:25:54 -0800 Subject: [PATCH 001/111] pistol --- examples/weapons/pistol.js | 538 +++++++++++++++++++++++++++++++++++++ 1 file changed, 538 insertions(+) create mode 100644 examples/weapons/pistol.js diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js new file mode 100644 index 0000000000..a9836f949e --- /dev/null +++ b/examples/weapons/pistol.js @@ -0,0 +1,538 @@ +// +// gun.js +// examples +// +// Created by Brad Hefta-Gaub on 12/31/13. +// Modified by Philip on 3/3/14 +// Modified by Thijs Wenker on 3/31/15 +// Copyright 2013 High Fidelity, Inc. +// +// This is an example script that turns the hydra controllers and mouse into a entity gun. +// It reads the controller, watches for trigger pulls, and launches entities. +// When entities collide with voxels they blow little holes out of the voxels. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +// FIXME kickback functionality was removed because the joint setting interface in +// MyAvatar has apparently changed, breaking it. + +Script.include("../../libraries/utils.js"); +Script.include("../../libraries/constants.js"); +Script.include("../../libraries/toolBars.js"); + +HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; + +var LASER_WIDTH = 2; +var POSE_CONTROLS = [ Controller.Standard.LeftHand, Controller.Standard.RightHand ]; +var TRIGGER_CONTROLS = [ Controller.Standard.LT, Controller.Standard.RT ]; +var MIN_THROWER_DELAY = 1000; +var MAX_THROWER_DELAY = 1000; +var RELOAD_INTERVAL = 5; +var GUN_MODEL = HIFI_PUBLIC_BUCKET + "cozza13/gun/m1911-handgun+1.fbx?v=4"; +var BULLET_VELOCITY = 10.0; +var GUN_OFFSETS = [ { + x: -0.04, + y: 0.26, + z: 0.04 +}, { + x: 0.04, + y: 0.26, + z: 0.04 +} ]; + +var GUN_ORIENTATIONS = [ Quat.fromPitchYawRollDegrees(0, 90, 90), Quat.fromPitchYawRollDegrees(0, -90, 270) ]; + +var BARREL_OFFSETS = [ { + x: -0.12, + y: 0.12, + z: 0.04 +}, { + x: 0.12, + y: 0.12, + z: 0.04 +} ]; + +var mapping = Controller.newMapping(); +var validPoses = [ false, false ]; +var barrelVectors = [ 0, 0 ]; +var barrelTips = [ 0, 0 ]; +var pointer = []; + +pointer.push(Overlays.addOverlay("line3d", { + start: ZERO_VECTOR, + end: ZERO_VECTOR, + color: COLORS.RED, + alpha: 1, + visible: true, + lineWidth: LASER_WIDTH +})); + +pointer.push(Overlays.addOverlay("line3d", { + start: ZERO_VECTOR, + end: ZERO_VECTOR, + color: COLORS.RED, + alpha: 1, + visible: true, + lineWidth: LASER_WIDTH +})); + +function getRandomFloat(min, max) { + return Math.random() * (max - min) + min; +} + +var showScore = false; +// Load some sound to use for loading and firing +var fireSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/GUN-SHOT2.raw"); +var loadSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/Gun_Reload_Weapon22.raw"); +var impactSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/BulletImpact2.raw"); +var targetHitSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Space%20Invaders/hit.raw"); +var targetLaunchSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Space%20Invaders/shoot.raw"); + +var audioOptions = { + volume: 0.9 +} + +var shotsFired = 0; +var shotTime = new Date(); +var isLaunchButtonPressed = false; +var score = 0; + +var bulletID = false; +var targetID = false; + +// Create overlay buttons and reticle +var BUTTON_SIZE = 32; +var PADDING = 3; +var NUM_BUTTONS = 3; +var screenSize = Controller.getViewportDimensions(); +var startX = screenSize.x / 2 - (NUM_BUTTONS * (BUTTON_SIZE + PADDING)) / 2; +var toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.gun.toolbar", function(screenSize) { + return { + x: startX, + y: (screenSize.y - (BUTTON_SIZE + PADDING)), + }; +}); + +var offButton = toolBar.addOverlay("image", { + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/gun/close.svg", + alpha: 1 +}); + +startX += BUTTON_SIZE + PADDING; +var platformButton = toolBar.addOverlay("image", { + x: startX, + y: screenSize.y - (BUTTON_SIZE + PADDING), + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/gun/platform-targets.svg", + alpha: 1 +}); + +startX += BUTTON_SIZE + PADDING; +var gridButton = toolBar.addOverlay("image", { + x: startX, + y: screenSize.y - (BUTTON_SIZE + PADDING), + width: BUTTON_SIZE, + height: BUTTON_SIZE, + imageURL: HIFI_PUBLIC_BUCKET + "images/gun/floating-targets.svg", + alpha: 1 +}); + +if (showScore) { + var text = Overlays.addOverlay("text", { + x: screenSize.x / 2 - 100, + y: screenSize.y / 2 - 50, + width: 150, + height: 50, + color: { + red: 0, + green: 0, + blue: 0 + }, + textColor: { + red: 255, + green: 0, + blue: 0 + }, + topMargin: 4, + leftMargin: 4, + text: "Score: " + score + }); +} + +function entityCollisionWithEntity(entity1, entity2, collision) { + if (entity2 === targetID) { + score++; + if (showScore) { + Overlays.editOverlay(text, { + text: "Score: " + score + }); + } + + // We will delete the bullet and target in 1/2 sec, but for now we can + // see them bounce! + Script.setTimeout(deleteBulletAndTarget, 500); + + // Turn the target and the bullet white + Entities.editEntity(entity1, { + color: { + red: 255, + green: 255, + blue: 255 + } + }); + Entities.editEntity(entity2, { + color: { + red: 255, + green: 255, + blue: 255 + } + }); + + // play the sound near the camera so the shooter can hear it + audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())); + Audio.playSound(targetHitSound, audioOptions); + } +} + +function shootBullet(position, velocity, grenade) { + var BULLET_SIZE = .09; + var BULLET_LIFETIME = 10.0; + var BULLET_GRAVITY = -0.25; + var GRENADE_VELOCITY = 15.0; + var GRENADE_SIZE = 0.35; + var GRENADE_GRAVITY = -9.8; + + var bVelocity = grenade ? Vec3.multiply(GRENADE_VELOCITY, Vec3.normalize(velocity)) : velocity; + var bSize = grenade ? GRENADE_SIZE : BULLET_SIZE; + var bGravity = grenade ? GRENADE_GRAVITY : BULLET_GRAVITY; + + bulletID = Entities.addEntity({ + type: "Sphere", + position: position, + dimensions: { + x: bSize, + y: bSize, + z: bSize + }, + color: { + red: 0, + green: 0, + blue: 0 + }, + velocity: bVelocity, + lifetime: BULLET_LIFETIME, + gravity: { + x: 0, + y: bGravity, + z: 0 + }, + damping: 0.01, + density: 8000, + ignoreCollisions: false, + collisionsWillMove: true + }); + + Script.addEventHandler(bulletID, "collisionWithEntity", entityCollisionWithEntity); + + // Play firing sounds + audioOptions.position = position; + Audio.playSound(fireSound, audioOptions); + shotsFired++; + if ((shotsFired % RELOAD_INTERVAL) == 0) { + Audio.playSound(loadSound, audioOptions); + } +} + +function shootTarget() { + var TARGET_SIZE = 0.50; + var TARGET_GRAVITY = 0.0; + var TARGET_LIFETIME = 300.0; + var TARGET_UP_VELOCITY = 0.0; + var TARGET_FWD_VELOCITY = 0.0; + var DISTANCE_TO_LAUNCH_FROM = 5.0; + var ANGLE_RANGE_FOR_LAUNCH = 20.0; + var camera = Camera.getPosition(); + + var targetDirection = Quat.angleAxis(getRandomFloat(-ANGLE_RANGE_FOR_LAUNCH, ANGLE_RANGE_FOR_LAUNCH), { + x: 0, + y: 1, + z: 0 + }); + targetDirection = Quat.multiply(Camera.getOrientation(), targetDirection); + var forwardVector = Quat.getFront(targetDirection); + + var newPosition = Vec3.sum(camera, Vec3.multiply(forwardVector, DISTANCE_TO_LAUNCH_FROM)); + + var velocity = Vec3.multiply(forwardVector, TARGET_FWD_VELOCITY); + velocity.y += TARGET_UP_VELOCITY; + + targetID = Entities.addEntity({ + type: "Box", + position: newPosition, + dimensions: { + x: TARGET_SIZE * (0.5 + Math.random()), + y: TARGET_SIZE * (0.5 + Math.random()), + z: TARGET_SIZE * (0.5 + Math.random()) / 4.0 + }, + color: { + red: Math.random() * 255, + green: Math.random() * 255, + blue: Math.random() * 255 + }, + velocity: velocity, + gravity: { + x: 0, + y: TARGET_GRAVITY, + z: 0 + }, + lifetime: TARGET_LIFETIME, + rotation: Camera.getOrientation(), + damping: 0.1, + density: 100.0, + collisionsWillMove: true + }); + + // Record start time + shotTime = new Date(); + + // Play target shoot sound + audioOptions.position = newPosition; + Audio.playSound(targetLaunchSound, audioOptions); +} + +function makeGrid(type, scale, size) { + var separation = scale * 2; + var pos = Vec3.sum(Camera.getPosition(), Vec3.multiply(10.0 * scale * separation, Quat.getFront(Camera.getOrientation()))); + var x, y, z; + var GRID_LIFE = 60.0; + var dimensions; + + for (x = 0; x < size; x++) { + for (y = 0; y < size; y++) { + for (z = 0; z < size; z++) { + + dimensions = { + x: separation / 2.0 * (0.5 + Math.random()), + y: separation / 2.0 * (0.5 + Math.random()), + z: separation / 2.0 * (0.5 + Math.random()) / 4.0 + }; + + Entities.addEntity({ + type: type, + position: { + x: pos.x + x * separation, + y: pos.y + y * separation, + z: pos.z + z * separation + }, + dimensions: dimensions, + color: { + red: Math.random() * 255, + green: Math.random() * 255, + blue: Math.random() * 255 + }, + velocity: { + x: 0, + y: 0, + z: 0 + }, + gravity: { + x: 0, + y: 0, + z: 0 + }, + lifetime: GRID_LIFE, + rotation: Camera.getOrientation(), + damping: 0.1, + density: 100.0, + collisionsWillMove: true + }); + } + } + } +} + +function makePlatform(gravity, scale, size) { + var separation = scale * 2; + var pos = Vec3.sum(Camera.getPosition(), Vec3.multiply(10.0 * scale * separation, Quat.getFront(Camera.getOrientation()))); + pos.y -= separation * size; + var x, y, z; + var TARGET_LIFE = 60.0; + var INITIAL_GAP = 0.5; + var dimensions; + + for (x = 0; x < size; x++) { + for (y = 0; y < size; y++) { + for (z = 0; z < size; z++) { + + dimensions = { + x: separation / 2.0, + y: separation, + z: separation / 2.0 + }; + + Entities.addEntity({ + type: "Box", + position: { + x: pos.x - (separation * size / 2.0) + x * separation, + y: pos.y + y * (separation + INITIAL_GAP), + z: pos.z - (separation * size / 2.0) + z * separation + }, + dimensions: dimensions, + color: { + red: Math.random() * 255, + green: Math.random() * 255, + blue: Math.random() * 255 + }, + velocity: { + x: 0, + y: 0.05, + z: 0 + }, + gravity: { + x: 0, + y: gravity, + z: 0 + }, + lifetime: TARGET_LIFE, + damping: 0.1, + density: 100.0, + collisionsWillMove: true + }); + } + } + } + + // Make a floor for this stuff to fall onto + Entities.addEntity({ + type: "Box", + position: { + x: pos.x, + y: pos.y - separation / 2.0, + z: pos.z + }, + dimensions: { + x: 2.0 * separation * size, + y: separation / 2.0, + z: 2.0 * separation * size + }, + color: { + red: 100, + green: 100, + blue: 100 + }, + lifetime: TARGET_LIFE + }); + +} + + + +function playLoadSound() { + audioOptions.position = MyAvatar.leftHandPose.translation; + Audio.playSound(loadSound, audioOptions); +} + +function deleteBulletAndTarget() { + Entities.deleteEntity(bulletID); + Entities.deleteEntity(targetID); + bulletID = false; + targetID = false; +} + +function update(deltaTime) { + // FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2] + var tipPoses = [ MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose ]; + + for (var side = 0; side < 2; side++) { + // First check if the controller is valid + var controllerPose = Controller.getPoseValue(POSE_CONTROLS[side]); + validPoses[side] = controllerPose.valid; + if (!controllerPose.valid) { + Overlays.editOverlay(pointer[side], { + visible: false + }); + continue; + } + + // Need to adjust the laser + var tipPose = tipPoses[side]; + var handRotation = tipPoses[side].rotation; + var barrelOffset = Vec3.multiplyQbyV(handRotation, BARREL_OFFSETS[side]); + barrelTips[side] = Vec3.sum(tipPose.translation, barrelOffset); + barrelVectors[side] = Vec3.multiplyQbyV(handRotation, { + x: 0, + y: 1, + z: 0 + }); + + var laserTip = Vec3.sum(Vec3.multiply(100.0, barrelVectors[side]), barrelTips[side]); + // Update Lasers + Overlays.editOverlay(pointer[side], { + start: barrelTips[side], + end: laserTip, + alpha: 1, + visible: true + }); + } +} + +function triggerChanged(side, value) { + var pressed = (value != 0); + if (pressed) { + var position = barrelTips[side]; + var velocity = Vec3.multiply(BULLET_VELOCITY, Vec3.normalize(barrelVectors[side])); + shootBullet(position, velocity, false); + } +} + +function mousePressEvent(event) { + var clickedOverlay = Overlays.getOverlayAtPoint({ + x: event.x, + y: event.y + }); + if (clickedOverlay == offButton) { + Script.stop(); + } else if (clickedOverlay == platformButton) { + var platformSize = 3; + makePlatform(-9.8, 1.0, platformSize); + } else if (clickedOverlay == gridButton) { + makeGrid("Box", 1.0, 3); + } +} + +function scriptEnding() { + mapping.disable(); + toolBar.cleanup(); + for (var i = 0; i < pointer.length; ++i) { + Overlays.deleteOverlay(pointer[i]); + } + Overlays.deleteOverlay(text); + MyAvatar.detachOne(GUN_MODEL); + MyAvatar.detachOne(GUN_MODEL); + clearPose(); +} + +MyAvatar.attach(GUN_MODEL, "LeftHand", GUN_OFFSETS[0], GUN_ORIENTATIONS[0], 0.40); +MyAvatar.attach(GUN_MODEL, "RightHand", GUN_OFFSETS[1], GUN_ORIENTATIONS[1], 0.40); + +// Give a bit of time to load before playing sound +Script.setTimeout(playLoadSound, 2000); + +mapping.from(Controller.Standard.LT).hysteresis(0.1, 0.5).to(function(value) { + triggerChanged(0, value); +}); + +mapping.from(Controller.Standard.RT).hysteresis(0.1, 0.5).to(function(value) { + triggerChanged(1, value); +}); +mapping.enable(); + +Script.scriptEnding.connect(scriptEnding); +Script.update.connect(update); +Controller.mousePressEvent.connect(mousePressEvent); +Controller.keyPressEvent.connect(keyPressEvent); From 40a15a15f30b81fbceee03ce1f2f342950444a6e Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 09:11:03 -0800 Subject: [PATCH 002/111] cleaning up pistol --- examples/weapons/pistol.js | 289 ++----------------------------------- 1 file changed, 14 insertions(+), 275 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index a9836f949e..e24b020181 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -18,9 +18,9 @@ // FIXME kickback functionality was removed because the joint setting interface in // MyAvatar has apparently changed, breaking it. -Script.include("../../libraries/utils.js"); -Script.include("../../libraries/constants.js"); -Script.include("../../libraries/toolBars.js"); +Script.include("../libraries/utils.js"); +Script.include("../libraries/constants.js"); +Script.include("../libraries/toolBars.js"); HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; @@ -82,7 +82,6 @@ function getRandomFloat(min, max) { return Math.random() * (max - min) + min; } -var showScore = false; // Load some sound to use for loading and firing var fireSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/GUN-SHOT2.raw"); var loadSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/Gun_Reload_Weapon22.raw"); @@ -115,89 +114,7 @@ var toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.gun.toolbar", }; }); -var offButton = toolBar.addOverlay("image", { - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/gun/close.svg", - alpha: 1 -}); -startX += BUTTON_SIZE + PADDING; -var platformButton = toolBar.addOverlay("image", { - x: startX, - y: screenSize.y - (BUTTON_SIZE + PADDING), - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/gun/platform-targets.svg", - alpha: 1 -}); - -startX += BUTTON_SIZE + PADDING; -var gridButton = toolBar.addOverlay("image", { - x: startX, - y: screenSize.y - (BUTTON_SIZE + PADDING), - width: BUTTON_SIZE, - height: BUTTON_SIZE, - imageURL: HIFI_PUBLIC_BUCKET + "images/gun/floating-targets.svg", - alpha: 1 -}); - -if (showScore) { - var text = Overlays.addOverlay("text", { - x: screenSize.x / 2 - 100, - y: screenSize.y / 2 - 50, - width: 150, - height: 50, - color: { - red: 0, - green: 0, - blue: 0 - }, - textColor: { - red: 255, - green: 0, - blue: 0 - }, - topMargin: 4, - leftMargin: 4, - text: "Score: " + score - }); -} - -function entityCollisionWithEntity(entity1, entity2, collision) { - if (entity2 === targetID) { - score++; - if (showScore) { - Overlays.editOverlay(text, { - text: "Score: " + score - }); - } - - // We will delete the bullet and target in 1/2 sec, but for now we can - // see them bounce! - Script.setTimeout(deleteBulletAndTarget, 500); - - // Turn the target and the bullet white - Entities.editEntity(entity1, { - color: { - red: 255, - green: 255, - blue: 255 - } - }); - Entities.editEntity(entity2, { - color: { - red: 255, - green: 255, - blue: 255 - } - }); - - // play the sound near the camera so the shooter can hear it - audioOptions.position = Vec3.sum(Camera.getPosition(), Quat.getFront(Camera.getOrientation())); - Audio.playSound(targetHitSound, audioOptions); - } -} function shootBullet(position, velocity, grenade) { var BULLET_SIZE = .09; @@ -248,202 +165,25 @@ function shootBullet(position, velocity, grenade) { } } -function shootTarget() { - var TARGET_SIZE = 0.50; - var TARGET_GRAVITY = 0.0; - var TARGET_LIFETIME = 300.0; - var TARGET_UP_VELOCITY = 0.0; - var TARGET_FWD_VELOCITY = 0.0; - var DISTANCE_TO_LAUNCH_FROM = 5.0; - var ANGLE_RANGE_FOR_LAUNCH = 20.0; - var camera = Camera.getPosition(); - - var targetDirection = Quat.angleAxis(getRandomFloat(-ANGLE_RANGE_FOR_LAUNCH, ANGLE_RANGE_FOR_LAUNCH), { - x: 0, - y: 1, - z: 0 - }); - targetDirection = Quat.multiply(Camera.getOrientation(), targetDirection); - var forwardVector = Quat.getFront(targetDirection); - - var newPosition = Vec3.sum(camera, Vec3.multiply(forwardVector, DISTANCE_TO_LAUNCH_FROM)); - - var velocity = Vec3.multiply(forwardVector, TARGET_FWD_VELOCITY); - velocity.y += TARGET_UP_VELOCITY; - - targetID = Entities.addEntity({ - type: "Box", - position: newPosition, - dimensions: { - x: TARGET_SIZE * (0.5 + Math.random()), - y: TARGET_SIZE * (0.5 + Math.random()), - z: TARGET_SIZE * (0.5 + Math.random()) / 4.0 - }, - color: { - red: Math.random() * 255, - green: Math.random() * 255, - blue: Math.random() * 255 - }, - velocity: velocity, - gravity: { - x: 0, - y: TARGET_GRAVITY, - z: 0 - }, - lifetime: TARGET_LIFETIME, - rotation: Camera.getOrientation(), - damping: 0.1, - density: 100.0, - collisionsWillMove: true - }); - - // Record start time - shotTime = new Date(); - - // Play target shoot sound - audioOptions.position = newPosition; - Audio.playSound(targetLaunchSound, audioOptions); -} - -function makeGrid(type, scale, size) { - var separation = scale * 2; - var pos = Vec3.sum(Camera.getPosition(), Vec3.multiply(10.0 * scale * separation, Quat.getFront(Camera.getOrientation()))); - var x, y, z; - var GRID_LIFE = 60.0; - var dimensions; - - for (x = 0; x < size; x++) { - for (y = 0; y < size; y++) { - for (z = 0; z < size; z++) { - - dimensions = { - x: separation / 2.0 * (0.5 + Math.random()), - y: separation / 2.0 * (0.5 + Math.random()), - z: separation / 2.0 * (0.5 + Math.random()) / 4.0 - }; - - Entities.addEntity({ - type: type, - position: { - x: pos.x + x * separation, - y: pos.y + y * separation, - z: pos.z + z * separation - }, - dimensions: dimensions, - color: { - red: Math.random() * 255, - green: Math.random() * 255, - blue: Math.random() * 255 - }, - velocity: { - x: 0, - y: 0, - z: 0 - }, - gravity: { - x: 0, - y: 0, - z: 0 - }, - lifetime: GRID_LIFE, - rotation: Camera.getOrientation(), - damping: 0.1, - density: 100.0, - collisionsWillMove: true - }); - } - } +function keyPressEvent(event) { + // if our tools are off, then don't do anything + if (event.text == "t") { + var time = MIN_THROWER_DELAY + Math.random() * MAX_THROWER_DELAY; + Script.setTimeout(shootTarget, time); + } else if ((event.text == ".") || (event.text == "SPACE")) { + shootFromMouse(false); + } else if (event.text == ",") { + shootFromMouse(true); + } else if (event.text == "r") { + playLoadSound(); } } -function makePlatform(gravity, scale, size) { - var separation = scale * 2; - var pos = Vec3.sum(Camera.getPosition(), Vec3.multiply(10.0 * scale * separation, Quat.getFront(Camera.getOrientation()))); - pos.y -= separation * size; - var x, y, z; - var TARGET_LIFE = 60.0; - var INITIAL_GAP = 0.5; - var dimensions; - - for (x = 0; x < size; x++) { - for (y = 0; y < size; y++) { - for (z = 0; z < size; z++) { - - dimensions = { - x: separation / 2.0, - y: separation, - z: separation / 2.0 - }; - - Entities.addEntity({ - type: "Box", - position: { - x: pos.x - (separation * size / 2.0) + x * separation, - y: pos.y + y * (separation + INITIAL_GAP), - z: pos.z - (separation * size / 2.0) + z * separation - }, - dimensions: dimensions, - color: { - red: Math.random() * 255, - green: Math.random() * 255, - blue: Math.random() * 255 - }, - velocity: { - x: 0, - y: 0.05, - z: 0 - }, - gravity: { - x: 0, - y: gravity, - z: 0 - }, - lifetime: TARGET_LIFE, - damping: 0.1, - density: 100.0, - collisionsWillMove: true - }); - } - } - } - - // Make a floor for this stuff to fall onto - Entities.addEntity({ - type: "Box", - position: { - x: pos.x, - y: pos.y - separation / 2.0, - z: pos.z - }, - dimensions: { - x: 2.0 * separation * size, - y: separation / 2.0, - z: 2.0 * separation * size - }, - color: { - red: 100, - green: 100, - blue: 100 - }, - lifetime: TARGET_LIFE - }); - -} - - - function playLoadSound() { audioOptions.position = MyAvatar.leftHandPose.translation; Audio.playSound(loadSound, audioOptions); } -function deleteBulletAndTarget() { - Entities.deleteEntity(bulletID); - Entities.deleteEntity(targetID); - bulletID = false; - targetID = false; -} - function update(deltaTime) { // FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2] var tipPoses = [ MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose ]; @@ -511,7 +251,6 @@ function scriptEnding() { for (var i = 0; i < pointer.length; ++i) { Overlays.deleteOverlay(pointer[i]); } - Overlays.deleteOverlay(text); MyAvatar.detachOne(GUN_MODEL); MyAvatar.detachOne(GUN_MODEL); clearPose(); From 667464b49368d064745ca52961008964b906a1b8 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 11:01:34 -0800 Subject: [PATCH 003/111] rat moves back with force when shot --- examples/weapons/pistol.js | 144 ++++----------------------------- examples/weapons/rat.js | 41 ++++++++++ examples/weapons/ratSpawner.js | 17 ++++ 3 files changed, 72 insertions(+), 130 deletions(-) create mode 100644 examples/weapons/rat.js create mode 100644 examples/weapons/ratSpawner.js diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index e24b020181..0b2d79021f 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -20,7 +20,6 @@ Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); -Script.include("../libraries/toolBars.js"); HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; @@ -54,6 +53,7 @@ var BARREL_OFFSETS = [ { z: 0.04 } ]; + var mapping = Controller.newMapping(); var validPoses = [ false, false ]; var barrelVectors = [ 0, 0 ]; @@ -78,111 +78,6 @@ pointer.push(Overlays.addOverlay("line3d", { lineWidth: LASER_WIDTH })); -function getRandomFloat(min, max) { - return Math.random() * (max - min) + min; -} - -// Load some sound to use for loading and firing -var fireSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/GUN-SHOT2.raw"); -var loadSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/Gun_Reload_Weapon22.raw"); -var impactSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Guns/BulletImpact2.raw"); -var targetHitSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Space%20Invaders/hit.raw"); -var targetLaunchSound = SoundCache.getSound(HIFI_PUBLIC_BUCKET + "sounds/Space%20Invaders/shoot.raw"); - -var audioOptions = { - volume: 0.9 -} - -var shotsFired = 0; -var shotTime = new Date(); -var isLaunchButtonPressed = false; -var score = 0; - -var bulletID = false; -var targetID = false; - -// Create overlay buttons and reticle -var BUTTON_SIZE = 32; -var PADDING = 3; -var NUM_BUTTONS = 3; -var screenSize = Controller.getViewportDimensions(); -var startX = screenSize.x / 2 - (NUM_BUTTONS * (BUTTON_SIZE + PADDING)) / 2; -var toolBar = new ToolBar(0, 0, ToolBar.HORIZONTAL, "highfidelity.gun.toolbar", function(screenSize) { - return { - x: startX, - y: (screenSize.y - (BUTTON_SIZE + PADDING)), - }; -}); - - - -function shootBullet(position, velocity, grenade) { - var BULLET_SIZE = .09; - var BULLET_LIFETIME = 10.0; - var BULLET_GRAVITY = -0.25; - var GRENADE_VELOCITY = 15.0; - var GRENADE_SIZE = 0.35; - var GRENADE_GRAVITY = -9.8; - - var bVelocity = grenade ? Vec3.multiply(GRENADE_VELOCITY, Vec3.normalize(velocity)) : velocity; - var bSize = grenade ? GRENADE_SIZE : BULLET_SIZE; - var bGravity = grenade ? GRENADE_GRAVITY : BULLET_GRAVITY; - - bulletID = Entities.addEntity({ - type: "Sphere", - position: position, - dimensions: { - x: bSize, - y: bSize, - z: bSize - }, - color: { - red: 0, - green: 0, - blue: 0 - }, - velocity: bVelocity, - lifetime: BULLET_LIFETIME, - gravity: { - x: 0, - y: bGravity, - z: 0 - }, - damping: 0.01, - density: 8000, - ignoreCollisions: false, - collisionsWillMove: true - }); - - Script.addEventHandler(bulletID, "collisionWithEntity", entityCollisionWithEntity); - - // Play firing sounds - audioOptions.position = position; - Audio.playSound(fireSound, audioOptions); - shotsFired++; - if ((shotsFired % RELOAD_INTERVAL) == 0) { - Audio.playSound(loadSound, audioOptions); - } -} - -function keyPressEvent(event) { - // if our tools are off, then don't do anything - if (event.text == "t") { - var time = MIN_THROWER_DELAY + Math.random() * MAX_THROWER_DELAY; - Script.setTimeout(shootTarget, time); - } else if ((event.text == ".") || (event.text == "SPACE")) { - shootFromMouse(false); - } else if (event.text == ",") { - shootFromMouse(true); - } else if (event.text == "r") { - playLoadSound(); - } -} - -function playLoadSound() { - audioOptions.position = MyAvatar.leftHandPose.translation; - Audio.playSound(loadSound, audioOptions); -} function update(deltaTime) { // FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2] @@ -224,30 +119,24 @@ function update(deltaTime) { function triggerChanged(side, value) { var pressed = (value != 0); if (pressed) { - var position = barrelTips[side]; - var velocity = Vec3.multiply(BULLET_VELOCITY, Vec3.normalize(barrelVectors[side])); - shootBullet(position, velocity, false); + var pickRay = { + origin: barrelTips[side], + direction: Vec3.normalize(barrelVectors[side]) + } + var intersection = Entities.findRayIntersection(pickRay, true); + if (intersection.intersects) { + if (intersection.properties.name === "rat") { + print("HOLY SHIT YOU JUST HIT A RAT!"); + var forceDirection = JSON.stringify({forceDirection: barrelVectors[side]}); + Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); + } + } } } -function mousePressEvent(event) { - var clickedOverlay = Overlays.getOverlayAtPoint({ - x: event.x, - y: event.y - }); - if (clickedOverlay == offButton) { - Script.stop(); - } else if (clickedOverlay == platformButton) { - var platformSize = 3; - makePlatform(-9.8, 1.0, platformSize); - } else if (clickedOverlay == gridButton) { - makeGrid("Box", 1.0, 3); - } -} function scriptEnding() { mapping.disable(); - toolBar.cleanup(); for (var i = 0; i < pointer.length; ++i) { Overlays.deleteOverlay(pointer[i]); } @@ -259,9 +148,6 @@ function scriptEnding() { MyAvatar.attach(GUN_MODEL, "LeftHand", GUN_OFFSETS[0], GUN_ORIENTATIONS[0], 0.40); MyAvatar.attach(GUN_MODEL, "RightHand", GUN_OFFSETS[1], GUN_ORIENTATIONS[1], 0.40); -// Give a bit of time to load before playing sound -Script.setTimeout(playLoadSound, 2000); - mapping.from(Controller.Standard.LT).hysteresis(0.1, 0.5).to(function(value) { triggerChanged(0, value); }); @@ -272,6 +158,4 @@ mapping.from(Controller.Standard.RT).hysteresis(0.1, 0.5).to(function(value) { mapping.enable(); Script.scriptEnding.connect(scriptEnding); -Script.update.connect(update); -Controller.mousePressEvent.connect(mousePressEvent); -Controller.keyPressEvent.connect(keyPressEvent); +Script.update.connect(update); \ No newline at end of file diff --git a/examples/weapons/rat.js b/examples/weapons/rat.js new file mode 100644 index 0000000000..f270146ac7 --- /dev/null +++ b/examples/weapons/rat.js @@ -0,0 +1,41 @@ +// +// Rat.js +// examples/toybox/entityScripts +// +// Created by Eric Levin on11/11/15. +// Copyright 2015 High Fidelity, Inc. +// + +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +/*global Rat, print, MyAvatar, Entities, AnimationCache, SoundCache, Scene, Camera, Overlays, Audio, HMD, AvatarList, AvatarManager, Controller, UndoStack, Window, Account, GlobalServices, Script, ScriptDiscoveryService, LODManager, Menu, Vec3, Quat, AudioDevice, Paths, Clipboard, Settings, XMLHttpRequest, randFloat, randInt */ + + +(function() { + + var _this; + Rat = function() { + _this = this; + }; + + Rat.prototype = { + + onHit: function(myId, params) { + print("OOWWW YOU SHOT ME!"); + var force = JSON.parse(params[0]); + // print(JSON.stringify(force)) + Entities.editEntity(this.entityID, { + velocity: force.forceDirection + }); + }, + + preload: function(entityID) { + print("HEY HEY I'm a rat!"); + this.entityID = entityID; + }, + + }; + + // entity scripts always need to return a newly constructed object of our type + return new Rat(); +}); \ No newline at end of file diff --git a/examples/weapons/ratSpawner.js b/examples/weapons/ratSpawner.js new file mode 100644 index 0000000000..e901fa028e --- /dev/null +++ b/examples/weapons/ratSpawner.js @@ -0,0 +1,17 @@ +var scriptURL = Script.resolvePath('rat.js'); +var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); +var rat = Entities.addEntity({ + type: "Box", + name: 'rat', + position: center, + dimensions: {x: 0.4, y: 0.4, z: 0.4}, + color: {red: 200, green: 20, blue: 200}, + collisionsWillMove: true, + script: scriptURL +}); + +function cleanup() { + Entities.deleteEntity(rat); +} + +Script.scriptEnding.connect(cleanup) \ No newline at end of file From 1e653133b44161612412ef328f94c007e395ea41 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 13:30:46 -0800 Subject: [PATCH 004/111] muzzle flashes --- examples/weapons/pistol.js | 93 ++++++++++++++++++++++++++++++++------ examples/weapons/rat.js | 3 -- 2 files changed, 79 insertions(+), 17 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 0b2d79021f..478fec88ec 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -22,16 +22,17 @@ Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; +var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); var LASER_WIDTH = 2; -var POSE_CONTROLS = [ Controller.Standard.LeftHand, Controller.Standard.RightHand ]; -var TRIGGER_CONTROLS = [ Controller.Standard.LT, Controller.Standard.RT ]; +var POSE_CONTROLS = [Controller.Standard.LeftHand, Controller.Standard.RightHand]; +var TRIGGER_CONTROLS = [Controller.Standard.LT, Controller.Standard.RT]; var MIN_THROWER_DELAY = 1000; var MAX_THROWER_DELAY = 1000; var RELOAD_INTERVAL = 5; var GUN_MODEL = HIFI_PUBLIC_BUCKET + "cozza13/gun/m1911-handgun+1.fbx?v=4"; var BULLET_VELOCITY = 10.0; -var GUN_OFFSETS = [ { +var GUN_OFFSETS = [{ x: -0.04, y: 0.26, z: 0.04 @@ -39,11 +40,11 @@ var GUN_OFFSETS = [ { x: 0.04, y: 0.26, z: 0.04 -} ]; +}]; -var GUN_ORIENTATIONS = [ Quat.fromPitchYawRollDegrees(0, 90, 90), Quat.fromPitchYawRollDegrees(0, -90, 270) ]; +var GUN_ORIENTATIONS = [Quat.fromPitchYawRollDegrees(0, 90, 90), Quat.fromPitchYawRollDegrees(0, -90, 270)]; -var BARREL_OFFSETS = [ { +var BARREL_OFFSETS = [{ x: -0.12, y: 0.12, z: 0.04 @@ -51,13 +52,13 @@ var BARREL_OFFSETS = [ { x: 0.12, y: 0.12, z: 0.04 -} ]; +}]; var mapping = Controller.newMapping(); -var validPoses = [ false, false ]; -var barrelVectors = [ 0, 0 ]; -var barrelTips = [ 0, 0 ]; +var validPoses = [false, false]; +var barrelVectors = [0, 0]; +var barrelTips = [0, 0]; var pointer = []; pointer.push(Overlays.addOverlay("line3d", { @@ -81,7 +82,7 @@ pointer.push(Overlays.addOverlay("line3d", { function update(deltaTime) { // FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2] - var tipPoses = [ MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose ]; + var tipPoses = [MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose]; for (var side = 0; side < 2; side++) { // First check if the controller is valid @@ -119,15 +120,22 @@ function update(deltaTime) { function triggerChanged(side, value) { var pressed = (value != 0); if (pressed) { + Audio.playSound(fireSound, { + position: barrelTips[side], + volume: 1.0 + }); var pickRay = { origin: barrelTips[side], direction: Vec3.normalize(barrelVectors[side]) - } + }; + createMuzzleFlash(barrelTips[side]); var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { if (intersection.properties.name === "rat") { print("HOLY SHIT YOU JUST HIT A RAT!"); - var forceDirection = JSON.stringify({forceDirection: barrelVectors[side]}); + var forceDirection = JSON.stringify({ + forceDirection: barrelVectors[side] + }); Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); } } @@ -135,6 +143,7 @@ function triggerChanged(side, value) { } + function scriptEnding() { mapping.disable(); for (var i = 0; i < pointer.length; ++i) { @@ -158,4 +167,60 @@ mapping.from(Controller.Standard.RT).hysteresis(0.1, 0.5).to(function(value) { mapping.enable(); Script.scriptEnding.connect(scriptEnding); -Script.update.connect(update); \ No newline at end of file +Script.update.connect(update); + + +function createMuzzleFlash(position) { + var flash = Entities.addEntity({ + type: "ParticleEffect", + position: position, + "name": "ParticlesTest Emitter", + "color": {}, + "maxParticles": 1000, + "lifespan": 0.1, + "emitRate": 200, + "emitSpeed": 0.3, + "speedSpread": 0, + "emitOrientation": { + "x": -0.4, + "y": 1, + "z": -0.2, + "w": 0.7071068286895752 + }, + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "emitRadiusStart": 1, + "polarStart": 0, + "polarFinish": 2, + "azimuthStart": -3.1415927410125732, + "azimuthFinish": 2, + "emitAcceleration": { + "x": 0, + "y": 0, + "z": 0 + }, + "accelerationSpread": { + "x": 0, + "y": 0, + "z": 0 + }, + "particleRadius": 0.006, + "radiusSpread": 0, + "radiusStart": 0.01, + "radiusFinish": 0.02, + "colorSpread": {}, + "colorStart": {}, + "colorFinish": {}, + "alpha": 1, + "alphaSpread": 0, + "alphaStart": 1, + "alphaFinish": 1, + "additiveBlending": 0, + "textures": "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png" + }); + + +} \ No newline at end of file diff --git a/examples/weapons/rat.js b/examples/weapons/rat.js index f270146ac7..9df1b1032e 100644 --- a/examples/weapons/rat.js +++ b/examples/weapons/rat.js @@ -21,16 +21,13 @@ Rat.prototype = { onHit: function(myId, params) { - print("OOWWW YOU SHOT ME!"); var force = JSON.parse(params[0]); - // print(JSON.stringify(force)) Entities.editEntity(this.entityID, { velocity: force.forceDirection }); }, preload: function(entityID) { - print("HEY HEY I'm a rat!"); this.entityID = entityID; }, From 5789ca20178311848d73956e11784f68cbf14ac1 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 14:20:58 -0800 Subject: [PATCH 005/111] particle hit against walls --- examples/weapons/pistol.js | 158 +++++++++++++++++++++++++++++---- examples/weapons/rat.js | 6 +- examples/weapons/ratSpawner.js | 1 + 3 files changed, 148 insertions(+), 17 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 478fec88ec..8e14db1b66 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -137,6 +137,10 @@ function triggerChanged(side, value) { forceDirection: barrelVectors[side] }); Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); + } else { + Script.setTimeout(function() { + createWallHit(intersection.intersection); + }, 50); } } } @@ -170,16 +174,21 @@ Script.scriptEnding.connect(scriptEnding); Script.update.connect(update); -function createMuzzleFlash(position) { +function createWallHit(position) { var flash = Entities.addEntity({ type: "ParticleEffect", position: position, - "name": "ParticlesTest Emitter", - "color": {}, + lifetime: 4, + "name": "Wall hit emitter", + "color": { + red: 228, + green: 128, + blue: 12 + }, "maxParticles": 1000, "lifespan": 0.1, - "emitRate": 200, - "emitSpeed": 0.3, + "emitRate": 1000, + "emitSpeed": 1, "speedSpread": 0, "emitOrientation": { "x": -0.4, @@ -192,9 +201,8 @@ function createMuzzleFlash(position) { "y": 0, "z": 0 }, - "emitRadiusStart": 1, "polarStart": 0, - "polarFinish": 2, + "polarFinish": Math.PI, "azimuthStart": -3.1415927410125732, "azimuthFinish": 2, "emitAcceleration": { @@ -207,20 +215,140 @@ function createMuzzleFlash(position) { "y": 0, "z": 0 }, - "particleRadius": 0.006, - "radiusSpread": 0, + "particleRadius": 0.01, + "radiusSpread": 0.005, "radiusStart": 0.01, - "radiusFinish": 0.02, - "colorSpread": {}, - "colorStart": {}, - "colorFinish": {}, + "radiusFinish": 0.01, + "colorSpread": { + red: 100, + green: 100, + blue: 20 + }, "alpha": 1, "alphaSpread": 0, - "alphaStart": 1, - "alphaFinish": 1, + "alphaStart": 0, + "alphaFinish": 0, + "additiveBlending": true, + "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" + }); + + Script.setTimeout(function() { + Entities.editEntity(flash, { + isEmitting: false + }); + }, 100); + +} + +function createMuzzleFlash(position) { + var smoke = Entities.addEntity({ + type: "ParticleEffect", + position: position, + lifetime: 2, + "name": "Smoke Hit Emitter", + "maxParticles": 1000, + "lifespan": 4, + "emitRate": 20, + emitSpeed: 0, + "speedSpread": 0, + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "polarStart": 0, + "polarFinish": 0, + "azimuthStart": -3.1415927410125732, + "azimuthFinish": 3.14, + "emitAcceleration": { + "x": 0, + "y": 0.5, + "z": 0 + }, + "accelerationSpread": { + "x": .2, + "y": 0, + "z": .2 + }, + "radiusSpread": .02, + "particleRadius": 0.05, + "radiusStart": 0.05, + "radiusFinish": 0.05, + "alpha": 1, + "alphaSpread": 0, + "alphaStart": 0, + "alphaFinish": 0, "additiveBlending": 0, "textures": "https://hifi-public.s3.amazonaws.com/alan/Particles/Particle-Sprite-Smoke-1.png" }); + Script.setTimeout(function() { + Entities.editEntity(smoke, { + isEmitting: false + }) + }, 100); + + var flash = Entities.addEntity({ + type: "ParticleEffect", + position: position, + lifetime: 4, + "name": "Flash Emitter", + "color": { + red: 228, + green: 128, + blue: 12 + }, + "maxParticles": 1000, + "lifespan": 0.1, + "emitRate": 1000, + "emitSpeed": 1, + "speedSpread": 0, + "emitOrientation": { + "x": -0.4, + "y": 1, + "z": -0.2, + "w": 0.7071068286895752 + }, + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "polarStart": 0, + "polarFinish": Math.PI, + "azimuthStart": -3.1415927410125732, + "azimuthFinish": 2, + "emitAcceleration": { + "x": 0, + "y": 0, + "z": 0 + }, + "accelerationSpread": { + "x": 0, + "y": 0, + "z": 0 + }, + "particleRadius": 0.01, + "radiusSpread": 0.005, + "radiusStart": 0.01, + "radiusFinish": 0.01, + "colorSpread": { + red: 100, + green: 100, + blue: 20 + }, + "alpha": 1, + "alphaSpread": 0, + "alphaStart": 0, + "alphaFinish": 0, + "additiveBlending": true, + "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" + }); + + Script.setTimeout(function() { + Entities.editEntity(flash, { + isEmitting: false + }); + }, 100) } \ No newline at end of file diff --git a/examples/weapons/rat.js b/examples/weapons/rat.js index 9df1b1032e..39845667f0 100644 --- a/examples/weapons/rat.js +++ b/examples/weapons/rat.js @@ -16,14 +16,16 @@ var _this; Rat = function() { _this = this; + this.forceMultiplier = 10.0 }; Rat.prototype = { onHit: function(myId, params) { - var force = JSON.parse(params[0]); + var data = JSON.parse(params[0]); + var force = Vec3.multiply(data.forceDirection, this.forceMultiplier); Entities.editEntity(this.entityID, { - velocity: force.forceDirection + velocity: force }); }, diff --git a/examples/weapons/ratSpawner.js b/examples/weapons/ratSpawner.js index e901fa028e..9390604daf 100644 --- a/examples/weapons/ratSpawner.js +++ b/examples/weapons/ratSpawner.js @@ -6,6 +6,7 @@ var rat = Entities.addEntity({ position: center, dimensions: {x: 0.4, y: 0.4, z: 0.4}, color: {red: 200, green: 20, blue: 200}, + gravity: {x: 0, y: -9.8, z: 0}, collisionsWillMove: true, script: scriptURL }); From e78a079796c05102a50c4116f0f82a54b13542ee Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 14:28:30 -0800 Subject: [PATCH 006/111] tweaks to particle props --- examples/weapons/pistol.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 8e14db1b66..8e8dfd6be1 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -179,7 +179,7 @@ function createWallHit(position) { type: "ParticleEffect", position: position, lifetime: 4, - "name": "Wall hit emitter", + "name": "Flash Emitter", "color": { red: 228, green: 128, @@ -244,7 +244,7 @@ function createMuzzleFlash(position) { var smoke = Entities.addEntity({ type: "ParticleEffect", position: position, - lifetime: 2, + lifetime: 1, "name": "Smoke Hit Emitter", "maxParticles": 1000, "lifespan": 4, @@ -270,11 +270,11 @@ function createMuzzleFlash(position) { "y": 0, "z": .2 }, - "radiusSpread": .02, - "particleRadius": 0.05, - "radiusStart": 0.05, - "radiusFinish": 0.05, - "alpha": 1, + "radiusSpread": .04, + "particleRadius": 0.07, + "radiusStart": 0.07, + "radiusFinish": 0.07, + "alpha": 0.7, "alphaSpread": 0, "alphaStart": 0, "alphaFinish": 0, @@ -291,7 +291,7 @@ function createMuzzleFlash(position) { type: "ParticleEffect", position: position, lifetime: 4, - "name": "Flash Emitter", + "name": "Wall hit emitter", "color": { red: 228, green: 128, @@ -300,7 +300,7 @@ function createMuzzleFlash(position) { "maxParticles": 1000, "lifespan": 0.1, "emitRate": 1000, - "emitSpeed": 1, + "emitSpeed": 0.5, "speedSpread": 0, "emitOrientation": { "x": -0.4, @@ -327,10 +327,10 @@ function createMuzzleFlash(position) { "y": 0, "z": 0 }, - "particleRadius": 0.01, - "radiusSpread": 0.005, - "radiusStart": 0.01, - "radiusFinish": 0.01, + "particleRadius": 0.05, + "radiusSpread": 0.01, + "radiusStart": 0.05, + "radiusFinish": 0.05, "colorSpread": { red: 100, green: 100, From fa244f8670d6dbf63a53f855e8a0441b4e482f62 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 14:50:16 -0800 Subject: [PATCH 007/111] updated rat model --- examples/weapons/pistol.js | 14 ++++++++++++-- examples/weapons/ratSpawner.js | 9 +++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 8e8dfd6be1..7fb620550d 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -25,6 +25,7 @@ HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); var LASER_WIDTH = 2; +var GUN_FORCE = 10; var POSE_CONTROLS = [Controller.Standard.LeftHand, Controller.Standard.RightHand]; var TRIGGER_CONTROLS = [Controller.Standard.LT, Controller.Standard.RT]; var MIN_THROWER_DELAY = 1000; @@ -61,6 +62,8 @@ var barrelVectors = [0, 0]; var barrelTips = [0, 0]; var pointer = []; +var shootAnything = false; + pointer.push(Overlays.addOverlay("line3d", { start: ZERO_VECTOR, end: ZERO_VECTOR, @@ -124,9 +127,11 @@ function triggerChanged(side, value) { position: barrelTips[side], volume: 1.0 }); + + var shotDirection = Vec3.normalize(barrelVectors[side]); var pickRay = { origin: barrelTips[side], - direction: Vec3.normalize(barrelVectors[side]) + direction: shotDirection }; createMuzzleFlash(barrelTips[side]); var intersection = Entities.findRayIntersection(pickRay, true); @@ -134,11 +139,16 @@ function triggerChanged(side, value) { if (intersection.properties.name === "rat") { print("HOLY SHIT YOU JUST HIT A RAT!"); var forceDirection = JSON.stringify({ - forceDirection: barrelVectors[side] + forceDirection: shotDirection }); Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); } else { Script.setTimeout(function() { + if (shootAnything) { + Entities.editEntity(intersection.entityID, { + velocity: Vec3.multiply(shotDirection, GUN_FORCE) + }); + } createWallHit(intersection.intersection); }, 50); } diff --git a/examples/weapons/ratSpawner.js b/examples/weapons/ratSpawner.js index 9390604daf..f49f7ac4b9 100644 --- a/examples/weapons/ratSpawner.js +++ b/examples/weapons/ratSpawner.js @@ -1,10 +1,15 @@ var scriptURL = Script.resolvePath('rat.js'); +var naturalDimensions = {x: 11.31, y: 3.18, z: 2.19}; +var sizeScale = 0.1; +var dimensions = Vec3.multiply(naturalDimensions, sizeScale); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); var rat = Entities.addEntity({ - type: "Box", + type: "Model", + modelURL: "https://hifi-public.s3.amazonaws.com/ryan/rat.fbx", name: 'rat', position: center, - dimensions: {x: 0.4, y: 0.4, z: 0.4}, + dimensions: dimensions, + shapeType: 'box', color: {red: 200, green: 20, blue: 200}, gravity: {x: 0, y: -9.8, z: 0}, collisionsWillMove: true, From 466caffd00d12018544919eb0eb22977daf8944a Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 15:48:06 -0800 Subject: [PATCH 008/111] trying to add ik --- examples/weapons/pistol.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 7fb620550d..117c8b4c0a 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -15,12 +15,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -// FIXME kickback functionality was removed because the joint setting interface in -// MyAvatar has apparently changed, breaking it. - Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); +var animVarName = "rightHandPosition"; +var jointName = "RightHand"; + HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); @@ -120,6 +120,11 @@ function update(deltaTime) { } } +var kickback = function() { + print("TEST PARAM *******"); + print(JSON.stringify(testParam)); +} + function triggerChanged(side, value) { var pressed = (value != 0); if (pressed) { @@ -134,10 +139,10 @@ function triggerChanged(side, value) { direction: shotDirection }; createMuzzleFlash(barrelTips[side]); + var kickbackHandler = MyAvatar.addAnimationStateHandler(kickback, ["yo"]); var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { if (intersection.properties.name === "rat") { - print("HOLY SHIT YOU JUST HIT A RAT!"); var forceDirection = JSON.stringify({ forceDirection: shotDirection }); @@ -226,9 +231,9 @@ function createWallHit(position) { "z": 0 }, "particleRadius": 0.01, - "radiusSpread": 0.005, - "radiusStart": 0.01, - "radiusFinish": 0.01, + "radiusSpread": 0.01, + "radiusStart": 0.02, + "radiusFinish": 0.02, "colorSpread": { red: 100, green: 100, From 1bf8ced2c4425d6c894e2a04035d7f6741726f8b Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Wed, 11 Nov 2015 16:40:44 -0800 Subject: [PATCH 009/111] kickback working --- examples/weapons/pistol.js | 57 +++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 117c8b4c0a..4294a54afb 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -19,8 +19,11 @@ Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); var animVarName = "rightHandPosition"; +var handlerId; var jointName = "RightHand"; +var kickBackForce = 0.01; + HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); @@ -83,6 +86,34 @@ pointer.push(Overlays.addOverlay("line3d", { })); +// For transforming between world space and our avatar's model space. +var myHipsJointIndex, avatarToModelTranslation, avatarToWorldTranslation, avatarToWorldRotation, worldToAvatarRotation; +var avatarToModelRotation = Quat.angleAxis(180, { + x: 0, + y: 1, + z: 0 +}); // N.B.: Our C++ angleAxis takes radians, while our javascript angleAxis takes degrees! +var modelToAvatarRotation = Quat.inverse(avatarToModelRotation); // Flip 180 gives same result without inverse, but being explicit to track the math. + +function updateMyCoordinateSystem() { + avatarToWorldTranslation = MyAvatar.position; + avatarToWorldRotation = MyAvatar.orientation; + worldToAvatarRotation = Quat.inverse(avatarToWorldRotation); + avatarToModelTranslation = MyAvatar.getJointTranslation(myHipsJointIndex); // Should really be done on the bind pose. +} + +// Just math. + +function modelToWorld(modelPoint) { + var avatarPoint = Vec3.subtract(Vec3.multiplyQbyV(modelToAvatarRotation, modelPoint), avatarToModelTranslation); + return Vec3.sum(Vec3.multiplyQbyV(avatarToWorldRotation, avatarPoint), avatarToWorldTranslation); +} + +function worldToModel(worldPoint) { + var avatarPoint = Vec3.multiplyQbyV(worldToAvatarRotation, Vec3.subtract(worldPoint, avatarToWorldTranslation)); + return Vec3.multiplyQbyV(avatarToModelRotation, Vec3.sum(avatarPoint, avatarToModelTranslation)); +} + function update(deltaTime) { // FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2] var tipPoses = [MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose]; @@ -120,12 +151,19 @@ function update(deltaTime) { } } -var kickback = function() { - print("TEST PARAM *******"); - print(JSON.stringify(testParam)); +var kickback = function(animationProperties) { + var modelHandPosition = animationProperties[animVarName]; + var worldHandPosition = modelToWorld(modelHandPosition); + var targetHandWorldPosition = Vec3.sum(worldHandPosition, {x: 0, y: .1, z: 0}); + var targetHandModelPosition = worldToModel(targetHandWorldPosition); + var result = {}; + result[animVarName] = targetHandModelPosition; + return result; + } function triggerChanged(side, value) { + var pressed = (value != 0); if (pressed) { Audio.playSound(fireSound, { @@ -139,7 +177,8 @@ function triggerChanged(side, value) { direction: shotDirection }; createMuzzleFlash(barrelTips[side]); - var kickbackHandler = MyAvatar.addAnimationStateHandler(kickback, ["yo"]); + startKickback(); + var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { if (intersection.properties.name === "rat") { @@ -161,6 +200,16 @@ function triggerChanged(side, value) { } } +function startKickback() { + handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); + Script.setTimeout(function() { + kickBackForce *= -1; + Script.setTimeout(function() { + MyAvatar.removeAnimationStateHandler(handlerId); + }, 100) + }, 100); +} + function scriptEnding() { From d43c63c109cfb7e4abb12fac3f744b832be00107 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 12 Nov 2015 10:28:51 -0800 Subject: [PATCH 010/111] going to add kickback decay --- examples/weapons/pistol.js | 63 +++++++++----------------------------- 1 file changed, 15 insertions(+), 48 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 4294a54afb..f1b0761933 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -63,27 +63,9 @@ var mapping = Controller.newMapping(); var validPoses = [false, false]; var barrelVectors = [0, 0]; var barrelTips = [0, 0]; -var pointer = []; -var shootAnything = false; -pointer.push(Overlays.addOverlay("line3d", { - start: ZERO_VECTOR, - end: ZERO_VECTOR, - color: COLORS.RED, - alpha: 1, - visible: true, - lineWidth: LASER_WIDTH -})); - -pointer.push(Overlays.addOverlay("line3d", { - start: ZERO_VECTOR, - end: ZERO_VECTOR, - color: COLORS.RED, - alpha: 1, - visible: true, - lineWidth: LASER_WIDTH -})); +var shootAnything = true; // For transforming between world space and our avatar's model space. @@ -122,13 +104,6 @@ function update(deltaTime) { // First check if the controller is valid var controllerPose = Controller.getPoseValue(POSE_CONTROLS[side]); validPoses[side] = controllerPose.valid; - if (!controllerPose.valid) { - Overlays.editOverlay(pointer[side], { - visible: false - }); - continue; - } - // Need to adjust the laser var tipPose = tipPoses[side]; var handRotation = tipPoses[side].rotation; @@ -140,21 +115,13 @@ function update(deltaTime) { z: 0 }); - var laserTip = Vec3.sum(Vec3.multiply(100.0, barrelVectors[side]), barrelTips[side]); - // Update Lasers - Overlays.editOverlay(pointer[side], { - start: barrelTips[side], - end: laserTip, - alpha: 1, - visible: true - }); } } var kickback = function(animationProperties) { var modelHandPosition = animationProperties[animVarName]; var worldHandPosition = modelToWorld(modelHandPosition); - var targetHandWorldPosition = Vec3.sum(worldHandPosition, {x: 0, y: .1, z: 0}); + var targetHandWorldPosition = Vec3.sum(worldHandPosition, {x: 0, y: .05, z: 0}); var targetHandModelPosition = worldToModel(targetHandWorldPosition); var result = {}; result[animVarName] = targetHandModelPosition; @@ -201,22 +168,22 @@ function triggerChanged(side, value) { } function startKickback() { - handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); - Script.setTimeout(function() { - kickBackForce *= -1; + if (!handlerId) { + handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); Script.setTimeout(function() { - MyAvatar.removeAnimationStateHandler(handlerId); - }, 100) - }, 100); + kickBackForce *= -1; + Script.setTimeout(function() { + MyAvatar.removeAnimationStateHandler(handlerId); + handlerId = null; + }, 100) + }, 100); + } } function scriptEnding() { mapping.disable(); - for (var i = 0; i < pointer.length; ++i) { - Overlays.deleteOverlay(pointer[i]); - } MyAvatar.detachOne(GUN_MODEL); MyAvatar.detachOne(GUN_MODEL); clearPose(); @@ -250,7 +217,7 @@ function createWallHit(position) { blue: 12 }, "maxParticles": 1000, - "lifespan": 0.1, + "lifespan": 0.15, "emitRate": 1000, "emitSpeed": 1, "speedSpread": 0, @@ -279,10 +246,10 @@ function createWallHit(position) { "y": 0, "z": 0 }, - "particleRadius": 0.01, - "radiusSpread": 0.01, + "particleRadius": 0.03, + "radiusSpread": 0.02, "radiusStart": 0.02, - "radiusFinish": 0.02, + "radiusFinish": 0.03, "colorSpread": { red: 100, green: 100, From 13ec8f631d5b22127dc042e1015dd9512b801021 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 12 Nov 2015 16:20:17 -0800 Subject: [PATCH 011/111] Workign with ik, fixed a bug --- .../whiteboard/eraseBoardEntityScript.js | 1 + examples/weapons/pistol.js | 62 ++++++++++--------- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/examples/painting/whiteboard/eraseBoardEntityScript.js b/examples/painting/whiteboard/eraseBoardEntityScript.js index dd36242a6d..f291d4b41b 100644 --- a/examples/painting/whiteboard/eraseBoardEntityScript.js +++ b/examples/painting/whiteboard/eraseBoardEntityScript.js @@ -29,6 +29,7 @@ }, eraseBoard: function() { + print("SHNUUR") Entities.callEntityMethod(this.whiteboard, "eraseBoard"); }, diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index f1b0761933..07bd694f1d 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -1,16 +1,13 @@ // -// gun.js +// pistol.js // examples // -// Created by Brad Hefta-Gaub on 12/31/13. -// Modified by Philip on 3/3/14 -// Modified by Thijs Wenker on 3/31/15 +// Created by Eric Levin on 11/12/2015 // Copyright 2013 High Fidelity, Inc. // // This is an example script that turns the hydra controllers and mouse into a entity gun. -// It reads the controller, watches for trigger pulls, and launches entities. -// When entities collide with voxels they blow little holes out of the voxels. -// +// It reads the controller, watches for trigger pulls, and adds a force to any entity it hits + // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // @@ -19,8 +16,11 @@ Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); var animVarName = "rightHandPosition"; -var handlerId; -var jointName = "RightHand"; +var handlerId = null; +var rightHandJoint = "RightHand"; +var rightHandJointIndex = MyAvatar.getJointIndex(rightHandJoint); +var hipJoint = 'Hips'; +var myHipsJointIndex = MyAvatar.getJointIndex(hipJoint); var kickBackForce = 0.01; @@ -69,7 +69,7 @@ var shootAnything = true; // For transforming between world space and our avatar's model space. -var myHipsJointIndex, avatarToModelTranslation, avatarToWorldTranslation, avatarToWorldRotation, worldToAvatarRotation; +var avatarToModelTranslation, avatarToWorldTranslation, avatarToWorldRotation, worldToAvatarRotation; var avatarToModelRotation = Quat.angleAxis(180, { x: 0, y: 1, @@ -78,6 +78,7 @@ var avatarToModelRotation = Quat.angleAxis(180, { var modelToAvatarRotation = Quat.inverse(avatarToModelRotation); // Flip 180 gives same result without inverse, but being explicit to track the math. function updateMyCoordinateSystem() { + print("SJHSJKHSJKHS") avatarToWorldTranslation = MyAvatar.position; avatarToWorldRotation = MyAvatar.orientation; worldToAvatarRotation = Quat.inverse(avatarToWorldRotation); @@ -118,19 +119,37 @@ function update(deltaTime) { } } +var kickbackAmount = 0.5; +var k = 1; var kickback = function(animationProperties) { - var modelHandPosition = animationProperties[animVarName]; - var worldHandPosition = modelToWorld(modelHandPosition); - var targetHandWorldPosition = Vec3.sum(worldHandPosition, {x: 0, y: .05, z: 0}); + targetHandWorldPosition= Vec3.mix(finalTargetHandWorldPosition, targetHandWorldPosition, 0.1); + // print("WORLD POS " + JSON.stringify(targetHandWorldPosition)); var targetHandModelPosition = worldToModel(targetHandWorldPosition); + print("MODEL POS " + JSON.stringify(targetHandModelPosition)); var result = {}; result[animVarName] = targetHandModelPosition; return result; - } -function triggerChanged(side, value) { +var finalTargetHandWorldPosition, targetHandWorldPosition; + +function startKickback() { + if (!handlerId) { + updateMyCoordinateSystem(); + finalTargetHandWorldPosition = MyAvatar.getJointPosition(rightHandJointIndex); + targetHandWorldPosition = Vec3.sum(finalTargetHandWorldPosition, {x: 0, y: kickbackAmount, y: 0}); + handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); + Script.setTimeout(function() { + MyAvatar.removeAnimationStateHandler(handlerId); + handlerId = null; + k = 0; + }, 1000); + } +} + + +function triggerChanged(side, value) { var pressed = (value != 0); if (pressed) { Audio.playSound(fireSound, { @@ -167,19 +186,6 @@ function triggerChanged(side, value) { } } -function startKickback() { - if (!handlerId) { - handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); - Script.setTimeout(function() { - kickBackForce *= -1; - Script.setTimeout(function() { - MyAvatar.removeAnimationStateHandler(handlerId); - handlerId = null; - }, 100) - }, 100); - } -} - function scriptEnding() { From f50c4d3b181490fc3954fed158908aa62a396b3d Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Thu, 12 Nov 2015 16:32:37 -0800 Subject: [PATCH 012/111] IK working --- examples/weapons/pistol.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 07bd694f1d..ef9acc4c74 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -119,32 +119,33 @@ function update(deltaTime) { } } -var kickbackAmount = 0.5; -var k = 1; +var kickbackAmount = -0.5; +var k = 0; +var decaySpeed = .1; var kickback = function(animationProperties) { - targetHandWorldPosition= Vec3.mix(finalTargetHandWorldPosition, targetHandWorldPosition, 0.1); - // print("WORLD POS " + JSON.stringify(targetHandWorldPosition)); - var targetHandModelPosition = worldToModel(targetHandWorldPosition); - print("MODEL POS " + JSON.stringify(targetHandModelPosition)); + var currentTargetHandWorldPosition= Vec3.mix(startingTargetHandWorldPosition, finalTargetHandWorldPosition, k); + k += decaySpeed; + // print("WORLD POS " + JSON.stringify(startingTargetHandWorldPosition)); + var targetHandModelPosition = worldToModel(currentTargetHandWorldPosition); var result = {}; result[animVarName] = targetHandModelPosition; return result; } -var finalTargetHandWorldPosition, targetHandWorldPosition; +var finalTargetHandWorldPosition, startingTargetHandWorldPosition; function startKickback() { if (!handlerId) { updateMyCoordinateSystem(); finalTargetHandWorldPosition = MyAvatar.getJointPosition(rightHandJointIndex); - targetHandWorldPosition = Vec3.sum(finalTargetHandWorldPosition, {x: 0, y: kickbackAmount, y: 0}); + startingTargetHandWorldPosition = Vec3.sum(finalTargetHandWorldPosition, {x: 0, y: 0.5, z: 0}); handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); Script.setTimeout(function() { MyAvatar.removeAnimationStateHandler(handlerId); handlerId = null; k = 0; - }, 1000); + }, 200); } } From 281d058a24f44255ae9537361fcf12c28fc80c8e Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 13 Nov 2015 10:55:09 -0800 Subject: [PATCH 013/111] pistol tweaking --- examples/weapons/pistol.js | 183 ++++++++++++++++++------------------- 1 file changed, 90 insertions(+), 93 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index ef9acc4c74..42e3d033bb 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -15,18 +15,9 @@ Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); -var animVarName = "rightHandPosition"; -var handlerId = null; -var rightHandJoint = "RightHand"; -var rightHandJointIndex = MyAvatar.getJointIndex(rightHandJoint); -var hipJoint = 'Hips'; -var myHipsJointIndex = MyAvatar.getJointIndex(hipJoint); - -var kickBackForce = 0.01; - HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); - +var LASER_LENGTH = 10; var LASER_WIDTH = 2; var GUN_FORCE = 10; var POSE_CONTROLS = [Controller.Standard.LeftHand, Controller.Standard.RightHand]; @@ -59,6 +50,26 @@ var BARREL_OFFSETS = [{ }]; +var pointers = []; + +pointers.push(Overlays.addOverlay("line3d", { + start: ZERO_VECTOR, + end: ZERO_VECTOR, + color: COLORS.RED, + alpha: 1, + visible: true, + lineWidth: LASER_WIDTH +})); + +pointers.push(Overlays.addOverlay("line3d", { + start: ZERO_VECTOR, + end: ZERO_VECTOR, + color: COLORS.RED, + alpha: 1, + visible: true, + lineWidth: LASER_WIDTH +})); + var mapping = Controller.newMapping(); var validPoses = [false, false]; var barrelVectors = [0, 0]; @@ -68,35 +79,6 @@ var barrelTips = [0, 0]; var shootAnything = true; -// For transforming between world space and our avatar's model space. -var avatarToModelTranslation, avatarToWorldTranslation, avatarToWorldRotation, worldToAvatarRotation; -var avatarToModelRotation = Quat.angleAxis(180, { - x: 0, - y: 1, - z: 0 -}); // N.B.: Our C++ angleAxis takes radians, while our javascript angleAxis takes degrees! -var modelToAvatarRotation = Quat.inverse(avatarToModelRotation); // Flip 180 gives same result without inverse, but being explicit to track the math. - -function updateMyCoordinateSystem() { - print("SJHSJKHSJKHS") - avatarToWorldTranslation = MyAvatar.position; - avatarToWorldRotation = MyAvatar.orientation; - worldToAvatarRotation = Quat.inverse(avatarToWorldRotation); - avatarToModelTranslation = MyAvatar.getJointTranslation(myHipsJointIndex); // Should really be done on the bind pose. -} - -// Just math. - -function modelToWorld(modelPoint) { - var avatarPoint = Vec3.subtract(Vec3.multiplyQbyV(modelToAvatarRotation, modelPoint), avatarToModelTranslation); - return Vec3.sum(Vec3.multiplyQbyV(avatarToWorldRotation, avatarPoint), avatarToWorldTranslation); -} - -function worldToModel(worldPoint) { - var avatarPoint = Vec3.multiplyQbyV(worldToAvatarRotation, Vec3.subtract(worldPoint, avatarToWorldTranslation)); - return Vec3.multiplyQbyV(avatarToModelRotation, Vec3.sum(avatarPoint, avatarToModelTranslation)); -} - function update(deltaTime) { // FIXME we should also expose MyAvatar.handPoses[2], MyAvatar.tipPoses[2] var tipPoses = [MyAvatar.leftHandTipPose, MyAvatar.rightHandTipPose]; @@ -116,14 +98,37 @@ function update(deltaTime) { z: 0 }); + var laserTip = Vec3.sum(Vec3.multiply(LASER_LENGTH, barrelVectors[side]), barrelTips[side]); + // Update Lasers + Overlays.editOverlay(pointers[side], { + start: barrelTips[side], + end: laserTip, + alpha: 1, + }); + + } } -var kickbackAmount = -0.5; + + +function displayPointer(side) { + Overlays.editOverlay(pointers[side], { + visible: true + }); +} + +function hidePointer(side) { + Overlays.editOverlay(pointers[side], { + visible: false + }); +} + +var kickbackAmount = -0.1; var k = 0; -var decaySpeed = .1; +var decaySpeed = .02; var kickback = function(animationProperties) { - var currentTargetHandWorldPosition= Vec3.mix(startingTargetHandWorldPosition, finalTargetHandWorldPosition, k); + var currentTargetHandWorldPosition = Vec3.mix(startingTargetHandWorldPosition, finalTargetHandWorldPosition, k); k += decaySpeed; // print("WORLD POS " + JSON.stringify(startingTargetHandWorldPosition)); var targetHandModelPosition = worldToModel(currentTargetHandWorldPosition); @@ -133,56 +138,38 @@ var kickback = function(animationProperties) { } -var finalTargetHandWorldPosition, startingTargetHandWorldPosition; - -function startKickback() { - if (!handlerId) { - updateMyCoordinateSystem(); - finalTargetHandWorldPosition = MyAvatar.getJointPosition(rightHandJointIndex); - startingTargetHandWorldPosition = Vec3.sum(finalTargetHandWorldPosition, {x: 0, y: 0.5, z: 0}); - handlerId = MyAvatar.addAnimationStateHandler(kickback, [animVarName]); - Script.setTimeout(function() { - MyAvatar.removeAnimationStateHandler(handlerId); - handlerId = null; - k = 0; - }, 200); +function fire(side, value) { + if(value == 0) { + return; } -} + Audio.playSound(fireSound, { + position: barrelTips[side], + volume: 1.0 + }); + var shotDirection = Vec3.normalize(barrelVectors[side]); + var pickRay = { + origin: barrelTips[side], + direction: shotDirection + }; + createMuzzleFlash(barrelTips[side]); -function triggerChanged(side, value) { - var pressed = (value != 0); - if (pressed) { - Audio.playSound(fireSound, { - position: barrelTips[side], - volume: 1.0 - }); - - var shotDirection = Vec3.normalize(barrelVectors[side]); - var pickRay = { - origin: barrelTips[side], - direction: shotDirection - }; - createMuzzleFlash(barrelTips[side]); - startKickback(); - - var intersection = Entities.findRayIntersection(pickRay, true); - if (intersection.intersects) { - if (intersection.properties.name === "rat") { - var forceDirection = JSON.stringify({ - forceDirection: shotDirection - }); - Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); - } else { - Script.setTimeout(function() { - if (shootAnything) { - Entities.editEntity(intersection.entityID, { - velocity: Vec3.multiply(shotDirection, GUN_FORCE) - }); - } - createWallHit(intersection.intersection); - }, 50); - } + var intersection = Entities.findRayIntersection(pickRay, true); + if (intersection.intersects) { + if (intersection.properties.name === "rat") { + var forceDirection = JSON.stringify({ + forceDirection: shotDirection + }); + Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); + } else { + Script.setTimeout(function() { + if (shootAnything) { + Entities.editEntity(intersection.entityID, { + velocity: Vec3.multiply(shotDirection, GUN_FORCE) + }); + } + createWallHit(intersection.intersection); + }, 50); } } } @@ -191,6 +178,9 @@ function triggerChanged(side, value) { function scriptEnding() { mapping.disable(); + for (var i = 0; i < pointers.length; ++i) { + Overlays.deleteOverlay(pointers[i]); + } MyAvatar.detachOne(GUN_MODEL); MyAvatar.detachOne(GUN_MODEL); clearPose(); @@ -199,12 +189,19 @@ function scriptEnding() { MyAvatar.attach(GUN_MODEL, "LeftHand", GUN_OFFSETS[0], GUN_ORIENTATIONS[0], 0.40); MyAvatar.attach(GUN_MODEL, "RightHand", GUN_OFFSETS[1], GUN_ORIENTATIONS[1], 0.40); -mapping.from(Controller.Standard.LT).hysteresis(0.1, 0.5).to(function(value) { - triggerChanged(0, value); +function showPointer(side) { + Overlays.editOverlay(pointers[side], { + visible: true + }); +} + +mapping.from(Controller.Standard.LT).hysteresis(0.0, 0.5).to(function(value) { + fire(0, value); }); -mapping.from(Controller.Standard.RT).hysteresis(0.1, 0.5).to(function(value) { - triggerChanged(1, value); + +mapping.from(Controller.Standard.RT).hysteresis(0.0, 0.5).to(function(value) { + fire(1, value); }); mapping.enable(); From 6f994d6274888417b96b70174344a1b01abfd87f Mon Sep 17 00:00:00 2001 From: Eric Levin Date: Fri, 13 Nov 2015 11:15:44 -0800 Subject: [PATCH 014/111] shootable --- examples/weapons/pistol.js | 35 +++++++++++++++++----------------- examples/weapons/ratSpawner.js | 3 ++- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 42e3d033bb..6c329fdbba 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -76,6 +76,7 @@ var barrelVectors = [0, 0]; var barrelTips = [0, 0]; +// If enabled, anything can be shot, otherwise, an entity needs to have "isShootable" set in its userData var shootAnything = true; @@ -106,7 +107,6 @@ function update(deltaTime) { alpha: 1, }); - } } @@ -139,7 +139,7 @@ var kickback = function(animationProperties) { function fire(side, value) { - if(value == 0) { + if (value == 0) { return; } Audio.playSound(fireSound, { @@ -156,24 +156,23 @@ function fire(side, value) { var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { - if (intersection.properties.name === "rat") { - var forceDirection = JSON.stringify({ - forceDirection: shotDirection - }); - Entities.callEntityMethod(intersection.entityID, 'onHit', [forceDirection]); - } else { - Script.setTimeout(function() { - if (shootAnything) { - Entities.editEntity(intersection.entityID, { - velocity: Vec3.multiply(shotDirection, GUN_FORCE) - }); - } - createWallHit(intersection.intersection); - }, 50); - } + + Script.setTimeout(function() { + if (shootAnything && intersection.properties.collisionsWillMove === true) { + // Any entity with collisions will move can be shot + Entities.editEntity(intersection.entityID, { + velocity: Vec3.multiply(shotDirection, GUN_FORCE) + }); + createEntityHitEffect(intersection.intersection); + } else { + + } + }, 50); + } } +function function scriptEnding() { @@ -209,7 +208,7 @@ Script.scriptEnding.connect(scriptEnding); Script.update.connect(update); -function createWallHit(position) { +function createEntityHitEffect(position) { var flash = Entities.addEntity({ type: "ParticleEffect", position: position, diff --git a/examples/weapons/ratSpawner.js b/examples/weapons/ratSpawner.js index f49f7ac4b9..28fe5e8b8a 100644 --- a/examples/weapons/ratSpawner.js +++ b/examples/weapons/ratSpawner.js @@ -13,7 +13,8 @@ var rat = Entities.addEntity({ color: {red: 200, green: 20, blue: 200}, gravity: {x: 0, y: -9.8, z: 0}, collisionsWillMove: true, - script: scriptURL + script: scriptURL, + userData: JSON.stringify({isShootable: true}); }); function cleanup() { From 9fb74667d9949aa00b0e50d4d69950eba30bf242 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 13 Nov 2015 11:20:52 -0800 Subject: [PATCH 015/111] Don't move non physical objects --- examples/weapons/pistol.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 6c329fdbba..7a10bee2ed 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -158,12 +158,12 @@ function fire(side, value) { if (intersection.intersects) { Script.setTimeout(function() { + createEntityHitEffect(intersection.intersection); if (shootAnything && intersection.properties.collisionsWillMove === true) { // Any entity with collisions will move can be shot Entities.editEntity(intersection.entityID, { velocity: Vec3.multiply(shotDirection, GUN_FORCE) }); - createEntityHitEffect(intersection.intersection); } else { } @@ -172,8 +172,6 @@ function fire(side, value) { } } -function - function scriptEnding() { mapping.disable(); From 2e2dc57b23128372fc13c30dd17dcd5143785706 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Fri, 13 Nov 2015 14:00:03 -0800 Subject: [PATCH 016/111] pushing for test --- examples/weapons/pistol.js | 18 ++++++++++++------ examples/weapons/rat.js | 9 +++++++-- examples/weapons/ratSpawner.js | 5 ++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 7a10bee2ed..1de6603590 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -17,7 +17,7 @@ Script.include("../libraries/constants.js"); HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); -var LASER_LENGTH = 10; +var LASER_LENGTH = 100; var LASER_WIDTH = 2; var GUN_FORCE = 10; var POSE_CONTROLS = [Controller.Standard.LeftHand, Controller.Standard.RightHand]; @@ -139,6 +139,7 @@ var kickback = function(animationProperties) { function fire(side, value) { + print("FIRE") if (value == 0) { return; } @@ -156,23 +157,28 @@ function fire(side, value) { var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { - + print("INTERSECTION") Script.setTimeout(function() { createEntityHitEffect(intersection.intersection); - if (shootAnything && intersection.properties.collisionsWillMove === true) { + if (shootAnything && intersection.properties.collisionsWillMove === 1) { // Any entity with collisions will move can be shot Entities.editEntity(intersection.entityID, { velocity: Vec3.multiply(shotDirection, GUN_FORCE) }); - } else { - } + //Attempt to call entity method's shot method + var forceDirection = JSON.stringify({ + forceDirection: shotDirection + }); + Entities.callEntityMethod(intersection.entityID, 'onShot', [forceDirection]); + }, 50); } } + function scriptEnding() { mapping.disable(); for (var i = 0; i < pointers.length; ++i) { @@ -316,7 +322,7 @@ function createMuzzleFlash(position) { Script.setTimeout(function() { Entities.editEntity(smoke, { isEmitting: false - }) + }); }, 100); var flash = Entities.addEntity({ diff --git a/examples/weapons/rat.js b/examples/weapons/rat.js index 39845667f0..a6fdcb1d31 100644 --- a/examples/weapons/rat.js +++ b/examples/weapons/rat.js @@ -16,17 +16,22 @@ var _this; Rat = function() { _this = this; - this.forceMultiplier = 10.0 + this.forceMultiplier = 10; }; Rat.prototype = { - onHit: function(myId, params) { + onShot: function(myId, params) { var data = JSON.parse(params[0]); var force = Vec3.multiply(data.forceDirection, this.forceMultiplier); Entities.editEntity(this.entityID, { velocity: force }); + + Script.setTimeout(function() { + var vel = Entities.getEntityProperties(_this.entityID, 'velocity').velocity; + print("velocity length " + Vec3.length(vel)); + }, 1000); }, preload: function(entityID) { diff --git a/examples/weapons/ratSpawner.js b/examples/weapons/ratSpawner.js index 28fe5e8b8a..6676775c77 100644 --- a/examples/weapons/ratSpawner.js +++ b/examples/weapons/ratSpawner.js @@ -1,6 +1,6 @@ var scriptURL = Script.resolvePath('rat.js'); var naturalDimensions = {x: 11.31, y: 3.18, z: 2.19}; -var sizeScale = 0.1; +var sizeScale = 0.2; var dimensions = Vec3.multiply(naturalDimensions, sizeScale); var center = Vec3.sum(MyAvatar.position, Vec3.multiply(3, Quat.getFront(Camera.getOrientation()))); var rat = Entities.addEntity({ @@ -13,8 +13,7 @@ var rat = Entities.addEntity({ color: {red: 200, green: 20, blue: 200}, gravity: {x: 0, y: -9.8, z: 0}, collisionsWillMove: true, - script: scriptURL, - userData: JSON.stringify({isShootable: true}); + script: scriptURL }); function cleanup() { From ceca720fae143a96eb707691622e08dc18a0deef Mon Sep 17 00:00:00 2001 From: Eric Levin Date: Fri, 13 Nov 2015 17:08:17 -0800 Subject: [PATCH 017/111] fix bug --- examples/weapons/pistol.js | 4 ++-- examples/weapons/ratSpawner.js | 1 - examples/weapons/socketTest.js | 18 ++++++++++++++++++ npm-debug.log | 19 +++++++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 examples/weapons/socketTest.js create mode 100644 npm-debug.log diff --git a/examples/weapons/pistol.js b/examples/weapons/pistol.js index 6c329fdbba..fbcfe93115 100644 --- a/examples/weapons/pistol.js +++ b/examples/weapons/pistol.js @@ -165,14 +165,14 @@ function fire(side, value) { }); createEntityHitEffect(intersection.intersection); } else { - + } }, 50); } } -function +function function scriptEnding() { diff --git a/examples/weapons/ratSpawner.js b/examples/weapons/ratSpawner.js index 28fe5e8b8a..34bbd69611 100644 --- a/examples/weapons/ratSpawner.js +++ b/examples/weapons/ratSpawner.js @@ -14,7 +14,6 @@ var rat = Entities.addEntity({ gravity: {x: 0, y: -9.8, z: 0}, collisionsWillMove: true, script: scriptURL, - userData: JSON.stringify({isShootable: true}); }); function cleanup() { diff --git a/examples/weapons/socketTest.js b/examples/weapons/socketTest.js new file mode 100644 index 0000000000..2db6df77ec --- /dev/null +++ b/examples/weapons/socketTest.js @@ -0,0 +1,18 @@ +var HOST = "localhost:5000" + // var HOST = "https://thawing-hamlet-8433.herokuapp.com/"; +var client = new WebSocket("ws://" + HOST); +client.onerror - function() { + console.log("CONNECTION ERROR"); +} +print("TESSST"); +client.onopen = function() { + print("Web Socket client connected"); + + function sendMessage() { + // if(client.readyState === client.OPEN) { + client.send("HEY"); + Script.setTimeout(sendMessage, 1000); + // } + } + sendMessage(); +} \ No newline at end of file diff --git a/npm-debug.log b/npm-debug.log new file mode 100644 index 0000000000..c1234881e9 --- /dev/null +++ b/npm-debug.log @@ -0,0 +1,19 @@ +0 info it worked if it ends with ok +1 verbose cli [ '/usr/local/bin/node', '/usr/local/bin/npm', 'run', 'build' ] +2 info using npm@2.5.1 +3 info using node@v0.12.0 +4 verbose node symlink /usr/local/bin/node +5 verbose stack Error: ENOENT, open '/Users/ericlevin1/hifi/package.json' +5 verbose stack at Error (native) +6 verbose cwd /Users/ericlevin1/hifi +7 error Darwin 14.3.0 +8 error argv "/usr/local/bin/node" "/usr/local/bin/npm" "run" "build" +9 error node v0.12.0 +10 error npm v2.5.1 +11 error path /Users/ericlevin1/hifi/package.json +12 error code ENOENT +13 error errno -2 +14 error enoent ENOENT, open '/Users/ericlevin1/hifi/package.json' +14 error enoent This is most likely not a problem with npm itself +14 error enoent and is related to npm not being able to find a file. +15 verbose exit [ -2, true ] From eca11d3a996fc53270e9fe1f2fb11a3b3520ca0c Mon Sep 17 00:00:00 2001 From: Eric Levin Date: Mon, 16 Nov 2015 12:47:16 -0800 Subject: [PATCH 018/111] sending message in correct format --- examples/weapons/socketTest.js | 20 ++++++++++++-------- libraries/avatars/src/AvatarData.h | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/examples/weapons/socketTest.js b/examples/weapons/socketTest.js index 2db6df77ec..d4ba68fb03 100644 --- a/examples/weapons/socketTest.js +++ b/examples/weapons/socketTest.js @@ -1,18 +1,22 @@ -var HOST = "localhost:5000" - // var HOST = "https://thawing-hamlet-8433.herokuapp.com/"; +// var HOST = "localhost:5000" +var HOST = "desolate-bastion-1742.herokuapp.com"; var client = new WebSocket("ws://" + HOST); -client.onerror - function() { - console.log("CONNECTION ERROR"); +var score = 1; +var username = GlobalServices.username; +client.onerror = function() { + print("CONNECTION ERROR"); } print("TESSST"); client.onopen = function() { print("Web Socket client connected"); function sendMessage() { - // if(client.readyState === client.OPEN) { - client.send("HEY"); - Script.setTimeout(sendMessage, 1000); - // } + if(client.readyState === client.OPEN) { + + Script.setTimeout(sendMessage, 3000); + client.send(JSON.stringify({id: score, username: username, score: score})) + score++; + } } sendMessage(); } \ No newline at end of file diff --git a/libraries/avatars/src/AvatarData.h b/libraries/avatars/src/AvatarData.h index 9079f15f53..29303f4467 100644 --- a/libraries/avatars/src/AvatarData.h +++ b/libraries/avatars/src/AvatarData.h @@ -167,7 +167,7 @@ public: AvatarData(); virtual ~AvatarData(); - static const QUrl& defaultFullAvatarModelUrl(); + static const QUrl& defaultFullAvatarcodelUrl(); virtual bool isMyAvatar() const { return false; } From ed1ee5086522961b82b0a364c70e4d11899aa1d1 Mon Sep 17 00:00:00 2001 From: Eric Levin Date: Mon, 16 Nov 2015 13:54:24 -0800 Subject: [PATCH 019/111] rand names for testing --- examples/weapons/socketTest.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/weapons/socketTest.js b/examples/weapons/socketTest.js index d4ba68fb03..f9bf443046 100644 --- a/examples/weapons/socketTest.js +++ b/examples/weapons/socketTest.js @@ -1,20 +1,20 @@ -// var HOST = "localhost:5000" -var HOST = "desolate-bastion-1742.herokuapp.com"; +var HOST = "localhost:5000" +// var HOST = "desolate-bastion-1742.herokuapp.com"; var client = new WebSocket("ws://" + HOST); var score = 1; -var username = GlobalServices.username; +// var username = GlobalServices.username; +var username = "rand " + Math.floor(Math.random() * 100); client.onerror = function() { print("CONNECTION ERROR"); } -print("TESSST"); client.onopen = function() { print("Web Socket client connected"); function sendMessage() { if(client.readyState === client.OPEN) { - Script.setTimeout(sendMessage, 3000); - client.send(JSON.stringify({id: score, username: username, score: score})) + Script.setTimeout(sendMessage, 10000); + client.send(JSON.stringify({username: username, score: score})) score++; } } From f77b7b9105ca84efbc2d4ae971e8fd9adc8b5a58 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Mon, 16 Nov 2015 14:09:47 -0800 Subject: [PATCH 020/111] renamed weapons folder to exterminatorGame --- examples/{weapons => exterminatorGame}/pistol.js | 5 ++++- examples/{weapons => exterminatorGame}/rat.js | 0 examples/{weapons => exterminatorGame}/ratSpawner.js | 0 examples/{weapons => exterminatorGame}/socketTest.js | 0 4 files changed, 4 insertions(+), 1 deletion(-) rename examples/{weapons => exterminatorGame}/pistol.js (99%) rename examples/{weapons => exterminatorGame}/rat.js (100%) rename examples/{weapons => exterminatorGame}/ratSpawner.js (100%) rename examples/{weapons => exterminatorGame}/socketTest.js (100%) diff --git a/examples/weapons/pistol.js b/examples/exterminatorGame/pistol.js similarity index 99% rename from examples/weapons/pistol.js rename to examples/exterminatorGame/pistol.js index e3fb5c8a5e..f509e890c1 100644 --- a/examples/weapons/pistol.js +++ b/examples/exterminatorGame/pistol.js @@ -12,6 +12,7 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // + Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); @@ -166,7 +167,9 @@ function fire(side, value) { velocity: Vec3.multiply(shotDirection, GUN_FORCE) }); createEntityHitEffect(intersection.intersection); - } else { + } + + if (intersection.properties.name === "rat") { } //Attempt to call entity method's shot method diff --git a/examples/weapons/rat.js b/examples/exterminatorGame/rat.js similarity index 100% rename from examples/weapons/rat.js rename to examples/exterminatorGame/rat.js diff --git a/examples/weapons/ratSpawner.js b/examples/exterminatorGame/ratSpawner.js similarity index 100% rename from examples/weapons/ratSpawner.js rename to examples/exterminatorGame/ratSpawner.js diff --git a/examples/weapons/socketTest.js b/examples/exterminatorGame/socketTest.js similarity index 100% rename from examples/weapons/socketTest.js rename to examples/exterminatorGame/socketTest.js From 763c6383f581107a18a51b3dd9ce85f7b3f3ab06 Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Mon, 16 Nov 2015 14:44:42 -0800 Subject: [PATCH 021/111] added blood splattering effect, and sending score to server --- examples/exterminatorGame/pistol.js | 95 ++++++++++++++++++++++++++--- examples/exterminatorGame/rat.js | 3 +- 2 files changed, 89 insertions(+), 9 deletions(-) diff --git a/examples/exterminatorGame/pistol.js b/examples/exterminatorGame/pistol.js index f509e890c1..ccfe7bff67 100644 --- a/examples/exterminatorGame/pistol.js +++ b/examples/exterminatorGame/pistol.js @@ -16,6 +16,22 @@ Script.include("../libraries/utils.js"); Script.include("../libraries/constants.js"); +var HOST = "localhost:5000" +var socketClient = new WebSocket("ws://" + HOST); +var username = GlobalServices.username; +var username = "rand " + Math.floor(Math.random() * 100); +var currentScore = 0; +// var HOST = "desolate-bastion-1742.herokuapp.com"; + +function score() { + currentScore++; + socketClient.send(JSON.stringify({ + username: username, + score: currentScore + })) +} + + HIFI_PUBLIC_BUCKET = "http://s3.amazonaws.com/hifi-public/"; var fireSound = SoundCache.getSound("https://s3.amazonaws.com/hifi-public/sounds/Guns/GUN-SHOT2.raw"); var LASER_LENGTH = 100; @@ -131,7 +147,6 @@ var decaySpeed = .02; var kickback = function(animationProperties) { var currentTargetHandWorldPosition = Vec3.mix(startingTargetHandWorldPosition, finalTargetHandWorldPosition, k); k += decaySpeed; - // print("WORLD POS " + JSON.stringify(startingTargetHandWorldPosition)); var targetHandModelPosition = worldToModel(currentTargetHandWorldPosition); var result = {}; result[animVarName] = targetHandModelPosition; @@ -140,7 +155,6 @@ var kickback = function(animationProperties) { function fire(side, value) { - print("FIRE") if (value == 0) { return; } @@ -158,18 +172,18 @@ function fire(side, value) { var intersection = Entities.findRayIntersection(pickRay, true); if (intersection.intersects) { - print("INTERSECTION") Script.setTimeout(function() { - createEntityHitEffect(intersection.intersection); if (shootAnything && intersection.properties.collisionsWillMove === 1) { // Any entity with collisions will move can be shot Entities.editEntity(intersection.entityID, { velocity: Vec3.multiply(shotDirection, GUN_FORCE) }); - createEntityHitEffect(intersection.intersection); - } + //createEntityHitEffect(intersection.intersection); + } if (intersection.properties.name === "rat") { + score(); + createBloodSplatter(intersection.intersection); } //Attempt to call entity method's shot method @@ -283,6 +297,73 @@ function createEntityHitEffect(position) { } + +function createBloodSplatter(position) { + var splatter = Entities.addEntity({ + type: "ParticleEffect", + position: position, + lifetime: 4, + "name": "Blood SPlatter", + "color": { + red: 230, + green: 2, + blue: 30 + }, + "maxParticles": 1000, + "lifespan": 0.3, + "emitRate": 1000, + "emitSpeed": 0.5, + "speedSpread": 0, + "emitOrientation": { + "x": -0.4, + "y": 1, + "z": -0.2, + "w": 0.7071068286895752 + }, + "emitDimensions": { + "x": 0, + "y": 0, + "z": 0 + }, + "polarStart": 0, + "polarFinish": Math.PI, + "azimuthStart": -3.1415927410125732, + "azimuthFinish": 2, + "emitAcceleration": { + "x": 0, + "y": -5, + "z": 0 + }, + "accelerationSpread": { + "x": 0, + "y": 0, + "z": 0 + }, + "particleRadius": 0.05, + "radiusSpread": 0.03, + "radiusStart": 0.05, + "radiusFinish": 0.05, + "colorSpread": { + red: 40, + green: 0, + blue: 30 + }, + "alpha": 1, + "alphaSpread": 0, + "alphaStart": 0, + "alphaFinish": 0, + "textures": "http://ericrius1.github.io/PartiArt/assets/star.png" + }); + + Script.setTimeout(function() { + Entities.editEntity(splatter, { + isEmitting: false + }); + }, 100) + +} + + function createMuzzleFlash(position) { var smoke = Entities.addEntity({ type: "ParticleEffect", @@ -334,7 +415,7 @@ function createMuzzleFlash(position) { type: "ParticleEffect", position: position, lifetime: 4, - "name": "Wall hit emitter", + "name": "Muzzle Flash", "color": { red: 228, green: 128, diff --git a/examples/exterminatorGame/rat.js b/examples/exterminatorGame/rat.js index a6fdcb1d31..10de6738b7 100644 --- a/examples/exterminatorGame/rat.js +++ b/examples/exterminatorGame/rat.js @@ -16,7 +16,7 @@ var _this; Rat = function() { _this = this; - this.forceMultiplier = 10; + this.forceMultiplier = 1; }; Rat.prototype = { @@ -30,7 +30,6 @@ Script.setTimeout(function() { var vel = Entities.getEntityProperties(_this.entityID, 'velocity').velocity; - print("velocity length " + Vec3.length(vel)); }, 1000); }, From 6ac53c7276f1136fbe45a38e36cc97b7d09509aa Mon Sep 17 00:00:00 2001 From: ericrius1 Date: Mon, 16 Nov 2015 15:26:17 -0800 Subject: [PATCH 022/111] added game server code --- .../exterminatorGame/gameServer/.gitignore | 1 + examples/exterminatorGame/gameServer/Procfile | 1 + examples/exterminatorGame/gameServer/app.js | 64 + .../gameServer/client/app.jsx | 70 + .../exterminatorGame/gameServer/gulpfile.js | 15 + .../exterminatorGame/gameServer/package.json | 22 + .../gameServer/public/css/style.css | 13 + .../gameServer/public/index.html | 13 + .../gameServer/public/js/app.js | 21449 ++++++++++++++++ .../gameServer/public/libs/jquery.js | 5 + 10 files changed, 21653 insertions(+) create mode 100644 examples/exterminatorGame/gameServer/.gitignore create mode 100644 examples/exterminatorGame/gameServer/Procfile create mode 100644 examples/exterminatorGame/gameServer/app.js create mode 100644 examples/exterminatorGame/gameServer/client/app.jsx create mode 100644 examples/exterminatorGame/gameServer/gulpfile.js create mode 100644 examples/exterminatorGame/gameServer/package.json create mode 100644 examples/exterminatorGame/gameServer/public/css/style.css create mode 100644 examples/exterminatorGame/gameServer/public/index.html create mode 100644 examples/exterminatorGame/gameServer/public/js/app.js create mode 100644 examples/exterminatorGame/gameServer/public/libs/jquery.js diff --git a/examples/exterminatorGame/gameServer/.gitignore b/examples/exterminatorGame/gameServer/.gitignore new file mode 100644 index 0000000000..30bc162798 --- /dev/null +++ b/examples/exterminatorGame/gameServer/.gitignore @@ -0,0 +1 @@ +/node_modules \ No newline at end of file diff --git a/examples/exterminatorGame/gameServer/Procfile b/examples/exterminatorGame/gameServer/Procfile new file mode 100644 index 0000000000..207d22f803 --- /dev/null +++ b/examples/exterminatorGame/gameServer/Procfile @@ -0,0 +1 @@ +web: node app.js \ No newline at end of file diff --git a/examples/exterminatorGame/gameServer/app.js b/examples/exterminatorGame/gameServer/app.js new file mode 100644 index 0000000000..e05c0fa284 --- /dev/null +++ b/examples/exterminatorGame/gameServer/app.js @@ -0,0 +1,64 @@ +'use strict'; + +/** + * Module dependencies. + */ + +var express = require('express'); +var http = require('http'); +var _ = require('underscore'); +var shortid = require('shortid'); + + +var app = express(); +var server = http.createServer(app); + +var WebSocketServer = require('websocket').server; +var wsServer = new WebSocketServer({ + httpServer: server +}); + +var users = []; + +wsServer.on('request', function(request) { + console.log("SOMEONE JOINED"); + var connection = request.accept(null, request.origin); + connection.on('message', function(data) { + var userData= JSON.parse(data.utf8Data); + var user = _.find(users, function(user){ + return user.username === userData.username; + }); + if(user) { + // This user already exists, so just update score + users[users.indexOf(user)].score = userData.score; + } else { + users.push({id: shortid.generate(), username: userData.username, score: userData.score}); + } + }); +}); + +app.get('/users', function(req, res) { + res.send({users: users}); +}); + + + +/* Configuration */ +app.set('views', __dirname + '/views'); +app.use(express.static(__dirname + '/public')); +app.set('port', (process.env.PORT || 5000)); + +if (process.env.NODE_ENV === 'development') { + app.use(express.errorHandler({ + dumpExceptions: true, + showStack: true + })); +} + + +/* Start server */ +server.listen(app.get('port'), function() { + console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env')); +}); + +module.exports = app; \ No newline at end of file diff --git a/examples/exterminatorGame/gameServer/client/app.jsx b/examples/exterminatorGame/gameServer/client/app.jsx new file mode 100644 index 0000000000..c99b2c030d --- /dev/null +++ b/examples/exterminatorGame/gameServer/client/app.jsx @@ -0,0 +1,70 @@ +'use strict'; + +var React = require('react'); +var _ = require('underscore') + +var UserList = React.createClass({ +render: function(){ + var sortedUsers = _.sortBy(this.props.data.users, function(users){ + //Show higher scorers at top of board + return 1 - users.score; + }); + var users = sortedUsers.map(function(user) { + + console.log('user', user) + return ( + + ) + }); + return ( +
{users}
+ ) + } +}); + +var GameBoard = React.createClass({ + loadDataFromServer: function(data) { + $.ajax({ + url: this.props.url, + dataType: 'json', + cache: false, + success: function(data) { + this.setState({data: data}); + }.bind(this), + error: function(xhr, status, err) { + console.error(this.props.url, status, err.toString()); + }.bind(this) + }); + }, + getInitialState: function() { + return {data: {users: []}}; + }, + componentDidMount: function() { + this.loadDataFromServer(); + setInterval(this.loadDataFromServer, 1000); + }, + render: function() { + + return ( +
+ +
+ ); + } +}); + +var User = React.createClass({ + render: function() { + return ( +
+
{this.props.username}
+
{this.props.score}
+
+ ); + } +}) + +React.render( + , + document.getElementById('app') +); \ No newline at end of file diff --git a/examples/exterminatorGame/gameServer/gulpfile.js b/examples/exterminatorGame/gameServer/gulpfile.js new file mode 100644 index 0000000000..84543dc28c --- /dev/null +++ b/examples/exterminatorGame/gameServer/gulpfile.js @@ -0,0 +1,15 @@ +var gulp = require('gulp'); +var exec = require('child_process').exec; + +gulp.task('build', function() { + exec('npm run build', function(msg){ + console.log(msg); + }); +}); + +gulp.task('watch', function() { + gulp.watch('client/*.jsx', ['build']); +}); + + +gulp.task('default', ['build', 'watch']) \ No newline at end of file diff --git a/examples/exterminatorGame/gameServer/package.json b/examples/exterminatorGame/gameServer/package.json new file mode 100644 index 0000000000..ff170d2a24 --- /dev/null +++ b/examples/exterminatorGame/gameServer/package.json @@ -0,0 +1,22 @@ +{ + "name": "WinterIsComing", + "version": "0.6.9", + "scripts": { + "build": "browserify ./client/app.jsx -t babelify --outfile ./public/js/app.js", + "start": "node app.js" + }, + "dependencies": { + "express": "^4.13.1", + "gulp": "^3.9.0", + "react": "^0.13.3", + "react-websocket": "^1.0.0", + "shortid": "^2.2.4", + "socket.io": "^1.3.5", + "underscore": "^1.8.3", + "websocket": "^1.0.22" + }, + "devDependencies": { + "babelify": "^6.1.3", + "browserify": "^10.2.6" + } +} diff --git a/examples/exterminatorGame/gameServer/public/css/style.css b/examples/exterminatorGame/gameServer/public/css/style.css new file mode 100644 index 0000000000..025e9e2332 --- /dev/null +++ b/examples/exterminatorGame/gameServer/public/css/style.css @@ -0,0 +1,13 @@ +.entry{ + width:100%; + height:50px; + border:1px solid red; + margin-right:10px; + float:left; + font-size: 40px; +} + +.username{ + float: left; + margin-right: 50%; +} \ No newline at end of file diff --git a/examples/exterminatorGame/gameServer/public/index.html b/examples/exterminatorGame/gameServer/public/index.html new file mode 100644 index 0000000000..d3aa525236 --- /dev/null +++ b/examples/exterminatorGame/gameServer/public/index.html @@ -0,0 +1,13 @@ + + + + + + Winter Is Coming + + +
+ + + + diff --git a/examples/exterminatorGame/gameServer/public/js/app.js b/examples/exterminatorGame/gameServer/public/js/app.js new file mode 100644 index 0000000000..43975772e8 --- /dev/null +++ b/examples/exterminatorGame/gameServer/public/js/app.js @@ -0,0 +1,21449 @@ +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 1) { + for (var i = 1; i < arguments.length; i++) { + args[i - 1] = arguments[i]; + } + } + queue.push(new Item(fun, args)); + if (queue.length === 1 && !draining) { + setTimeout(drainQueue, 0); + } +}; + +// v8 likes predictible objects +function Item(fun, array) { + this.fun = fun; + this.array = array; +} +Item.prototype.run = function () { + this.fun.apply(null, this.array); +}; +process.title = 'browser'; +process.browser = true; +process.env = {}; +process.argv = []; +process.version = ''; // empty string to avoid regexp issues +process.versions = {}; + +function noop() {} + +process.on = noop; +process.addListener = noop; +process.once = noop; +process.off = noop; +process.removeListener = noop; +process.removeAllListeners = noop; +process.emit = noop; + +process.binding = function (name) { + throw new Error('process.binding is not supported'); +}; + +process.cwd = function () { return '/' }; +process.chdir = function (dir) { + throw new Error('process.chdir is not supported'); +}; +process.umask = function() { return 0; }; + +},{}],3:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule AutoFocusMixin + * @typechecks static-only + */ + +'use strict'; + +var focusNode = require("./focusNode"); + +var AutoFocusMixin = { + componentDidMount: function() { + if (this.props.autoFocus) { + focusNode(this.getDOMNode()); + } + } +}; + +module.exports = AutoFocusMixin; + +},{"./focusNode":121}],4:[function(require,module,exports){ +/** + * Copyright 2013-2015 Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule BeforeInputEventPlugin + * @typechecks static-only + */ + +'use strict'; + +var EventConstants = require("./EventConstants"); +var EventPropagators = require("./EventPropagators"); +var ExecutionEnvironment = require("./ExecutionEnvironment"); +var FallbackCompositionState = require("./FallbackCompositionState"); +var SyntheticCompositionEvent = require("./SyntheticCompositionEvent"); +var SyntheticInputEvent = require("./SyntheticInputEvent"); + +var keyOf = require("./keyOf"); + +var END_KEYCODES = [9, 13, 27, 32]; // Tab, Return, Esc, Space +var START_KEYCODE = 229; + +var canUseCompositionEvent = ( + ExecutionEnvironment.canUseDOM && + 'CompositionEvent' in window +); + +var documentMode = null; +if (ExecutionEnvironment.canUseDOM && 'documentMode' in document) { + documentMode = document.documentMode; +} + +// Webkit offers a very useful `textInput` event that can be used to +// directly represent `beforeInput`. The IE `textinput` event is not as +// useful, so we don't use it. +var canUseTextInputEvent = ( + ExecutionEnvironment.canUseDOM && + 'TextEvent' in window && + !documentMode && + !isPresto() +); + +// In IE9+, we have access to composition events, but the data supplied +// by the native compositionend event may be incorrect. Japanese ideographic +// spaces, for instance (\u3000) are not recorded correctly. +var useFallbackCompositionData = ( + ExecutionEnvironment.canUseDOM && + ( + (!canUseCompositionEvent || documentMode && documentMode > 8 && documentMode <= 11) + ) +); + +/** + * Opera <= 12 includes TextEvent in window, but does not fire + * text input events. Rely on keypress instead. + */ +function isPresto() { + var opera = window.opera; + return ( + typeof opera === 'object' && + typeof opera.version === 'function' && + parseInt(opera.version(), 10) <= 12 + ); +} + +var SPACEBAR_CODE = 32; +var SPACEBAR_CHAR = String.fromCharCode(SPACEBAR_CODE); + +var topLevelTypes = EventConstants.topLevelTypes; + +// Events and their corresponding property names. +var eventTypes = { + beforeInput: { + phasedRegistrationNames: { + bubbled: keyOf({onBeforeInput: null}), + captured: keyOf({onBeforeInputCapture: null}) + }, + dependencies: [ + topLevelTypes.topCompositionEnd, + topLevelTypes.topKeyPress, + topLevelTypes.topTextInput, + topLevelTypes.topPaste + ] + }, + compositionEnd: { + phasedRegistrationNames: { + bubbled: keyOf({onCompositionEnd: null}), + captured: keyOf({onCompositionEndCapture: null}) + }, + dependencies: [ + topLevelTypes.topBlur, + topLevelTypes.topCompositionEnd, + topLevelTypes.topKeyDown, + topLevelTypes.topKeyPress, + topLevelTypes.topKeyUp, + topLevelTypes.topMouseDown + ] + }, + compositionStart: { + phasedRegistrationNames: { + bubbled: keyOf({onCompositionStart: null}), + captured: keyOf({onCompositionStartCapture: null}) + }, + dependencies: [ + topLevelTypes.topBlur, + topLevelTypes.topCompositionStart, + topLevelTypes.topKeyDown, + topLevelTypes.topKeyPress, + topLevelTypes.topKeyUp, + topLevelTypes.topMouseDown + ] + }, + compositionUpdate: { + phasedRegistrationNames: { + bubbled: keyOf({onCompositionUpdate: null}), + captured: keyOf({onCompositionUpdateCapture: null}) + }, + dependencies: [ + topLevelTypes.topBlur, + topLevelTypes.topCompositionUpdate, + topLevelTypes.topKeyDown, + topLevelTypes.topKeyPress, + topLevelTypes.topKeyUp, + topLevelTypes.topMouseDown + ] + } +}; + +// Track whether we've ever handled a keypress on the space key. +var hasSpaceKeypress = false; + +/** + * Return whether a native keypress event is assumed to be a command. + * This is required because Firefox fires `keypress` events for key commands + * (cut, copy, select-all, etc.) even though no character is inserted. + */ +function isKeypressCommand(nativeEvent) { + return ( + (nativeEvent.ctrlKey || nativeEvent.altKey || nativeEvent.metaKey) && + // ctrlKey && altKey is equivalent to AltGr, and is not a command. + !(nativeEvent.ctrlKey && nativeEvent.altKey) + ); +} + + +/** + * Translate native top level events into event types. + * + * @param {string} topLevelType + * @return {object} + */ +function getCompositionEventType(topLevelType) { + switch (topLevelType) { + case topLevelTypes.topCompositionStart: + return eventTypes.compositionStart; + case topLevelTypes.topCompositionEnd: + return eventTypes.compositionEnd; + case topLevelTypes.topCompositionUpdate: + return eventTypes.compositionUpdate; + } +} + +/** + * Does our fallback best-guess model think this event signifies that + * composition has begun? + * + * @param {string} topLevelType + * @param {object} nativeEvent + * @return {boolean} + */ +function isFallbackCompositionStart(topLevelType, nativeEvent) { + return ( + topLevelType === topLevelTypes.topKeyDown && + nativeEvent.keyCode === START_KEYCODE + ); +} + +/** + * Does our fallback mode think that this event is the end of composition? + * + * @param {string} topLevelType + * @param {object} nativeEvent + * @return {boolean} + */ +function isFallbackCompositionEnd(topLevelType, nativeEvent) { + switch (topLevelType) { + case topLevelTypes.topKeyUp: + // Command keys insert or clear IME input. + return (END_KEYCODES.indexOf(nativeEvent.keyCode) !== -1); + case topLevelTypes.topKeyDown: + // Expect IME keyCode on each keydown. If we get any other + // code we must have exited earlier. + return (nativeEvent.keyCode !== START_KEYCODE); + case topLevelTypes.topKeyPress: + case topLevelTypes.topMouseDown: + case topLevelTypes.topBlur: + // Events are not possible without cancelling IME. + return true; + default: + return false; + } +} + +/** + * Google Input Tools provides composition data via a CustomEvent, + * with the `data` property populated in the `detail` object. If this + * is available on the event object, use it. If not, this is a plain + * composition event and we have nothing special to extract. + * + * @param {object} nativeEvent + * @return {?string} + */ +function getDataFromCustomEvent(nativeEvent) { + var detail = nativeEvent.detail; + if (typeof detail === 'object' && 'data' in detail) { + return detail.data; + } + return null; +} + +// Track the current IME composition fallback object, if any. +var currentComposition = null; + +/** + * @param {string} topLevelType Record from `EventConstants`. + * @param {DOMEventTarget} topLevelTarget The listening component root node. + * @param {string} topLevelTargetID ID of `topLevelTarget`. + * @param {object} nativeEvent Native browser event. + * @return {?object} A SyntheticCompositionEvent. + */ +function extractCompositionEvent( + topLevelType, + topLevelTarget, + topLevelTargetID, + nativeEvent +) { + var eventType; + var fallbackData; + + if (canUseCompositionEvent) { + eventType = getCompositionEventType(topLevelType); + } else if (!currentComposition) { + if (isFallbackCompositionStart(topLevelType, nativeEvent)) { + eventType = eventTypes.compositionStart; + } + } else if (isFallbackCompositionEnd(topLevelType, nativeEvent)) { + eventType = eventTypes.compositionEnd; + } + + if (!eventType) { + return null; + } + + if (useFallbackCompositionData) { + // The current composition is stored statically and must not be + // overwritten while composition continues. + if (!currentComposition && eventType === eventTypes.compositionStart) { + currentComposition = FallbackCompositionState.getPooled(topLevelTarget); + } else if (eventType === eventTypes.compositionEnd) { + if (currentComposition) { + fallbackData = currentComposition.getData(); + } + } + } + + var event = SyntheticCompositionEvent.getPooled( + eventType, + topLevelTargetID, + nativeEvent + ); + + if (fallbackData) { + // Inject data generated from fallback path into the synthetic event. + // This matches the property of native CompositionEventInterface. + event.data = fallbackData; + } else { + var customData = getDataFromCustomEvent(nativeEvent); + if (customData !== null) { + event.data = customData; + } + } + + EventPropagators.accumulateTwoPhaseDispatches(event); + return event; +} + +/** + * @param {string} topLevelType Record from `EventConstants`. + * @param {object} nativeEvent Native browser event. + * @return {?string} The string corresponding to this `beforeInput` event. + */ +function getNativeBeforeInputChars(topLevelType, nativeEvent) { + switch (topLevelType) { + case topLevelTypes.topCompositionEnd: + return getDataFromCustomEvent(nativeEvent); + case topLevelTypes.topKeyPress: + /** + * If native `textInput` events are available, our goal is to make + * use of them. However, there is a special case: the spacebar key. + * In Webkit, preventing default on a spacebar `textInput` event + * cancels character insertion, but it *also* causes the browser + * to fall back to its default spacebar behavior of scrolling the + * page. + * + * Tracking at: + * https://code.google.com/p/chromium/issues/detail?id=355103 + * + * To avoid this issue, use the keypress event as if no `textInput` + * event is available. + */ + var which = nativeEvent.which; + if (which !== SPACEBAR_CODE) { + return null; + } + + hasSpaceKeypress = true; + return SPACEBAR_CHAR; + + case topLevelTypes.topTextInput: + // Record the characters to be added to the DOM. + var chars = nativeEvent.data; + + // If it's a spacebar character, assume that we have already handled + // it at the keypress level and bail immediately. Android Chrome + // doesn't give us keycodes, so we need to blacklist it. + if (chars === SPACEBAR_CHAR && hasSpaceKeypress) { + return null; + } + + return chars; + + default: + // For other native event types, do nothing. + return null; + } +} + +/** + * For browsers that do not provide the `textInput` event, extract the + * appropriate string to use for SyntheticInputEvent. + * + * @param {string} topLevelType Record from `EventConstants`. + * @param {object} nativeEvent Native browser event. + * @return {?string} The fallback string for this `beforeInput` event. + */ +function getFallbackBeforeInputChars(topLevelType, nativeEvent) { + // If we are currently composing (IME) and using a fallback to do so, + // try to extract the composed characters from the fallback object. + if (currentComposition) { + if ( + topLevelType === topLevelTypes.topCompositionEnd || + isFallbackCompositionEnd(topLevelType, nativeEvent) + ) { + var chars = currentComposition.getData(); + FallbackCompositionState.release(currentComposition); + currentComposition = null; + return chars; + } + return null; + } + + switch (topLevelType) { + case topLevelTypes.topPaste: + // If a paste event occurs after a keypress, throw out the input + // chars. Paste events should not lead to BeforeInput events. + return null; + case topLevelTypes.topKeyPress: + /** + * As of v27, Firefox may fire keypress events even when no character + * will be inserted. A few possibilities: + * + * - `which` is `0`. Arrow keys, Esc key, etc. + * + * - `which` is the pressed key code, but no char is available. + * Ex: 'AltGr + d` in Polish. There is no modified character for + * this key combination and no character is inserted into the + * document, but FF fires the keypress for char code `100` anyway. + * No `input` event will occur. + * + * - `which` is the pressed key code, but a command combination is + * being used. Ex: `Cmd+C`. No character is inserted, and no + * `input` event will occur. + */ + if (nativeEvent.which && !isKeypressCommand(nativeEvent)) { + return String.fromCharCode(nativeEvent.which); + } + return null; + case topLevelTypes.topCompositionEnd: + return useFallbackCompositionData ? null : nativeEvent.data; + default: + return null; + } +} + +/** + * Extract a SyntheticInputEvent for `beforeInput`, based on either native + * `textInput` or fallback behavior. + * + * @param {string} topLevelType Record from `EventConstants`. + * @param {DOMEventTarget} topLevelTarget The listening component root node. + * @param {string} topLevelTargetID ID of `topLevelTarget`. + * @param {object} nativeEvent Native browser event. + * @return {?object} A SyntheticInputEvent. + */ +function extractBeforeInputEvent( + topLevelType, + topLevelTarget, + topLevelTargetID, + nativeEvent +) { + var chars; + + if (canUseTextInputEvent) { + chars = getNativeBeforeInputChars(topLevelType, nativeEvent); + } else { + chars = getFallbackBeforeInputChars(topLevelType, nativeEvent); + } + + // If no characters are being inserted, no BeforeInput event should + // be fired. + if (!chars) { + return null; + } + + var event = SyntheticInputEvent.getPooled( + eventTypes.beforeInput, + topLevelTargetID, + nativeEvent + ); + + event.data = chars; + EventPropagators.accumulateTwoPhaseDispatches(event); + return event; +} + +/** + * Create an `onBeforeInput` event to match + * http://www.w3.org/TR/2013/WD-DOM-Level-3-Events-20131105/#events-inputevents. + * + * This event plugin is based on the native `textInput` event + * available in Chrome, Safari, Opera, and IE. This event fires after + * `onKeyPress` and `onCompositionEnd`, but before `onInput`. + * + * `beforeInput` is spec'd but not implemented in any browsers, and + * the `input` event does not provide any useful information about what has + * actually been added, contrary to the spec. Thus, `textInput` is the best + * available event to identify the characters that have actually been inserted + * into the target node. + * + * This plugin is also responsible for emitting `composition` events, thus + * allowing us to share composition fallback code for both `beforeInput` and + * `composition` event types. + */ +var BeforeInputEventPlugin = { + + eventTypes: eventTypes, + + /** + * @param {string} topLevelType Record from `EventConstants`. + * @param {DOMEventTarget} topLevelTarget The listening component root node. + * @param {string} topLevelTargetID ID of `topLevelTarget`. + * @param {object} nativeEvent Native browser event. + * @return {*} An accumulation of synthetic events. + * @see {EventPluginHub.extractEvents} + */ + extractEvents: function( + topLevelType, + topLevelTarget, + topLevelTargetID, + nativeEvent + ) { + return [ + extractCompositionEvent( + topLevelType, + topLevelTarget, + topLevelTargetID, + nativeEvent + ), + extractBeforeInputEvent( + topLevelType, + topLevelTarget, + topLevelTargetID, + nativeEvent + ) + ]; + } +}; + +module.exports = BeforeInputEventPlugin; + +},{"./EventConstants":16,"./EventPropagators":21,"./ExecutionEnvironment":22,"./FallbackCompositionState":23,"./SyntheticCompositionEvent":95,"./SyntheticInputEvent":99,"./keyOf":143}],5:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule CSSProperty + */ + +'use strict'; + +/** + * CSS properties which accept numbers but are not in units of "px". + */ +var isUnitlessNumber = { + boxFlex: true, + boxFlexGroup: true, + columnCount: true, + flex: true, + flexGrow: true, + flexPositive: true, + flexShrink: true, + flexNegative: true, + fontWeight: true, + lineClamp: true, + lineHeight: true, + opacity: true, + order: true, + orphans: true, + widows: true, + zIndex: true, + zoom: true, + + // SVG-related properties + fillOpacity: true, + strokeDashoffset: true, + strokeOpacity: true, + strokeWidth: true +}; + +/** + * @param {string} prefix vendor-specific prefix, eg: Webkit + * @param {string} key style name, eg: transitionDuration + * @return {string} style name prefixed with `prefix`, properly camelCased, eg: + * WebkitTransitionDuration + */ +function prefixKey(prefix, key) { + return prefix + key.charAt(0).toUpperCase() + key.substring(1); +} + +/** + * Support style names that may come passed in prefixed by adding permutations + * of vendor prefixes. + */ +var prefixes = ['Webkit', 'ms', 'Moz', 'O']; + +// Using Object.keys here, or else the vanilla for-in loop makes IE8 go into an +// infinite loop, because it iterates over the newly added props too. +Object.keys(isUnitlessNumber).forEach(function(prop) { + prefixes.forEach(function(prefix) { + isUnitlessNumber[prefixKey(prefix, prop)] = isUnitlessNumber[prop]; + }); +}); + +/** + * Most style properties can be unset by doing .style[prop] = '' but IE8 + * doesn't like doing that with shorthand properties so for the properties that + * IE8 breaks on, which are listed here, we instead unset each of the + * individual properties. See http://bugs.jquery.com/ticket/12385. + * The 4-value 'clock' properties like margin, padding, border-width seem to + * behave without any problems. Curiously, list-style works too without any + * special prodding. + */ +var shorthandPropertyExpansions = { + background: { + backgroundImage: true, + backgroundPosition: true, + backgroundRepeat: true, + backgroundColor: true + }, + border: { + borderWidth: true, + borderStyle: true, + borderColor: true + }, + borderBottom: { + borderBottomWidth: true, + borderBottomStyle: true, + borderBottomColor: true + }, + borderLeft: { + borderLeftWidth: true, + borderLeftStyle: true, + borderLeftColor: true + }, + borderRight: { + borderRightWidth: true, + borderRightStyle: true, + borderRightColor: true + }, + borderTop: { + borderTopWidth: true, + borderTopStyle: true, + borderTopColor: true + }, + font: { + fontStyle: true, + fontVariant: true, + fontWeight: true, + fontSize: true, + lineHeight: true, + fontFamily: true + } +}; + +var CSSProperty = { + isUnitlessNumber: isUnitlessNumber, + shorthandPropertyExpansions: shorthandPropertyExpansions +}; + +module.exports = CSSProperty; + +},{}],6:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule CSSPropertyOperations + * @typechecks static-only + */ + +'use strict'; + +var CSSProperty = require("./CSSProperty"); +var ExecutionEnvironment = require("./ExecutionEnvironment"); + +var camelizeStyleName = require("./camelizeStyleName"); +var dangerousStyleValue = require("./dangerousStyleValue"); +var hyphenateStyleName = require("./hyphenateStyleName"); +var memoizeStringOnly = require("./memoizeStringOnly"); +var warning = require("./warning"); + +var processStyleName = memoizeStringOnly(function(styleName) { + return hyphenateStyleName(styleName); +}); + +var styleFloatAccessor = 'cssFloat'; +if (ExecutionEnvironment.canUseDOM) { + // IE8 only supports accessing cssFloat (standard) as styleFloat + if (document.documentElement.style.cssFloat === undefined) { + styleFloatAccessor = 'styleFloat'; + } +} + +if ("production" !== process.env.NODE_ENV) { + // 'msTransform' is correct, but the other prefixes should be capitalized + var badVendoredStyleNamePattern = /^(?:webkit|moz|o)[A-Z]/; + + // style values shouldn't contain a semicolon + var badStyleValueWithSemicolonPattern = /;\s*$/; + + var warnedStyleNames = {}; + var warnedStyleValues = {}; + + var warnHyphenatedStyleName = function(name) { + if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) { + return; + } + + warnedStyleNames[name] = true; + ("production" !== process.env.NODE_ENV ? warning( + false, + 'Unsupported style property %s. Did you mean %s?', + name, + camelizeStyleName(name) + ) : null); + }; + + var warnBadVendoredStyleName = function(name) { + if (warnedStyleNames.hasOwnProperty(name) && warnedStyleNames[name]) { + return; + } + + warnedStyleNames[name] = true; + ("production" !== process.env.NODE_ENV ? warning( + false, + 'Unsupported vendor-prefixed style property %s. Did you mean %s?', + name, + name.charAt(0).toUpperCase() + name.slice(1) + ) : null); + }; + + var warnStyleValueWithSemicolon = function(name, value) { + if (warnedStyleValues.hasOwnProperty(value) && warnedStyleValues[value]) { + return; + } + + warnedStyleValues[value] = true; + ("production" !== process.env.NODE_ENV ? warning( + false, + 'Style property values shouldn\'t contain a semicolon. ' + + 'Try "%s: %s" instead.', + name, + value.replace(badStyleValueWithSemicolonPattern, '') + ) : null); + }; + + /** + * @param {string} name + * @param {*} value + */ + var warnValidStyle = function(name, value) { + if (name.indexOf('-') > -1) { + warnHyphenatedStyleName(name); + } else if (badVendoredStyleNamePattern.test(name)) { + warnBadVendoredStyleName(name); + } else if (badStyleValueWithSemicolonPattern.test(value)) { + warnStyleValueWithSemicolon(name, value); + } + }; +} + +/** + * Operations for dealing with CSS properties. + */ +var CSSPropertyOperations = { + + /** + * Serializes a mapping of style properties for use as inline styles: + * + * > createMarkupForStyles({width: '200px', height: 0}) + * "width:200px;height:0;" + * + * Undefined values are ignored so that declarative programming is easier. + * The result should be HTML-escaped before insertion into the DOM. + * + * @param {object} styles + * @return {?string} + */ + createMarkupForStyles: function(styles) { + var serialized = ''; + for (var styleName in styles) { + if (!styles.hasOwnProperty(styleName)) { + continue; + } + var styleValue = styles[styleName]; + if ("production" !== process.env.NODE_ENV) { + warnValidStyle(styleName, styleValue); + } + if (styleValue != null) { + serialized += processStyleName(styleName) + ':'; + serialized += dangerousStyleValue(styleName, styleValue) + ';'; + } + } + return serialized || null; + }, + + /** + * Sets the value for multiple styles on a node. If a value is specified as + * '' (empty string), the corresponding style property will be unset. + * + * @param {DOMElement} node + * @param {object} styles + */ + setValueForStyles: function(node, styles) { + var style = node.style; + for (var styleName in styles) { + if (!styles.hasOwnProperty(styleName)) { + continue; + } + if ("production" !== process.env.NODE_ENV) { + warnValidStyle(styleName, styles[styleName]); + } + var styleValue = dangerousStyleValue(styleName, styles[styleName]); + if (styleName === 'float') { + styleName = styleFloatAccessor; + } + if (styleValue) { + style[styleName] = styleValue; + } else { + var expansion = CSSProperty.shorthandPropertyExpansions[styleName]; + if (expansion) { + // Shorthand property that IE8 won't like unsetting, so unset each + // component to placate it + for (var individualStyleName in expansion) { + style[individualStyleName] = ''; + } + } else { + style[styleName] = ''; + } + } + } + } + +}; + +module.exports = CSSPropertyOperations; + +}).call(this,require('_process')) +},{"./CSSProperty":5,"./ExecutionEnvironment":22,"./camelizeStyleName":110,"./dangerousStyleValue":115,"./hyphenateStyleName":135,"./memoizeStringOnly":145,"./warning":156,"_process":2}],7:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule CallbackQueue + */ + +'use strict'; + +var PooledClass = require("./PooledClass"); + +var assign = require("./Object.assign"); +var invariant = require("./invariant"); + +/** + * A specialized pseudo-event module to help keep track of components waiting to + * be notified when their DOM representations are available for use. + * + * This implements `PooledClass`, so you should never need to instantiate this. + * Instead, use `CallbackQueue.getPooled()`. + * + * @class ReactMountReady + * @implements PooledClass + * @internal + */ +function CallbackQueue() { + this._callbacks = null; + this._contexts = null; +} + +assign(CallbackQueue.prototype, { + + /** + * Enqueues a callback to be invoked when `notifyAll` is invoked. + * + * @param {function} callback Invoked when `notifyAll` is invoked. + * @param {?object} context Context to call `callback` with. + * @internal + */ + enqueue: function(callback, context) { + this._callbacks = this._callbacks || []; + this._contexts = this._contexts || []; + this._callbacks.push(callback); + this._contexts.push(context); + }, + + /** + * Invokes all enqueued callbacks and clears the queue. This is invoked after + * the DOM representation of a component has been created or updated. + * + * @internal + */ + notifyAll: function() { + var callbacks = this._callbacks; + var contexts = this._contexts; + if (callbacks) { + ("production" !== process.env.NODE_ENV ? invariant( + callbacks.length === contexts.length, + 'Mismatched list of contexts in callback queue' + ) : invariant(callbacks.length === contexts.length)); + this._callbacks = null; + this._contexts = null; + for (var i = 0, l = callbacks.length; i < l; i++) { + callbacks[i].call(contexts[i]); + } + callbacks.length = 0; + contexts.length = 0; + } + }, + + /** + * Resets the internal queue. + * + * @internal + */ + reset: function() { + this._callbacks = null; + this._contexts = null; + }, + + /** + * `PooledClass` looks for this. + */ + destructor: function() { + this.reset(); + } + +}); + +PooledClass.addPoolingTo(CallbackQueue); + +module.exports = CallbackQueue; + +}).call(this,require('_process')) +},{"./Object.assign":28,"./PooledClass":29,"./invariant":137,"_process":2}],8:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ChangeEventPlugin + */ + +'use strict'; + +var EventConstants = require("./EventConstants"); +var EventPluginHub = require("./EventPluginHub"); +var EventPropagators = require("./EventPropagators"); +var ExecutionEnvironment = require("./ExecutionEnvironment"); +var ReactUpdates = require("./ReactUpdates"); +var SyntheticEvent = require("./SyntheticEvent"); + +var isEventSupported = require("./isEventSupported"); +var isTextInputElement = require("./isTextInputElement"); +var keyOf = require("./keyOf"); + +var topLevelTypes = EventConstants.topLevelTypes; + +var eventTypes = { + change: { + phasedRegistrationNames: { + bubbled: keyOf({onChange: null}), + captured: keyOf({onChangeCapture: null}) + }, + dependencies: [ + topLevelTypes.topBlur, + topLevelTypes.topChange, + topLevelTypes.topClick, + topLevelTypes.topFocus, + topLevelTypes.topInput, + topLevelTypes.topKeyDown, + topLevelTypes.topKeyUp, + topLevelTypes.topSelectionChange + ] + } +}; + +/** + * For IE shims + */ +var activeElement = null; +var activeElementID = null; +var activeElementValue = null; +var activeElementValueProp = null; + +/** + * SECTION: handle `change` event + */ +function shouldUseChangeEvent(elem) { + return ( + elem.nodeName === 'SELECT' || + (elem.nodeName === 'INPUT' && elem.type === 'file') + ); +} + +var doesChangeEventBubble = false; +if (ExecutionEnvironment.canUseDOM) { + // See `handleChange` comment below + doesChangeEventBubble = isEventSupported('change') && ( + (!('documentMode' in document) || document.documentMode > 8) + ); +} + +function manualDispatchChangeEvent(nativeEvent) { + var event = SyntheticEvent.getPooled( + eventTypes.change, + activeElementID, + nativeEvent + ); + EventPropagators.accumulateTwoPhaseDispatches(event); + + // If change and propertychange bubbled, we'd just bind to it like all the + // other events and have it go through ReactBrowserEventEmitter. Since it + // doesn't, we manually listen for the events and so we have to enqueue and + // process the abstract event manually. + // + // Batching is necessary here in order to ensure that all event handlers run + // before the next rerender (including event handlers attached to ancestor + // elements instead of directly on the input). Without this, controlled + // components don't work properly in conjunction with event bubbling because + // the component is rerendered and the value reverted before all the event + // handlers can run. See https://github.com/facebook/react/issues/708. + ReactUpdates.batchedUpdates(runEventInBatch, event); +} + +function runEventInBatch(event) { + EventPluginHub.enqueueEvents(event); + EventPluginHub.processEventQueue(); +} + +function startWatchingForChangeEventIE8(target, targetID) { + activeElement = target; + activeElementID = targetID; + activeElement.attachEvent('onchange', manualDispatchChangeEvent); +} + +function stopWatchingForChangeEventIE8() { + if (!activeElement) { + return; + } + activeElement.detachEvent('onchange', manualDispatchChangeEvent); + activeElement = null; + activeElementID = null; +} + +function getTargetIDForChangeEvent( + topLevelType, + topLevelTarget, + topLevelTargetID) { + if (topLevelType === topLevelTypes.topChange) { + return topLevelTargetID; + } +} +function handleEventsForChangeEventIE8( + topLevelType, + topLevelTarget, + topLevelTargetID) { + if (topLevelType === topLevelTypes.topFocus) { + // stopWatching() should be a noop here but we call it just in case we + // missed a blur event somehow. + stopWatchingForChangeEventIE8(); + startWatchingForChangeEventIE8(topLevelTarget, topLevelTargetID); + } else if (topLevelType === topLevelTypes.topBlur) { + stopWatchingForChangeEventIE8(); + } +} + + +/** + * SECTION: handle `input` event + */ +var isInputEventSupported = false; +if (ExecutionEnvironment.canUseDOM) { + // IE9 claims to support the input event but fails to trigger it when + // deleting text, so we ignore its input events + isInputEventSupported = isEventSupported('input') && ( + (!('documentMode' in document) || document.documentMode > 9) + ); +} + +/** + * (For old IE.) Replacement getter/setter for the `value` property that gets + * set on the active element. + */ +var newValueProp = { + get: function() { + return activeElementValueProp.get.call(this); + }, + set: function(val) { + // Cast to a string so we can do equality checks. + activeElementValue = '' + val; + activeElementValueProp.set.call(this, val); + } +}; + +/** + * (For old IE.) Starts tracking propertychange events on the passed-in element + * and override the value property so that we can distinguish user events from + * value changes in JS. + */ +function startWatchingForValueChange(target, targetID) { + activeElement = target; + activeElementID = targetID; + activeElementValue = target.value; + activeElementValueProp = Object.getOwnPropertyDescriptor( + target.constructor.prototype, + 'value' + ); + + Object.defineProperty(activeElement, 'value', newValueProp); + activeElement.attachEvent('onpropertychange', handlePropertyChange); +} + +/** + * (For old IE.) Removes the event listeners from the currently-tracked element, + * if any exists. + */ +function stopWatchingForValueChange() { + if (!activeElement) { + return; + } + + // delete restores the original property definition + delete activeElement.value; + activeElement.detachEvent('onpropertychange', handlePropertyChange); + + activeElement = null; + activeElementID = null; + activeElementValue = null; + activeElementValueProp = null; +} + +/** + * (For old IE.) Handles a propertychange event, sending a `change` event if + * the value of the active element has changed. + */ +function handlePropertyChange(nativeEvent) { + if (nativeEvent.propertyName !== 'value') { + return; + } + var value = nativeEvent.srcElement.value; + if (value === activeElementValue) { + return; + } + activeElementValue = value; + + manualDispatchChangeEvent(nativeEvent); +} + +/** + * If a `change` event should be fired, returns the target's ID. + */ +function getTargetIDForInputEvent( + topLevelType, + topLevelTarget, + topLevelTargetID) { + if (topLevelType === topLevelTypes.topInput) { + // In modern browsers (i.e., not IE8 or IE9), the input event is exactly + // what we want so fall through here and trigger an abstract event + return topLevelTargetID; + } +} + +// For IE8 and IE9. +function handleEventsForInputEventIE( + topLevelType, + topLevelTarget, + topLevelTargetID) { + if (topLevelType === topLevelTypes.topFocus) { + // In IE8, we can capture almost all .value changes by adding a + // propertychange handler and looking for events with propertyName + // equal to 'value' + // In IE9, propertychange fires for most input events but is buggy and + // doesn't fire when text is deleted, but conveniently, selectionchange + // appears to fire in all of the remaining cases so we catch those and + // forward the event if the value has changed + // In either case, we don't want to call the event handler if the value + // is changed from JS so we redefine a setter for `.value` that updates + // our activeElementValue variable, allowing us to ignore those changes + // + // stopWatching() should be a noop here but we call it just in case we + // missed a blur event somehow. + stopWatchingForValueChange(); + startWatchingForValueChange(topLevelTarget, topLevelTargetID); + } else if (topLevelType === topLevelTypes.topBlur) { + stopWatchingForValueChange(); + } +} + +// For IE8 and IE9. +function getTargetIDForInputEventIE( + topLevelType, + topLevelTarget, + topLevelTargetID) { + if (topLevelType === topLevelTypes.topSelectionChange || + topLevelType === topLevelTypes.topKeyUp || + topLevelType === topLevelTypes.topKeyDown) { + // On the selectionchange event, the target is just document which isn't + // helpful for us so just check activeElement instead. + // + // 99% of the time, keydown and keyup aren't necessary. IE8 fails to fire + // propertychange on the first input event after setting `value` from a + // script and fires only keydown, keypress, keyup. Catching keyup usually + // gets it and catching keydown lets us fire an event for the first + // keystroke if user does a key repeat (it'll be a little delayed: right + // before the second keystroke). Other input methods (e.g., paste) seem to + // fire selectionchange normally. + if (activeElement && activeElement.value !== activeElementValue) { + activeElementValue = activeElement.value; + return activeElementID; + } + } +} + + +/** + * SECTION: handle `click` event + */ +function shouldUseClickEvent(elem) { + // Use the `click` event to detect changes to checkbox and radio inputs. + // This approach works across all browsers, whereas `change` does not fire + // until `blur` in IE8. + return ( + elem.nodeName === 'INPUT' && + (elem.type === 'checkbox' || elem.type === 'radio') + ); +} + +function getTargetIDForClickEvent( + topLevelType, + topLevelTarget, + topLevelTargetID) { + if (topLevelType === topLevelTypes.topClick) { + return topLevelTargetID; + } +} + +/** + * This plugin creates an `onChange` event that normalizes change events + * across form elements. This event fires at a time when it's possible to + * change the element's value without seeing a flicker. + * + * Supported elements are: + * - input (see `isTextInputElement`) + * - textarea + * - select + */ +var ChangeEventPlugin = { + + eventTypes: eventTypes, + + /** + * @param {string} topLevelType Record from `EventConstants`. + * @param {DOMEventTarget} topLevelTarget The listening component root node. + * @param {string} topLevelTargetID ID of `topLevelTarget`. + * @param {object} nativeEvent Native browser event. + * @return {*} An accumulation of synthetic events. + * @see {EventPluginHub.extractEvents} + */ + extractEvents: function( + topLevelType, + topLevelTarget, + topLevelTargetID, + nativeEvent) { + + var getTargetIDFunc, handleEventFunc; + if (shouldUseChangeEvent(topLevelTarget)) { + if (doesChangeEventBubble) { + getTargetIDFunc = getTargetIDForChangeEvent; + } else { + handleEventFunc = handleEventsForChangeEventIE8; + } + } else if (isTextInputElement(topLevelTarget)) { + if (isInputEventSupported) { + getTargetIDFunc = getTargetIDForInputEvent; + } else { + getTargetIDFunc = getTargetIDForInputEventIE; + handleEventFunc = handleEventsForInputEventIE; + } + } else if (shouldUseClickEvent(topLevelTarget)) { + getTargetIDFunc = getTargetIDForClickEvent; + } + + if (getTargetIDFunc) { + var targetID = getTargetIDFunc( + topLevelType, + topLevelTarget, + topLevelTargetID + ); + if (targetID) { + var event = SyntheticEvent.getPooled( + eventTypes.change, + targetID, + nativeEvent + ); + EventPropagators.accumulateTwoPhaseDispatches(event); + return event; + } + } + + if (handleEventFunc) { + handleEventFunc( + topLevelType, + topLevelTarget, + topLevelTargetID + ); + } + } + +}; + +module.exports = ChangeEventPlugin; + +},{"./EventConstants":16,"./EventPluginHub":18,"./EventPropagators":21,"./ExecutionEnvironment":22,"./ReactUpdates":89,"./SyntheticEvent":97,"./isEventSupported":138,"./isTextInputElement":140,"./keyOf":143}],9:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ClientReactRootIndex + * @typechecks + */ + +'use strict'; + +var nextReactRootIndex = 0; + +var ClientReactRootIndex = { + createReactRootIndex: function() { + return nextReactRootIndex++; + } +}; + +module.exports = ClientReactRootIndex; + +},{}],10:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule DOMChildrenOperations + * @typechecks static-only + */ + +'use strict'; + +var Danger = require("./Danger"); +var ReactMultiChildUpdateTypes = require("./ReactMultiChildUpdateTypes"); + +var setTextContent = require("./setTextContent"); +var invariant = require("./invariant"); + +/** + * Inserts `childNode` as a child of `parentNode` at the `index`. + * + * @param {DOMElement} parentNode Parent node in which to insert. + * @param {DOMElement} childNode Child node to insert. + * @param {number} index Index at which to insert the child. + * @internal + */ +function insertChildAt(parentNode, childNode, index) { + // By exploiting arrays returning `undefined` for an undefined index, we can + // rely exclusively on `insertBefore(node, null)` instead of also using + // `appendChild(node)`. However, using `undefined` is not allowed by all + // browsers so we must replace it with `null`. + parentNode.insertBefore( + childNode, + parentNode.childNodes[index] || null + ); +} + +/** + * Operations for updating with DOM children. + */ +var DOMChildrenOperations = { + + dangerouslyReplaceNodeWithMarkup: Danger.dangerouslyReplaceNodeWithMarkup, + + updateTextContent: setTextContent, + + /** + * Updates a component's children by processing a series of updates. The + * update configurations are each expected to have a `parentNode` property. + * + * @param {array} updates List of update configurations. + * @param {array} markupList List of markup strings. + * @internal + */ + processUpdates: function(updates, markupList) { + var update; + // Mapping from parent IDs to initial child orderings. + var initialChildren = null; + // List of children that will be moved or removed. + var updatedChildren = null; + + for (var i = 0; i < updates.length; i++) { + update = updates[i]; + if (update.type === ReactMultiChildUpdateTypes.MOVE_EXISTING || + update.type === ReactMultiChildUpdateTypes.REMOVE_NODE) { + var updatedIndex = update.fromIndex; + var updatedChild = update.parentNode.childNodes[updatedIndex]; + var parentID = update.parentID; + + ("production" !== process.env.NODE_ENV ? invariant( + updatedChild, + 'processUpdates(): Unable to find child %s of element. This ' + + 'probably means the DOM was unexpectedly mutated (e.g., by the ' + + 'browser), usually due to forgetting a when using tables, ' + + 'nesting tags like
,

, or , or using non-SVG elements ' + + 'in an parent. Try inspecting the child nodes of the element ' + + 'with React ID `%s`.', + updatedIndex, + parentID + ) : invariant(updatedChild)); + + initialChildren = initialChildren || {}; + initialChildren[parentID] = initialChildren[parentID] || []; + initialChildren[parentID][updatedIndex] = updatedChild; + + updatedChildren = updatedChildren || []; + updatedChildren.push(updatedChild); + } + } + + var renderedMarkup = Danger.dangerouslyRenderMarkup(markupList); + + // Remove updated children first so that `toIndex` is consistent. + if (updatedChildren) { + for (var j = 0; j < updatedChildren.length; j++) { + updatedChildren[j].parentNode.removeChild(updatedChildren[j]); + } + } + + for (var k = 0; k < updates.length; k++) { + update = updates[k]; + switch (update.type) { + case ReactMultiChildUpdateTypes.INSERT_MARKUP: + insertChildAt( + update.parentNode, + renderedMarkup[update.markupIndex], + update.toIndex + ); + break; + case ReactMultiChildUpdateTypes.MOVE_EXISTING: + insertChildAt( + update.parentNode, + initialChildren[update.parentID][update.fromIndex], + update.toIndex + ); + break; + case ReactMultiChildUpdateTypes.TEXT_CONTENT: + setTextContent( + update.parentNode, + update.textContent + ); + break; + case ReactMultiChildUpdateTypes.REMOVE_NODE: + // Already removed by the for-loop above. + break; + } + } + } + +}; + +module.exports = DOMChildrenOperations; + +}).call(this,require('_process')) +},{"./Danger":13,"./ReactMultiChildUpdateTypes":74,"./invariant":137,"./setTextContent":151,"_process":2}],11:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule DOMProperty + * @typechecks static-only + */ + +/*jslint bitwise: true */ + +'use strict'; + +var invariant = require("./invariant"); + +function checkMask(value, bitmask) { + return (value & bitmask) === bitmask; +} + +var DOMPropertyInjection = { + /** + * Mapping from normalized, camelcased property names to a configuration that + * specifies how the associated DOM property should be accessed or rendered. + */ + MUST_USE_ATTRIBUTE: 0x1, + MUST_USE_PROPERTY: 0x2, + HAS_SIDE_EFFECTS: 0x4, + HAS_BOOLEAN_VALUE: 0x8, + HAS_NUMERIC_VALUE: 0x10, + HAS_POSITIVE_NUMERIC_VALUE: 0x20 | 0x10, + HAS_OVERLOADED_BOOLEAN_VALUE: 0x40, + + /** + * Inject some specialized knowledge about the DOM. This takes a config object + * with the following properties: + * + * isCustomAttribute: function that given an attribute name will return true + * if it can be inserted into the DOM verbatim. Useful for data-* or aria-* + * attributes where it's impossible to enumerate all of the possible + * attribute names, + * + * Properties: object mapping DOM property name to one of the + * DOMPropertyInjection constants or null. If your attribute isn't in here, + * it won't get written to the DOM. + * + * DOMAttributeNames: object mapping React attribute name to the DOM + * attribute name. Attribute names not specified use the **lowercase** + * normalized name. + * + * DOMPropertyNames: similar to DOMAttributeNames but for DOM properties. + * Property names not specified use the normalized name. + * + * DOMMutationMethods: Properties that require special mutation methods. If + * `value` is undefined, the mutation method should unset the property. + * + * @param {object} domPropertyConfig the config as described above. + */ + injectDOMPropertyConfig: function(domPropertyConfig) { + var Properties = domPropertyConfig.Properties || {}; + var DOMAttributeNames = domPropertyConfig.DOMAttributeNames || {}; + var DOMPropertyNames = domPropertyConfig.DOMPropertyNames || {}; + var DOMMutationMethods = domPropertyConfig.DOMMutationMethods || {}; + + if (domPropertyConfig.isCustomAttribute) { + DOMProperty._isCustomAttributeFunctions.push( + domPropertyConfig.isCustomAttribute + ); + } + + for (var propName in Properties) { + ("production" !== process.env.NODE_ENV ? invariant( + !DOMProperty.isStandardName.hasOwnProperty(propName), + 'injectDOMPropertyConfig(...): You\'re trying to inject DOM property ' + + '\'%s\' which has already been injected. You may be accidentally ' + + 'injecting the same DOM property config twice, or you may be ' + + 'injecting two configs that have conflicting property names.', + propName + ) : invariant(!DOMProperty.isStandardName.hasOwnProperty(propName))); + + DOMProperty.isStandardName[propName] = true; + + var lowerCased = propName.toLowerCase(); + DOMProperty.getPossibleStandardName[lowerCased] = propName; + + if (DOMAttributeNames.hasOwnProperty(propName)) { + var attributeName = DOMAttributeNames[propName]; + DOMProperty.getPossibleStandardName[attributeName] = propName; + DOMProperty.getAttributeName[propName] = attributeName; + } else { + DOMProperty.getAttributeName[propName] = lowerCased; + } + + DOMProperty.getPropertyName[propName] = + DOMPropertyNames.hasOwnProperty(propName) ? + DOMPropertyNames[propName] : + propName; + + if (DOMMutationMethods.hasOwnProperty(propName)) { + DOMProperty.getMutationMethod[propName] = DOMMutationMethods[propName]; + } else { + DOMProperty.getMutationMethod[propName] = null; + } + + var propConfig = Properties[propName]; + DOMProperty.mustUseAttribute[propName] = + checkMask(propConfig, DOMPropertyInjection.MUST_USE_ATTRIBUTE); + DOMProperty.mustUseProperty[propName] = + checkMask(propConfig, DOMPropertyInjection.MUST_USE_PROPERTY); + DOMProperty.hasSideEffects[propName] = + checkMask(propConfig, DOMPropertyInjection.HAS_SIDE_EFFECTS); + DOMProperty.hasBooleanValue[propName] = + checkMask(propConfig, DOMPropertyInjection.HAS_BOOLEAN_VALUE); + DOMProperty.hasNumericValue[propName] = + checkMask(propConfig, DOMPropertyInjection.HAS_NUMERIC_VALUE); + DOMProperty.hasPositiveNumericValue[propName] = + checkMask(propConfig, DOMPropertyInjection.HAS_POSITIVE_NUMERIC_VALUE); + DOMProperty.hasOverloadedBooleanValue[propName] = + checkMask(propConfig, DOMPropertyInjection.HAS_OVERLOADED_BOOLEAN_VALUE); + + ("production" !== process.env.NODE_ENV ? invariant( + !DOMProperty.mustUseAttribute[propName] || + !DOMProperty.mustUseProperty[propName], + 'DOMProperty: Cannot require using both attribute and property: %s', + propName + ) : invariant(!DOMProperty.mustUseAttribute[propName] || + !DOMProperty.mustUseProperty[propName])); + ("production" !== process.env.NODE_ENV ? invariant( + DOMProperty.mustUseProperty[propName] || + !DOMProperty.hasSideEffects[propName], + 'DOMProperty: Properties that have side effects must use property: %s', + propName + ) : invariant(DOMProperty.mustUseProperty[propName] || + !DOMProperty.hasSideEffects[propName])); + ("production" !== process.env.NODE_ENV ? invariant( + !!DOMProperty.hasBooleanValue[propName] + + !!DOMProperty.hasNumericValue[propName] + + !!DOMProperty.hasOverloadedBooleanValue[propName] <= 1, + 'DOMProperty: Value can be one of boolean, overloaded boolean, or ' + + 'numeric value, but not a combination: %s', + propName + ) : invariant(!!DOMProperty.hasBooleanValue[propName] + + !!DOMProperty.hasNumericValue[propName] + + !!DOMProperty.hasOverloadedBooleanValue[propName] <= 1)); + } + } +}; +var defaultValueCache = {}; + +/** + * DOMProperty exports lookup objects that can be used like functions: + * + * > DOMProperty.isValid['id'] + * true + * > DOMProperty.isValid['foobar'] + * undefined + * + * Although this may be confusing, it performs better in general. + * + * @see http://jsperf.com/key-exists + * @see http://jsperf.com/key-missing + */ +var DOMProperty = { + + ID_ATTRIBUTE_NAME: 'data-reactid', + + /** + * Checks whether a property name is a standard property. + * @type {Object} + */ + isStandardName: {}, + + /** + * Mapping from lowercase property names to the properly cased version, used + * to warn in the case of missing properties. + * @type {Object} + */ + getPossibleStandardName: {}, + + /** + * Mapping from normalized names to attribute names that differ. Attribute + * names are used when rendering markup or with `*Attribute()`. + * @type {Object} + */ + getAttributeName: {}, + + /** + * Mapping from normalized names to properties on DOM node instances. + * (This includes properties that mutate due to external factors.) + * @type {Object} + */ + getPropertyName: {}, + + /** + * Mapping from normalized names to mutation methods. This will only exist if + * mutation cannot be set simply by the property or `setAttribute()`. + * @type {Object} + */ + getMutationMethod: {}, + + /** + * Whether the property must be accessed and mutated as an object property. + * @type {Object} + */ + mustUseAttribute: {}, + + /** + * Whether the property must be accessed and mutated using `*Attribute()`. + * (This includes anything that fails ` in `.) + * @type {Object} + */ + mustUseProperty: {}, + + /** + * Whether or not setting a value causes side effects such as triggering + * resources to be loaded or text selection changes. We must ensure that + * the value is only set if it has changed. + * @type {Object} + */ + hasSideEffects: {}, + + /** + * Whether the property should be removed when set to a falsey value. + * @type {Object} + */ + hasBooleanValue: {}, + + /** + * Whether the property must be numeric or parse as a + * numeric and should be removed when set to a falsey value. + * @type {Object} + */ + hasNumericValue: {}, + + /** + * Whether the property must be positive numeric or parse as a positive + * numeric and should be removed when set to a falsey value. + * @type {Object} + */ + hasPositiveNumericValue: {}, + + /** + * Whether the property can be used as a flag as well as with a value. Removed + * when strictly equal to false; present without a value when strictly equal + * to true; present with a value otherwise. + * @type {Object} + */ + hasOverloadedBooleanValue: {}, + + /** + * All of the isCustomAttribute() functions that have been injected. + */ + _isCustomAttributeFunctions: [], + + /** + * Checks whether a property name is a custom attribute. + * @method + */ + isCustomAttribute: function(attributeName) { + for (var i = 0; i < DOMProperty._isCustomAttributeFunctions.length; i++) { + var isCustomAttributeFn = DOMProperty._isCustomAttributeFunctions[i]; + if (isCustomAttributeFn(attributeName)) { + return true; + } + } + return false; + }, + + /** + * Returns the default property value for a DOM property (i.e., not an + * attribute). Most default values are '' or false, but not all. Worse yet, + * some (in particular, `type`) vary depending on the type of element. + * + * TODO: Is it better to grab all the possible properties when creating an + * element to avoid having to create the same element twice? + */ + getDefaultValueForProperty: function(nodeName, prop) { + var nodeDefaults = defaultValueCache[nodeName]; + var testElement; + if (!nodeDefaults) { + defaultValueCache[nodeName] = nodeDefaults = {}; + } + if (!(prop in nodeDefaults)) { + testElement = document.createElement(nodeName); + nodeDefaults[prop] = testElement[prop]; + } + return nodeDefaults[prop]; + }, + + injection: DOMPropertyInjection +}; + +module.exports = DOMProperty; + +}).call(this,require('_process')) +},{"./invariant":137,"_process":2}],12:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule DOMPropertyOperations + * @typechecks static-only + */ + +'use strict'; + +var DOMProperty = require("./DOMProperty"); + +var quoteAttributeValueForBrowser = require("./quoteAttributeValueForBrowser"); +var warning = require("./warning"); + +function shouldIgnoreValue(name, value) { + return value == null || + (DOMProperty.hasBooleanValue[name] && !value) || + (DOMProperty.hasNumericValue[name] && isNaN(value)) || + (DOMProperty.hasPositiveNumericValue[name] && (value < 1)) || + (DOMProperty.hasOverloadedBooleanValue[name] && value === false); +} + +if ("production" !== process.env.NODE_ENV) { + var reactProps = { + children: true, + dangerouslySetInnerHTML: true, + key: true, + ref: true + }; + var warnedProperties = {}; + + var warnUnknownProperty = function(name) { + if (reactProps.hasOwnProperty(name) && reactProps[name] || + warnedProperties.hasOwnProperty(name) && warnedProperties[name]) { + return; + } + + warnedProperties[name] = true; + var lowerCasedName = name.toLowerCase(); + + // data-* attributes should be lowercase; suggest the lowercase version + var standardName = ( + DOMProperty.isCustomAttribute(lowerCasedName) ? + lowerCasedName : + DOMProperty.getPossibleStandardName.hasOwnProperty(lowerCasedName) ? + DOMProperty.getPossibleStandardName[lowerCasedName] : + null + ); + + // For now, only warn when we have a suggested correction. This prevents + // logging too much when using transferPropsTo. + ("production" !== process.env.NODE_ENV ? warning( + standardName == null, + 'Unknown DOM property %s. Did you mean %s?', + name, + standardName + ) : null); + + }; +} + +/** + * Operations for dealing with DOM properties. + */ +var DOMPropertyOperations = { + + /** + * Creates markup for the ID property. + * + * @param {string} id Unescaped ID. + * @return {string} Markup string. + */ + createMarkupForID: function(id) { + return DOMProperty.ID_ATTRIBUTE_NAME + '=' + + quoteAttributeValueForBrowser(id); + }, + + /** + * Creates markup for a property. + * + * @param {string} name + * @param {*} value + * @return {?string} Markup string, or null if the property was invalid. + */ + createMarkupForProperty: function(name, value) { + if (DOMProperty.isStandardName.hasOwnProperty(name) && + DOMProperty.isStandardName[name]) { + if (shouldIgnoreValue(name, value)) { + return ''; + } + var attributeName = DOMProperty.getAttributeName[name]; + if (DOMProperty.hasBooleanValue[name] || + (DOMProperty.hasOverloadedBooleanValue[name] && value === true)) { + return attributeName; + } + return attributeName + '=' + quoteAttributeValueForBrowser(value); + } else if (DOMProperty.isCustomAttribute(name)) { + if (value == null) { + return ''; + } + return name + '=' + quoteAttributeValueForBrowser(value); + } else if ("production" !== process.env.NODE_ENV) { + warnUnknownProperty(name); + } + return null; + }, + + /** + * Sets the value for a property on a node. + * + * @param {DOMElement} node + * @param {string} name + * @param {*} value + */ + setValueForProperty: function(node, name, value) { + if (DOMProperty.isStandardName.hasOwnProperty(name) && + DOMProperty.isStandardName[name]) { + var mutationMethod = DOMProperty.getMutationMethod[name]; + if (mutationMethod) { + mutationMethod(node, value); + } else if (shouldIgnoreValue(name, value)) { + this.deleteValueForProperty(node, name); + } else if (DOMProperty.mustUseAttribute[name]) { + // `setAttribute` with objects becomes only `[object]` in IE8/9, + // ('' + value) makes it output the correct toString()-value. + node.setAttribute(DOMProperty.getAttributeName[name], '' + value); + } else { + var propName = DOMProperty.getPropertyName[name]; + // Must explicitly cast values for HAS_SIDE_EFFECTS-properties to the + // property type before comparing; only `value` does and is string. + if (!DOMProperty.hasSideEffects[name] || + ('' + node[propName]) !== ('' + value)) { + // Contrary to `setAttribute`, object properties are properly + // `toString`ed by IE8/9. + node[propName] = value; + } + } + } else if (DOMProperty.isCustomAttribute(name)) { + if (value == null) { + node.removeAttribute(name); + } else { + node.setAttribute(name, '' + value); + } + } else if ("production" !== process.env.NODE_ENV) { + warnUnknownProperty(name); + } + }, + + /** + * Deletes the value for a property on a node. + * + * @param {DOMElement} node + * @param {string} name + */ + deleteValueForProperty: function(node, name) { + if (DOMProperty.isStandardName.hasOwnProperty(name) && + DOMProperty.isStandardName[name]) { + var mutationMethod = DOMProperty.getMutationMethod[name]; + if (mutationMethod) { + mutationMethod(node, undefined); + } else if (DOMProperty.mustUseAttribute[name]) { + node.removeAttribute(DOMProperty.getAttributeName[name]); + } else { + var propName = DOMProperty.getPropertyName[name]; + var defaultValue = DOMProperty.getDefaultValueForProperty( + node.nodeName, + propName + ); + if (!DOMProperty.hasSideEffects[name] || + ('' + node[propName]) !== defaultValue) { + node[propName] = defaultValue; + } + } + } else if (DOMProperty.isCustomAttribute(name)) { + node.removeAttribute(name); + } else if ("production" !== process.env.NODE_ENV) { + warnUnknownProperty(name); + } + } + +}; + +module.exports = DOMPropertyOperations; + +}).call(this,require('_process')) +},{"./DOMProperty":11,"./quoteAttributeValueForBrowser":149,"./warning":156,"_process":2}],13:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule Danger + * @typechecks static-only + */ + +/*jslint evil: true, sub: true */ + +'use strict'; + +var ExecutionEnvironment = require("./ExecutionEnvironment"); + +var createNodesFromMarkup = require("./createNodesFromMarkup"); +var emptyFunction = require("./emptyFunction"); +var getMarkupWrap = require("./getMarkupWrap"); +var invariant = require("./invariant"); + +var OPEN_TAG_NAME_EXP = /^(<[^ \/>]+)/; +var RESULT_INDEX_ATTR = 'data-danger-index'; + +/** + * Extracts the `nodeName` from a string of markup. + * + * NOTE: Extracting the `nodeName` does not require a regular expression match + * because we make assumptions about React-generated markup (i.e. there are no + * spaces surrounding the opening tag and there is at least one attribute). + * + * @param {string} markup String of markup. + * @return {string} Node name of the supplied markup. + * @see http://jsperf.com/extract-nodename + */ +function getNodeName(markup) { + return markup.substring(1, markup.indexOf(' ')); +} + +var Danger = { + + /** + * Renders markup into an array of nodes. The markup is expected to render + * into a list of root nodes. Also, the length of `resultList` and + * `markupList` should be the same. + * + * @param {array} markupList List of markup strings to render. + * @return {array} List of rendered nodes. + * @internal + */ + dangerouslyRenderMarkup: function(markupList) { + ("production" !== process.env.NODE_ENV ? invariant( + ExecutionEnvironment.canUseDOM, + 'dangerouslyRenderMarkup(...): Cannot render markup in a worker ' + + 'thread. Make sure `window` and `document` are available globally ' + + 'before requiring React when unit testing or use ' + + 'React.renderToString for server rendering.' + ) : invariant(ExecutionEnvironment.canUseDOM)); + var nodeName; + var markupByNodeName = {}; + // Group markup by `nodeName` if a wrap is necessary, else by '*'. + for (var i = 0; i < markupList.length; i++) { + ("production" !== process.env.NODE_ENV ? invariant( + markupList[i], + 'dangerouslyRenderMarkup(...): Missing markup.' + ) : invariant(markupList[i])); + nodeName = getNodeName(markupList[i]); + nodeName = getMarkupWrap(nodeName) ? nodeName : '*'; + markupByNodeName[nodeName] = markupByNodeName[nodeName] || []; + markupByNodeName[nodeName][i] = markupList[i]; + } + var resultList = []; + var resultListAssignmentCount = 0; + for (nodeName in markupByNodeName) { + if (!markupByNodeName.hasOwnProperty(nodeName)) { + continue; + } + var markupListByNodeName = markupByNodeName[nodeName]; + + // This for-in loop skips the holes of the sparse array. The order of + // iteration should follow the order of assignment, which happens to match + // numerical index order, but we don't rely on that. + var resultIndex; + for (resultIndex in markupListByNodeName) { + if (markupListByNodeName.hasOwnProperty(resultIndex)) { + var markup = markupListByNodeName[resultIndex]; + + // Push the requested markup with an additional RESULT_INDEX_ATTR + // attribute. If the markup does not start with a < character, it + // will be discarded below (with an appropriate console.error). + markupListByNodeName[resultIndex] = markup.replace( + OPEN_TAG_NAME_EXP, + // This index will be parsed back out below. + '$1 ' + RESULT_INDEX_ATTR + '="' + resultIndex + '" ' + ); + } + } + + // Render each group of markup with similar wrapping `nodeName`. + var renderNodes = createNodesFromMarkup( + markupListByNodeName.join(''), + emptyFunction // Do nothing special with

; + * } + * }); + * + * The class specification supports a specific protocol of methods that have + * special meaning (e.g. `render`). See `ReactClassInterface` for + * more the comprehensive protocol. Any other properties and methods in the + * class specification will available on the prototype. + * + * @interface ReactClassInterface + * @internal + */ +var ReactClassInterface = { + + /** + * An array of Mixin objects to include when defining your component. + * + * @type {array} + * @optional + */ + mixins: SpecPolicy.DEFINE_MANY, + + /** + * An object containing properties and methods that should be defined on + * the component's constructor instead of its prototype (static methods). + * + * @type {object} + * @optional + */ + statics: SpecPolicy.DEFINE_MANY, + + /** + * Definition of prop types for this component. + * + * @type {object} + * @optional + */ + propTypes: SpecPolicy.DEFINE_MANY, + + /** + * Definition of context types for this component. + * + * @type {object} + * @optional + */ + contextTypes: SpecPolicy.DEFINE_MANY, + + /** + * Definition of context types this component sets for its children. + * + * @type {object} + * @optional + */ + childContextTypes: SpecPolicy.DEFINE_MANY, + + // ==== Definition methods ==== + + /** + * Invoked when the component is mounted. Values in the mapping will be set on + * `this.props` if that prop is not specified (i.e. using an `in` check). + * + * This method is invoked before `getInitialState` and therefore cannot rely + * on `this.state` or use `this.setState`. + * + * @return {object} + * @optional + */ + getDefaultProps: SpecPolicy.DEFINE_MANY_MERGED, + + /** + * Invoked once before the component is mounted. The return value will be used + * as the initial value of `this.state`. + * + * getInitialState: function() { + * return { + * isOn: false, + * fooBaz: new BazFoo() + * } + * } + * + * @return {object} + * @optional + */ + getInitialState: SpecPolicy.DEFINE_MANY_MERGED, + + /** + * @return {object} + * @optional + */ + getChildContext: SpecPolicy.DEFINE_MANY_MERGED, + + /** + * Uses props from `this.props` and state from `this.state` to render the + * structure of the component. + * + * No guarantees are made about when or how often this method is invoked, so + * it must not have side effects. + * + * render: function() { + * var name = this.props.name; + * return
Hello, {name}!
; + * } + * + * @return {ReactComponent} + * @nosideeffects + * @required + */ + render: SpecPolicy.DEFINE_ONCE, + + + + // ==== Delegate methods ==== + + /** + * Invoked when the component is initially created and about to be mounted. + * This may have side effects, but any external subscriptions or data created + * by this method must be cleaned up in `componentWillUnmount`. + * + * @optional + */ + componentWillMount: SpecPolicy.DEFINE_MANY, + + /** + * Invoked when the component has been mounted and has a DOM representation. + * However, there is no guarantee that the DOM node is in the document. + * + * Use this as an opportunity to operate on the DOM when the component has + * been mounted (initialized and rendered) for the first time. + * + * @param {DOMElement} rootNode DOM element representing the component. + * @optional + */ + componentDidMount: SpecPolicy.DEFINE_MANY, + + /** + * Invoked before the component receives new props. + * + * Use this as an opportunity to react to a prop transition by updating the + * state using `this.setState`. Current props are accessed via `this.props`. + * + * componentWillReceiveProps: function(nextProps, nextContext) { + * this.setState({ + * likesIncreasing: nextProps.likeCount > this.props.likeCount + * }); + * } + * + * NOTE: There is no equivalent `componentWillReceiveState`. An incoming prop + * transition may cause a state change, but the opposite is not true. If you + * need it, you are probably looking for `componentWillUpdate`. + * + * @param {object} nextProps + * @optional + */ + componentWillReceiveProps: SpecPolicy.DEFINE_MANY, + + /** + * Invoked while deciding if the component should be updated as a result of + * receiving new props, state and/or context. + * + * Use this as an opportunity to `return false` when you're certain that the + * transition to the new props/state/context will not require a component + * update. + * + * shouldComponentUpdate: function(nextProps, nextState, nextContext) { + * return !equal(nextProps, this.props) || + * !equal(nextState, this.state) || + * !equal(nextContext, this.context); + * } + * + * @param {object} nextProps + * @param {?object} nextState + * @param {?object} nextContext + * @return {boolean} True if the component should update. + * @optional + */ + shouldComponentUpdate: SpecPolicy.DEFINE_ONCE, + + /** + * Invoked when the component is about to update due to a transition from + * `this.props`, `this.state` and `this.context` to `nextProps`, `nextState` + * and `nextContext`. + * + * Use this as an opportunity to perform preparation before an update occurs. + * + * NOTE: You **cannot** use `this.setState()` in this method. + * + * @param {object} nextProps + * @param {?object} nextState + * @param {?object} nextContext + * @param {ReactReconcileTransaction} transaction + * @optional + */ + componentWillUpdate: SpecPolicy.DEFINE_MANY, + + /** + * Invoked when the component's DOM representation has been updated. + * + * Use this as an opportunity to operate on the DOM when the component has + * been updated. + * + * @param {object} prevProps + * @param {?object} prevState + * @param {?object} prevContext + * @param {DOMElement} rootNode DOM element representing the component. + * @optional + */ + componentDidUpdate: SpecPolicy.DEFINE_MANY, + + /** + * Invoked when the component is about to be removed from its parent and have + * its DOM representation destroyed. + * + * Use this as an opportunity to deallocate any external resources. + * + * NOTE: There is no `componentDidUnmount` since your component will have been + * destroyed by that point. + * + * @optional + */ + componentWillUnmount: SpecPolicy.DEFINE_MANY, + + + + // ==== Advanced methods ==== + + /** + * Updates the component's currently mounted DOM representation. + * + * By default, this implements React's rendering and reconciliation algorithm. + * Sophisticated clients may wish to override this. + * + * @param {ReactReconcileTransaction} transaction + * @internal + * @overridable + */ + updateComponent: SpecPolicy.OVERRIDE_BASE + +}; + +/** + * Mapping from class specification keys to special processing functions. + * + * Although these are declared like instance properties in the specification + * when defining classes using `React.createClass`, they are actually static + * and are accessible on the constructor instead of the prototype. Despite + * being static, they must be defined outside of the "statics" key under + * which all other static methods are defined. + */ +var RESERVED_SPEC_KEYS = { + displayName: function(Constructor, displayName) { + Constructor.displayName = displayName; + }, + mixins: function(Constructor, mixins) { + if (mixins) { + for (var i = 0; i < mixins.length; i++) { + mixSpecIntoComponent(Constructor, mixins[i]); + } + } + }, + childContextTypes: function(Constructor, childContextTypes) { + if ("production" !== process.env.NODE_ENV) { + validateTypeDef( + Constructor, + childContextTypes, + ReactPropTypeLocations.childContext + ); + } + Constructor.childContextTypes = assign( + {}, + Constructor.childContextTypes, + childContextTypes + ); + }, + contextTypes: function(Constructor, contextTypes) { + if ("production" !== process.env.NODE_ENV) { + validateTypeDef( + Constructor, + contextTypes, + ReactPropTypeLocations.context + ); + } + Constructor.contextTypes = assign( + {}, + Constructor.contextTypes, + contextTypes + ); + }, + /** + * Special case getDefaultProps which should move into statics but requires + * automatic merging. + */ + getDefaultProps: function(Constructor, getDefaultProps) { + if (Constructor.getDefaultProps) { + Constructor.getDefaultProps = createMergedResultFunction( + Constructor.getDefaultProps, + getDefaultProps + ); + } else { + Constructor.getDefaultProps = getDefaultProps; + } + }, + propTypes: function(Constructor, propTypes) { + if ("production" !== process.env.NODE_ENV) { + validateTypeDef( + Constructor, + propTypes, + ReactPropTypeLocations.prop + ); + } + Constructor.propTypes = assign( + {}, + Constructor.propTypes, + propTypes + ); + }, + statics: function(Constructor, statics) { + mixStaticSpecIntoComponent(Constructor, statics); + } +}; + +function validateTypeDef(Constructor, typeDef, location) { + for (var propName in typeDef) { + if (typeDef.hasOwnProperty(propName)) { + // use a warning instead of an invariant so components + // don't show up in prod but not in __DEV__ + ("production" !== process.env.NODE_ENV ? warning( + typeof typeDef[propName] === 'function', + '%s: %s type `%s` is invalid; it must be a function, usually from ' + + 'React.PropTypes.', + Constructor.displayName || 'ReactClass', + ReactPropTypeLocationNames[location], + propName + ) : null); + } + } +} + +function validateMethodOverride(proto, name) { + var specPolicy = ReactClassInterface.hasOwnProperty(name) ? + ReactClassInterface[name] : + null; + + // Disallow overriding of base class methods unless explicitly allowed. + if (ReactClassMixin.hasOwnProperty(name)) { + ("production" !== process.env.NODE_ENV ? invariant( + specPolicy === SpecPolicy.OVERRIDE_BASE, + 'ReactClassInterface: You are attempting to override ' + + '`%s` from your class specification. Ensure that your method names ' + + 'do not overlap with React methods.', + name + ) : invariant(specPolicy === SpecPolicy.OVERRIDE_BASE)); + } + + // Disallow defining methods more than once unless explicitly allowed. + if (proto.hasOwnProperty(name)) { + ("production" !== process.env.NODE_ENV ? invariant( + specPolicy === SpecPolicy.DEFINE_MANY || + specPolicy === SpecPolicy.DEFINE_MANY_MERGED, + 'ReactClassInterface: You are attempting to define ' + + '`%s` on your component more than once. This conflict may be due ' + + 'to a mixin.', + name + ) : invariant(specPolicy === SpecPolicy.DEFINE_MANY || + specPolicy === SpecPolicy.DEFINE_MANY_MERGED)); + } +} + +/** + * Mixin helper which handles policy validation and reserved + * specification keys when building React classses. + */ +function mixSpecIntoComponent(Constructor, spec) { + if (!spec) { + return; + } + + ("production" !== process.env.NODE_ENV ? invariant( + typeof spec !== 'function', + 'ReactClass: You\'re attempting to ' + + 'use a component class as a mixin. Instead, just use a regular object.' + ) : invariant(typeof spec !== 'function')); + ("production" !== process.env.NODE_ENV ? invariant( + !ReactElement.isValidElement(spec), + 'ReactClass: You\'re attempting to ' + + 'use a component as a mixin. Instead, just use a regular object.' + ) : invariant(!ReactElement.isValidElement(spec))); + + var proto = Constructor.prototype; + + // By handling mixins before any other properties, we ensure the same + // chaining order is applied to methods with DEFINE_MANY policy, whether + // mixins are listed before or after these methods in the spec. + if (spec.hasOwnProperty(MIXINS_KEY)) { + RESERVED_SPEC_KEYS.mixins(Constructor, spec.mixins); + } + + for (var name in spec) { + if (!spec.hasOwnProperty(name)) { + continue; + } + + if (name === MIXINS_KEY) { + // We have already handled mixins in a special case above + continue; + } + + var property = spec[name]; + validateMethodOverride(proto, name); + + if (RESERVED_SPEC_KEYS.hasOwnProperty(name)) { + RESERVED_SPEC_KEYS[name](Constructor, property); + } else { + // Setup methods on prototype: + // The following member methods should not be automatically bound: + // 1. Expected ReactClass methods (in the "interface"). + // 2. Overridden methods (that were mixed in). + var isReactClassMethod = + ReactClassInterface.hasOwnProperty(name); + var isAlreadyDefined = proto.hasOwnProperty(name); + var markedDontBind = property && property.__reactDontBind; + var isFunction = typeof property === 'function'; + var shouldAutoBind = + isFunction && + !isReactClassMethod && + !isAlreadyDefined && + !markedDontBind; + + if (shouldAutoBind) { + if (!proto.__reactAutoBindMap) { + proto.__reactAutoBindMap = {}; + } + proto.__reactAutoBindMap[name] = property; + proto[name] = property; + } else { + if (isAlreadyDefined) { + var specPolicy = ReactClassInterface[name]; + + // These cases should already be caught by validateMethodOverride + ("production" !== process.env.NODE_ENV ? invariant( + isReactClassMethod && ( + (specPolicy === SpecPolicy.DEFINE_MANY_MERGED || specPolicy === SpecPolicy.DEFINE_MANY) + ), + 'ReactClass: Unexpected spec policy %s for key %s ' + + 'when mixing in component specs.', + specPolicy, + name + ) : invariant(isReactClassMethod && ( + (specPolicy === SpecPolicy.DEFINE_MANY_MERGED || specPolicy === SpecPolicy.DEFINE_MANY) + ))); + + // For methods which are defined more than once, call the existing + // methods before calling the new property, merging if appropriate. + if (specPolicy === SpecPolicy.DEFINE_MANY_MERGED) { + proto[name] = createMergedResultFunction(proto[name], property); + } else if (specPolicy === SpecPolicy.DEFINE_MANY) { + proto[name] = createChainedFunction(proto[name], property); + } + } else { + proto[name] = property; + if ("production" !== process.env.NODE_ENV) { + // Add verbose displayName to the function, which helps when looking + // at profiling tools. + if (typeof property === 'function' && spec.displayName) { + proto[name].displayName = spec.displayName + '_' + name; + } + } + } + } + } + } +} + +function mixStaticSpecIntoComponent(Constructor, statics) { + if (!statics) { + return; + } + for (var name in statics) { + var property = statics[name]; + if (!statics.hasOwnProperty(name)) { + continue; + } + + var isReserved = name in RESERVED_SPEC_KEYS; + ("production" !== process.env.NODE_ENV ? invariant( + !isReserved, + 'ReactClass: You are attempting to define a reserved ' + + 'property, `%s`, that shouldn\'t be on the "statics" key. Define it ' + + 'as an instance property instead; it will still be accessible on the ' + + 'constructor.', + name + ) : invariant(!isReserved)); + + var isInherited = name in Constructor; + ("production" !== process.env.NODE_ENV ? invariant( + !isInherited, + 'ReactClass: You are attempting to define ' + + '`%s` on your component more than once. This conflict may be ' + + 'due to a mixin.', + name + ) : invariant(!isInherited)); + Constructor[name] = property; + } +} + +/** + * Merge two objects, but throw if both contain the same key. + * + * @param {object} one The first object, which is mutated. + * @param {object} two The second object + * @return {object} one after it has been mutated to contain everything in two. + */ +function mergeIntoWithNoDuplicateKeys(one, two) { + ("production" !== process.env.NODE_ENV ? invariant( + one && two && typeof one === 'object' && typeof two === 'object', + 'mergeIntoWithNoDuplicateKeys(): Cannot merge non-objects.' + ) : invariant(one && two && typeof one === 'object' && typeof two === 'object')); + + for (var key in two) { + if (two.hasOwnProperty(key)) { + ("production" !== process.env.NODE_ENV ? invariant( + one[key] === undefined, + 'mergeIntoWithNoDuplicateKeys(): ' + + 'Tried to merge two objects with the same key: `%s`. This conflict ' + + 'may be due to a mixin; in particular, this may be caused by two ' + + 'getInitialState() or getDefaultProps() methods returning objects ' + + 'with clashing keys.', + key + ) : invariant(one[key] === undefined)); + one[key] = two[key]; + } + } + return one; +} + +/** + * Creates a function that invokes two functions and merges their return values. + * + * @param {function} one Function to invoke first. + * @param {function} two Function to invoke second. + * @return {function} Function that invokes the two argument functions. + * @private + */ +function createMergedResultFunction(one, two) { + return function mergedResult() { + var a = one.apply(this, arguments); + var b = two.apply(this, arguments); + if (a == null) { + return b; + } else if (b == null) { + return a; + } + var c = {}; + mergeIntoWithNoDuplicateKeys(c, a); + mergeIntoWithNoDuplicateKeys(c, b); + return c; + }; +} + +/** + * Creates a function that invokes two functions and ignores their return vales. + * + * @param {function} one Function to invoke first. + * @param {function} two Function to invoke second. + * @return {function} Function that invokes the two argument functions. + * @private + */ +function createChainedFunction(one, two) { + return function chainedFunction() { + one.apply(this, arguments); + two.apply(this, arguments); + }; +} + +/** + * Binds a method to the component. + * + * @param {object} component Component whose method is going to be bound. + * @param {function} method Method to be bound. + * @return {function} The bound method. + */ +function bindAutoBindMethod(component, method) { + var boundMethod = method.bind(component); + if ("production" !== process.env.NODE_ENV) { + boundMethod.__reactBoundContext = component; + boundMethod.__reactBoundMethod = method; + boundMethod.__reactBoundArguments = null; + var componentName = component.constructor.displayName; + var _bind = boundMethod.bind; + /* eslint-disable block-scoped-var, no-undef */ + boundMethod.bind = function(newThis ) {for (var args=[],$__0=1,$__1=arguments.length;$__0<$__1;$__0++) args.push(arguments[$__0]); + // User is trying to bind() an autobound method; we effectively will + // ignore the value of "this" that the user is trying to use, so + // let's warn. + if (newThis !== component && newThis !== null) { + ("production" !== process.env.NODE_ENV ? warning( + false, + 'bind(): React component methods may only be bound to the ' + + 'component instance. See %s', + componentName + ) : null); + } else if (!args.length) { + ("production" !== process.env.NODE_ENV ? warning( + false, + 'bind(): You are binding a component method to the component. ' + + 'React does this for you automatically in a high-performance ' + + 'way, so you can safely remove this call. See %s', + componentName + ) : null); + return boundMethod; + } + var reboundMethod = _bind.apply(boundMethod, arguments); + reboundMethod.__reactBoundContext = component; + reboundMethod.__reactBoundMethod = method; + reboundMethod.__reactBoundArguments = args; + return reboundMethod; + /* eslint-enable */ + }; + } + return boundMethod; +} + +/** + * Binds all auto-bound methods in a component. + * + * @param {object} component Component whose method is going to be bound. + */ +function bindAutoBindMethods(component) { + for (var autoBindKey in component.__reactAutoBindMap) { + if (component.__reactAutoBindMap.hasOwnProperty(autoBindKey)) { + var method = component.__reactAutoBindMap[autoBindKey]; + component[autoBindKey] = bindAutoBindMethod( + component, + ReactErrorUtils.guard( + method, + component.constructor.displayName + '.' + autoBindKey + ) + ); + } + } +} + +var typeDeprecationDescriptor = { + enumerable: false, + get: function() { + var displayName = this.displayName || this.name || 'Component'; + ("production" !== process.env.NODE_ENV ? warning( + false, + '%s.type is deprecated. Use %s directly to access the class.', + displayName, + displayName + ) : null); + Object.defineProperty(this, 'type', { + value: this + }); + return this; + } +}; + +/** + * Add more to the ReactClass base class. These are all legacy features and + * therefore not already part of the modern ReactComponent. + */ +var ReactClassMixin = { + + /** + * TODO: This will be deprecated because state should always keep a consistent + * type signature and the only use case for this, is to avoid that. + */ + replaceState: function(newState, callback) { + ReactUpdateQueue.enqueueReplaceState(this, newState); + if (callback) { + ReactUpdateQueue.enqueueCallback(this, callback); + } + }, + + /** + * Checks whether or not this composite component is mounted. + * @return {boolean} True if mounted, false otherwise. + * @protected + * @final + */ + isMounted: function() { + if ("production" !== process.env.NODE_ENV) { + var owner = ReactCurrentOwner.current; + if (owner !== null) { + ("production" !== process.env.NODE_ENV ? warning( + owner._warnedAboutRefsInRender, + '%s is accessing isMounted inside its render() function. ' + + 'render() should be a pure function of props and state. It should ' + + 'never access something that requires stale data from the previous ' + + 'render, such as refs. Move this logic to componentDidMount and ' + + 'componentDidUpdate instead.', + owner.getName() || 'A component' + ) : null); + owner._warnedAboutRefsInRender = true; + } + } + var internalInstance = ReactInstanceMap.get(this); + return ( + internalInstance && + internalInstance !== ReactLifeCycle.currentlyMountingInstance + ); + }, + + /** + * Sets a subset of the props. + * + * @param {object} partialProps Subset of the next props. + * @param {?function} callback Called after props are updated. + * @final + * @public + * @deprecated + */ + setProps: function(partialProps, callback) { + ReactUpdateQueue.enqueueSetProps(this, partialProps); + if (callback) { + ReactUpdateQueue.enqueueCallback(this, callback); + } + }, + + /** + * Replace all the props. + * + * @param {object} newProps Subset of the next props. + * @param {?function} callback Called after props are updated. + * @final + * @public + * @deprecated + */ + replaceProps: function(newProps, callback) { + ReactUpdateQueue.enqueueReplaceProps(this, newProps); + if (callback) { + ReactUpdateQueue.enqueueCallback(this, callback); + } + } +}; + +var ReactClassComponent = function() {}; +assign( + ReactClassComponent.prototype, + ReactComponent.prototype, + ReactClassMixin +); + +/** + * Module for creating composite components. + * + * @class ReactClass + */ +var ReactClass = { + + /** + * Creates a composite component class given a class specification. + * + * @param {object} spec Class specification (which must define `render`). + * @return {function} Component constructor function. + * @public + */ + createClass: function(spec) { + var Constructor = function(props, context) { + // This constructor is overridden by mocks. The argument is used + // by mocks to assert on what gets mounted. + + if ("production" !== process.env.NODE_ENV) { + ("production" !== process.env.NODE_ENV ? warning( + this instanceof Constructor, + 'Something is calling a React component directly. Use a factory or ' + + 'JSX instead. See: https://fb.me/react-legacyfactory' + ) : null); + } + + // Wire up auto-binding + if (this.__reactAutoBindMap) { + bindAutoBindMethods(this); + } + + this.props = props; + this.context = context; + this.state = null; + + // ReactClasses doesn't have constructors. Instead, they use the + // getInitialState and componentWillMount methods for initialization. + + var initialState = this.getInitialState ? this.getInitialState() : null; + if ("production" !== process.env.NODE_ENV) { + // We allow auto-mocks to proceed as if they're returning null. + if (typeof initialState === 'undefined' && + this.getInitialState._isMockFunction) { + // This is probably bad practice. Consider warning here and + // deprecating this convenience. + initialState = null; + } + } + ("production" !== process.env.NODE_ENV ? invariant( + typeof initialState === 'object' && !Array.isArray(initialState), + '%s.getInitialState(): must return an object or null', + Constructor.displayName || 'ReactCompositeComponent' + ) : invariant(typeof initialState === 'object' && !Array.isArray(initialState))); + + this.state = initialState; + }; + Constructor.prototype = new ReactClassComponent(); + Constructor.prototype.constructor = Constructor; + + injectedMixins.forEach( + mixSpecIntoComponent.bind(null, Constructor) + ); + + mixSpecIntoComponent(Constructor, spec); + + // Initialize the defaultProps property after all mixins have been merged + if (Constructor.getDefaultProps) { + Constructor.defaultProps = Constructor.getDefaultProps(); + } + + if ("production" !== process.env.NODE_ENV) { + // This is a tag to indicate that the use of these method names is ok, + // since it's used with createClass. If it's not, then it's likely a + // mistake so we'll warn you to use the static property, property + // initializer or constructor respectively. + if (Constructor.getDefaultProps) { + Constructor.getDefaultProps.isReactClassApproved = {}; + } + if (Constructor.prototype.getInitialState) { + Constructor.prototype.getInitialState.isReactClassApproved = {}; + } + } + + ("production" !== process.env.NODE_ENV ? invariant( + Constructor.prototype.render, + 'createClass(...): Class specification must implement a `render` method.' + ) : invariant(Constructor.prototype.render)); + + if ("production" !== process.env.NODE_ENV) { + ("production" !== process.env.NODE_ENV ? warning( + !Constructor.prototype.componentShouldUpdate, + '%s has a method called ' + + 'componentShouldUpdate(). Did you mean shouldComponentUpdate()? ' + + 'The name is phrased as a question because the function is ' + + 'expected to return a value.', + spec.displayName || 'A component' + ) : null); + } + + // Reduce time spent doing lookups by setting these on the prototype. + for (var methodName in ReactClassInterface) { + if (!Constructor.prototype[methodName]) { + Constructor.prototype[methodName] = null; + } + } + + // Legacy hook + Constructor.type = Constructor; + if ("production" !== process.env.NODE_ENV) { + try { + Object.defineProperty(Constructor, 'type', typeDeprecationDescriptor); + } catch (x) { + // IE will fail on defineProperty (es5-shim/sham too) + } + } + + return Constructor; + }, + + injection: { + injectMixin: function(mixin) { + injectedMixins.push(mixin); + } + } + +}; + +module.exports = ReactClass; + +}).call(this,require('_process')) +},{"./Object.assign":28,"./ReactComponent":36,"./ReactCurrentOwner":41,"./ReactElement":59,"./ReactErrorUtils":62,"./ReactInstanceMap":69,"./ReactLifeCycle":70,"./ReactPropTypeLocationNames":78,"./ReactPropTypeLocations":79,"./ReactUpdateQueue":88,"./invariant":137,"./keyMirror":142,"./keyOf":143,"./warning":156,"_process":2}],36:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactComponent + */ + +'use strict'; + +var ReactUpdateQueue = require("./ReactUpdateQueue"); + +var invariant = require("./invariant"); +var warning = require("./warning"); + +/** + * Base class helpers for the updating state of a component. + */ +function ReactComponent(props, context) { + this.props = props; + this.context = context; +} + +/** + * Sets a subset of the state. Always use this to mutate + * state. You should treat `this.state` as immutable. + * + * There is no guarantee that `this.state` will be immediately updated, so + * accessing `this.state` after calling this method may return the old value. + * + * There is no guarantee that calls to `setState` will run synchronously, + * as they may eventually be batched together. You can provide an optional + * callback that will be executed when the call to setState is actually + * completed. + * + * When a function is provided to setState, it will be called at some point in + * the future (not synchronously). It will be called with the up to date + * component arguments (state, props, context). These values can be different + * from this.* because your function may be called after receiveProps but before + * shouldComponentUpdate, and this new state, props, and context will not yet be + * assigned to this. + * + * @param {object|function} partialState Next partial state or function to + * produce next partial state to be merged with current state. + * @param {?function} callback Called after state is updated. + * @final + * @protected + */ +ReactComponent.prototype.setState = function(partialState, callback) { + ("production" !== process.env.NODE_ENV ? invariant( + typeof partialState === 'object' || + typeof partialState === 'function' || + partialState == null, + 'setState(...): takes an object of state variables to update or a ' + + 'function which returns an object of state variables.' + ) : invariant(typeof partialState === 'object' || + typeof partialState === 'function' || + partialState == null)); + if ("production" !== process.env.NODE_ENV) { + ("production" !== process.env.NODE_ENV ? warning( + partialState != null, + 'setState(...): You passed an undefined or null state object; ' + + 'instead, use forceUpdate().' + ) : null); + } + ReactUpdateQueue.enqueueSetState(this, partialState); + if (callback) { + ReactUpdateQueue.enqueueCallback(this, callback); + } +}; + +/** + * Forces an update. This should only be invoked when it is known with + * certainty that we are **not** in a DOM transaction. + * + * You may want to call this when you know that some deeper aspect of the + * component's state has changed but `setState` was not called. + * + * This will not invoke `shouldComponentUpdate`, but it will invoke + * `componentWillUpdate` and `componentDidUpdate`. + * + * @param {?function} callback Called after update is complete. + * @final + * @protected + */ +ReactComponent.prototype.forceUpdate = function(callback) { + ReactUpdateQueue.enqueueForceUpdate(this); + if (callback) { + ReactUpdateQueue.enqueueCallback(this, callback); + } +}; + +/** + * Deprecated APIs. These APIs used to exist on classic React classes but since + * we would like to deprecate them, we're not going to move them over to this + * modern base class. Instead, we define a getter that warns if it's accessed. + */ +if ("production" !== process.env.NODE_ENV) { + var deprecatedAPIs = { + getDOMNode: [ + 'getDOMNode', + 'Use React.findDOMNode(component) instead.' + ], + isMounted: [ + 'isMounted', + 'Instead, make sure to clean up subscriptions and pending requests in ' + + 'componentWillUnmount to prevent memory leaks.' + ], + replaceProps: [ + 'replaceProps', + 'Instead, call React.render again at the top level.' + ], + replaceState: [ + 'replaceState', + 'Refactor your code to use setState instead (see ' + + 'https://github.com/facebook/react/issues/3236).' + ], + setProps: [ + 'setProps', + 'Instead, call React.render again at the top level.' + ] + }; + var defineDeprecationWarning = function(methodName, info) { + try { + Object.defineProperty(ReactComponent.prototype, methodName, { + get: function() { + ("production" !== process.env.NODE_ENV ? warning( + false, + '%s(...) is deprecated in plain JavaScript React classes. %s', + info[0], + info[1] + ) : null); + return undefined; + } + }); + } catch (x) { + // IE will fail on defineProperty (es5-shim/sham too) + } + }; + for (var fnName in deprecatedAPIs) { + if (deprecatedAPIs.hasOwnProperty(fnName)) { + defineDeprecationWarning(fnName, deprecatedAPIs[fnName]); + } + } +} + +module.exports = ReactComponent; + +}).call(this,require('_process')) +},{"./ReactUpdateQueue":88,"./invariant":137,"./warning":156,"_process":2}],37:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactComponentBrowserEnvironment + */ + +/*jslint evil: true */ + +'use strict'; + +var ReactDOMIDOperations = require("./ReactDOMIDOperations"); +var ReactMount = require("./ReactMount"); + +/** + * Abstracts away all functionality of the reconciler that requires knowledge of + * the browser context. TODO: These callers should be refactored to avoid the + * need for this injection. + */ +var ReactComponentBrowserEnvironment = { + + processChildrenUpdates: + ReactDOMIDOperations.dangerouslyProcessChildrenUpdates, + + replaceNodeWithMarkupByID: + ReactDOMIDOperations.dangerouslyReplaceNodeWithMarkupByID, + + /** + * If a particular environment requires that some resources be cleaned up, + * specify this in the injected Mixin. In the DOM, we would likely want to + * purge any cached node ID lookups. + * + * @private + */ + unmountIDFromEnvironment: function(rootNodeID) { + ReactMount.purgeID(rootNodeID); + } + +}; + +module.exports = ReactComponentBrowserEnvironment; + +},{"./ReactDOMIDOperations":46,"./ReactMount":72}],38:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2014-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactComponentEnvironment + */ + +'use strict'; + +var invariant = require("./invariant"); + +var injected = false; + +var ReactComponentEnvironment = { + + /** + * Optionally injectable environment dependent cleanup hook. (server vs. + * browser etc). Example: A browser system caches DOM nodes based on component + * ID and must remove that cache entry when this instance is unmounted. + */ + unmountIDFromEnvironment: null, + + /** + * Optionally injectable hook for swapping out mount images in the middle of + * the tree. + */ + replaceNodeWithMarkupByID: null, + + /** + * Optionally injectable hook for processing a queue of child updates. Will + * later move into MultiChildComponents. + */ + processChildrenUpdates: null, + + injection: { + injectEnvironment: function(environment) { + ("production" !== process.env.NODE_ENV ? invariant( + !injected, + 'ReactCompositeComponent: injectEnvironment() can only be called once.' + ) : invariant(!injected)); + ReactComponentEnvironment.unmountIDFromEnvironment = + environment.unmountIDFromEnvironment; + ReactComponentEnvironment.replaceNodeWithMarkupByID = + environment.replaceNodeWithMarkupByID; + ReactComponentEnvironment.processChildrenUpdates = + environment.processChildrenUpdates; + injected = true; + } + } + +}; + +module.exports = ReactComponentEnvironment; + +}).call(this,require('_process')) +},{"./invariant":137,"_process":2}],39:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactCompositeComponent + */ + +'use strict'; + +var ReactComponentEnvironment = require("./ReactComponentEnvironment"); +var ReactContext = require("./ReactContext"); +var ReactCurrentOwner = require("./ReactCurrentOwner"); +var ReactElement = require("./ReactElement"); +var ReactElementValidator = require("./ReactElementValidator"); +var ReactInstanceMap = require("./ReactInstanceMap"); +var ReactLifeCycle = require("./ReactLifeCycle"); +var ReactNativeComponent = require("./ReactNativeComponent"); +var ReactPerf = require("./ReactPerf"); +var ReactPropTypeLocations = require("./ReactPropTypeLocations"); +var ReactPropTypeLocationNames = require("./ReactPropTypeLocationNames"); +var ReactReconciler = require("./ReactReconciler"); +var ReactUpdates = require("./ReactUpdates"); + +var assign = require("./Object.assign"); +var emptyObject = require("./emptyObject"); +var invariant = require("./invariant"); +var shouldUpdateReactComponent = require("./shouldUpdateReactComponent"); +var warning = require("./warning"); + +function getDeclarationErrorAddendum(component) { + var owner = component._currentElement._owner || null; + if (owner) { + var name = owner.getName(); + if (name) { + return ' Check the render method of `' + name + '`.'; + } + } + return ''; +} + +/** + * ------------------ The Life-Cycle of a Composite Component ------------------ + * + * - constructor: Initialization of state. The instance is now retained. + * - componentWillMount + * - render + * - [children's constructors] + * - [children's componentWillMount and render] + * - [children's componentDidMount] + * - componentDidMount + * + * Update Phases: + * - componentWillReceiveProps (only called if parent updated) + * - shouldComponentUpdate + * - componentWillUpdate + * - render + * - [children's constructors or receive props phases] + * - componentDidUpdate + * + * - componentWillUnmount + * - [children's componentWillUnmount] + * - [children destroyed] + * - (destroyed): The instance is now blank, released by React and ready for GC. + * + * ----------------------------------------------------------------------------- + */ + +/** + * An incrementing ID assigned to each component when it is mounted. This is + * used to enforce the order in which `ReactUpdates` updates dirty components. + * + * @private + */ +var nextMountID = 1; + +/** + * @lends {ReactCompositeComponent.prototype} + */ +var ReactCompositeComponentMixin = { + + /** + * Base constructor for all composite component. + * + * @param {ReactElement} element + * @final + * @internal + */ + construct: function(element) { + this._currentElement = element; + this._rootNodeID = null; + this._instance = null; + + // See ReactUpdateQueue + this._pendingElement = null; + this._pendingStateQueue = null; + this._pendingReplaceState = false; + this._pendingForceUpdate = false; + + this._renderedComponent = null; + + this._context = null; + this._mountOrder = 0; + this._isTopLevel = false; + + // See ReactUpdates and ReactUpdateQueue. + this._pendingCallbacks = null; + }, + + /** + * Initializes the component, renders markup, and registers event listeners. + * + * @param {string} rootID DOM ID of the root node. + * @param {ReactReconcileTransaction|ReactServerRenderingTransaction} transaction + * @return {?string} Rendered markup to be inserted into the DOM. + * @final + * @internal + */ + mountComponent: function(rootID, transaction, context) { + this._context = context; + this._mountOrder = nextMountID++; + this._rootNodeID = rootID; + + var publicProps = this._processProps(this._currentElement.props); + var publicContext = this._processContext(this._currentElement._context); + + var Component = ReactNativeComponent.getComponentClassForElement( + this._currentElement + ); + + // Initialize the public class + var inst = new Component(publicProps, publicContext); + + if ("production" !== process.env.NODE_ENV) { + // This will throw later in _renderValidatedComponent, but add an early + // warning now to help debugging + ("production" !== process.env.NODE_ENV ? warning( + inst.render != null, + '%s(...): No `render` method found on the returned component ' + + 'instance: you may have forgotten to define `render` in your ' + + 'component or you may have accidentally tried to render an element ' + + 'whose type is a function that isn\'t a React component.', + Component.displayName || Component.name || 'Component' + ) : null); + } + + // These should be set up in the constructor, but as a convenience for + // simpler class abstractions, we set them up after the fact. + inst.props = publicProps; + inst.context = publicContext; + inst.refs = emptyObject; + + this._instance = inst; + + // Store a reference from the instance back to the internal representation + ReactInstanceMap.set(inst, this); + + if ("production" !== process.env.NODE_ENV) { + this._warnIfContextsDiffer(this._currentElement._context, context); + } + + if ("production" !== process.env.NODE_ENV) { + // Since plain JS classes are defined without any special initialization + // logic, we can not catch common errors early. Therefore, we have to + // catch them here, at initialization time, instead. + ("production" !== process.env.NODE_ENV ? warning( + !inst.getInitialState || + inst.getInitialState.isReactClassApproved, + 'getInitialState was defined on %s, a plain JavaScript class. ' + + 'This is only supported for classes created using React.createClass. ' + + 'Did you mean to define a state property instead?', + this.getName() || 'a component' + ) : null); + ("production" !== process.env.NODE_ENV ? warning( + !inst.getDefaultProps || + inst.getDefaultProps.isReactClassApproved, + 'getDefaultProps was defined on %s, a plain JavaScript class. ' + + 'This is only supported for classes created using React.createClass. ' + + 'Use a static property to define defaultProps instead.', + this.getName() || 'a component' + ) : null); + ("production" !== process.env.NODE_ENV ? warning( + !inst.propTypes, + 'propTypes was defined as an instance property on %s. Use a static ' + + 'property to define propTypes instead.', + this.getName() || 'a component' + ) : null); + ("production" !== process.env.NODE_ENV ? warning( + !inst.contextTypes, + 'contextTypes was defined as an instance property on %s. Use a ' + + 'static property to define contextTypes instead.', + this.getName() || 'a component' + ) : null); + ("production" !== process.env.NODE_ENV ? warning( + typeof inst.componentShouldUpdate !== 'function', + '%s has a method called ' + + 'componentShouldUpdate(). Did you mean shouldComponentUpdate()? ' + + 'The name is phrased as a question because the function is ' + + 'expected to return a value.', + (this.getName() || 'A component') + ) : null); + } + + var initialState = inst.state; + if (initialState === undefined) { + inst.state = initialState = null; + } + ("production" !== process.env.NODE_ENV ? invariant( + typeof initialState === 'object' && !Array.isArray(initialState), + '%s.state: must be set to an object or null', + this.getName() || 'ReactCompositeComponent' + ) : invariant(typeof initialState === 'object' && !Array.isArray(initialState))); + + this._pendingStateQueue = null; + this._pendingReplaceState = false; + this._pendingForceUpdate = false; + + var childContext; + var renderedElement; + + var previouslyMounting = ReactLifeCycle.currentlyMountingInstance; + ReactLifeCycle.currentlyMountingInstance = this; + try { + if (inst.componentWillMount) { + inst.componentWillMount(); + // When mounting, calls to `setState` by `componentWillMount` will set + // `this._pendingStateQueue` without triggering a re-render. + if (this._pendingStateQueue) { + inst.state = this._processPendingState(inst.props, inst.context); + } + } + + childContext = this._getValidatedChildContext(context); + renderedElement = this._renderValidatedComponent(childContext); + } finally { + ReactLifeCycle.currentlyMountingInstance = previouslyMounting; + } + + this._renderedComponent = this._instantiateReactComponent( + renderedElement, + this._currentElement.type // The wrapping type + ); + + var markup = ReactReconciler.mountComponent( + this._renderedComponent, + rootID, + transaction, + this._mergeChildContext(context, childContext) + ); + if (inst.componentDidMount) { + transaction.getReactMountReady().enqueue(inst.componentDidMount, inst); + } + + return markup; + }, + + /** + * Releases any resources allocated by `mountComponent`. + * + * @final + * @internal + */ + unmountComponent: function() { + var inst = this._instance; + + if (inst.componentWillUnmount) { + var previouslyUnmounting = ReactLifeCycle.currentlyUnmountingInstance; + ReactLifeCycle.currentlyUnmountingInstance = this; + try { + inst.componentWillUnmount(); + } finally { + ReactLifeCycle.currentlyUnmountingInstance = previouslyUnmounting; + } + } + + ReactReconciler.unmountComponent(this._renderedComponent); + this._renderedComponent = null; + + // Reset pending fields + this._pendingStateQueue = null; + this._pendingReplaceState = false; + this._pendingForceUpdate = false; + this._pendingCallbacks = null; + this._pendingElement = null; + + // These fields do not really need to be reset since this object is no + // longer accessible. + this._context = null; + this._rootNodeID = null; + + // Delete the reference from the instance to this internal representation + // which allow the internals to be properly cleaned up even if the user + // leaks a reference to the public instance. + ReactInstanceMap.remove(inst); + + // Some existing components rely on inst.props even after they've been + // destroyed (in event handlers). + // TODO: inst.props = null; + // TODO: inst.state = null; + // TODO: inst.context = null; + }, + + /** + * Schedule a partial update to the props. Only used for internal testing. + * + * @param {object} partialProps Subset of the next props. + * @param {?function} callback Called after props are updated. + * @final + * @internal + */ + _setPropsInternal: function(partialProps, callback) { + // This is a deoptimized path. We optimize for always having an element. + // This creates an extra internal element. + var element = this._pendingElement || this._currentElement; + this._pendingElement = ReactElement.cloneAndReplaceProps( + element, + assign({}, element.props, partialProps) + ); + ReactUpdates.enqueueUpdate(this, callback); + }, + + /** + * Filters the context object to only contain keys specified in + * `contextTypes` + * + * @param {object} context + * @return {?object} + * @private + */ + _maskContext: function(context) { + var maskedContext = null; + // This really should be getting the component class for the element, + // but we know that we're not going to need it for built-ins. + if (typeof this._currentElement.type === 'string') { + return emptyObject; + } + var contextTypes = this._currentElement.type.contextTypes; + if (!contextTypes) { + return emptyObject; + } + maskedContext = {}; + for (var contextName in contextTypes) { + maskedContext[contextName] = context[contextName]; + } + return maskedContext; + }, + + /** + * Filters the context object to only contain keys specified in + * `contextTypes`, and asserts that they are valid. + * + * @param {object} context + * @return {?object} + * @private + */ + _processContext: function(context) { + var maskedContext = this._maskContext(context); + if ("production" !== process.env.NODE_ENV) { + var Component = ReactNativeComponent.getComponentClassForElement( + this._currentElement + ); + if (Component.contextTypes) { + this._checkPropTypes( + Component.contextTypes, + maskedContext, + ReactPropTypeLocations.context + ); + } + } + return maskedContext; + }, + + /** + * @param {object} currentContext + * @return {object} + * @private + */ + _getValidatedChildContext: function(currentContext) { + var inst = this._instance; + var childContext = inst.getChildContext && inst.getChildContext(); + if (childContext) { + ("production" !== process.env.NODE_ENV ? invariant( + typeof inst.constructor.childContextTypes === 'object', + '%s.getChildContext(): childContextTypes must be defined in order to ' + + 'use getChildContext().', + this.getName() || 'ReactCompositeComponent' + ) : invariant(typeof inst.constructor.childContextTypes === 'object')); + if ("production" !== process.env.NODE_ENV) { + this._checkPropTypes( + inst.constructor.childContextTypes, + childContext, + ReactPropTypeLocations.childContext + ); + } + for (var name in childContext) { + ("production" !== process.env.NODE_ENV ? invariant( + name in inst.constructor.childContextTypes, + '%s.getChildContext(): key "%s" is not defined in childContextTypes.', + this.getName() || 'ReactCompositeComponent', + name + ) : invariant(name in inst.constructor.childContextTypes)); + } + return childContext; + } + return null; + }, + + _mergeChildContext: function(currentContext, childContext) { + if (childContext) { + return assign({}, currentContext, childContext); + } + return currentContext; + }, + + /** + * Processes props by setting default values for unspecified props and + * asserting that the props are valid. Does not mutate its argument; returns + * a new props object with defaults merged in. + * + * @param {object} newProps + * @return {object} + * @private + */ + _processProps: function(newProps) { + if ("production" !== process.env.NODE_ENV) { + var Component = ReactNativeComponent.getComponentClassForElement( + this._currentElement + ); + if (Component.propTypes) { + this._checkPropTypes( + Component.propTypes, + newProps, + ReactPropTypeLocations.prop + ); + } + } + return newProps; + }, + + /** + * Assert that the props are valid + * + * @param {object} propTypes Map of prop name to a ReactPropType + * @param {object} props + * @param {string} location e.g. "prop", "context", "child context" + * @private + */ + _checkPropTypes: function(propTypes, props, location) { + // TODO: Stop validating prop types here and only use the element + // validation. + var componentName = this.getName(); + for (var propName in propTypes) { + if (propTypes.hasOwnProperty(propName)) { + var error; + try { + // This is intentionally an invariant that gets caught. It's the same + // behavior as without this statement except with a better message. + ("production" !== process.env.NODE_ENV ? invariant( + typeof propTypes[propName] === 'function', + '%s: %s type `%s` is invalid; it must be a function, usually ' + + 'from React.PropTypes.', + componentName || 'React class', + ReactPropTypeLocationNames[location], + propName + ) : invariant(typeof propTypes[propName] === 'function')); + error = propTypes[propName](props, propName, componentName, location); + } catch (ex) { + error = ex; + } + if (error instanceof Error) { + // We may want to extend this logic for similar errors in + // React.render calls, so I'm abstracting it away into + // a function to minimize refactoring in the future + var addendum = getDeclarationErrorAddendum(this); + + if (location === ReactPropTypeLocations.prop) { + // Preface gives us something to blacklist in warning module + ("production" !== process.env.NODE_ENV ? warning( + false, + 'Failed Composite propType: %s%s', + error.message, + addendum + ) : null); + } else { + ("production" !== process.env.NODE_ENV ? warning( + false, + 'Failed Context Types: %s%s', + error.message, + addendum + ) : null); + } + } + } + } + }, + + receiveComponent: function(nextElement, transaction, nextContext) { + var prevElement = this._currentElement; + var prevContext = this._context; + + this._pendingElement = null; + + this.updateComponent( + transaction, + prevElement, + nextElement, + prevContext, + nextContext + ); + }, + + /** + * If any of `_pendingElement`, `_pendingStateQueue`, or `_pendingForceUpdate` + * is set, update the component. + * + * @param {ReactReconcileTransaction} transaction + * @internal + */ + performUpdateIfNecessary: function(transaction) { + if (this._pendingElement != null) { + ReactReconciler.receiveComponent( + this, + this._pendingElement || this._currentElement, + transaction, + this._context + ); + } + + if (this._pendingStateQueue !== null || this._pendingForceUpdate) { + if ("production" !== process.env.NODE_ENV) { + ReactElementValidator.checkAndWarnForMutatedProps( + this._currentElement + ); + } + + this.updateComponent( + transaction, + this._currentElement, + this._currentElement, + this._context, + this._context + ); + } + }, + + /** + * Compare two contexts, warning if they are different + * TODO: Remove this check when owner-context is removed + */ + _warnIfContextsDiffer: function(ownerBasedContext, parentBasedContext) { + ownerBasedContext = this._maskContext(ownerBasedContext); + parentBasedContext = this._maskContext(parentBasedContext); + var parentKeys = Object.keys(parentBasedContext).sort(); + var displayName = this.getName() || 'ReactCompositeComponent'; + for (var i = 0; i < parentKeys.length; i++) { + var key = parentKeys[i]; + ("production" !== process.env.NODE_ENV ? warning( + ownerBasedContext[key] === parentBasedContext[key], + 'owner-based and parent-based contexts differ ' + + '(values: `%s` vs `%s`) for key (%s) while mounting %s ' + + '(see: http://fb.me/react-context-by-parent)', + ownerBasedContext[key], + parentBasedContext[key], + key, + displayName + ) : null); + } + }, + + /** + * Perform an update to a mounted component. The componentWillReceiveProps and + * shouldComponentUpdate methods are called, then (assuming the update isn't + * skipped) the remaining update lifecycle methods are called and the DOM + * representation is updated. + * + * By default, this implements React's rendering and reconciliation algorithm. + * Sophisticated clients may wish to override this. + * + * @param {ReactReconcileTransaction} transaction + * @param {ReactElement} prevParentElement + * @param {ReactElement} nextParentElement + * @internal + * @overridable + */ + updateComponent: function( + transaction, + prevParentElement, + nextParentElement, + prevUnmaskedContext, + nextUnmaskedContext + ) { + var inst = this._instance; + + var nextContext = inst.context; + var nextProps = inst.props; + + // Distinguish between a props update versus a simple state update + if (prevParentElement !== nextParentElement) { + nextContext = this._processContext(nextParentElement._context); + nextProps = this._processProps(nextParentElement.props); + + if ("production" !== process.env.NODE_ENV) { + if (nextUnmaskedContext != null) { + this._warnIfContextsDiffer( + nextParentElement._context, + nextUnmaskedContext + ); + } + } + + // An update here will schedule an update but immediately set + // _pendingStateQueue which will ensure that any state updates gets + // immediately reconciled instead of waiting for the next batch. + + if (inst.componentWillReceiveProps) { + inst.componentWillReceiveProps(nextProps, nextContext); + } + } + + var nextState = this._processPendingState(nextProps, nextContext); + + var shouldUpdate = + this._pendingForceUpdate || + !inst.shouldComponentUpdate || + inst.shouldComponentUpdate(nextProps, nextState, nextContext); + + if ("production" !== process.env.NODE_ENV) { + ("production" !== process.env.NODE_ENV ? warning( + typeof shouldUpdate !== 'undefined', + '%s.shouldComponentUpdate(): Returned undefined instead of a ' + + 'boolean value. Make sure to return true or false.', + this.getName() || 'ReactCompositeComponent' + ) : null); + } + + if (shouldUpdate) { + this._pendingForceUpdate = false; + // Will set `this.props`, `this.state` and `this.context`. + this._performComponentUpdate( + nextParentElement, + nextProps, + nextState, + nextContext, + transaction, + nextUnmaskedContext + ); + } else { + // If it's determined that a component should not update, we still want + // to set props and state but we shortcut the rest of the update. + this._currentElement = nextParentElement; + this._context = nextUnmaskedContext; + inst.props = nextProps; + inst.state = nextState; + inst.context = nextContext; + } + }, + + _processPendingState: function(props, context) { + var inst = this._instance; + var queue = this._pendingStateQueue; + var replace = this._pendingReplaceState; + this._pendingReplaceState = false; + this._pendingStateQueue = null; + + if (!queue) { + return inst.state; + } + + if (replace && queue.length === 1) { + return queue[0]; + } + + var nextState = assign({}, replace ? queue[0] : inst.state); + for (var i = replace ? 1 : 0; i < queue.length; i++) { + var partial = queue[i]; + assign( + nextState, + typeof partial === 'function' ? + partial.call(inst, nextState, props, context) : + partial + ); + } + + return nextState; + }, + + /** + * Merges new props and state, notifies delegate methods of update and + * performs update. + * + * @param {ReactElement} nextElement Next element + * @param {object} nextProps Next public object to set as properties. + * @param {?object} nextState Next object to set as state. + * @param {?object} nextContext Next public object to set as context. + * @param {ReactReconcileTransaction} transaction + * @param {?object} unmaskedContext + * @private + */ + _performComponentUpdate: function( + nextElement, + nextProps, + nextState, + nextContext, + transaction, + unmaskedContext + ) { + var inst = this._instance; + + var prevProps = inst.props; + var prevState = inst.state; + var prevContext = inst.context; + + if (inst.componentWillUpdate) { + inst.componentWillUpdate(nextProps, nextState, nextContext); + } + + this._currentElement = nextElement; + this._context = unmaskedContext; + inst.props = nextProps; + inst.state = nextState; + inst.context = nextContext; + + this._updateRenderedComponent(transaction, unmaskedContext); + + if (inst.componentDidUpdate) { + transaction.getReactMountReady().enqueue( + inst.componentDidUpdate.bind(inst, prevProps, prevState, prevContext), + inst + ); + } + }, + + /** + * Call the component's `render` method and update the DOM accordingly. + * + * @param {ReactReconcileTransaction} transaction + * @internal + */ + _updateRenderedComponent: function(transaction, context) { + var prevComponentInstance = this._renderedComponent; + var prevRenderedElement = prevComponentInstance._currentElement; + var childContext = this._getValidatedChildContext(); + var nextRenderedElement = this._renderValidatedComponent(childContext); + if (shouldUpdateReactComponent(prevRenderedElement, nextRenderedElement)) { + ReactReconciler.receiveComponent( + prevComponentInstance, + nextRenderedElement, + transaction, + this._mergeChildContext(context, childContext) + ); + } else { + // These two IDs are actually the same! But nothing should rely on that. + var thisID = this._rootNodeID; + var prevComponentID = prevComponentInstance._rootNodeID; + ReactReconciler.unmountComponent(prevComponentInstance); + + this._renderedComponent = this._instantiateReactComponent( + nextRenderedElement, + this._currentElement.type + ); + var nextMarkup = ReactReconciler.mountComponent( + this._renderedComponent, + thisID, + transaction, + this._mergeChildContext(context, childContext) + ); + this._replaceNodeWithMarkupByID(prevComponentID, nextMarkup); + } + }, + + /** + * @protected + */ + _replaceNodeWithMarkupByID: function(prevComponentID, nextMarkup) { + ReactComponentEnvironment.replaceNodeWithMarkupByID( + prevComponentID, + nextMarkup + ); + }, + + /** + * @protected + */ + _renderValidatedComponentWithoutOwnerOrContext: function() { + var inst = this._instance; + var renderedComponent = inst.render(); + if ("production" !== process.env.NODE_ENV) { + // We allow auto-mocks to proceed as if they're returning null. + if (typeof renderedComponent === 'undefined' && + inst.render._isMockFunction) { + // This is probably bad practice. Consider warning here and + // deprecating this convenience. + renderedComponent = null; + } + } + + return renderedComponent; + }, + + /** + * @private + */ + _renderValidatedComponent: function(childContext) { + var renderedComponent; + var previousContext = ReactContext.current; + ReactContext.current = this._mergeChildContext( + this._currentElement._context, + childContext + ); + ReactCurrentOwner.current = this; + try { + renderedComponent = + this._renderValidatedComponentWithoutOwnerOrContext(); + } finally { + ReactContext.current = previousContext; + ReactCurrentOwner.current = null; + } + ("production" !== process.env.NODE_ENV ? invariant( + // TODO: An `isValidNode` function would probably be more appropriate + renderedComponent === null || renderedComponent === false || + ReactElement.isValidElement(renderedComponent), + '%s.render(): A valid ReactComponent must be returned. You may have ' + + 'returned undefined, an array or some other invalid object.', + this.getName() || 'ReactCompositeComponent' + ) : invariant(// TODO: An `isValidNode` function would probably be more appropriate + renderedComponent === null || renderedComponent === false || + ReactElement.isValidElement(renderedComponent))); + return renderedComponent; + }, + + /** + * Lazily allocates the refs object and stores `component` as `ref`. + * + * @param {string} ref Reference name. + * @param {component} component Component to store as `ref`. + * @final + * @private + */ + attachRef: function(ref, component) { + var inst = this.getPublicInstance(); + var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs; + refs[ref] = component.getPublicInstance(); + }, + + /** + * Detaches a reference name. + * + * @param {string} ref Name to dereference. + * @final + * @private + */ + detachRef: function(ref) { + var refs = this.getPublicInstance().refs; + delete refs[ref]; + }, + + /** + * Get a text description of the component that can be used to identify it + * in error messages. + * @return {string} The name or null. + * @internal + */ + getName: function() { + var type = this._currentElement.type; + var constructor = this._instance && this._instance.constructor; + return ( + type.displayName || (constructor && constructor.displayName) || + type.name || (constructor && constructor.name) || + null + ); + }, + + /** + * Get the publicly accessible representation of this component - i.e. what + * is exposed by refs and returned by React.render. Can be null for stateless + * components. + * + * @return {ReactComponent} the public component instance. + * @internal + */ + getPublicInstance: function() { + return this._instance; + }, + + // Stub + _instantiateReactComponent: null + +}; + +ReactPerf.measureMethods( + ReactCompositeComponentMixin, + 'ReactCompositeComponent', + { + mountComponent: 'mountComponent', + updateComponent: 'updateComponent', + _renderValidatedComponent: '_renderValidatedComponent' + } +); + +var ReactCompositeComponent = { + + Mixin: ReactCompositeComponentMixin + +}; + +module.exports = ReactCompositeComponent; + +}).call(this,require('_process')) +},{"./Object.assign":28,"./ReactComponentEnvironment":38,"./ReactContext":40,"./ReactCurrentOwner":41,"./ReactElement":59,"./ReactElementValidator":60,"./ReactInstanceMap":69,"./ReactLifeCycle":70,"./ReactNativeComponent":75,"./ReactPerf":77,"./ReactPropTypeLocationNames":78,"./ReactPropTypeLocations":79,"./ReactReconciler":83,"./ReactUpdates":89,"./emptyObject":117,"./invariant":137,"./shouldUpdateReactComponent":153,"./warning":156,"_process":2}],40:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactContext + */ + +'use strict'; + +var assign = require("./Object.assign"); +var emptyObject = require("./emptyObject"); +var warning = require("./warning"); + +var didWarn = false; + +/** + * Keeps track of the current context. + * + * The context is automatically passed down the component ownership hierarchy + * and is accessible via `this.context` on ReactCompositeComponents. + */ +var ReactContext = { + + /** + * @internal + * @type {object} + */ + current: emptyObject, + + /** + * Temporarily extends the current context while executing scopedCallback. + * + * A typical use case might look like + * + * render: function() { + * var children = ReactContext.withContext({foo: 'foo'}, () => ( + * + * )); + * return
{children}
; + * } + * + * @param {object} newContext New context to merge into the existing context + * @param {function} scopedCallback Callback to run with the new context + * @return {ReactComponent|array} + */ + withContext: function(newContext, scopedCallback) { + if ("production" !== process.env.NODE_ENV) { + ("production" !== process.env.NODE_ENV ? warning( + didWarn, + 'withContext is deprecated and will be removed in a future version. ' + + 'Use a wrapper component with getChildContext instead.' + ) : null); + + didWarn = true; + } + + var result; + var previousContext = ReactContext.current; + ReactContext.current = assign({}, previousContext, newContext); + try { + result = scopedCallback(); + } finally { + ReactContext.current = previousContext; + } + return result; + } + +}; + +module.exports = ReactContext; + +}).call(this,require('_process')) +},{"./Object.assign":28,"./emptyObject":117,"./warning":156,"_process":2}],41:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactCurrentOwner + */ + +'use strict'; + +/** + * Keeps track of the current owner. + * + * The current owner is the component who should own any components that are + * currently being constructed. + * + * The depth indicate how many composite components are above this render level. + */ +var ReactCurrentOwner = { + + /** + * @internal + * @type {ReactComponent} + */ + current: null + +}; + +module.exports = ReactCurrentOwner; + +},{}],42:[function(require,module,exports){ +(function (process){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactDOM + * @typechecks static-only + */ + +'use strict'; + +var ReactElement = require("./ReactElement"); +var ReactElementValidator = require("./ReactElementValidator"); + +var mapObject = require("./mapObject"); + +/** + * Create a factory that creates HTML tag elements. + * + * @param {string} tag Tag name (e.g. `div`). + * @private + */ +function createDOMFactory(tag) { + if ("production" !== process.env.NODE_ENV) { + return ReactElementValidator.createFactory(tag); + } + return ReactElement.createFactory(tag); +} + +/** + * Creates a mapping from supported HTML tags to `ReactDOMComponent` classes. + * This is also accessible via `React.DOM`. + * + * @public + */ +var ReactDOM = mapObject({ + a: 'a', + abbr: 'abbr', + address: 'address', + area: 'area', + article: 'article', + aside: 'aside', + audio: 'audio', + b: 'b', + base: 'base', + bdi: 'bdi', + bdo: 'bdo', + big: 'big', + blockquote: 'blockquote', + body: 'body', + br: 'br', + button: 'button', + canvas: 'canvas', + caption: 'caption', + cite: 'cite', + code: 'code', + col: 'col', + colgroup: 'colgroup', + data: 'data', + datalist: 'datalist', + dd: 'dd', + del: 'del', + details: 'details', + dfn: 'dfn', + dialog: 'dialog', + div: 'div', + dl: 'dl', + dt: 'dt', + em: 'em', + embed: 'embed', + fieldset: 'fieldset', + figcaption: 'figcaption', + figure: 'figure', + footer: 'footer', + form: 'form', + h1: 'h1', + h2: 'h2', + h3: 'h3', + h4: 'h4', + h5: 'h5', + h6: 'h6', + head: 'head', + header: 'header', + hr: 'hr', + html: 'html', + i: 'i', + iframe: 'iframe', + img: 'img', + input: 'input', + ins: 'ins', + kbd: 'kbd', + keygen: 'keygen', + label: 'label', + legend: 'legend', + li: 'li', + link: 'link', + main: 'main', + map: 'map', + mark: 'mark', + menu: 'menu', + menuitem: 'menuitem', + meta: 'meta', + meter: 'meter', + nav: 'nav', + noscript: 'noscript', + object: 'object', + ol: 'ol', + optgroup: 'optgroup', + option: 'option', + output: 'output', + p: 'p', + param: 'param', + picture: 'picture', + pre: 'pre', + progress: 'progress', + q: 'q', + rp: 'rp', + rt: 'rt', + ruby: 'ruby', + s: 's', + samp: 'samp', + script: 'script', + section: 'section', + select: 'select', + small: 'small', + source: 'source', + span: 'span', + strong: 'strong', + style: 'style', + sub: 'sub', + summary: 'summary', + sup: 'sup', + table: 'table', + tbody: 'tbody', + td: 'td', + textarea: 'textarea', + tfoot: 'tfoot', + th: 'th', + thead: 'thead', + time: 'time', + title: 'title', + tr: 'tr', + track: 'track', + u: 'u', + ul: 'ul', + 'var': 'var', + video: 'video', + wbr: 'wbr', + + // SVG + circle: 'circle', + clipPath: 'clipPath', + defs: 'defs', + ellipse: 'ellipse', + g: 'g', + line: 'line', + linearGradient: 'linearGradient', + mask: 'mask', + path: 'path', + pattern: 'pattern', + polygon: 'polygon', + polyline: 'polyline', + radialGradient: 'radialGradient', + rect: 'rect', + stop: 'stop', + svg: 'svg', + text: 'text', + tspan: 'tspan' + +}, createDOMFactory); + +module.exports = ReactDOM; + +}).call(this,require('_process')) +},{"./ReactElement":59,"./ReactElementValidator":60,"./mapObject":144,"_process":2}],43:[function(require,module,exports){ +/** + * Copyright 2013-2015, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @providesModule ReactDOMButton + */ + +'use strict'; + +var AutoFocusMixin = require("./AutoFocusMixin"); +var ReactBrowserComponentMixin = require("./ReactBrowserComponentMixin"); +var ReactClass = require("./ReactClass"); +var ReactElement = require("./ReactElement"); + +var keyMirror = require("./keyMirror"); + +var button = ReactElement.createFactory('button'); + +var mouseListenerNames = keyMirror({ + onClick: true, + onDoubleClick: true, + onMouseDown: true, + onMouseMove: true, + onMouseUp: true, + onClickCapture: true, + onDoubleClickCapture: true, + onMouseDownCapture: true, + onMouseMoveCapture: true, + onMouseUpCapture: true +}); + +/** + * Implements a