Skip to content

Commit

Permalink
Sync
Browse files Browse the repository at this point in the history
  • Loading branch information
twangodev committed Aug 17, 2024
1 parent c5ea05f commit 274fccf
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 42 deletions.
21 changes: 18 additions & 3 deletions lib/extensions/canvas_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ extension CanvasExtension on Canvas {

Path path = Path();
path.moveTo(a.x, a.y);
FixedPoint lastOut = a + spline.aRelativeOut;
FixedPoint aOut = a + spline.aRelativeOut;
FixedPoint lastOut = aOut;

topLayer.add(() {
drawPoint(a, scaleInverse);

drawPoint(aOut, scaleInverse);
drawLine(a.toOffset(), aOut.toOffset(), tangentHandleLinePaint);
});

for (int i = 0; i < spline.controlPoints.length; i++) {
ControlPoint current = spline.controlPoints[i];
Expand Down Expand Up @@ -66,10 +74,17 @@ extension CanvasExtension on Canvas {

path.cubicTo(lastOut.x, lastOut.y, bIn.x, bIn.y, b.x, b.y);

topLayer.add(() {
drawPoint(b, scaleInverse);

drawPoint(bIn, scaleInverse);
drawLine(b.toOffset(), bIn.toOffset(), tangentHandleLinePaint);
});

drawPath(path, splinePaint);

for (VoidCallback topLayerAction in topLayer) {
topLayerAction();
for (VoidCallback action in topLayer) {
action.call();
}
}
}
2 changes: 2 additions & 0 deletions lib/geometry/curve.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import 'package:toolfoam/models/tf_id.dart';
abstract class Curve implements JsonSerializable {
abstract TfId a;
abstract TfId b;

bool contains(TfId id) => id == a || id == b;
}
11 changes: 3 additions & 8 deletions lib/geometry/fit_point_spline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,9 @@ class FitPointSpline extends Curve {
return FitPointSpline(aId, aPoint - mid, bId, bPoint - mid, []);
}

List<ControlPoint> controlPoints =
ControlPoint.autoTangent(points, aPoint, bPoint);
FixedPoint aRelativeOut =
TangentHandle.autoNeighbours(aPoint, controlPoints.first)
.relativeInTangent;
FixedPoint bRelativeIn =
TangentHandle.autoNeighbours(controlPoints.last, bPoint)
.relativeOutTangent;
List<ControlPoint> controlPoints = ControlPoint.autoTangent(points, aPoint, bPoint);
FixedPoint aRelativeOut = TangentHandle.autoNeighbours(aPoint, controlPoints.first).relativeInTangent;
FixedPoint bRelativeIn = TangentHandle.autoNeighbours(controlPoints.last, bPoint).relativeOutTangent;

return FitPointSpline(aId, aRelativeOut, bId, bRelativeIn, controlPoints);
}
Expand Down
2 changes: 0 additions & 2 deletions lib/geometry/segment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ class Segment extends Curve {

Segment(this.a, this.b);

bool contains(TfId id) => id == a || id == b;

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
Expand Down
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:toolfoam/pages/home_page.dart';
void main() async {
Logger.root.level = kDebugMode ? Level.ALL : Level.INFO;
Logger.root.onRecord.listen((record) {
log(record.message,
log('[${record.level.name}] ${record.message}',
level: record.level.value,
name: record.loggerName,
time: record.time,
Expand All @@ -21,7 +21,7 @@ void main() async {
stackTrace: record.stackTrace);
});

Logger.root.info('Starting Toolfoam');
Logger('toolfoam.main').info('Starting Toolfoam');

runApp(const Toolfoam());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/models/line_data.dart → lib/models/segment_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:toolfoam/geometry/segment.dart';
import 'package:toolfoam/models/identifiable_json_map.dart';
import 'package:toolfoam/models/tf_id.dart';

class LineData extends IdentifiableJsonHashMap<Segment> {
class SegmentData extends IdentifiableJsonHashMap<Segment> {
Iterable<TfId> dependsOn(TfId id) {
return map.entries
.where((entry) => entry.value.contains(id))
Expand Down
11 changes: 10 additions & 1 deletion lib/models/tf_tool.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import 'json_serializable.dart';
import 'tf_collection.dart';

class TfTool extends DiskIOEntity implements JsonSerializable {
static final Logger logger = Logger('TfTool');
static final Logger staticLogger = Logger('toolfoam.models.tf_tool');
late final Logger logger = Logger('toolfoam.models.tf_tool.${id.toString()}');

TfToolMetadata metadata = TfToolMetadata.empty();
TfCollection owner;
Expand All @@ -31,13 +32,15 @@ class TfTool extends DiskIOEntity implements JsonSerializable {
Map<String, dynamic> json = jsonDecode(rawJson);
String name = p.basenameWithoutExtension(file.path);
TfId id = TfId(name);
staticLogger.finer('Loaded tool $id from file');
return TfTool.fromJson(json, id, owner);
}

static Future<List<TfTool>> fromFiles(
List<File> files, TfCollection owner) async {
List<Future<TfTool>> futures =
files.map((file) => fromFile(file, owner)).toList();
staticLogger.finer('Loading ${futures.length} tools from files');
return await Future.wait(futures);
}

Expand Down Expand Up @@ -65,6 +68,7 @@ class TfTool extends DiskIOEntity implements JsonSerializable {
}

void rename(String name) {
logger.finer('Renaming tool to $name');
metadata.name = name;
}

Expand All @@ -76,6 +80,7 @@ class TfTool extends DiskIOEntity implements JsonSerializable {
TfTool diskTool = TfTool.fromJson(jsonMap, id, owner);

copy(diskTool);
logger.finer('Pulled tool $id from file');
}

@override
Expand All @@ -84,17 +89,21 @@ class TfTool extends DiskIOEntity implements JsonSerializable {
String json = jsonEncode(toJson());
await StorageFileSystemUtil.writeToFile(file, json);
await owner.lastChangedNow();
logger.finer('Pushed tool $id to file');
}

@override
Future<void> create(String name) async {
(await _getFile()).create();
metadata = TfToolMetadata.name(name);
await push();
logger.finer('Created tool $id');
}

@override
Future<void> delete() async {
(await _getFile()).delete();
await owner.lastChangedNow();
logger.finer('Deleted tool $id');
}
}
33 changes: 31 additions & 2 deletions lib/models/tf_tool_data.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
import 'dart:collection';

import 'package:logging/logging.dart';
import 'package:toolfoam/models/fixed_point_data.dart';
import 'package:toolfoam/models/line_data.dart';
import 'package:toolfoam/models/segment_data.dart';
import 'package:toolfoam/models/tf_id.dart';

import '../geometry/fit_point_spline.dart';
import '../geometry/segment.dart';
import 'fit_point_spline_data.dart';
import 'json_serializable.dart';

class TfToolData implements JsonSerializable {
TfToolData();

final logger = Logger('toolfoam.models.tf_tool_data');

final FixedPointData fixedPoints = FixedPointData();
final LineData segments = LineData();
final SegmentData segments = SegmentData();
final FitPointSplineData fitPointSplines = FitPointSplineData();

// TODO get to the point where theres a intermediately unsaved state layer, so that optimize is no longer needed to catch strays, because this is "relatively" expensive
void optimize() {
HashSet<TfId> fixedPointsToRemove = HashSet();
fixedPointsToRemove.addAll(fixedPoints.ids);

for (Segment segment in segments.values) {
fixedPointsToRemove.remove(segment.a);
fixedPointsToRemove.remove(segment.b);
}

for (FitPointSpline fitPointSpline in fitPointSplines.values) {
fixedPointsToRemove.remove(fitPointSpline.a);
fixedPointsToRemove.remove(fitPointSpline.b);
}

logger.fine('Found ${fixedPointsToRemove.length} stray points');
for (TfId id in fixedPointsToRemove) {
fixedPoints.remove(id);
}
}

TfToolData.fromJson(Map<String, dynamic> json) {
throw UnimplementedError();
}
Expand Down
1 change: 0 additions & 1 deletion lib/pages/collections_page.dart

This file was deleted.

8 changes: 8 additions & 0 deletions lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:toolfoam/pages/layouts_page.dart';
import 'package:toolfoam/pages/tools_page.dart';
import 'package:toolfoam/widgets/breadcrumb.dart';
Expand All @@ -15,6 +16,8 @@ class HomePage extends StatefulWidget {
}

class _HomePageState extends State<HomePage> {
final Logger logger = Logger('toolfoam.pages.home_page');

final _pageController = PageController();
final TextBreadcrumbItem _headerBreadcrumbItem =
const TextBreadcrumbItem(text: 'Toolfoam');
Expand All @@ -25,6 +28,7 @@ class _HomePageState extends State<HomePage> {
List<BreadcrumbItem> breadcrumbItems = [];

_onCollectionSelected(TfCollection? collection) {
logger.finer('Collection selected: ${collection?.id}');
setState(() {
selectedCollection = collection;
});
Expand Down Expand Up @@ -53,6 +57,9 @@ class _HomePageState extends State<HomePage> {
items.add(TextBreadcrumbItem(text: 'Layouts', onTap: () {}));
}

logger.finest(
'Breadcrumb items: ${items.map((item) => item.text).join(' > ')}');

return items;
}

Expand Down Expand Up @@ -108,6 +115,7 @@ class _HomePageState extends State<HomePage> {
}

void syncCollectionDetails() async {
logger.finer('Syncing collection details');
String? updatedName = await selectedCollection?.getName();

setState(() {
Expand Down
4 changes: 4 additions & 0 deletions lib/pages/tool_editor_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:toolfoam/models/tf_tool.dart';
import 'package:toolfoam/widgets/breadcrumb.dart';
import 'package:toolfoam/widgets/editor/editor.dart';
Expand All @@ -13,9 +14,12 @@ class ToolEditorPage extends StatefulWidget {
}

class _ToolEditorPageState extends State<ToolEditorPage> {
final Logger logger = Logger('toolfoam.pages.tool_editor_page');

String? name;

void onRename(String newName) {
logger.fine('Renaming tool to $newName');
setState(() {
name = newName;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/tools_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ToolsPage extends StatefulWidget {
}

class _ToolsPageState extends State<ToolsPage> {
static final Logger logger = Logger('ToolsPage');
static final Logger logger = Logger('toolfoam.pages.tools_page');

List<TfTool> tools = [];

Expand Down
4 changes: 2 additions & 2 deletions lib/utilities/storage_file_system_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

class StorageFileSystemUtil {
static final Logger logger = Logger('StorageFileSystemUtil');
static final Logger logger = Logger('toolfoam.utilities.storage_file_system');

static Future<Directory> getStorage() async {
Directory dir = await getApplicationSupportDirectory();
logger.finest('Application Storage directory requested: ${dir.path}');
logger.finest('Storage directory: ${dir.path}');
return dir;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/dialogs/collection_manager_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class CollectionCard extends StatefulWidget {
}

class _CollectionCardState extends State<CollectionCard> {
Logger log = Logger('CollectionCard');
Logger log = Logger('toolfoam.widgets.dialogs.collection_card');
String? name;
bool hasSynced = false;
bool starred = false;
Expand Down
29 changes: 19 additions & 10 deletions lib/widgets/editor/editor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:toolfoam/models/tf_tool.dart';
import 'package:toolfoam/widgets/editor/editor_config.dart';
import 'package:toolfoam/widgets/editor/editor_interactive_viewer.dart';
import 'package:toolfoam/widgets/editor/editor_painter.dart';
import 'package:toolfoam/widgets/editor/editor_painter_data.dart';
import 'package:toolfoam/widgets/editor/editor_data.dart';
import 'package:toolfoam/widgets/editor/editor_toolbar.dart';
import 'package:vector_math/vector_math_64.dart';

Expand All @@ -26,7 +26,7 @@ class Editor extends StatefulWidget {
}

class _EditorState extends State<Editor> {
static final logger = Logger('TFEditorState');
static final logger = Logger('toolfoam.widgets.editor');

final transformationController = TransformationController();

Expand All @@ -42,17 +42,23 @@ class _EditorState extends State<Editor> {
toolData: widget.tool.data, gridToggleState: () => gridToggleState);

void toggleGrid(bool newState) {
logger.finer('Toggling grid to $newState');
setState(() {
gridToggleState = newState;
});
}

void setTool(EditingTool tool) {
data.actionPointerStack.clear();
logger.finer('Setting tool to ${tool.tooltip}');
logger.finer(
'Clearing current contents of action stack: ${data.pointStack}');
data.pointStack.clear();
setState(() {
activeEditingTool = tool;
allowPrimaryMouseButtonPan = tool == EditingTool.pan;
});
logger.finer('Optimizing tool data');
data.toolData.optimize();
}

void updatePointer(PointerEvent event) {
Expand Down Expand Up @@ -102,19 +108,18 @@ class _EditorState extends State<Editor> {
if (activeEditingTool == EditingTool.line) {
if (data.shouldConfirm(scenePointer)) {
logger.finer('Confirming line, clearing action stack');
data.actionPointerStack.clear();
data.pointStack.clear();
data.redraw();
return;
}

TfToolData toolData = data.toolData;
TfId pointId =
toolData.fixedPoints.add(FixedPoint.fromOffset(effectivePointer));
data.actionPointerStack.add(pointId);
TfId pointId = toolData.fixedPoints.add(FixedPoint.fromOffset(effectivePointer));
data.pointStack.add(pointId);

if (data.actionPointerStack.length >= 2) {
TfId start = data.actionPointerStack.secondLast;
TfId end = data.actionPointerStack.last;
if (data.pointStack.length >= 2) {
TfId start = data.pointStack.secondLast;
TfId end = data.pointStack.last;

Segment line = Segment(start, end);
toolData.segments.add(line);
Expand All @@ -123,6 +128,10 @@ class _EditorState extends State<Editor> {
data.redraw();
return;
}

if (activeEditingTool == EditingTool.line) {

}
}

void onPointerUp(PointerUpEvent event) {
Expand Down
Loading

0 comments on commit 274fccf

Please sign in to comment.