Skip to content

Commit

Permalink
Fix scaling issues with text
Browse files Browse the repository at this point in the history
  • Loading branch information
twangodev committed Jul 24, 2024
1 parent 22179d2 commit a37578c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
3 changes: 1 addition & 2 deletions lib/widgets/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ class _EditorState extends State<Editor> {
toggleGrid: gridToggleState,
editingTool: activeEditingTool,
),
size: Size(constraints.maxWidth,
constraints.maxHeight),
size: Size(constraints.maxWidth, constraints.maxHeight),
);
});
})));
Expand Down
5 changes: 3 additions & 2 deletions lib/widgets/editor/editor_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import 'dart:ui';
import '../../models/editing_tool.dart';

class EditorConfig {
static const minScale = 1e-6;
static const maxScale = 1e6;
static const scaleFactor = 2000.0;
static const minScale = 1 / scaleFactor;
static const maxScale = scaleFactor;
static const frictionCoefficient =
0.0; // TODO 0.0 set for stability, any other positive value leads to funky issue with sliding/no feasible callback updating pointer (experiences on trackpad)

Expand Down
18 changes: 16 additions & 2 deletions lib/widgets/editor/editor_painter.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:collection';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:toolfoam/extensions/list_extensions.dart';
Expand Down Expand Up @@ -246,6 +247,10 @@ class EditorPainter extends CustomPainter {
Offset start = offset + perpendicular * startLength;
Offset end = offset + perpendicular * endLength;

if (!visibleRect.contains(start) && !visibleRect.contains(end)) {
return;
}

canvas.drawLine(start, end, distancePaint);
}

Expand All @@ -254,6 +259,10 @@ class EditorPainter extends CustomPainter {
..color = Colors.grey.shade500
..strokeWidth = 0.5 * scaleInverse;

if (!visibleRect.contains(head) && !visibleRect.contains(end)) {
return;
}

canvas.drawLine(head, end, linePaint);

final Paint arrowPaint = Paint()
Expand Down Expand Up @@ -292,15 +301,20 @@ class EditorPainter extends CustomPainter {
drawArrow(canvas, arrowStart, arrowMidpoint);
drawArrow(canvas, arrowEnd, arrowMidpoint);

final TextPainter textPainter = TextPainter(
if (!visibleRect.contains(arrowMidpoint)) {
return;
}

TextPainter textPainter = TextPainter(
text: TextSpan(
text: text,
style: TextStyle(
color: Colors.grey.shade500,
fontSize: 12 * scaleInverse,
fontSize: 12 ,
),
),
textDirection: TextDirection.ltr,
textScaler: TextScaler.linear(scaleInverse),
);

textPainter.layout();
Expand Down
4 changes: 4 additions & 0 deletions lib/widgets/editor/editor_painter_data.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:toolfoam/models/tools/tf_path_data.dart';
import 'package:toolfoam/widgets/editor/editor_config.dart';
import 'package:toolfoam/widgets/editor/editor_logic.dart';
Expand All @@ -10,6 +11,8 @@ class EditorData extends ChangeNotifier {
required this.toolData,
});

static final Logger logger = Logger('EditorData');

void redraw() {
notifyListeners();
}
Expand All @@ -19,6 +22,7 @@ class EditorData extends ChangeNotifier {
double get scale => _scale;
set scale(double value) {
if (value == _scale) return;
logger.finest('Scaling update: $value, inverse: ${1 / value}, old: $_scale, ratio: ${value / _scale}');
_scale = value;
notifyListeners();
}
Expand Down

0 comments on commit a37578c

Please sign in to comment.