Skip to content

Commit

Permalink
Add offset to grid, add docs pages
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Jan 1, 2025
1 parent 8a59547 commit 887cfa9
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 15 deletions.
2 changes: 2 additions & 0 deletions api/lib/src/models/tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ sealed class Tool with _$Tool {
@Default(SRGBColor.black) @ColorJsonConverter() SRGBColor color,
@Default(20) double xSize,
@Default(20) double ySize,
@Default(0) double xOffset,
@Default(0) double yOffset,
}) = GridTool;

factory Tool.eyeDropper({
Expand Down
30 changes: 27 additions & 3 deletions api/lib/src/models/tool.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2933,7 +2933,9 @@ abstract class _$$GridToolImplCopyWith<$Res> implements $ToolCopyWith<$Res> {
String displayIcon,
@ColorJsonConverter() SRGBColor color,
double xSize,
double ySize});
double ySize,
double xOffset,
double yOffset});
}

/// @nodoc
Expand All @@ -2954,6 +2956,8 @@ class __$$GridToolImplCopyWithImpl<$Res>
Object? color = null,
Object? xSize = null,
Object? ySize = null,
Object? xOffset = null,
Object? yOffset = null,
}) {
return _then(_$GridToolImpl(
name: null == name
Expand All @@ -2976,6 +2980,14 @@ class __$$GridToolImplCopyWithImpl<$Res>
? _value.ySize
: ySize // ignore: cast_nullable_to_non_nullable
as double,
xOffset: null == xOffset
? _value.xOffset
: xOffset // ignore: cast_nullable_to_non_nullable
as double,
yOffset: null == yOffset
? _value.yOffset
: yOffset // ignore: cast_nullable_to_non_nullable
as double,
));
}
}
Expand All @@ -2989,6 +3001,8 @@ class _$GridToolImpl extends GridTool {
@ColorJsonConverter() this.color = SRGBColor.black,
this.xSize = 20,
this.ySize = 20,
this.xOffset = 0,
this.yOffset = 0,
final String? $type})
: $type = $type ?? 'grid',
super._();
Expand All @@ -3012,13 +3026,19 @@ class _$GridToolImpl extends GridTool {
@override
@JsonKey()
final double ySize;
@override
@JsonKey()
final double xOffset;
@override
@JsonKey()
final double yOffset;

@JsonKey(name: 'type')
final String $type;

@override
String toString() {
return 'Tool.grid(name: $name, displayIcon: $displayIcon, color: $color, xSize: $xSize, ySize: $ySize)';
return 'Tool.grid(name: $name, displayIcon: $displayIcon, color: $color, xSize: $xSize, ySize: $ySize, xOffset: $xOffset, yOffset: $yOffset)';
}

/// Create a copy of Tool
Expand All @@ -3043,7 +3063,9 @@ abstract class GridTool extends Tool {
final String displayIcon,
@ColorJsonConverter() final SRGBColor color,
final double xSize,
final double ySize}) = _$GridToolImpl;
final double ySize,
final double xOffset,
final double yOffset}) = _$GridToolImpl;
GridTool._() : super._();

