-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PAINTROID-768: Add Spray Can tool (#98)
* test * test * PAINTROID-768: Flutter: Add Spray Can tool * PAINTROID-768: Flutter: Add Spray Can tool * PAINTROID-768: Flutter: Add Spray Can tool - tests * PAINTROID-768: Flutter: Add Spray Can tool - CR * PAINTROID-768: Flutter: Add Spray Can tool - CR * PAINTROID-768: Flutter: Add Spray Can tool - CR * PAINTROID-768: Flutter: Add Spray Can tool - CR
- Loading branch information
1 parent
269d3c8
commit 3a48447
Showing
28 changed files
with
810 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
lib/core/commands/command_implementation/graphic/spray_command.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:ui'; | ||
|
||
import 'package:freezed_annotation/freezed_annotation.dart'; | ||
import 'package:paintroid/core/commands/command_implementation/graphic/graphic_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 'spray_command.g.dart'; | ||
|
||
@JsonSerializable() | ||
class SprayCommand extends GraphicCommand { | ||
final String type; | ||
final int version; | ||
|
||
@OffsetConverter() | ||
List<Offset> points; | ||
|
||
SprayCommand( | ||
this.points, | ||
super.paint, { | ||
this.type = SerializerType.SPRAY_COMMAND, | ||
int? version, | ||
}) : version = | ||
version ?? VersionStrategyManager.strategy.getSprayCommandVersion(); | ||
|
||
@override | ||
void call(Canvas canvas) { | ||
canvas.drawPoints(PointMode.points, points, paint); | ||
} | ||
|
||
@override | ||
List<Object?> get props => [paint, points]; | ||
|
||
@override | ||
Map<String, dynamic> toJson() => _$SprayCommandToJson(this); | ||
|
||
factory SprayCommand.fromJson(Map<String, dynamic> json) { | ||
int version = json['version'] as int; | ||
|
||
switch (version) { | ||
case Version.v1: | ||
return _$SprayCommandFromJson(json); | ||
default: | ||
return _$SprayCommandFromJson(json); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
lib/core/commands/command_implementation/graphic/spray_command.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:paintroid/core/commands/command_factory/command_factory_provider.dart'; | ||
import 'package:paintroid/core/commands/command_manager/command_manager_provider.dart'; | ||
import 'package:paintroid/core/commands/graphic_factory/graphic_factory_provider.dart'; | ||
import 'package:paintroid/core/enums/tool_types.dart'; | ||
import 'package:paintroid/core/providers/state/canvas_state_provider.dart'; | ||
import 'package:paintroid/core/tools/implementation/spray_tool.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
part 'spray_tool_provider.g.dart'; | ||
|
||
@riverpod | ||
class SprayToolProvider extends _$SprayToolProvider { | ||
@override | ||
SprayTool build() { | ||
return SprayTool( | ||
type: ToolType.SPRAY, | ||
commandManager: ref.watch(commandManagerProvider), | ||
commandFactory: ref.watch(commandFactoryProvider), | ||
graphicFactory: ref.watch(graphicFactoryProvider), | ||
drawingSurfaceSize: | ||
ref.watch(canvasStateProvider.select((state) => state.size)), | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'dart:math'; | ||
import 'dart:ui'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:paintroid/core/commands/command_implementation/graphic/spray_command.dart'; | ||
import 'package:paintroid/core/commands/graphic_factory/graphic_factory.dart'; | ||
import 'package:paintroid/core/tools/tool.dart'; | ||
|
||
const SPRAY_TOOL_RADIUS = 10.0; | ||
|
||
class SprayTool extends Tool { | ||
final GraphicFactory graphicFactory; | ||
final Size drawingSurfaceSize; | ||
|
||
@visibleForTesting | ||
late SprayCommand sprayCommand; | ||
late Paint paint; | ||
|
||
SprayTool({ | ||
required super.commandFactory, | ||
required super.commandManager, | ||
required this.graphicFactory, | ||
required super.type, | ||
required this.drawingSurfaceSize, | ||
super.hasAddFunctionality = false, | ||
super.hasFinalizeFunctionality = false, | ||
}); | ||
|
||
double sprayRadius = 20; | ||
final Random random = Random(); | ||
|
||
@override | ||
void onDown(Offset point, Paint paint) { | ||
this.paint = graphicFactory.copyPaint(paint); | ||
final initialPoints = _generateSprayPoints(point); | ||
sprayCommand = commandFactory.createSprayCommand(initialPoints, this.paint); | ||
commandManager.addGraphicCommand(sprayCommand); | ||
} | ||
|
||
@override | ||
void onDrag(Offset point, Paint paint) { | ||
final newPoints = _generateSprayPoints(point); | ||
sprayCommand.points.addAll(newPoints); | ||
} | ||
|
||
@override | ||
void onUp(Offset point, Paint paint) {} | ||
|
||
@override | ||
void onCancel() { | ||
commandManager.discardLastCommand(); | ||
} | ||
|
||
@override | ||
void onCheckmark(Paint paint) {} | ||
|
||
@override | ||
void onPlus() {} | ||
|
||
@override | ||
void onRedo() { | ||
commandManager.redo(); | ||
} | ||
|
||
@override | ||
void onUndo() { | ||
commandManager.undo(); | ||
} | ||
|
||
void updateSprayRadius(double newRadius) { | ||
sprayRadius = newRadius; | ||
} | ||
|
||
List<Offset> _generateSprayPoints(Offset center) { | ||
List<Offset> points = []; | ||
final density = sprayRadius / 3; | ||
for (int i = 0; i < density; i++) { | ||
final angle = random.nextDouble() * 2 * pi; | ||
final radius = sqrt(random.nextDouble()) * sprayRadius * 2; | ||
final dx = center.dx + radius * cos(angle); | ||
final dy = center.dy + radius * sin(angle); | ||
points.add(Offset(dx, dy)); | ||
} | ||
return points; | ||
} | ||
} |
Oops, something went wrong.