• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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'use strict';
6
7/**
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
11 * mouse events.
12 * @constructor
13 */
14function ImageBuffer() {
15  this.overlays_ = [];
16}
17
18/**
19 * TODO(JSDOC).
20 * @param {ImageBuffer.Overlay} overlay  // TODO(JSDOC).
21 */
22ImageBuffer.prototype.addOverlay = function(overlay) {
23  var zIndex = overlay.getZIndex();
24  // Store the overlays in the ascending Z-order.
25  var i;
26  for (i = 0; i != this.overlays_.length; i++) {
27    if (zIndex < this.overlays_[i].getZIndex()) break;
28  }
29  this.overlays_.splice(i, 0, overlay);
30};
31
32/**
33 * TODO(JSDOC).
34 * @param {ImageBuffer.Overlay} overlay  // TODO(JSDOC).
35 */
36ImageBuffer.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);
40      return;
41    }
42  }
43  throw new Error('Cannot remove overlay ' + overlay);
44};
45
46/**
47 * Draws overlays in the ascending Z-order.
48 */
49ImageBuffer.prototype.draw = function() {
50  for (var i = 0; i != this.overlays_.length; i++) {
51    this.overlays_[i].draw();
52  }
53};
54
55/**
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.
61 */
62ImageBuffer.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;
66  }
67  return 'default';
68};
69
70/**
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.
75 */
76ImageBuffer.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;
79  }
80  return false;
81};
82
83/**
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.
89 */
90ImageBuffer.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);
93    if (handler)
94      return handler;
95  }
96  return null;
97};
98
99/**
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.
105 */
106ImageBuffer.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)
110      return action;
111  }
112  return ImageBuffer.DoubleTapAction.NOTHING;
113};
114
115/**
116 * Possible double tap actions.
117 * @enum
118 */
119ImageBuffer.DoubleTapAction = {
120  NOTHING: 0,
121  COMMIT: 1,
122  CANCEL: 2
123};
124
125/**
126 * ImageBuffer.Overlay is a pluggable extension that modifies the outlook
127 * and the behavior of the ImageBuffer instance.
128 * @class
129 */
130ImageBuffer.Overlay = function() {};
131
132/**
133 * TODO(JSDOC).
134 * @return {number}  // TODO(JSDOC).
135 */
136ImageBuffer.Overlay.prototype.getZIndex = function() { return 0 };
137
138/**
139 * TODO(JSDOC).
140 */
141ImageBuffer.Overlay.prototype.draw = function() {};
142
143/**
144 * TODO(JSDOC).
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
149 *     default.
150 */
151ImageBuffer.Overlay.prototype.getCursorStyle = function(x, y, mouseDown) {
152  return null;
153};
154
155/**
156 * TODO(JSDOC).
157 * @param {number} x  // TODO(JSDOC).
158 * @param {number} y  // TODO(JSDOC).
159 * @return {boolean}  // TODO(JSDOC).
160 */
161ImageBuffer.Overlay.prototype.onClick = function(x, y) {
162  return false;
163};
164
165/**
166 * TODO(JSDOC).
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.
171 */
172ImageBuffer.Overlay.prototype.getDragHandler = function(x, y, touch) {
173  return null;
174};
175
176/**
177 * TODO(JSDOC).
178 * @param {number} x  // TODO(JSDOC).
179 * @param {number} y  // TODO(JSDOC).
180 * @return {ImageBuffer.DoubleTapAction}  // TODO(JSDOC).
181 */
182ImageBuffer.Overlay.prototype.getDoubleTapAction = function(x, y) {
183  return ImageBuffer.DoubleTapAction.NOTHING;
184};
185