Skip to content
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
74 changes: 74 additions & 0 deletions lib/components/canvas/_circle_stroke.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import 'package:flutter/material.dart';
import 'package:one_dollar_unistroke_recognizer/one_dollar_unistroke_recognizer.dart';
import 'package:perfect_freehand/perfect_freehand.dart';
import 'package:saber/components/canvas/_stroke.dart';
import 'package:saber/data/editor/binary_writer.dart';
import 'package:saber/data/editor/page.dart';
import 'package:saber/data/tools/shape_pen.dart';

class CircleStroke extends Stroke {

Offset center;
double radius;

Expand Down Expand Up @@ -74,6 +76,78 @@ class CircleStroke extends Stroke {
}..addAll(options.toJson());
}

/// Serializes a Stroke object into a compact binary format.
@override
void toBinary(BinaryWriter writer) {
writer.writeString(StrokeBinaryKeys.shape,"circle");
writer.writeInt(StrokeBinaryKeys.pageIndex,pageIndex);
writer.writeScaledFloat(StrokeBinaryKeys.cx, center.dx);
writer.writeScaledFloat(StrokeBinaryKeys.cy, center.dy);
writer.writeScaledFloat(StrokeBinaryKeys.r, radius);
writer.writeBool(StrokeBinaryKeys.pressureEnabled,pressureEnabled);
writer.writeInt(StrokeBinaryKeys.color,color.toARGB32());
BinaryOptions().optionsToBinary(writer,options); // store stroke options
// TODO: Add serialization of StrokeOptions if needed
}

/// Deserializes a Stroke object from binary data starting at [offset].
factory CircleStroke.fromBinary(BinaryReader reader,
{required HasSize page,
}){
int key;

key = reader.readKey();
if (key != StrokeBinaryKeys.pageIndex) {
throw Exception('StrokefromBinary no pageIndex');
}
final int pageIndex= reader.readIntNoKey();

key = reader.readKey();
if (key != StrokeBinaryKeys.cx) {
throw Exception('StrokefromBinary no cx');
}
final double cx= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.cy) {
throw Exception('StrokefromBinary no cy');
}
final double cy= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.r) {
throw Exception('StrokefromBinary no r');
}
final double r= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.pressureEnabled) {
throw Exception('StrokefromBinary no pressureEnabled');
}
final bool pressureEnabled = reader.readBoolNoKey();

key=reader.readKey();
if (key!=StrokeBinaryKeys.color) {
throw Exception('StrokefromBinary no color');
}
final Color color = reader.readColor();


final options = BinaryOptions().optionsFromBinary(reader); // adjust this as per your needs

return CircleStroke(
color: color,
pressureEnabled: pressureEnabled,
options: options,
pageIndex: pageIndex,
page: page,
penType: (ShapePen).toString(),
center: Offset(cx,cy),
radius: r,
);

}

@override
bool get isEmpty => radius <= 0;
@override
Expand Down
84 changes: 84 additions & 0 deletions lib/components/canvas/_rectangle_stroke.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:flutter/material.dart';
import 'package:one_dollar_unistroke_recognizer/one_dollar_unistroke_recognizer.dart';
import 'package:perfect_freehand/perfect_freehand.dart';
import 'package:saber/components/canvas/_stroke.dart';
import 'package:saber/data/editor/binary_writer.dart';
import 'package:saber/data/editor/page.dart';
import 'package:saber/data/tools/shape_pen.dart';

Expand Down Expand Up @@ -72,6 +73,89 @@ class RectangleStroke extends Stroke {
}..addAll(options.toJson());
}

/// Serializes a Stroke object into a compact binary format.
@override
void toBinary(BinaryWriter writer) {
writer.writeString(StrokeBinaryKeys.shape,"rect");
writer.writeInt(StrokeBinaryKeys.pageIndex,pageIndex);
writer.writeScaledFloat(StrokeBinaryKeys.left, rect.left);
writer.writeScaledFloat(StrokeBinaryKeys.top, rect.top);
writer.writeScaledFloat(StrokeBinaryKeys.width, rect.width);
writer.writeScaledFloat(StrokeBinaryKeys.height, rect.height);
writer.writeBool(StrokeBinaryKeys.pressureEnabled,pressureEnabled);
writer.writeInt(StrokeBinaryKeys.color,color.toARGB32());
BinaryOptions().optionsToBinary(writer,options); // store stroke options
// TODO: Add serialization of StrokeOptions if needed
}

/// Deserializes a Stroke object from binary data starting at [offset].
factory RectangleStroke.fromBinary(BinaryReader reader,
{required HasSize page,
}){
int key;

key = reader.readKey();
if (key != StrokeBinaryKeys.pageIndex) {
throw Exception('StrokefromBinary no pageIndex');
}
final int pageIndex= reader.readIntNoKey();

key = reader.readKey();
if (key != StrokeBinaryKeys.left) {
throw Exception('StrokefromBinary no left');
}
final double left= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.top) {
throw Exception('StrokefromBinary no top');
}
final double top= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.width) {
throw Exception('StrokefromBinary no width');
}
final double width= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.height) {
throw Exception('StrokefromBinary no height');
}
final double height= reader.readScaledFloat();

key = reader.readKey();
if (key != StrokeBinaryKeys.pressureEnabled) {
throw Exception('StrokefromBinary no pressureEnabled');
}
final bool pressureEnabled = reader.readBoolNoKey();

key=reader.readKey();
if (key!=StrokeBinaryKeys.color) {
throw Exception('StrokefromBinary no color');
}
final Color color = reader.readColor();

final options = BinaryOptions().optionsFromBinary(reader); // adjust this as per your needs

return RectangleStroke(
color: color,
pressureEnabled: pressureEnabled,
options: options,
pageIndex: pageIndex,
page: page,
penType: (ShapePen).toString(),
rect: Rect.fromLTWH(
left,
top,
width,
height,
),
);

}


@override
bool get isEmpty => rect.isEmpty;
@override
Expand Down
Loading