Skip to content
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

PAINTROID-764 Add more shapes to Shapes Tool #103

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,17 @@ SPEC CHECKSUMS:
file_picker: ce3938a0df3cc1ef404671531facef740d03f920
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
flutter_localization: f43b18844a2b3d2c71fd64f04ffd6b1e64dd54d4
image_picker_ios: c560581cceedb403a6ff17f2f816d7fea1421fc1
image_picker_ios: 99dfe1854b4fa34d0364e74a78448a0151025425
integration_test: ce0a3ffa1de96d1a89ca0ac26fca7ea18a749ef4
launch_review: 75d5a956ba8eaa493e9c9d4bf4c05e505e8d5ed0
package_info_plus: 115f4ad11e0698c8c1c5d8a689390df880f47e85
path_provider_foundation: 2b6b4c569c0fb62ec74538f866245ac84301af46
path_provider_foundation: 3784922295ac71e43754bd15e0653ccfd36a147c
permission_handler_apple: e76247795d700c14ea09e3a2d8855d41ee80a2e6
SDWebImage: dfe95b2466a9823cf9f0c6d01217c06550d7b29a
shared_preferences_foundation: fcdcbc04712aee1108ac7fda236f363274528f78
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
sqflite: 673a0e54cc04b7d6dba8d24fb8095b31c3a99eec
SwiftyGif: 706c60cf65fa2bc5ee0313beece843c8eb8194d4
url_launcher_ios: 5334b05cef931de560670eeae103fd3e431ac3fe
url_launcher_ios: bbd758c6e7f9fd7b5b1d4cde34d2b95fcce5e812

PODFILE CHECKSUM: 303789365c3a8d7bc562e5e65d7e8e15218ec5c6

Expand Down
26 changes: 26 additions & 0 deletions lib/core/commands/command_factory/command_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:ui';
import 'package:paintroid/core/commands/command_implementation/graphic/line_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/path_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/circle_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/heart_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/square_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'package:paintroid/core/commands/path_with_action_history.dart';

class CommandFactory {
Expand Down Expand Up @@ -38,4 +40,28 @@ class CommandFactory {
Offset center,
) =>
CircleShapeCommand(paint, radius, center);

StarShapeCommand createStarShapeCommand(
Paint paint,
int numPoints,
double radius,
double angle,
Offset center,
) =>
StarShapeCommand(
paint,
numPoints,
radius,
angle,
center,
);

HeartShapeCommand createHeartShapeCommand(
Paint paint,
double width,
double height,
double angle,
Offset center,
) =>
HeartShapeCommand(paint, width, height, angle, center);
}
3 changes: 3 additions & 0 deletions lib/core/commands/command_implementation/command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:paintroid/core/commands/command_implementation/graphic/line_comm
import 'package:paintroid/core/commands/command_implementation/graphic/path_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/circle_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/square_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart';

abstract class Command with EquatableMixin {
Expand All @@ -21,6 +22,8 @@ abstract class Command with EquatableMixin {
return SquareShapeCommand.fromJson(json);
case SerializerType.CIRCLE_SHAPE_COMMAND:
return CircleShapeCommand.fromJson(json);
case SerializerType.STAR_SHAPE_COMMAND:
return StarShapeCommand.fromJson(json);
default:
return PathCommand.fromJson(json);
}
Expand Down
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;
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()
..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.

7 changes: 7 additions & 0 deletions lib/core/commands/command_manager/command_manager.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:paintroid/core/commands/command_implementation/command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/graphic_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/line_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/circle_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/heart_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/square_shape_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/shape/star_shape_command.dart';
import 'package:paintroid/core/tools/line_tool/vertex.dart';
import 'package:paintroid/core/tools/line_tool/vertex_stack.dart';
import 'package:paintroid/core/tools/tool_data.dart';
Expand Down Expand Up @@ -109,6 +112,10 @@ class CommandManager {
return ToolData.SHAPES;
} else if (command.runtimeType == CircleShapeCommand) {
return ToolData.SHAPES;
} else if (command.runtimeType == StarShapeCommand) {
return ToolData.SHAPES;
} else if (command.runtimeType == HeartShapeCommand) {
return ToolData.SHAPES;
} else {
return ToolData.BRUSH;
}
Expand Down
27 changes: 8 additions & 19 deletions lib/core/database/project_database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading