Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(742)

Side by Side Diff: sky/examples/raw/sector_layout.dart

Issue 1164303002: Introduce sky.Sky.zero (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: less sky.Foo Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sky/engine/core/painting/Size.dart ('k') | sky/sdk/lib/framework/rendering/box.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 'package:sky/framework/app.dart'; 7 import 'package:sky/framework/app.dart';
8 import 'package:sky/framework/rendering/box.dart'; 8 import 'package:sky/framework/rendering/box.dart';
9 import 'package:sky/framework/rendering/object.dart'; 9 import 'package:sky/framework/rendering/object.dart';
10 10
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 // origin must be set to the center of the circle 117 // origin must be set to the center of the circle
118 void paint(RenderObjectDisplayList canvas) { 118 void paint(RenderObjectDisplayList canvas) {
119 assert(deltaRadius != null); 119 assert(deltaRadius != null);
120 assert(deltaTheta != null); 120 assert(deltaTheta != null);
121 assert(parentData is SectorParentData); 121 assert(parentData is SectorParentData);
122 122
123 if (_decoration == null) 123 if (_decoration == null)
124 return; 124 return;
125 125
126 if (_decoration.backgroundColor != null) { 126 if (_decoration.backgroundColor != null) {
127 sky.Paint paint = new sky.Paint()..color = _decoration.backgroundColor; 127 Paint paint = new Paint()..color = _decoration.backgroundColor;
128 sky.Path path = new sky.Path(); 128 Path path = new Path();
129 double outerRadius = (parentData.radius + deltaRadius); 129 double outerRadius = (parentData.radius + deltaRadius);
130 sky.Rect outerBounds = new sky.Rect.fromLTRB(-outerRadius, -outerRadius, o uterRadius, outerRadius); 130 Rect outerBounds = new Rect.fromLTRB(-outerRadius, -outerRadius, outerRadi us, outerRadius);
131 path.arcTo(outerBounds, deg(parentData.theta), deg(deltaTheta), true); 131 path.arcTo(outerBounds, deg(parentData.theta), deg(deltaTheta), true);
132 double innerRadius = parentData.radius; 132 double innerRadius = parentData.radius;
133 sky.Rect innerBounds = new sky.Rect.fromLTRB(-innerRadius, -innerRadius, i nnerRadius, innerRadius); 133 Rect innerBounds = new Rect.fromLTRB(-innerRadius, -innerRadius, innerRadi us, innerRadius);
134 path.arcTo(innerBounds, deg(parentData.theta + deltaTheta), deg(-deltaThet a), false); 134 path.arcTo(innerBounds, deg(parentData.theta + deltaTheta), deg(-deltaThet a), false);
135 path.close(); 135 path.close();
136 canvas.drawPath(path, paint); 136 canvas.drawPath(path, paint);
137 } 137 }
138 } 138 }
139 } 139 }
140 140
141 class SectorChildListParentData extends SectorParentData with ContainerParentDat aMixin<RenderSector> { } 141 class SectorChildListParentData extends SectorParentData with ContainerParentDat aMixin<RenderSector> { }
142 142
143 class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRende rObjectMixin<RenderSector, SectorChildListParentData> { 143 class RenderSectorWithChildren extends RenderDecoratedSector with ContainerRende rObjectMixin<RenderSector, SectorChildListParentData> {
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 } 250 }
251 251
252 // paint origin is 0,0 of our circle 252 // paint origin is 0,0 of our circle
253 // each sector then knows how to paint itself at its location 253 // each sector then knows how to paint itself at its location
254 void paint(RenderObjectDisplayList canvas) { 254 void paint(RenderObjectDisplayList canvas) {
255 // TODO(ianh): avoid code duplication 255 // TODO(ianh): avoid code duplication
256 super.paint(canvas); 256 super.paint(canvas);
257 RenderSector child = firstChild; 257 RenderSector child = firstChild;
258 while (child != null) { 258 while (child != null) {
259 assert(child.parentData is SectorChildListParentData); 259 assert(child.parentData is SectorChildListParentData);
260 canvas.paintChild(child, new sky.Point(0.0, 0.0)); 260 canvas.paintChild(child, new Point(0.0, 0.0));
261 child = child.parentData.nextSibling; 261 child = child.parentData.nextSibling;
262 } 262 }
263 } 263 }
264 264
265 } 265 }
266 266
267 class RenderSectorSlice extends RenderSectorWithChildren { 267 class RenderSectorSlice extends RenderSectorWithChildren {
268 // lays out RenderSector children in a stack 268 // lays out RenderSector children in a stack
269 269
270 RenderSectorSlice({ 270 RenderSectorSlice({
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 } 355 }
356 356
357 // paint origin is 0,0 of our circle 357 // paint origin is 0,0 of our circle
358 // each sector then knows how to paint itself at its location 358 // each sector then knows how to paint itself at its location
359 void paint(RenderObjectDisplayList canvas) { 359 void paint(RenderObjectDisplayList canvas) {
360 // TODO(ianh): avoid code duplication 360 // TODO(ianh): avoid code duplication
361 super.paint(canvas); 361 super.paint(canvas);
362 RenderSector child = firstChild; 362 RenderSector child = firstChild;
363 while (child != null) { 363 while (child != null) {
364 assert(child.parentData is SectorChildListParentData); 364 assert(child.parentData is SectorChildListParentData);
365 canvas.paintChild(child, new sky.Point(0.0, 0.0)); 365 canvas.paintChild(child, new Point(0.0, 0.0));
366 child = child.parentData.nextSibling; 366 child = child.parentData.nextSibling;
367 } 367 }
368 } 368 }
369 369
370 } 370 }
371 371
372 class RenderBoxToRenderSectorAdapter extends RenderBox { 372 class RenderBoxToRenderSectorAdapter extends RenderBox {
373 373
374 RenderBoxToRenderSectorAdapter({ double innerRadius: 0.0, RenderSector child } ) : 374 RenderBoxToRenderSectorAdapter({ double innerRadius: 0.0, RenderSector child } ) :
375 _innerRadius = innerRadius { 375 _innerRadius = innerRadius {
(...skipping 16 matching lines...) Expand all
392 _child = value; 392 _child = value;
393 adoptChild(_child); 393 adoptChild(_child);
394 markNeedsLayout(); 394 markNeedsLayout();
395 } 395 }
396 396
397 void setParentData(RenderObject child) { 397 void setParentData(RenderObject child) {
398 if (child.parentData is! SectorParentData) 398 if (child.parentData is! SectorParentData)
399 child.parentData = new SectorParentData(); 399 child.parentData = new SectorParentData();
400 } 400 }
401 401
402 sky.Size getIntrinsicDimensions(BoxConstraints constraints) { 402 Size getIntrinsicDimensions(BoxConstraints constraints) {
403 if (child == null) 403 if (child == null)
404 return constraints.constrain(new sky.Size(0.0, 0.0)); 404 return constraints.constrain(Size.zero);
405 assert(child is RenderSector); 405 assert(child is RenderSector);
406 assert(child.parentData is SectorParentData); 406 assert(child.parentData is SectorParentData);
407 assert(!constraints.isInfinite); 407 assert(!constraints.isInfinite);
408 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxH eight) / 2.0 - innerRadius; 408 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.maxH eight) / 2.0 - innerRadius;
409 SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorCo nstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius); 409 SectorDimensions childDimensions = child.getIntrinsicDimensions(new SectorCo nstraints(maxDeltaRadius: maxChildDeltaRadius), innerRadius);
410 double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0; 410 double dimension = (innerRadius + childDimensions.deltaRadius) * 2.0;
411 return constraints.constrain(new sky.Size(dimension, dimension)); 411 return constraints.constrain(new Size(dimension, dimension));
412 } 412 }
413 413
414 void performLayout() { 414 void performLayout() {
415 if (child == null) { 415 if (child == null) {
416 size = constraints.constrain(new sky.Size(0.0, 0.0)); 416 size = constraints.constrain(Size.zero);
417 } else { 417 } else {
418 assert(child is RenderSector); 418 assert(child is RenderSector);
419 assert(!constraints.isInfinite); 419 assert(!constraints.isInfinite);
420 print("constraint maxes: ${constraints.maxWidth} and ${constraints.maxHeig ht}"); 420 print("constraint maxes: ${constraints.maxWidth} and ${constraints.maxHeig ht}");
421 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.ma xHeight) / 2.0 - innerRadius; 421 double maxChildDeltaRadius = math.min(constraints.maxWidth, constraints.ma xHeight) / 2.0 - innerRadius;
422 print("maxChildDeltaRadius = $maxChildDeltaRadius"); 422 print("maxChildDeltaRadius = $maxChildDeltaRadius");
423 assert(child.parentData is SectorParentData); 423 assert(child.parentData is SectorParentData);
424 child.parentData.radius = innerRadius; 424 child.parentData.radius = innerRadius;
425 child.parentData.theta = 0.0; 425 child.parentData.theta = 0.0;
426 child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), p arentUsesSize: true); 426 child.layout(new SectorConstraints(maxDeltaRadius: maxChildDeltaRadius), p arentUsesSize: true);
427 double dimension = (innerRadius + child.deltaRadius) * 2.0; 427 double dimension = (innerRadius + child.deltaRadius) * 2.0;
428 size = constraints.constrain(new sky.Size(dimension, dimension)); 428 size = constraints.constrain(new Size(dimension, dimension));
429 } 429 }
430 } 430 }
431 431
432 // paint origin is 0,0 of our circle 432 // paint origin is 0,0 of our circle
433 void paint(RenderObjectDisplayList canvas) { 433 void paint(RenderObjectDisplayList canvas) {
434 super.paint(canvas); 434 super.paint(canvas);
435 if (child != null) { 435 if (child != null) {
436 sky.Rect bounds = new sky.Rect.fromSize(size); 436 Rect bounds = new Rect.fromSize(size);
437 canvas.paintChild(child, bounds.center); 437 canvas.paintChild(child, bounds.center);
438 } 438 }
439 } 439 }
440 440
441 bool hitTest(HitTestResult result, { sky.Point position }) { 441 bool hitTest(HitTestResult result, { Point position }) {
442 double x = position.x; 442 double x = position.x;
443 double y = position.y; 443 double y = position.y;
444 if (child == null) 444 if (child == null)
445 return false; 445 return false;
446 // translate to our origin 446 // translate to our origin
447 x -= size.width/2.0; 447 x -= size.width/2.0;
448 y -= size.height/2.0; 448 y -= size.height/2.0;
449 // convert to radius/theta 449 // convert to radius/theta
450 double radius = math.sqrt(x*x+y*y); 450 double radius = math.sqrt(x*x+y*y);
451 double theta = (math.atan2(x, -y) - math.PI/2.0) % kTwoPi; 451 double theta = (math.atan2(x, -y) - math.PI/2.0) % kTwoPi;
452 if (radius < innerRadius) 452 if (radius < innerRadius)
453 return false; 453 return false;
454 if (radius >= innerRadius + child.deltaRadius) 454 if (radius >= innerRadius + child.deltaRadius)
455 return false; 455 return false;
456 if (theta > child.deltaTheta) 456 if (theta > child.deltaTheta)
457 return false; 457 return false;
458 child.hitTest(result, radius: radius, theta: theta); 458 child.hitTest(result, radius: radius, theta: theta);
459 result.add(this); 459 result.add(this);
460 return true; 460 return true;
461 } 461 }
462 462
463 } 463 }
464 464
465 class RenderSolidColor extends RenderDecoratedSector { 465 class RenderSolidColor extends RenderDecoratedSector {
466 RenderSolidColor(sky.Color backgroundColor, { 466 RenderSolidColor(Color backgroundColor, {
467 this.desiredDeltaRadius: double.INFINITY, 467 this.desiredDeltaRadius: double.INFINITY,
468 this.desiredDeltaTheta: kTwoPi 468 this.desiredDeltaTheta: kTwoPi
469 }) : this.backgroundColor = backgroundColor, 469 }) : this.backgroundColor = backgroundColor,
470 super(new BoxDecoration(backgroundColor: backgroundColor)); 470 super(new BoxDecoration(backgroundColor: backgroundColor));
471 471
472 double desiredDeltaRadius; 472 double desiredDeltaRadius;
473 double desiredDeltaTheta; 473 double desiredDeltaTheta;
474 final sky.Color backgroundColor; 474 final Color backgroundColor;
475 475
476 SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) { 476 SectorDimensions getIntrinsicDimensions(SectorConstraints constraints, double radius) {
477 return new SectorDimensions.withConstraints(constraints, deltaTheta: 1.0); / / 1.0 radians 477 return new SectorDimensions.withConstraints(constraints, deltaTheta: 1.0); / / 1.0 radians
478 } 478 }
479 479
480 void performLayout() { 480 void performLayout() {
481 deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius); 481 deltaRadius = constraints.constrainDeltaRadius(desiredDeltaRadius);
482 deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta); 482 deltaTheta = constraints.constrainDeltaTheta(desiredDeltaTheta);
483 } 483 }
484 484
485 void handleEvent(sky.Event event) { 485 void handleEvent(sky.Event event) {
486 if (event.type == 'pointerdown') 486 if (event.type == 'pointerdown')
487 decoration = new BoxDecoration(backgroundColor: const sky.Color(0xFFFF0000 )); 487 decoration = new BoxDecoration(backgroundColor: const Color(0xFFFF0000));
488 else if (event.type == 'pointerup') 488 else if (event.type == 'pointerup')
489 decoration = new BoxDecoration(backgroundColor: backgroundColor); 489 decoration = new BoxDecoration(backgroundColor: backgroundColor);
490 } 490 }
491 } 491 }
492 492
493 AppView app; 493 AppView app;
494 494
495 void main() { 495 void main() {
496 496
497 var rootCircle = new RenderSectorRing(padding: 20.0); 497 var rootCircle = new RenderSectorRing(padding: 20.0);
498 rootCircle.add(new RenderSolidColor(const sky.Color(0xFF00FFFF), desiredDeltaT heta: kTwoPi * 0.15)); 498 rootCircle.add(new RenderSolidColor(const Color(0xFF00FFFF), desiredDeltaTheta : kTwoPi * 0.15));
499 rootCircle.add(new RenderSolidColor(const sky.Color(0xFF0000FF), desiredDeltaT heta: kTwoPi * 0.4)); 499 rootCircle.add(new RenderSolidColor(const Color(0xFF0000FF), desiredDeltaTheta : kTwoPi * 0.4));
500 var stack = new RenderSectorSlice(padding: 2.0); 500 var stack = new RenderSectorSlice(padding: 2.0);
501 stack.add(new RenderSolidColor(const sky.Color(0xFFFFFF00), desiredDeltaRadius : 20.0)); 501 stack.add(new RenderSolidColor(const Color(0xFFFFFF00), desiredDeltaRadius: 20 .0));
502 stack.add(new RenderSolidColor(const sky.Color(0xFFFF9000), desiredDeltaRadius : 20.0)); 502 stack.add(new RenderSolidColor(const Color(0xFFFF9000), desiredDeltaRadius: 20 .0));
503 stack.add(new RenderSolidColor(const sky.Color(0xFF00FF00))); 503 stack.add(new RenderSolidColor(const Color(0xFF00FF00)));
504 rootCircle.add(stack); 504 rootCircle.add(stack);
505 505
506 var root = new RenderBoxToRenderSectorAdapter(innerRadius: 50.0, child: rootCi rcle); 506 var root = new RenderBoxToRenderSectorAdapter(innerRadius: 50.0, child: rootCi rcle);
507 app = new AppView(root); 507 app = new AppView(root);
508 } 508 }
OLDNEW
« no previous file with comments | « sky/engine/core/painting/Size.dart ('k') | sky/sdk/lib/framework/rendering/box.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698