From fe05ae7d1102d8bfa441dbab76773a25fd9fea28 Mon Sep 17 00:00:00 2001 From: Artemii Oliinyk Date: Wed, 21 Jun 2023 10:59:27 -0400 Subject: [PATCH] Change rotation center --- example/lib/main.dart | 24 +++++++++---------- lib/src/edit_map.dart | 2 -- .../src/builders/graphic/desk_builder.dart | 2 +- lib/src/src/models/graphic/base_graphic.dart | 4 +++- lib/src/src/models/graphic/desk/desk.dart | 4 ++-- lib/src/src/models/graphic/office_map.dart | 14 ----------- 6 files changed, 18 insertions(+), 32 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index d9e6f72..c8f7c59 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -76,7 +76,6 @@ class _MyHomeView extends StatefulWidget { } class _MyHomeViewState extends State<_MyHomeView> { - bool isDraggableDeskShown = false; List desks = []; DeskPayload? lastModifiedObject; @@ -103,18 +102,19 @@ class _MyHomeViewState extends State<_MyHomeView> { ) : null, actions: [ - InkWell( - onTap: () { - if (lastModifiedObject != null) { - desks = desks.toList()..add(lastModifiedObject!); - lastModifiedObject = null; - setState(() {}); - } - }, - child: const Center( - child: Text('Save'), + if (lastModifiedObject != null) + InkWell( + onTap: () { + if (lastModifiedObject != null) { + desks = desks.toList()..add(lastModifiedObject!); + lastModifiedObject = null; + setState(() {}); + } + }, + child: const Center( + child: Text('Save'), + ), ), - ), InkWell( onTap: () { lastModifiedObject = diff --git a/lib/src/edit_map.dart b/lib/src/edit_map.dart index a07726d..5ecef77 100644 --- a/lib/src/edit_map.dart +++ b/lib/src/edit_map.dart @@ -337,8 +337,6 @@ class _DraggableDeskWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Transform.rotate( - origin: Offset(-commonDeskSize.width * officeMap!.ratio / 2, - -commonDeskSize.height * officeMap!.ratio / 2), angle: rotation * math.pi / 180, child: Container( padding: const EdgeInsets.all(_draggableDeskPadding), diff --git a/lib/src/src/builders/graphic/desk_builder.dart b/lib/src/src/builders/graphic/desk_builder.dart index f1a382f..2575c6f 100644 --- a/lib/src/src/builders/graphic/desk_builder.dart +++ b/lib/src/src/builders/graphic/desk_builder.dart @@ -20,7 +20,7 @@ class DeskBuilder { params.area.position!.dy, rotation: params.area.rotation, stateType: StateType.main, - size: commonTabletopSize, + size: commonDeskSize, states: Params( params.isBooked ? bookedDeskState : defaultDeskState, activeDeskState, diff --git a/lib/src/src/models/graphic/base_graphic.dart b/lib/src/src/models/graphic/base_graphic.dart index abdfa5f..c8e5649 100644 --- a/lib/src/src/models/graphic/base_graphic.dart +++ b/lib/src/src/models/graphic/base_graphic.dart @@ -28,9 +28,11 @@ abstract class BaseGraphic implements ObjectInterface { Offset(topLeft.dx, topLeft.dy + commonDeskSize.height), ]; + final center = determinateRectCenter(shape); + return rotation == 0 ? shape - : shape.map((e) => rotatePoint(e, shape.first, rotation)).toList(); + : shape.map((e) => rotatePoint(e, center, rotation)).toList(); } Size getSize() { diff --git a/lib/src/src/models/graphic/desk/desk.dart b/lib/src/src/models/graphic/desk/desk.dart index 1a996b2..a5df3f8 100644 --- a/lib/src/src/models/graphic/desk/desk.dart +++ b/lib/src/src/models/graphic/desk/desk.dart @@ -46,9 +46,9 @@ class Desk extends BaseGraphic { void draw(Canvas canvas) { canvas.save(); - canvas.translate(x, y); + canvas.translate(x + (size.width / 2), y + (size.height / 2)); canvas.rotate(rotation * math.pi / 180); - canvas.translate(-(x), -(y)); + canvas.translate(-(x + (size.width / 2)), -(y + (size.height / 2))); final DeskState state = stateType == StateType.active ? states.active : states.main; diff --git a/lib/src/src/models/graphic/office_map.dart b/lib/src/src/models/graphic/office_map.dart index 25b11e2..3fdf8fc 100644 --- a/lib/src/src/models/graphic/office_map.dart +++ b/lib/src/src/models/graphic/office_map.dart @@ -103,20 +103,6 @@ class OfficeMap extends Object with IterableMixin { // the center of the board in both coordinate systems. If no BoardPoint at the // location, return null. BaseGraphic? determinateSelectedObject(Offset offset) { - // final pointCentered = Offset( - // point.dx - size.width / 2, - // point.dy - size.height / 2, - // ); - // final boardPoint = ObjectInterface( - // ((sqrt(3) / 3 * pointCentered.dx - 1 / 3 * pointCentered.dy) / - // hexagonRadius) - // .round(), - // ((2 / 3 * pointCentered.dy) / hexagonRadius).round(), - // ); - - // if (!_validateBoardPoint(boardPoint)) { - // return null; - // } Offset point = Offset(offset.dx, offset.dy); BaseGraphic? selectedGraphic; for (final object in _mapObjects) {