OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 /** |
| 32 * @constructor |
| 33 * @extends {WebInspector.View} |
| 34 * @param {WebInspector.LayerTreeModel} model |
| 35 */ |
| 36 WebInspector.Layers3DView = function(model) |
| 37 { |
| 38 WebInspector.View.call(this); |
| 39 this.element.classList.add("fill"); |
| 40 this.element.classList.add("layers-3d-view"); |
| 41 this._model = model; |
| 42 this._model.addEventListener(WebInspector.LayerTreeModel.Events.LayerTreeCha
nged, this._update, this); |
| 43 this._rotatingContainerElement = this.element.createChild("div", "fill rotat
ing-container"); |
| 44 this.element.addEventListener("mousemove", this._onMouseMove.bind(this), fal
se); |
| 45 this.element.addEventListener("mousedown", this._onMouseDown.bind(this), fal
se); |
| 46 this._elementsByLayerId = {}; |
| 47 this._rotateX = 0; |
| 48 this._rotateY = 0; |
| 49 } |
| 50 |
| 51 WebInspector.Layers3DView.prototype = { |
| 52 onResize: function() |
| 53 { |
| 54 this._scale(); |
| 55 }, |
| 56 |
| 57 wasShown: function() |
| 58 { |
| 59 if (this._needsUpdate) |
| 60 this._update(); |
| 61 }, |
| 62 |
| 63 _scale: function() |
| 64 { |
| 65 const padding = 40; |
| 66 var scale = 1; |
| 67 var root = this._model.root(); |
| 68 if (!root) |
| 69 return; |
| 70 var scaleX = this.element.clientWidth / (root.width() + 2 * padding); |
| 71 var scaleY = this.element.clientHeight / (root.height() + 2 * padding); |
| 72 scale = Math.min(scaleX, scaleY); |
| 73 var element = this._elementForLayer(root); |
| 74 element.style.webkitTransform = "scale(" + scale + "," + scale +")"; |
| 75 element.style.webkitTransformOrigin = padding + "px " + padding + "px"; |
| 76 }, |
| 77 |
| 78 _update: function() |
| 79 { |
| 80 if (!this.isShowing()) { |
| 81 this._needsUpdate = true; |
| 82 return; |
| 83 } |
| 84 function updateLayer(layer) |
| 85 { |
| 86 var element = this._elementForLayer(layer); |
| 87 this._updateLayerElement(element); |
| 88 for (var childElement = element.firstElementChild; childElement;) { |
| 89 var nextElement = childElement.nextSibling; |
| 90 if (childElement.__layer && !this._model.layerById(childElement.
__layer.id())) |
| 91 childElement.remove(); |
| 92 childElement = nextElement; |
| 93 } |
| 94 } |
| 95 this._model.forEachLayer(updateLayer.bind(this)); |
| 96 this._needsUpdate = false; |
| 97 this._scale(); |
| 98 }, |
| 99 |
| 100 /** |
| 101 * @param {WebInspector.Layer} layer |
| 102 * @return {Element} |
| 103 */ |
| 104 _elementForLayer: function(layer) |
| 105 { |
| 106 var element = this._elementsByLayerId[layer.id()]; |
| 107 if (element) |
| 108 return element; |
| 109 element = document.createElement("div"); |
| 110 element.className = "layer-container"; |
| 111 element.__layer = layer; |
| 112 ["fill back-wall", "side-wall top", "side-wall right", "side-wall bottom
", "side-wall left"].forEach(element.createChild.bind(element, "div")); |
| 113 this._elementsByLayerId[layer.id()] = element; |
| 114 return element; |
| 115 }, |
| 116 |
| 117 /** |
| 118 * @param {Element} element |
| 119 */ |
| 120 _updateLayerElement: function(element) |
| 121 { |
| 122 var layer = element.__layer; |
| 123 var style = element.style; |
| 124 var parentElement = layer.parent() ? this._elementForLayer(layer.parent(
)) : this._rotatingContainerElement; |
| 125 element.__depth = (parentElement.__depth || 0) + 1; |
| 126 style.backgroundColor = this._colorForLayer(layer, element.__depth); |
| 127 style.left = layer.offsetX() + "px"; |
| 128 style.top = layer.offsetY() + "px"; |
| 129 style.width = layer.width() + "px"; |
| 130 style.height = layer.height() + "px"; |
| 131 if (parentElement !== element.parentElement) |
| 132 parentElement.appendChild(element); |
| 133 }, |
| 134 |
| 135 /** |
| 136 * @param {WebInspector.Layer} layer |
| 137 * @param {number} depth |
| 138 * @return {string} |
| 139 */ |
| 140 _colorForLayer: function(layer, depth) |
| 141 { |
| 142 const base = 144; |
| 143 var component = base + 20 * ((depth - 1) % 5); |
| 144 return "rgb(" + component + "," + component + "," + component + ")"; |
| 145 }, |
| 146 |
| 147 /** |
| 148 * @param {Event} event |
| 149 */ |
| 150 _onMouseDown: function(event) |
| 151 { |
| 152 if (event.which !== 1) |
| 153 return; |
| 154 this._originX = event.clientX; |
| 155 this._originY = event.clientY; |
| 156 this._oldRotateX = this._rotateX; |
| 157 this._oldRotateY = this._rotateY; |
| 158 }, |
| 159 |
| 160 /** |
| 161 * @param {Event} event |
| 162 */ |
| 163 _onMouseMove: function(event) |
| 164 { |
| 165 if (event.which !== 1) |
| 166 return; |
| 167 this._rotateX = this._oldRotateX + (this._originY - event.clientY) / 2; |
| 168 this._rotateY = this._oldRotateY - (this._originX - event.clientX) / 4; |
| 169 this._rotatingContainerElement.style.webkitTransform = "rotateX(" + this
._rotateX + "deg) rotateY(" + this._rotateY + "deg)"; |
| 170 }, |
| 171 |
| 172 __proto__: WebInspector.View.prototype |
| 173 } |
OLD | NEW |