factory GridTool.fromJson(Map<String, dynamic> json) =
Expand All @@ -3057,6 +3079,8 @@ abstract class GridTool extends Tool {
SRGBColor get color;
double get xSize;
double get ySize;
double get xOffset;
double get yOffset;

/// Create a copy of Tool
/// with the given fields replaced by the non-null parameter values.
Expand Down
4 changes: 4 additions & 0 deletions api/lib/src/models/tool.g.dart

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

15 changes: 9 additions & 6 deletions app/lib/handlers/grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ class GridHandler extends Handler<GridTool> with PointerManipulationHandler {

@override
Offset getPointerPosition(Offset position, Size viewportSize) {
return Offset(
(position.dx / data.xSize).round() * data.xSize,
(position.dy / data.ySize).round() * data.ySize,
);
final xSize = data.xSize;
final ySize = data.ySize;
final xOffset = data.xOffset;
final yOffset = data.yOffset;
final x = (position.dx - xOffset) / xSize;
final y = (position.dy - yOffset) / ySize;
return Offset(x.round() * xSize + xOffset, y.round() * ySize + yOffset);
}
}

Expand All @@ -31,7 +34,7 @@ class GridRenderer extends Renderer<GridTool> {
DocumentInfo info, CameraTransform transform,
[ColorScheme? colorScheme, bool foreground = false]) {
if (element.xSize > 0) {
double x = 0;
double x = -element.xSize + element.xOffset % element.xSize;
while (x < size.width) {
final localX = x / transform.size;
canvas.drawLine(
Expand All @@ -46,7 +49,7 @@ class GridRenderer extends Renderer<GridTool> {
}
}
if (element.ySize > 0) {
double y = 0;
double y = -element.ySize + element.yOffset % element.ySize;
while (y < size.height) {
final localY = y / transform.size;
canvas.drawLine(
Expand Down
3 changes: 2 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -636,5 +636,6 @@
"confirmPassword": "Confirm password",
"passwordMismatch": "The passwords do not match",
"action": "Action",
"svgText": "SVG Text"
"svgText": "SVG Text",
"offset": "Offset"
}
1 change: 1 addition & 0 deletions app/lib/selections/properties/pen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class PenPropertySelection extends PropertySelection<PenProperty>
property.copyWith(color: value.withValues(a: property.color.a))),
title: Text(LeapLocalizations.of(context).color),
),
const SizedBox(height: 4),
ExactSlider(
value: property.color.a.toDouble(),
header: Text(AppLocalizations.of(context).alpha),
Expand Down
11 changes: 11 additions & 0 deletions app/lib/selections/tools/grid.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ class GridToolSelection extends ToolSelection<GridTool> {
),
),
const SizedBox(height: 8),
OffsetPropertyView(
title: Text(AppLocalizations.of(context).offset),
value: Offset(selected.first.xOffset, selected.first.yOffset),
onChanged: (value) => update(
context,
selected
.map((e) => e.copyWith(xOffset: value.dx, yOffset: value.dy))
.toList(),
),
),
const SizedBox(height: 8),
ColorField(
value: selected.first.color,
onChanged: (value) => update(
Expand Down
12 changes: 12 additions & 0 deletions docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export default defineConfig({
...getSidebarTranslatedLabel("Add"),
link: "/docs/v2/add/",
},
{
...getSidebarTranslatedLabel("Utilities"),
link: "/docs/v2/utilities/",
},
{
...getSidebarTranslatedLabel("Collaboration"),
link: "/docs/v2/collaboration/",
Expand Down Expand Up @@ -169,6 +173,14 @@ export default defineConfig({
...getSidebarTranslatedLabel("Presentation"),
link: "/docs/v2/tools/presentation/",
},
{
...getSidebarTranslatedLabel("Ruler"),
link: "/docs/v2/tools/ruler/",
},
{
...getSidebarTranslatedLabel("Grid"),
link: "/docs/v2/tools/grid/",
},
],
},
],
Expand Down
8 changes: 4 additions & 4 deletions docs/pnpm-lock.yaml

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

21 changes: 21 additions & 0 deletions docs/src/content/docs/docs/v2/tools/grid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: Grid tool
---

:::note[🔘 Toggleable tool]

This is a special tool.
You can't select it and it gets toggled if you click on it.

:::

With this tool you can show a grid over the canvas.
Inputs get snapped to the grid.

## Configuration

| Property | Default | Description |
| -------: | :------: | :--------------------- |
| Size | (20, 20) | The size of grid cells |
| Offset | (0, 0) | The offset of the grid |
| Color | Black | The color of the grid |
20 changes: 20 additions & 0 deletions docs/src/content/docs/docs/v2/tools/ruler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
title: Ruler tool
---

:::note[🔘 Toggleable tool]

This is a special tool.
You can't select it and it gets toggled if you click on it.

:::

With this tool you can show a ruler over the canvas.
Inputs get snapped to the ruler.

## Configuration

| Property | Default | Description |
| -------: | :-------: | :----------------------------------------------------------- |
| Size | 100 | The size of the ruler |
| Color | *Not set* | The color of the ruler. If not set, it uses the theme color. |
10 changes: 10 additions & 0 deletions docs/src/content/docs/docs/v2/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Here are all page specific properties. Currently you can find the background set

## View

:::note

This tab was removed in the nightly release.
Please visit the tools for more information:

- [Grid](/docs/v2/tools/grid)
- [Ruler](/docs/v2/tools/ruler)

:::

### Grid

The grid allows you to exactly position elements. You can use it to move elements or create shapes.
Expand Down
5 changes: 4 additions & 1 deletion docs/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@
"faq": "FAQ",
"versions": "Versions",
"nightly": "Nightly",
"privacy_policy": "Privacy policy"
"privacy_policy": "Privacy policy",
"ruler": "Ruler",
"grid": "Grid",
"utilities": "Utilities"
}
2 changes: 2 additions & 0 deletions metadata/en-US/changelogs/127.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
* Add toggleable tools
* Move ruler into own tool
* Add color property
* Add size property
* Move grid into own tool
* Add offset property
* Add password protected notes ([#771](https://github.com/LinwoodDev/Butterfly/issues/771))
* Add option to import svg as text ([#596](https://github.com/LinwoodDev/Butterfly/issues/596))
* Fix undo/redo tools not showing status correctly
Expand Down

0 comments on commit 887cfa9

Please sign in to comment.