-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcommand_manager.dart
135 lines (113 loc) · 3.8 KB
/
command_manager.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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';
enum ActionType { UNDO, REDO }
class CommandManager {
CommandManager();
final List<Command> _undoStack = [];
final List<Command> _redoStack = [];
void addGraphicCommand(GraphicCommand command) {
_undoStack.add(command);
}
void setUndoStack(List<Command> commands) {
_undoStack.clear();
_undoStack.addAll(commands);
}
void executeLastCommand(Canvas canvas) {
if (_undoStack.isEmpty) return;
final lastCommand = _undoStack.last;
if (lastCommand is GraphicCommand) {
lastCommand.call(canvas);
}
}
void executeAllCommands(Canvas canvas) {
for (final command in _undoStack) {
if (command is GraphicCommand) {
command.call(canvas);
}
}
}
void discardLastCommand() {
if (_undoStack.isNotEmpty) _undoStack.removeLast();
}
void clearUndoStack({Iterable<Command>? newCommands}) {
_undoStack.clear();
if (newCommands != null) {
_undoStack.addAll(newCommands);
}
}
void clearRedoStack() {
_redoStack.clear();
}
void drawLineToolGhostPaths(
Canvas canvas,
LineCommand? ingoingGhostPathCommand,
LineCommand? outgoingGhostPathCommand,
) {
ingoingGhostPathCommand?.call(canvas);
outgoingGhostPathCommand?.call(canvas);
}
void drawLineToolVertices(Canvas canvas, VertexStack vertexStack) {
for (var vertex in vertexStack) {
canvas.drawCircle(
vertex.vertexCenter,
Vertex.VERTEX_RADIUS,
Vertex.getVertexPaint(),
);
}
}
Command redo() {
final lastCommand = _redoStack.removeLast();
_undoStack.add(lastCommand);
return lastCommand;
}
void undo() {
final lastCommand = _undoStack.removeLast();
_redoStack.add(lastCommand);
}
List<Command> get redoStack => _redoStack;
List<Command> get undoStack => _undoStack;
ToolData getNextTool(ActionType actionType) {
Command? command;
switch (actionType) {
case ActionType.UNDO:
command = _undoStack.last;
break;
case ActionType.REDO:
command = _redoStack.last;
break;
}
///TODO implement for all tools after implementing unique commands
if (command.runtimeType == LineCommand) {
return ToolData.LINE;
} else if (command.runtimeType == SquareShapeCommand) {
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;
}
}
List<LineCommand> getTopLineCommandSequence() {
final List<LineCommand> lineCommands = [];
for (final command in _undoStack.reversed) {
if (command is! LineCommand) break;
lineCommands.add(command);
if (command.isSourcePath) break;
}
return lineCommands.reversed.toList();
}
}