1 // Copyright (c) 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.
7 tvcm.require('tvcm.range');
9 tvcm.exportTo('tracing', function() {
14 function SnapIndicator(y, height) {
20 * The interesting part of the world.
24 function TimelineInterestRange(vp) {
27 this.range_ = new tvcm.Range();
29 this.leftSelected_ = false;
30 this.rightSelected_ = false;
32 this.leftSnapIndicator_ = undefined;
33 this.rightSnapIndicator_ = undefined;
36 TimelineInterestRange.prototype = {
38 return this.range_.isEmpty;
43 this.leftSelected_ = false;
44 this.rightSelected_ = false;
45 this.leftSnapIndicator_ = undefined;
46 this.rightSnapIndicator_ = undefined;
47 this.viewport_.dispatchChangeEvent();
51 return this.range_.min;
55 this.range_.min = min;
56 this.viewport_.dispatchChangeEvent();
60 return this.range_.max;
64 this.range_.max = max;
65 this.viewport_.dispatchChangeEvent();
68 set: function(range) {
70 this.range_.addRange(range);
71 this.viewport_.dispatchChangeEvent();
74 setMinAndMax: function(min, max) {
75 this.range_.min = min;
76 this.range_.max = max;
77 this.viewport_.dispatchChangeEvent();
81 return this.range_.range;
84 asRangeObject: function() {
85 var range = new tvcm.Range();
86 range.addRange(this.range_);
91 return this.leftSelected_;
94 set leftSelected(leftSelected) {
95 if (this.leftSelected_ == leftSelected)
97 this.leftSelected_ = leftSelected;
98 this.viewport_.dispatchChangeEvent();
101 get rightSelected() {
102 return this.rightSelected_;
105 set rightSelected(rightSelected) {
106 if (this.rightSelected_ == rightSelected)
108 this.rightSelected_ = rightSelected;
109 this.viewport_.dispatchChangeEvent();
112 get leftSnapIndicator() {
113 return this.leftSnapIndicator_;
116 set leftSnapIndicator(leftSnapIndicator) {
117 this.leftSnapIndicator_ = leftSnapIndicator;
118 this.viewport_.dispatchChangeEvent();
121 get rightSnapIndicator() {
122 return this.rightSnapIndicator_;
125 set rightSnapIndicator(rightSnapIndicator) {
126 this.rightSnapIndicator_ = rightSnapIndicator;
127 this.viewport_.dispatchChangeEvent();
130 draw: function(ctx, viewLWorld, viewRWorld) {
131 if (this.range_.isEmpty)
133 var dt = this.viewport_.currentDisplayTransform;
135 var markerLWorld = this.min;
136 var markerRWorld = this.max;
138 var markerLView = Math.round(dt.xWorldToView(markerLWorld));
139 var markerRView = Math.round(dt.xWorldToView(markerRWorld));
141 ctx.fillStyle = 'rgba(0, 0, 0, 0.2)';
142 if (markerLWorld > viewLWorld) {
143 ctx.fillRect(dt.xWorldToView(viewLWorld), 0,
144 markerLView, ctx.canvas.height);
147 if (markerRWorld < viewRWorld) {
148 ctx.fillRect(markerRView, 0,
149 dt.xWorldToView(viewRWorld), ctx.canvas.height);
152 var pixelRatio = window.devicePixelRatio || 1;
153 ctx.lineWidth = Math.round(pixelRatio);
154 if (this.range_.range > 0) {
155 this.drawLine_(ctx, viewLWorld, viewRWorld,
156 ctx.canvas.height, this.min, this.leftSelected_);
157 this.drawLine_(ctx, viewLWorld, viewRWorld,
158 ctx.canvas.height, this.max, this.rightSelected_);
160 this.drawLine_(ctx, viewLWorld, viewRWorld,
161 ctx.canvas.height, this.min,
162 this.leftSelected_ || this.rightSelected_);
167 drawLine_: function(ctx, viewLWorld, viewRWorld, height, ts, selected) {
168 if (ts < viewLWorld || ts >= viewRWorld)
171 var dt = this.viewport_.currentDisplayTransform;
172 var viewX = Math.round(dt.xWorldToView(ts));
174 // Apply subpixel translate to get crisp lines.
175 // http://www.mobtowers.com/html5-canvas-crisp-lines-every-time/
177 ctx.translate((Math.round(ctx.lineWidth) % 2) / 2, 0);
180 tracing.drawLine(ctx, viewX, 0, viewX, height);
182 ctx.strokeStyle = 'rgb(255, 0, 0)';
184 ctx.strokeStyle = 'rgb(0, 0, 0)';
190 drawIndicators: function(ctx, viewLWorld, viewRWorld) {
191 if (this.leftSnapIndicator_) {
192 this.drawIndicator_(ctx, viewLWorld, viewRWorld,
194 this.leftSnapIndicator_,
197 if (this.rightSnapIndicator_) {
198 this.drawIndicator_(ctx, viewLWorld, viewRWorld,
200 this.rightSnapIndicator_,
201 this.rightSelected_);
205 drawIndicator_: function(ctx, viewLWorld, viewRWorld,
206 xWorld, si, selected) {
207 var dt = this.viewport_.currentDisplayTransform;
209 var viewX = Math.round(dt.xWorldToView(xWorld));
211 // Apply subpixel translate to get crisp lines.
212 // http://www.mobtowers.com/html5-canvas-crisp-lines-every-time/
214 ctx.translate((Math.round(ctx.lineWidth) % 2) / 2, 0);
216 var pixelRatio = window.devicePixelRatio || 1;
217 var viewY = si.y * devicePixelRatio;
218 var viewHeight = si.height * devicePixelRatio;
219 var arrowSize = 4 * pixelRatio;
222 ctx.fillStyle = 'rgb(255, 0, 0)';
224 ctx.fillStyle = 'rgb(0, 0, 0)';
225 tracing.drawTriangle(ctx,
226 viewX - arrowSize * 0.75, viewY,
227 viewX + arrowSize * 0.75, viewY,
228 viewX, viewY + arrowSize);
230 tracing.drawTriangle(ctx,
231 viewX - arrowSize * 0.75, viewY + viewHeight,
232 viewX + arrowSize * 0.75, viewY + viewHeight,
233 viewX, viewY + viewHeight - arrowSize);
241 SnapIndicator: SnapIndicator,
242 TimelineInterestRange: TimelineInterestRange