1 // Copyright 2014 The ChromeOS IME Authors. All Rights Reserved.
2 // limitations under the License.
3 // See the License for the specific language governing permissions and
4 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
5 // distributed under the License is distributed on an "AS-IS" BASIS,
6 // Unless required by applicable law or agreed to in writing, software
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // You may obtain a copy of the License at
11 // you may not use this file except in compliance with the License.
12 // Licensed under the Apache License, Version 2.0 (the "License");
16 * @fileoverview Defines the class i18n.input.hwt.StrokeHandler.
17 * @author fengyuan@google.com (Feng Yuan)
20 goog.provide('i18n.input.hwt.StrokeHandler');
21 goog.provide('i18n.input.hwt.StrokeHandler.Point');
22 goog.provide('i18n.input.hwt.StrokeHandler.StrokeEvent');
25 goog.require('goog.events');
26 goog.require('goog.events.Event');
27 goog.require('goog.events.EventHandler');
28 goog.require('goog.events.EventTarget');
29 goog.require('goog.events.EventType');
30 goog.require('goog.style');
31 goog.require('goog.userAgent');
32 goog.require('i18n.input.hwt.util');
37 * The handler for strokes.
39 * @param {!Element} canvas The handwriting canvas.
40 * @param {!Document} topDocument The top document.
42 * @extends {goog.events.EventTarget}
44 i18n.input.hwt.StrokeHandler = function(canvas, topDocument) {
50 * @type {goog.events.EventHandler}
53 this.eventHandler_ = new goog.events.EventHandler(this);
56 * Whether is drawing the stroke.
68 this.canvas_ = canvas;
70 // Always register mouse events. Some devices like Tablet PCs
71 // actually support both touch and mouse events, and the touch
72 // events don't get translated to mouse events on Tablet PCs.
74 listen(canvas, goog.events.EventType.MOUSEDOWN, this.onStrokeStart_).
75 listen(canvas, goog.events.EventType.MOUSEMOVE, this.onStroke_);
76 // Listen for touch events if they are supported.
77 if ('ontouchstart' in window) {
79 listen(canvas, goog.events.EventType.TOUCHSTART, this.onStrokeStart_).
80 listen(canvas, goog.events.EventType.TOUCHEND, this.onStrokeEnd_).
81 listen(canvas, goog.events.EventType.TOUCHCANCEL, this.onStrokeEnd_).
82 listen(canvas, goog.events.EventType.TOUCHMOVE, this.onStroke_);
85 i18n.input.hwt.util.listenPageEvent(this.eventHandler_, topDocument,
86 goog.events.EventType.MOUSEUP,
87 goog.bind(this.onStrokeEnd_, this));
89 goog.inherits(i18n.input.hwt.StrokeHandler, goog.events.EventTarget);
93 * Callback for the start of one stroke.
95 * @param {goog.events.BrowserEvent} e Event.
98 i18n.input.hwt.StrokeHandler.prototype.onStrokeStart_ = function(e) {
100 this.dispatchEvent(new i18n.input.hwt.StrokeHandler.StrokeEvent(
101 i18n.input.hwt.StrokeHandler.EventType.STROKE_START,
108 * Callback for the end of one stroke.
110 * @param {goog.events.BrowserEvent} e Event.
113 i18n.input.hwt.StrokeHandler.prototype.onStrokeEnd_ = function(e) {
115 this.drawing = false;
116 this.dispatchEvent(new i18n.input.hwt.StrokeHandler.StrokeEvent(
117 i18n.input.hwt.StrokeHandler.EventType.STROKE_END,
125 * Callback for stroke.
127 * @param {goog.events.BrowserEvent} e Event.
130 i18n.input.hwt.StrokeHandler.prototype.onStroke_ = function(e) {
132 this.dispatchEvent(new i18n.input.hwt.StrokeHandler.StrokeEvent(
133 i18n.input.hwt.StrokeHandler.EventType.STROKE,
141 * Given a mouse or touch event, figure out the coordinates where it occurred.
143 * @param {goog.events.BrowserEvent} e Event.
144 * @return {!i18n.input.hwt.StrokeHandler.Point} a point.
147 i18n.input.hwt.StrokeHandler.prototype.getPoint_ = function(e) {
148 var pos = goog.style.getPageOffset(this.canvas_);
149 var nativeEvent = e.getBrowserEvent();
151 if (!goog.userAgent.IE && nativeEvent.pageX && nativeEvent.pageY) {
152 x = nativeEvent.pageX;
153 y = nativeEvent.pageY;
155 var scrollX = (document.dir == 'rtl' ? -1 : 1) * (
156 document.body.scrollLeft ||
157 document.documentElement.scrollLeft || 0);
158 var scrollY = document.body.scrollTop ||
159 document.documentElement.scrollTop || 0;
160 x = nativeEvent.clientX + scrollX;
161 y = nativeEvent.clientY + scrollY;
163 if (nativeEvent.touches != null && nativeEvent.touches.length > 0) {
164 x = nativeEvent.touches[0].clientX;
165 y = nativeEvent.touches[0].clientY;
167 return new i18n.input.hwt.StrokeHandler.Point(x - pos.x, y - pos.y,
173 * Reset the drawing flag, in case the drawing canvas gets cleared while
174 * stroke is being drawn.
176 i18n.input.hwt.StrokeHandler.prototype.reset = function() {
177 this.drawing = false;
182 i18n.input.hwt.StrokeHandler.prototype.disposeInternal = function() {
183 goog.dispose(this.eventHandler_);
184 this.eventHandler_ = null;
190 * One point in the stroke.
192 * @param {number} x The x.
193 * @param {number} y The y.
194 * @param {number} time The time in miliseconds.
197 i18n.input.hwt.StrokeHandler.Point = function(x, y, time) {
199 * The left offset relative to the canvas, rounded to 2 decimal places.
203 this.x = Math.round(x * 100.0) * 0.01;
206 * The top offset relative to the canvas, rounded to 2 decimal places.
210 this.y = Math.round(y * 100.0) * 0.01;
213 * The time, rounded to the nearest millisecond.
217 this.time = Math.round(time);
226 i18n.input.hwt.StrokeHandler.EventType = {
227 STROKE: goog.events.getUniqueId('s'),
228 STROKE_END: goog.events.getUniqueId('se'),
229 STROKE_START: goog.events.getUniqueId('ss')
237 * @param {!i18n.input.hwt.StrokeHandler.EventType} type The event type.
238 * @param {!i18n.input.hwt.StrokeHandler.Point} point The point.
240 * @extends {goog.events.Event}
242 i18n.input.hwt.StrokeHandler.StrokeEvent = function(type, point) {
243 goog.base(this, type);
248 * @type {!i18n.input.hwt.StrokeHandler.Point}
252 goog.inherits(i18n.input.hwt.StrokeHandler.StrokeEvent, goog.events.Event);