OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 import 'dart:math' as math; | 5 import 'dart:math' as math; |
6 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
8 import 'node.dart'; | 8 import 'node.dart'; |
9 import 'package:vector_math/vector_math.dart'; | 9 import 'package:vector_math/vector_math.dart'; |
| 10 import 'package:sky/framework/net/image_cache.dart' as image_cache; |
10 | 11 |
11 // GENERIC BOX RENDERING | 12 // GENERIC BOX RENDERING |
12 // Anything that has a concept of x, y, width, height is going to derive from th
is | 13 // Anything that has a concept of x, y, width, height is going to derive from th
is |
13 | 14 |
14 class EdgeDims { | 15 class EdgeDims { |
15 // used for e.g. padding | 16 // used for e.g. padding |
16 const EdgeDims(this.top, this.right, this.bottom, this.left); | 17 const EdgeDims(this.top, this.right, this.bottom, this.left); |
17 const EdgeDims.all(double value) | 18 const EdgeDims.all(double value) |
18 : top = value, right = value, bottom = value, left = value; | 19 : top = value, right = value, bottom = value, left = value; |
19 | 20 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
248 sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.posi
tion, child.size); | 249 sky.Rect childBounds = new sky.Rect.fromPointAndSize(child.parentData.posi
tion, child.size); |
249 if (childBounds.contains(position)) { | 250 if (childBounds.contains(position)) { |
250 child.hitTest(result, position: new sky.Point(position.x - child.parentD
ata.position.x, | 251 child.hitTest(result, position: new sky.Point(position.x - child.parentD
ata.position.x, |
251 position.y - child.parentD
ata.position.y)); | 252 position.y - child.parentD
ata.position.y)); |
252 } | 253 } |
253 } | 254 } |
254 } | 255 } |
255 | 256 |
256 } | 257 } |
257 | 258 |
| 259 class RenderImage extends RenderBox { |
| 260 |
| 261 RenderImage(String url, sky.Size dimensions) { |
| 262 requestedSize = dimensions; |
| 263 src = url; |
| 264 } |
| 265 |
| 266 sky.Image _image; |
| 267 String _src; |
| 268 String get src => _src; |
| 269 void set src (String value) { |
| 270 if (value == _src) |
| 271 return; |
| 272 _src = value; |
| 273 image_cache.load(_src, (result) { |
| 274 _image = result; |
| 275 if (requestedSize.width == null || requestedSize.height == null) |
| 276 markNeedsLayout(); |
| 277 markNeedsPaint(); |
| 278 }); |
| 279 } |
| 280 |
| 281 sky.Size _requestedSize; |
| 282 sky.Size get requestedSize => _requestedSize; |
| 283 void set requestedSize (sky.Size value) { |
| 284 if (value == _requestedSize) |
| 285 return; |
| 286 _requestedSize = value; |
| 287 markNeedsLayout(); |
| 288 } |
| 289 |
| 290 void performLayout() { |
| 291 // If there's no image, we can't size ourselves automatically |
| 292 if (_image == null) { |
| 293 double width = requestedSize.width == null ? 0.0 : requestedSize.width; |
| 294 double height = requestedSize.height == null ? 0.0 : requestedSize.height; |
| 295 size = constraints.constrain(new sky.Size(width, height)); |
| 296 return; |
| 297 } |
| 298 |
| 299 // If neither height nor width are specified, use inherent image dimensions |
| 300 // If only one dimension is specified, adjust the other dimension to |
| 301 // maintain the aspect ratio |
| 302 if (requestedSize.width == null) { |
| 303 if (requestedSize.height == null) { |
| 304 size = constraints.constrain(new sky.Size(_image.width, _image.height)); |
| 305 } else { |
| 306 double width = requestedSize.height * _image.width / _image.height; |
| 307 size = constraints.constrain(new sky.Size(width, requestedSize.height)); |
| 308 } |
| 309 } else if (requestedSize.height == null) { |
| 310 double height = requestedSize.width * _image.height / _image.width; |
| 311 size = constraints.constrain(new sky.Size(requestedSize.width, height)); |
| 312 } else { |
| 313 size = constraints.constrain(requestedSize); |
| 314 } |
| 315 } |
| 316 |
| 317 void paint(RenderNodeDisplayList canvas) { |
| 318 if (_image == null) return; |
| 319 bool needsScale = size.width != _image.width || size.height != _image.height
; |
| 320 if (needsScale) { |
| 321 double widthScale = size.width / _image.width; |
| 322 double heightScale = size.height / _image.height; |
| 323 canvas.save(); |
| 324 canvas.scale(widthScale, heightScale); |
| 325 } |
| 326 sky.Paint paint = new sky.Paint(); |
| 327 canvas.drawImage(_image, 0.0, 0.0, paint); |
| 328 if (needsScale) |
| 329 canvas.restore(); |
| 330 } |
| 331 } |
| 332 |
258 // This must be immutable, because we won't notice when it changes | 333 // This must be immutable, because we won't notice when it changes |
259 class BoxDecoration { | 334 class BoxDecoration { |
260 const BoxDecoration({this.backgroundColor}); | 335 const BoxDecoration({this.backgroundColor}); |
261 | 336 |
262 final sky.Color backgroundColor; | 337 final sky.Color backgroundColor; |
263 } | 338 } |
264 | 339 |
265 class RenderDecoratedBox extends RenderProxyBox { | 340 class RenderDecoratedBox extends RenderProxyBox { |
266 | 341 |
267 RenderDecoratedBox({ | 342 RenderDecoratedBox({ |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 | 538 |
464 void defaultPaint(RenderNodeDisplayList canvas) { | 539 void defaultPaint(RenderNodeDisplayList canvas) { |
465 RenderBox child = firstChild; | 540 RenderBox child = firstChild; |
466 while (child != null) { | 541 while (child != null) { |
467 assert(child.parentData is ParentDataType); | 542 assert(child.parentData is ParentDataType); |
468 canvas.paintChild(child, child.parentData.position); | 543 canvas.paintChild(child, child.parentData.position); |
469 child = child.parentData.nextSibling; | 544 child = child.parentData.nextSibling; |
470 } | 545 } |
471 } | 546 } |
472 } | 547 } |
OLD | NEW |