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.
8 * A stack of overlays that display itself and handle mouse events.
9 * TODO(kaznacheev) Consider disbanding this class and moving
10 * the functionality to individual objects that display anything or handle
14 function ImageBuffer() {
20 * @param {ImageBuffer.Overlay} overlay // TODO(JSDOC).
22 ImageBuffer.prototype.addOverlay = function(overlay) {
23 var zIndex = overlay.getZIndex();
24 // Store the overlays in the ascending Z-order.
26 for (i = 0; i != this.overlays_.length; i++) {
27 if (zIndex < this.overlays_[i].getZIndex()) break;
29 this.overlays_.splice(i, 0, overlay);
34 * @param {ImageBuffer.Overlay} overlay // TODO(JSDOC).
36 ImageBuffer.prototype.removeOverlay = function(overlay) {
37 for (var i = 0; i != this.overlays_.length; i++) {
38 if (this.overlays_[i] == overlay) {
39 this.overlays_.splice(i, 1);
43 throw new Error('Cannot remove overlay ' + overlay);
47 * Draws overlays in the ascending Z-order.
49 ImageBuffer.prototype.draw = function() {
50 for (var i = 0; i != this.overlays_.length; i++) {
51 this.overlays_[i].draw();
56 * Searches for a cursor style in the descending Z-order.
57 * @param {number} x X coordinate for cursor.
58 * @param {number} y Y coordinate for cursor.
59 * @param {boolean} mouseDown If mouse button is down.
60 * @return {string} A value for style.cursor CSS property.
62 ImageBuffer.prototype.getCursorStyle = function(x, y, mouseDown) {
63 for (var i = this.overlays_.length - 1; i >= 0; i--) {
64 var style = this.overlays_[i].getCursorStyle(x, y, mouseDown);
65 if (style) return style;
71 * Searches for a click handler in the descending Z-order.
72 * @param {number} x X coordinate for click event.
73 * @param {number} y Y coordinate for click event.
74 * @return {boolean} True if handled.
76 ImageBuffer.prototype.onClick = function(x, y) {
77 for (var i = this.overlays_.length - 1; i >= 0; i--) {
78 if (this.overlays_[i].onClick(x, y)) return true;
84 * Searches for a drag handler in the descending Z-order.
85 * @param {number} x Event X coordinate.
86 * @param {number} y Event Y coordinate.
87 * @param {boolean} touch True if it's a touch event, false if mouse.
88 * @return {function(number,number)} A function to be called on mouse drag.
90 ImageBuffer.prototype.getDragHandler = function(x, y, touch) {
91 for (var i = this.overlays_.length - 1; i >= 0; i--) {
92 var handler = this.overlays_[i].getDragHandler(x, y, touch);
100 * Searches for an action for the double tap enumerating
101 * layers in the descending Z-order.
102 * @param {number} x X coordinate of the event.
103 * @param {number} y Y coordinate of the event.
104 * @return {ImageBuffer.DoubleTapAction} Action to perform as result.
106 ImageBuffer.prototype.getDoubleTapAction = function(x, y) {
107 for (var i = this.overlays_.length - 1; i >= 0; i--) {
108 var action = this.overlays_[i].getDoubleTapAction(x, y);
109 if (action != ImageBuffer.DoubleTapAction.NOTHING)
112 return ImageBuffer.DoubleTapAction.NOTHING;
116 * Possible double tap actions.
119 ImageBuffer.DoubleTapAction = {
126 * ImageBuffer.Overlay is a pluggable extension that modifies the outlook
127 * and the behavior of the ImageBuffer instance.
130 ImageBuffer.Overlay = function() {};
134 * @return {number} // TODO(JSDOC).
136 ImageBuffer.Overlay.prototype.getZIndex = function() { return 0; };
141 ImageBuffer.Overlay.prototype.draw = function() {};
145 * @param {number} x X coordinate for cursor.
146 * @param {number} y Y coordinate for cursor.
147 * @param {boolean} mouseDown If mouse button is down.
148 * @return {?string} A value for style.cursor CSS property or null for
151 ImageBuffer.Overlay.prototype.getCursorStyle = function(x, y, mouseDown) {
157 * @param {number} x // TODO(JSDOC).
158 * @param {number} y // TODO(JSDOC).
159 * @return {boolean} // TODO(JSDOC).
161 ImageBuffer.Overlay.prototype.onClick = function(x, y) {
167 * @param {number} x Event X coordinate.
168 * @param {number} y Event Y coordinate.
169 * @param {boolean} touch True if it's a touch event, false if mouse.
170 * @return {function(number,number)} A function to be called on mouse drag.
172 ImageBuffer.Overlay.prototype.getDragHandler = function(x, y, touch) {
178 * @param {number} x // TODO(JSDOC).
179 * @param {number} y // TODO(JSDOC).
180 * @return {ImageBuffer.DoubleTapAction} // TODO(JSDOC).
182 ImageBuffer.Overlay.prototype.getDoubleTapAction = function(x, y) {
183 return ImageBuffer.DoubleTapAction.NOTHING;