11part of '../renderer.dart' ;
22
3+ ({Offset tip, Offset left, Offset right}) _trianglePoints (
4+ Rect rect,
5+ ShapeElement element,
6+ ) {
7+ final flippedVertically = element.secondPosition.y < element.firstPosition.y;
8+ return flippedVertically
9+ ? (tip: rect.bottomCenter, left: rect.topLeft, right: rect.topRight)
10+ : (tip: rect.topCenter, left: rect.bottomLeft, right: rect.bottomRight);
11+ }
12+
313class ShapeRenderer extends Renderer <ShapeElement > {
414 final _strokePaint = ElementPaintRenderer ();
515 final _fillPaint = ElementPaintRenderer ();
@@ -161,11 +171,11 @@ class ShapeRenderer extends Renderer<ShapeElement> {
161171 ..lineTo (element.secondPosition.x, element.secondPosition.y);
162172 _drawStyledPath (canvas, path, paint);
163173 } else if (shape is TriangleShape ) {
164- final topCenter = drawRect.topCenter ;
174+ final points = _trianglePoints ( drawRect, element) ;
165175 final path = Path ()
166- ..moveTo (topCenter. dx, topCenter .dy)
167- ..lineTo (drawRect .right, drawRect.bottom )
168- ..lineTo (drawRect .left, drawRect.bottom )
176+ ..moveTo (points.tip. dx, points.tip .dy)
177+ ..lineTo (points .right.dx, points.right.dy )
178+ ..lineTo (points .left.dx, points.left.dy )
169179 ..close ();
170180 canvas.drawPath (
171181 path,
@@ -305,11 +315,11 @@ class ShapeRenderer extends Renderer<ShapeElement> {
305315 },
306316 );
307317 } else if (shape is TriangleShape ) {
308- final topCenter = drawRect.topCenter ;
318+ final points = _trianglePoints ( drawRect, element) ;
309319 final d =
310- 'M${topCenter . dx } ${topCenter .dy } '
311- 'L${drawRect .right } ${drawRect . bottom } '
312- 'L${drawRect .left } ${drawRect . bottom } Z' ;
320+ 'M${points . tip . dx } ${points . tip .dy } '
321+ 'L${points .right . dx } ${points . right . dy } '
322+ 'L${points .left . dx } ${points . left . dy } Z' ;
313323 xml
314324 .getElement ('svg' )
315325 ? .createElement (
@@ -322,23 +332,6 @@ class ShapeRenderer extends Renderer<ShapeElement> {
322332 'stroke-dasharray' : ? dashArray,
323333 },
324334 );
325- } else if (shape is TriangleShape ) {
326- final topCenter = drawRect.topCenter;
327- final d =
328- 'M${topCenter .dx } ${topCenter .dy } '
329- 'L${drawRect .right } ${drawRect .bottom } '
330- 'L${drawRect .left } ${drawRect .bottom } Z' ;
331- xml
332- .getElement ('svg' )
333- ? .createElement (
334- 'path' ,
335- attributes: {
336- 'd' : d,
337- 'fill' : shape.fillPaint.previewColor.toHexString (),
338- 'stroke' : element.property.paint.previewColor.toHexString (),
339- 'stroke-width' : '${element .property .strokeWidth }px' ,
340- },
341- );
342335 }
343336 }
344337
@@ -354,16 +347,13 @@ class ShapeRenderer extends Renderer<ShapeElement> {
354347 final previous = rect.topLeft;
355348 final localFirst = element.firstPosition.toOffset () - previous;
356349 final localSecond = element.secondPosition.toOffset () - previous;
357- final nextRotation = element.property.shape is TriangleShape && scaleY < 0
358- ? (rotation + 180 ) % 360
359- : rotation;
360350 return ShapeRenderer (
361351 element.copyWith (
362352 shear: shear,
363353 firstPosition: (localFirst.scale (scaleX, scaleY) + position).toPoint (),
364354 secondPosition: (localSecond.scale (scaleX, scaleY) + position)
365355 .toPoint (),
366- rotation: nextRotation ,
356+ rotation: rotation ,
367357 ),
368358 layer,
369359 );
@@ -503,9 +493,10 @@ class ShapeHitCalculator extends HitCalculator {
503493 }
504494
505495 bool hitTriangle () {
506- final triTop = this .rect.topCenter.rotate (center, rotation);
507- final triLeft = this .rect.bottomLeft.rotate (center, rotation);
508- final triRight = this .rect.bottomRight.rotate (center, rotation);
496+ final points = _trianglePoints (this .rect, element);
497+ final triTop = points.tip.rotate (center, rotation);
498+ final triLeft = points.left.rotate (center, rotation);
499+ final triRight = points.right.rotate (center, rotation);
509500
510501 return switch (hitElementMode) {
511502 HitElementMode .full =>
@@ -600,9 +591,10 @@ class ShapeHitCalculator extends HitCalculator {
600591 _ => false , // this shouldn't happen
601592 };
602593 case TriangleShape ():
603- final topCenter = rect.topCenter.rotate (center, rotation);
604- final bottomLeft = rect.bottomLeft.rotate (center, rotation);
605- final bottomRight = rect.bottomRight.rotate (center, rotation);
594+ final points = _trianglePoints (rect, element);
595+ final topCenter = points.tip.rotate (center, rotation);
596+ final bottomLeft = points.left.rotate (center, rotation);
597+ final bottomRight = points.right.rotate (center, rotation);
606598 var triPoints = [topCenter, bottomLeft, bottomRight];
607599 final inside = isPolygonInPolygon (polygon, triPoints);
608600 return switch (hitElementMode) {
0 commit comments