Skip to content
Open
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
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ analyzer:
exclude:
- lib/src/**.pb*.dart
- lib/**.g.dart
- test/**.mocks.dart
- test/**.mocks.dart
86 changes: 39 additions & 47 deletions lib/core/commands/command_factory/command_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/clipboard_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/delete_region_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/text_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/clip_area_command.dart';
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/ellipse_shape_command.dart';
Expand All @@ -23,16 +24,14 @@ class CommandFactory {
PathWithActionHistory path,
Paint paint, {
bool isCursor = false,
}) =>
PathCommand(path, paint, isCursorPath: isCursor);
}) => PathCommand(path, paint, isCursorPath: isCursor);

LineCommand createLineCommand(
PathWithActionHistory path,
Paint paint,
Offset startPoint,
Offset endPoint,
) =>
LineCommand(path, paint, startPoint, endPoint);
) => LineCommand(path, paint, startPoint, endPoint);

SquareShapeCommand createSquareShapeCommand(
Paint paint,
Expand All @@ -41,9 +40,14 @@ class CommandFactory {
Offset bottomLeft,
Offset bottomRight,
ShapeStyle style,
) =>
SquareShapeCommand(
paint, topLeft, topRight, bottomLeft, bottomRight, style);
) => SquareShapeCommand(
paint,
topLeft,
topRight,
bottomLeft,
bottomRight,
style,
);

EllipseShapeCommand createEllipseShapeCommand(
Paint paint,
Expand All @@ -52,30 +56,15 @@ class CommandFactory {
Offset center,
ShapeStyle style,
double angle,
) =>
EllipseShapeCommand(
paint,
radiusX,
radiusY,
center,
style,
angle,
);
) => EllipseShapeCommand(paint, radiusX, radiusY, center, style, angle);

ClipboardCommand createClipboardCommand(
Paint paint,
Uint8List imageData,
ui.Offset offset,
double scale,
double rotation,
) =>
ClipboardCommand(
paint,
imageData,
offset,
scale,
rotation,
);
) => ClipboardCommand(paint, imageData, offset, scale, rotation);

TextCommand createTextCommand(
Offset point,
Expand All @@ -86,17 +75,16 @@ class CommandFactory {
double rotationAngle, {
double scaleX = 1.0,
double scaleY = 1.0,
}) =>
TextCommand(
point,
text,
style,
fontSize,
paint,
rotationAngle: rotationAngle,
scaleX: scaleX,
scaleY: scaleY,
);
}) => TextCommand(
point,
text,
style,
fontSize,
paint,
rotationAngle: rotationAngle,
scaleX: scaleX,
scaleY: scaleY,
);

StarShapeCommand createStarShapeCommand(
Paint paint,
Expand All @@ -106,16 +94,15 @@ class CommandFactory {
ShapeStyle style,
double radiusX,
double radiusY,
) =>
StarShapeCommand(
paint,
numPoints,
angle,
center,
style,
radiusX,
radiusY,
);
) => StarShapeCommand(
paint,
numPoints,
angle,
center,
style,
radiusX,
radiusY,
);

HeartShapeCommand createHeartShapeCommand(
Paint paint,
Expand All @@ -124,8 +111,7 @@ class CommandFactory {
double angle,
Offset center,
ShapeStyle style,
) =>
HeartShapeCommand(paint, width, height, angle, center, style);
) => HeartShapeCommand(paint, width, height, angle, center, style);

SprayCommand createSprayCommand(List<Offset> points, Paint paint) {
return SprayCommand(points, paint);
Expand All @@ -139,6 +125,12 @@ class CommandFactory {
region,
);

ClipAreaCommand createClipAreaCommand(
PathWithActionHistory path,
Paint paint,
) =>
ClipAreaCommand(path, paint);

ColorChangedCommand createColorChangedCommand(
Color oldColor,
Color newColor,
Expand Down
3 changes: 3 additions & 0 deletions lib/core/commands/command_implementation/command.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:equatable/equatable.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/clipboard_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/delete_region_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/clip_area_command.dart';
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/ellipse_shape_command.dart';
Expand Down Expand Up @@ -39,6 +40,8 @@ abstract class Command with EquatableMixin {
return HeartShapeCommand.fromJson(json);
case SerializerType.STAR_SHAPE_COMMAND:
return StarShapeCommand.fromJson(json);
case SerializerType.CLIP_AREA_COMMAND:
return ClipAreaCommand.fromJson(json);
case SerializerType.COLOR_CHANGED_COMMAND:
return ColorChangedCommand.fromJson(json);
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import 'dart:ui';

import 'package:json_annotation/json_annotation.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/graphic_command.dart';
import 'package:paintroid/core/commands/path_with_action_history.dart';
import 'package:paintroid/core/json_serialization/converter/paint_converter.dart';
import 'package:paintroid/core/json_serialization/converter/path_with_action_history_converter.dart';
import 'package:paintroid/core/json_serialization/versioning/serializer_version.dart';
import 'package:paintroid/core/json_serialization/versioning/version_strategy.dart';

part 'clip_area_command.g.dart';

@JsonSerializable()
class ClipAreaCommand extends GraphicCommand {
final String type;
final int version;

@PathWithActionHistoryConverter()
final PathWithActionHistory clipPathData;

ClipAreaCommand(
this.clipPathData,
Paint paint, {
this.type = SerializerType.CLIP_AREA_COMMAND,
int? version,
}) : version = version ??
VersionStrategyManager.strategy.getClipAreaCommandVersion(),
super(paint);

@override
void call(Canvas canvas) {
final Rect canvasBounds = canvas.getLocalClipBounds();

canvas.saveLayer(canvasBounds, Paint()..blendMode = BlendMode.dstIn);

canvas.drawPath(
clipPathData.path,
Paint()
..color = const Color(0xFF000000)
..style = PaintingStyle.fill);

canvas.restore();
}

@override
List<Object?> get props => [paint, clipPathData, type, version];

factory ClipAreaCommand.fromJson(Map<String, dynamic> json) {
int version = json['version'] as int;

switch (version) {
case Version.v1:
return _$ClipAreaCommandFromJson(json);
case Version.v2:
// For different versions of ClipAreaCommand the deserialization
// has to be implemented manually.
// Autogenerated code can only be used for one version
default:
return _$ClipAreaCommandFromJson(json);
}
}

@override
Map<String, dynamic> toJson() => _$ClipAreaCommandToJson(this);
}

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
Expand Up @@ -29,10 +29,12 @@ class DeleteRegionCommand extends GraphicCommand {

@override
void call(ui.Canvas canvas) {
canvas.saveLayer(region, ui.Paint()..blendMode = ui.BlendMode.src);
final clearPaint = ui.Paint()
..blendMode = ui.BlendMode.clear
..style = ui.PaintingStyle.fill;
canvas.drawRect(region, clearPaint);
canvas.restore();
}

@override
Expand Down
15 changes: 13 additions & 2 deletions lib/core/commands/command_manager/command_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:ui';

import 'package:paintroid/core/commands/command_implementation/command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/clipboard_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/clip_area_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/text_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/graphic_command.dart';
import 'package:paintroid/core/commands/command_implementation/graphic/line_command.dart';
Expand All @@ -28,13 +29,19 @@ class CommandManager {
_undoStack.add(command);
}

void removeCommand(Command commandToRemove) {
_undoStack.remove(commandToRemove);
}

void setUndoStack(List<Command> commands) {
_undoStack.clear();
_undoStack.addAll(commands);
}

void executeLastCommand(Canvas canvas) {
if (_undoStack.isEmpty) return;
if (_undoStack.isEmpty) {
return;
}
final lastCommand = _undoStack.last;
if (lastCommand is GraphicCommand) {
lastCommand.call(canvas);
Expand All @@ -50,7 +57,9 @@ class CommandManager {
}

void discardLastCommand() {
if (_undoStack.isNotEmpty) _undoStack.removeLast();
if (_undoStack.isNotEmpty) {
_undoStack.removeLast();
}
}

void clearUndoStack({Iterable<Command>? newCommands}) {
Expand Down Expand Up @@ -128,6 +137,8 @@ class CommandManager {
return ToolData.SHAPES;
} else if (command.runtimeType == HeartShapeCommand) {
return ToolData.SHAPES;
} else if (command.runtimeType == ClipAreaCommand) {
return ToolData.CLIPPING;
} else if (command is PathCommand) {
if (command.isCursorPath) {
return ToolData.CURSOR;
Expand Down
5 changes: 5 additions & 0 deletions lib/core/commands/command_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:paintroid/core/tools/implementation/clipboard_tool.dart';
import 'package:paintroid/core/tools/implementation/cursor_tool.dart';
import 'package:paintroid/core/tools/implementation/shapes_tool.dart';
import 'package:paintroid/core/tools/implementation/text_tool.dart';
import 'package:paintroid/core/tools/implementation/clipping_tool.dart';
import 'package:paintroid/core/tools/implementation/brush_tool.dart';
import 'package:paintroid/core/tools/line_tool/line_tool.dart';
import 'package:paintroid/core/tools/tool.dart';
Expand Down Expand Up @@ -65,9 +66,13 @@ class CommandPainter extends CustomPainter {
break;
case ToolType.CLIPBOARD:
(currentTool as ClipboardTool).paint(canvas, size);
break;
case ToolType.TEXT:
(currentTool as TextTool).drawGuides(canvas, ref.read(paintProvider));
break;
case ToolType.CLIPPING:
(currentTool as ClippingTool).draw(canvas, ref.read(paintProvider));
break;
default:
if (currentTool is BrushTool) {
if ((currentTool as BrushTool).isDrawing || isCachingCommand) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SerializerVersion {
static const int SPRAY_COMMAND_VERSION = Version.v1;
static const int CLIPBOARD_COMMAND_VERSION = Version.v1;
static const int DELETE_REGION_COMMAND_VERSION = Version.v1;
static const int CLIP_AREA_COMMAND_VERSION = Version.v1;
static const int COLOR_CHANGED_COMMAND_VERSION = Version.v1;
}

Expand All @@ -34,5 +35,6 @@ class SerializerType {
static const String SPRAY_COMMAND = 'SprayCommand';
static const String CLIPBOARD_COMMAND = 'ClipboardCommand';
static const String DELETE_REGION_COMMAND = 'DeleteRegionCommand';
static const String CLIP_AREA_COMMAND = 'ClipAreaCommand';
static const String COLOR_CHANGED_COMMAND = 'ColorChangedCommand';
}
6 changes: 6 additions & 0 deletions lib/core/json_serialization/versioning/version_strategy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract class IVersionStrategy {

int getDeleteRegionCommandVersion();

int getClipAreaCommandVersion();

int getColorChangedCommandVersion();
}

Expand Down Expand Up @@ -66,6 +68,10 @@ class ProductionVersionStrategy implements IVersionStrategy {
int getDeleteRegionCommandVersion() =>
SerializerVersion.DELETE_REGION_COMMAND_VERSION;

@override
int getClipAreaCommandVersion() =>
SerializerVersion.CLIP_AREA_COMMAND_VERSION;

@override
int getColorChangedCommandVersion() =>
SerializerVersion.COLOR_CHANGED_COMMAND_VERSION;
Expand Down
Loading
Loading