-
Notifications
You must be signed in to change notification settings - Fork 79
PAINTROID-764 Add more shapes to Shapes Tool #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
0b09537
b3d2be9
3652d58
a2113c9
688386a
3e9c609
b1ce369
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import 'dart:math'; | ||
|
||
import 'package:flutter/widgets.dart'; | ||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:paintroid/core/commands/command_implementation/graphic/shape/shape_command.dart'; | ||
import 'package:paintroid/core/json_serialization/converter/offset_converter.dart'; | ||
import 'package:paintroid/core/json_serialization/converter/paint_converter.dart'; | ||
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart'; | ||
import 'package:paintroid/core/json_serialization/versioning/version_strategy.dart'; | ||
|
||
part 'heart_shape_command.g.dart'; | ||
|
||
@JsonSerializable() | ||
class HeartShapeCommand extends ShapeCommand { | ||
final double width; | ||
final double height; | ||
final double angle; | ||
@OffsetConverter() | ||
final Offset center; | ||
|
||
final int version; | ||
final String type; | ||
|
||
HeartShapeCommand( | ||
super.paint, | ||
this.width, | ||
this.height, | ||
this.angle, | ||
this.center, { | ||
int? version, | ||
this.type = SerializerType.HEART_SHAPE_COMMAND, | ||
}) : version = version ?? | ||
VersionStrategyManager.strategy.getHeartShapeCommandVersion(); | ||
|
||
Path get path => _getHeartPath(angle); | ||
|
||
@override | ||
void call(Canvas canvas) => canvas.drawPath(path, paint); | ||
|
||
@override | ||
List<Object?> get props => [paint, width, height, center]; | ||
|
||
@override | ||
Map<String, dynamic> toJson() => _$HeartShapeCommandToJson(this); | ||
|
||
factory HeartShapeCommand.fromJson(Map<String, dynamic> json) { | ||
int version = json['version'] as int; | ||
|
||
switch (version) { | ||
case Version.v1: | ||
return _$HeartShapeCommandFromJson(json); | ||
default: | ||
return _$HeartShapeCommandFromJson(json); | ||
} | ||
} | ||
|
||
Path _getHeartPath(double angle) { | ||
Path path = Path(); | ||
final double w = width / 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for explicit type |
||
final double h = height / 2; | ||
const double rotationOffset = 3 * pi / 4; | ||
|
||
path.moveTo(0, -h * 0.25); | ||
|
||
path.cubicTo( | ||
-w, | ||
-h * 1.25, | ||
-w * 1.25, | ||
h * 0.25, | ||
0, | ||
h * 0.75, | ||
); | ||
|
||
path.cubicTo( | ||
w * 1.25, | ||
h * 0.25, | ||
w, | ||
-h * 1.25, | ||
0, | ||
-h * 0.25, | ||
); | ||
|
||
path.close(); | ||
|
||
final Matrix4 rotationMatrix = Matrix4.identity() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for explicit type |
||
..rotateZ(angle + rotationOffset); | ||
|
||
path = path.transform(rotationMatrix.storage); | ||
|
||
path = path.shift(center); | ||
|
||
return path; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import 'dart:math'; | ||
import 'dart:ui'; | ||
|
||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:paintroid/core/commands/command_implementation/graphic/shape/shape_command.dart'; | ||
import 'package:paintroid/core/extensions/path_extension.dart'; | ||
import 'package:paintroid/core/json_serialization/converter/offset_converter.dart'; | ||
import 'package:paintroid/core/json_serialization/converter/paint_converter.dart'; | ||
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart'; | ||
import 'package:paintroid/core/json_serialization/versioning/version_strategy.dart'; | ||
|
||
part 'star_shape_command.g.dart'; | ||
|
||
@JsonSerializable() | ||
class StarShapeCommand extends ShapeCommand { | ||
final int numberOfPoints; | ||
final double radius; | ||
final double angle; | ||
@OffsetConverter() | ||
final Offset center; | ||
|
||
final int version; | ||
final String type; | ||
|
||
StarShapeCommand( | ||
super.paint, | ||
this.numberOfPoints, | ||
this.radius, | ||
this.angle, | ||
this.center, { | ||
int? version, | ||
this.type = SerializerType.STAR_SHAPE_COMMAND, | ||
}) : version = version ?? | ||
VersionStrategyManager.strategy.getStarShapeCommandVersion(); | ||
|
||
Path get path => _getStarPath(); | ||
|
||
@override | ||
void call(Canvas canvas) => canvas.drawPath(path, paint); | ||
|
||
@override | ||
List<Object?> get props => [paint, numberOfPoints, radius, center]; | ||
|
||
@override | ||
Map<String, dynamic> toJson() => _$StarShapeCommandToJson(this); | ||
|
||
factory StarShapeCommand.fromJson(Map<String, dynamic> json) { | ||
int version = json['version'] as int; | ||
|
||
switch (version) { | ||
case Version.v1: | ||
return _$StarShapeCommandFromJson(json); | ||
case Version.v2: | ||
// For different versions of StarShapeCommand the deserialization | ||
// has to be implemented manually. | ||
// Autogenerated code can only be used for one version | ||
default: | ||
return _$StarShapeCommandFromJson(json); | ||
} | ||
} | ||
|
||
Path _getStarPath() { | ||
final path = Path(); | ||
final innerRadius = radius / 2; | ||
final angleStep = pi / numberOfPoints; | ||
for (int i = 0; i < numberOfPoints * 2; i++) { | ||
final currentRadius = (i % 2 == 0) ? radius : innerRadius; | ||
final currentAngle = i * angleStep + angle; | ||
final point = Offset( | ||
center.dx + currentRadius * cos(currentAngle), | ||
center.dy + currentRadius * sin(currentAngle), | ||
); | ||
i == 0 ? path.moveToOffset(point) : path.lineToOffset(point); | ||
} | ||
path.close(); | ||
return path; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
enum ShapeType { | ||
circle, | ||
square, | ||
} | ||
enum ShapeType { circle, square, star, heart } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need for explicit type
Path