1 // Copyright 2014 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.
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
12 function ImageBuffer() {
18 * @param {ImageBuffer.Overlay} overlay // TODO(JSDOC).
20 ImageBuffer.prototype.addOverlay = function(overlay) {
21 var zIndex = overlay.getZIndex();
22 // Store the overlays in the ascending Z-order.
24 for (i = 0; i != this.overlays_.length; i++) {
25 if (zIndex < this.overlays_[i].getZIndex()) break;
27 this.overlays_.splice(i, 0, overlay);
32 * @param {ImageBuffer.Overlay} overlay // TODO(JSDOC).
34 ImageBuffer.prototype.removeOverlay = function(overlay) {
35 for (var i = 0; i != this.overlays_.length; i++) {
36 if (this.overlays_[i] == overlay) {
37 this.overlays_.splice(i, 1);
41 throw new Error('Cannot remove overlay ' + overlay);
45 * Draws overlays in the ascending Z-order.
47 ImageBuffer.prototype.draw = function() {
48 for (var i = 0; i != this.overlays_.length; i++) {
49 this.overlays_[i].draw();
54 * Searches for a cursor style in the descending Z-order.
55 * @param {number} x X coordinate for cursor.
56 * @param {number} y Y coordinate for cursor.
57 * @param {boolean} mouseDown If mouse button is down.
58 * @return {string} A value for style.cursor CSS property.
60 ImageBuffer.prototype.getCursorStyle = function(x, y, mouseDown) {
61 for (var i = this.overlays_.length - 1; i >= 0; i--) {
62 var style = this.overlays_[i].getCursorStyle(x, y, mouseDown);
63 if (style) return style;
69 * Searches for a click handler in the descending Z-order.
70 * @param {number} x X coordinate for click event.
71 * @param {number} y Y coordinate for click event.
72 * @return {boolean} True if handled.
74 ImageBuffer.prototype.onClick = function(x, y) {
75 for (var i = this.overlays_.length - 1; i >= 0; i--) {
76 if (this.overlays_[i].onClick(x, y)) return true;
82 * Searches for a drag handler in the descending Z-order.
83 * @param {number} x Event X coordinate.
84 * @param {number} y Event Y coordinate.
85 * @param {boolean} touch True if it's a touch event, false if mouse.
86 * @return {function(number,number)} A function to be called on mouse drag.
88 ImageBuffer.prototype.getDragHandler = function(x, y, touch) {
89 for (var i = this.overlays_.length - 1; i >= 0; i--) {
90 var handler = this.overlays_[i].getDragHandler(x, y, touch);
98 * Searches for an action for the double tap enumerating
99 * layers in the descending Z-order.
100 * @param {number} x X coordinate of the event.
101 * @param {number} y Y coordinate of the event.
102 * @return {ImageBuffer.DoubleTapAction} Action to perform as result.
104 ImageBuffer.prototype.getDoubleTapAction = function(x, y) {
105 for (var i = this.overlays_.length - 1; i >= 0; i--) {
106 var action = this.overlays_[i].getDoubleTapAction(x, y);
107 if (action != ImageBuffer.DoubleTapAction.NOTHING)
110 return ImageBuffer.DoubleTapAction.NOTHING;
114 * Possible double tap actions.
117 ImageBuffer.DoubleTapAction = {
124 * ImageBuffer.Overlay is a pluggable extension that modifies the outlook
125 * and the behavior of the ImageBuffer instance.
128 ImageBuffer.Overlay = function() {};
132 * @return {number} // TODO(JSDOC).
134 ImageBuffer.Overlay.prototype.getZIndex = function() { return 0; };
139 ImageBuffer.Overlay.prototype.draw = function() {};
143 * @param {number} x X coordinate for cursor.
144 * @param {number} y Y coordinate for cursor.
145 * @param {boolean} mouseDown If mouse button is down.
146 * @return {?string} A value for style.cursor CSS property or null for
149 ImageBuffer.Overlay.prototype.getCursorStyle = function(x, y, mouseDown) {
155 * @param {number} x // TODO(JSDOC).
156 * @param {number} y // TODO(JSDOC).
157 * @return {boolean} // TODO(JSDOC).
159 ImageBuffer.Overlay.prototype.onClick = function(x, y) {
165 * @param {number} x Event X coordinate.
166 * @param {number} y Event Y coordinate.
167 * @param {boolean} touch True if it's a touch event, false if mouse.
168 * @return {function(number,number)} A function to be called on mouse drag.
170 ImageBuffer.Overlay.prototype.getDragHandler = function(x, y, touch) {
176 * @param {number} x // TODO(JSDOC).
177 * @param {number} y // TODO(JSDOC).
178 * @return {ImageBuffer.DoubleTapAction} // TODO(JSDOC).
180 ImageBuffer.Overlay.prototype.getDoubleTapAction = function(x, y) {
181 return ImageBuffer.DoubleTapAction.NOTHING;