image_buffer.js revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * A stack of overlays that display itself and handle mouse events. 7 * TODO(kaznacheev) Consider disbanding this class and moving 8 * the functionality to individual objects that display anything or handle 9 * mouse events. 10 * @constructor 11 */ 12function ImageBuffer() { 13 this.overlays_ = []; 14} 15 16//TODO(JSDOC) 17ImageBuffer.prototype.addOverlay = function(overlay) { 18 var zIndex = overlay.getZIndex(); 19 // Store the overlays in the ascending Z-order. 20 var i; 21 for (i = 0; i != this.overlays_.length; i++) { 22 if (zIndex < this.overlays_[i].getZIndex()) break; 23 } 24 this.overlays_.splice(i, 0, overlay); 25}; 26 27//TODO(JSDOC) 28ImageBuffer.prototype.removeOverlay = function(overlay) { 29 for (var i = 0; i != this.overlays_.length; i++) { 30 if (this.overlays_[i] == overlay) { 31 this.overlays_.splice(i, 1); 32 return; 33 } 34 } 35 throw new Error('Cannot remove overlay ' + overlay); 36}; 37 38/** 39 * Draws overlays in the ascending Z-order. 40 */ 41ImageBuffer.prototype.draw = function() { 42 for (var i = 0; i != this.overlays_.length; i++) { 43 this.overlays_[i].draw(); 44 } 45}; 46 47/** 48 * Searches for a cursor style in the descending Z-order. 49 * @param {number} x X coordinate for cursor. 50 * @param {number} y Y coordinate for cursor. 51 * @param {boolean} mouseDown If mouse button is down. 52 * @return {String} A value for style.cursor CSS property. 53 */ 54ImageBuffer.prototype.getCursorStyle = function(x, y, mouseDown) { 55 for (var i = this.overlays_.length - 1; i >= 0; i--) { 56 var style = this.overlays_[i].getCursorStyle(x, y, mouseDown); 57 if (style) return style; 58 } 59 return 'default'; 60}; 61 62/** 63 * Searches for a click handler in the descending Z-order. 64 * @param {number} x X coordinate for click event. 65 * @param {number} y Y coordinate for click event. 66 * @return {Boolean} True if handled. 67 */ 68ImageBuffer.prototype.onClick = function(x, y) { 69 for (var i = this.overlays_.length - 1; i >= 0; i--) { 70 if (this.overlays_[i].onClick(x, y)) return true; 71 } 72 return false; 73}; 74 75/** 76 * Searches for a drag handler in the descending Z-order. 77 * @param {number} x Event X coordinate. 78 * @param {number} y Event Y coordinate. 79 * @param {boolean} touch True if it's a touch event, false if mouse. 80 * @return {function(number,number)} A function to be called on mouse drag. 81 */ 82ImageBuffer.prototype.getDragHandler = function(x, y, touch) { 83 for (var i = this.overlays_.length - 1; i >= 0; i--) { 84 var handler = this.overlays_[i].getDragHandler(x, y, touch); 85 if (handler) 86 return handler; 87 } 88 return null; 89}; 90 91/** 92 * Searches for an action for the double tap enumerating 93 * layers in the descending Z-order. 94 * @param {number} x X coordinate of the event. 95 * @param {number} y Y coordinate of the event. 96 * @return {ImageBuffer.DoubleTapAction} Action to perform as result. 97 */ 98ImageBuffer.prototype.getDoubleTapAction = function(x, y) { 99 for (var i = this.overlays_.length - 1; i >= 0; i--) { 100 var action = this.overlays_[i].getDoubleTapAction(x, y); 101 if (action != ImageBuffer.DoubleTapAction.NOTHING) 102 return action; 103 } 104 return ImageBuffer.DoubleTapAction.NOTHING; 105}; 106 107/** 108 * Possible double tap actions. 109 * @enum 110 */ 111ImageBuffer.DoubleTapAction = { 112 NOTHING: 0, 113 COMMIT: 1, 114 CANCEL: 2 115}; 116 117/** 118 * ImageBuffer.Overlay is a pluggable extension that modifies the outlook 119 * and the behavior of the ImageBuffer instance. 120 * @class 121 */ 122//TODO(JSDOC) 123ImageBuffer.Overlay = function() {}; 124 125//TODO(JSDOC) 126ImageBuffer.Overlay.prototype.getZIndex = function() { return 0 }; 127 128//TODO(JSDOC) 129ImageBuffer.Overlay.prototype.draw = function() {}; 130 131//TODO(JSDOC) 132ImageBuffer.Overlay.prototype.getCursorStyle = function() { return null }; 133 134//TODO(JSDOC) 135ImageBuffer.Overlay.prototype.onClick = function() { return false }; 136 137//TODO(JSDOC) 138ImageBuffer.Overlay.prototype.getDragHandler = function() { return null }; 139 140//TODO(JSDOC) 141ImageBuffer.Overlay.prototype.getDoubleTapAction = function(x, y) { 142 return ImageBuffer.DoubleTapAction.NOTHING; 143}; 144