Skip to content

Home Information - Your home, your data, organized.

Python

Finished rotate and scale edit operations.

Anthony Cassandra3ea4996a2f48
diff --git a/src/hi/apps/location/views.py b/src/hi/apps/location/views.py
index 8f0107ad..4edad260 100644
--- a/src/hi/apps/location/views.py
+++ b/src/hi/apps/location/views.py
@@ -4,6 +4,7 @@ from django.http import HttpResponseRedirect
from django.urls import reverse
from django.views.generic import View
+import hi.apps.common.antinode as antinode
from hi.apps.common.utils import is_ajax
from hi.hi_grid_view import HiGridView
@@ -57,6 +58,15 @@ class LocationViewView( HiGridView ):
request.view_parameters.location_view_id = location_view.id
request.view_parameters.to_session( request )
+ # When in edit mode, a location view change needs a full
+ # synchronous page load to ensure any front-end editing state and
+ # views are invalidated. Else, the editing state and edit side
+ # panel will be invalid for the new view.
+ #
+ if is_ajax( request ) and request.view_parameters.edit_mode.should_reload_on_location_view_change:
+ sync_url = reverse( 'location_view', kwargs = kwargs )
+ return antinode.redirect_response( url = sync_url )
+
location_view_data = LocationViewManager().get_location_view_data(
location_view = location_view,
)
diff --git a/src/hi/enums.py b/src/hi/enums.py
index 31d1b7ac..4c474687 100644
--- a/src/hi/enums.py
+++ b/src/hi/enums.py
@@ -9,3 +9,8 @@ class EditMode(LabeledEnum):
@property
def is_editing(self):
return bool( self != EditMode.OFF )
+
+ @property
+ def should_reload_on_location_view_change(self):
+ return bool( self != EditMode.OFF )
+
diff --git a/src/hi/static/js/edit.js b/src/hi/static/js/edit.js
index 35f5eb4a..512741bb 100644
--- a/src/hi/static/js/edit.js
+++ b/src/hi/static/js/edit.js
@@ -323,60 +323,10 @@
gDragData = null;
}
- function getSvgTransformValues(transform) {
- let scale = { x: 1, y: 1 }, rotate = { angle: 0, cx: 0, cy: 0 }, translate = { x: 0, y: 0 };
-
- let scaleMatch = transform.match(/scale\(([^)]+)\)/);
- if (scaleMatch) {
- let scaleValues = scaleMatch[1].trim().split(/[ ,]+/).map(parseFloat);
- scale.x = scaleValues[0];
- if ( scaleValues.length == 1 ) {
- scale.y = scale.x;
- } else {
- scale.y = scaleValues[1];
- }
- }
-
- let translateMatch = transform.match(/translate\(([^)]+)\)/);
- if (translateMatch) {
- let [x, y] = translateMatch[1].trim().split(/[ ,]+/).map(parseFloat);
- translate.x = x;
- translate.y = y;
- }
-
- let rotateMatch = transform.match(/rotate\(([^)]+)\)/);
- if (rotateMatch) {
- let rotateValues = rotateMatch[1].trim().split(/[ ,]+/).map(parseFloat);
- rotate.angle = rotateValues[0];
-
- // Check if cx and cy are provided
- if (rotateValues.length === 3) {
- rotate.cx = rotateValues[1];
- rotate.cy = rotateValues[2];
- } else {
- rotate.cx = 0;
- rotate.cy = 0;
- }
- }
-
- if ( DEBUG ) {
- console.log( `TRANSFORM:
- Raw: ${transform},
- Parsed: scale=${JSON.stringify(scale)} translate=${JSON.stringify(translate)} rotate=${JSON.stringify(rotate)}` );
- }
-
- return { scale, translate, rotate };
- }
-
- function setSvgTransformAttr( element, scale, translate, rotate ) {
- let newTransform = `scale(${scale.x} ${scale.y}) translate(${translate.x}, ${translate.y}) rotate(${rotate.angle}, ${rotate.cx}, ${rotate.cy})`;
- element.attr('transform', newTransform);
- }
-
function saveSvgPosition( element ) {
let transform = element.attr('transform');
- let { scale, translate, rotate } = getTransformValues( transform );
+ let { scale, translate, rotate } = getSvgTransformValues( transform );
const baseSvgElement = $(baseSvgSelector);
const center = getSvgCenterPoint( element, baseSvgElement );
@@ -394,8 +344,8 @@
function createEditActionData( actionState ) {
if ( gSelectedElement ) {
let transform = gSelectedElement.attr('transform');
- let { scale, translate, rotate } = getTransformValues( transform );
-
+ let { scale, translate, rotate } = getSvgTransformValues( transform );
+
gEditActionData = {
element: gSelectedElement,
scaleStart: scale,
@@ -424,50 +374,46 @@
createEditActionData( EditActionState.SCALE );
}
- function actionRotateStart() {
- createEditActionData( EditActionState.ROTATE );
- }
-
function actionScaleUpdate( currentMousePosition ) {
- console.log( 'Scale Update' );
- const deltaX = currentMousePosition.x - gLastMousePosition.x;
- const deltaY = currentMousePosition.y - gLastMousePosition.y;
- if (( deltaX <= CursorMovementThreshold ) && ( deltaY <= CursorMovementThreshold )) {
- return;
- }
+
+ let center = getScreenCenterPoint( gEditActionData.element );
+
+ let scaleFactor = getScaleFactor( center.x, center.y,
+ gLastMousePosition.x, gLastMousePosition.y,
+ currentMousePosition.x, currentMousePosition.y );
let transform = gEditActionData.element.attr('transform');
- console.log( `T = ${transform}` );
- let { scale, translate, rotate } = getTransformValues( transform );
- console.log( `SCALE = ${scale}, T = ${translate}, R = ${rotate}` );
+ let { scale, translate, rotate } = getSvgTransformValues( transform );
+
const newScale = {
- x: scale.x + 0.01 * deltaX,
- y: scale.x + 0.01 * deltaX
+ x: scale.x * scaleFactor,
+ y: scale.y * scaleFactor
};
-
+
translate.x = translate.x * scale.x / newScale.x;
- translate.y = translate.y * scale.x / newScale.x;
+ translate.y = translate.y * scale.y / newScale.y;
+
+ if ( DEBUG ) {
+ console.log( `Scale Update:
+ Transform: ${transform}
+ Scale = ${scale.x}, T = ${translate.x}, R = ${rotate.angle}` );
+ }
setSvgTransformAttr( gEditActionData.element, newScale, translate, rotate );
+
}
-
- function actionRotateUpdate( currentMousePosition ) {
- const deltaX = currentMousePosition.x - gLastMousePosition.x;
- const deltaY = currentMousePosition.y - gLastMousePosition.y;
- if (( deltaX <= CursorMovementThreshold )
- && ( deltaY <= CursorMovementThreshold )) {
- return;
- }
- center = getScreenCenterPoint( gEditActionData.element );
-
-
- const rotateDelta = {
- angle: deltaX
- };
- let transform = gEditActionData.element.attr('transform');
- let { scale, translate, rotate } = getTransformValues( transform );
- rotate.angle += deltaX;
- setSvgTransformAttr( gEditActionData.element, scale, translate, rotate );
+ function getScaleFactor( centerX, centerY, startX, startY, endX, endY ) {
+
+ const startDistance = Math.sqrt( Math.pow(startX - centerX, 2) + Math.pow(startY - centerY, 2) );
+ const endDistance = Math.sqrt( Math.pow(endX - centerX, 2) + Math.pow(endY - centerY, 2) );
+
+ let scaleFactor = 1;
+ if (endDistance > startDistance) {
+ scaleFactor = 1 + (endDistance - startDistance) / 100;
+ } else if (endDistance < startDistance) {
+ scaleFactor = 1 - (startDistance - endDistance) / 100;
+ }
+ return scaleFactor;
}
function actionScaleApply() {
@@ -475,10 +421,6 @@
saveSvgPosition( gEditActionData.element );
}
- function actionRotateApply() {
- saveSvgPosition( gEditActionData.element );
- }
-
function actionScaleAbort() {
if ( gEditActionState != EditActionState.SCALE ) {
return;
@@ -486,6 +428,56 @@
revertAction();
}
+ function actionRotateStart() {
+ createEditActionData( EditActionState.ROTATE );
+ }
+
+ function actionRotateUpdate( currentMousePosition ) {
+ if ( DEBUG ) { console.log( 'Rotate Update' ); }
+
+ center = getScreenCenterPoint( gEditActionData.element );
+
+ let deltaAngle = getRotationAngle( center.x, center.y,
+ gLastMousePosition.x, gLastMousePosition.y,
+ currentMousePosition.x, currentMousePosition.y );
+
+ let transform = gEditActionData.element.attr('transform');
+ let { scale, translate, rotate } = getSvgTransformValues( transform );
+ rotate.angle += deltaAngle;
+ setSvgTransformAttr( gEditActionData.element, scale, translate, rotate );
+ }
+
+ function getRotationAngle( centerX, centerY, startX, startY, endX, endY ) {
+
+ const startVectorX = startX - centerX;
+ const startVectorY = startY - centerY;
+
+ const endVectorX = endX - centerX;
+ const endVectorY = endY - centerY;
+
+ const startAngle = Math.atan2(startVectorY, startVectorX);
+ const endAngle = Math.atan2(endVectorY, endVectorX);
+
+ let angleDifference = endAngle - startAngle;
+
+ // Normalize the angle to be between -π and π
+ if (angleDifference > Math.PI) {
+ angleDifference -= 2 * Math.PI;
+ } else if (angleDifference < -Math.PI) {
+ angleDifference += 2 * Math.PI;
+ }
+
+ // Convert the angle to degrees (optional, depends on your rotation implementation)
+ const angleDifferenceDegrees = angleDifference * (180 / Math.PI);
+
+ return angleDifferenceDegrees;
+ }
+
+
+ function actionRotateApply() {
+ saveSvgPosition( gEditActionData.element );
+ }
+
function actionRotateAbort() {
if ( gEditActionState != EditActionState.ROTATE ) {
return;
@@ -495,7 +487,7 @@
function getScreenCenterPoint( element ) {
try {
- rect = element[0].getBoundingClientRect();
+ let rect = element[0].getBoundingClientRect();
if ( rect ) {
const screenCenterX = rect.left + ( rect.width / 2.0 );
const screenCenterY = rect.top + ( rect.height / 2.0 );
@@ -513,7 +505,7 @@
function getSvgCenterPoint( element, svgElement ) {
try {
- rect = element[0].getBoundingClientRect();
+ let rect = element[0].getBoundingClientRect();
if ( rect ) {
const screenCenterX = rect.left + ( rect.width / 2.0 );
const screenCenterY = rect.top + ( rect.height / 2.0 );
@@ -532,16 +524,54 @@
return point.matrixTransform( svgElement[0].getScreenCTM().inverse());
}
- function displayEventInfo( label, event ) {
- if ( ! DEBUG ) { return; }
- if ( ! event ) {
- console.log( 'No element to display info for.' );
- return;
+ function getSvgTransformValues(transform) {
+ let scale = { x: 1, y: 1 }, rotate = { angle: 0, cx: 0, cy: 0 }, translate = { x: 0, y: 0 };
+
+ let scaleMatch = transform.match(/scale\(([^)]+)\)/);
+ if (scaleMatch) {
+ let scaleValues = scaleMatch[1].trim().split(/[ ,]+/).map(parseFloat);
+ scale.x = scaleValues[0];
+ if ( scaleValues.length == 1 ) {
+ scale.y = scale.x;
+ } else {
+ scale.y = scaleValues[1];
+ }
}
- console.log( `${label} Event:
- Type: ${event.type},
- Key: ${event.key},
- Pos: ( ${event.clientX}, ${event.clientY} )` );
+
+ let translateMatch = transform.match(/translate\(([^)]+)\)/);
+ if (translateMatch) {
+ let [x, y] = translateMatch[1].trim().split(/[ ,]+/).map(parseFloat);
+ translate.x = x;
+ translate.y = y;
+ }
+
+ let rotateMatch = transform.match(/rotate\(([^)]+)\)/);
+ if (rotateMatch) {
+ let rotateValues = rotateMatch[1].trim().split(/[ ,]+/).map(parseFloat);
+ rotate.angle = rotateValues[0];
+
+ // Check if cx and cy are provided
+ if (rotateValues.length === 3) {
+ rotate.cx = rotateValues[1];
+ rotate.cy = rotateValues[2];
+ } else {
+ rotate.cx = 0;
+ rotate.cy = 0;
+ }
+ }
+
+ if ( DEBUG ) {
+ console.log( `TRANSFORM:
+ Raw: ${transform},
+ Parsed: scale=${JSON.stringify(scale)} translate=${JSON.stringify(translate)} rotate=${JSON.stringify(rotate)}` );
+ }
+
+ return { scale, translate, rotate };
+ }
+
+ function setSvgTransformAttr( element, scale, translate, rotate ) {
+ let newTransform = `scale(${scale.x} ${scale.y}) translate(${translate.x}, ${translate.y}) rotate(${rotate.angle}, ${rotate.cx}, ${rotate.cy})`;
+ element.attr('transform', newTransform);
}
function getSvgViewBox( svgElement ) {
@@ -567,6 +597,18 @@
return { x, y, width, height };
}
+ function displayEventInfo( label, event ) {
+ if ( ! DEBUG ) { return; }
+ if ( ! event ) {
+ console.log( 'No element to display info for.' );
+ return;
+ }
+ console.log( `${label} Event:
+ Type: ${event.type},
+ Key: ${event.key},
+ Pos: ( ${event.clientX}, ${event.clientY} )` );
+ }
+
function displayElementInfo( label, element ) {
if ( ! DEBUG ) { return; }
if ( ! element ) {
@@ -579,7 +621,7 @@
let rectStr = 'No Bounding Rect';
try {
- rect = element[0].getBoundingClientRect();
+ let rect = element[0].getBoundingClientRect();
if ( rect ) {
rectStr = `Dim: ${rect.width}px x ${rect.height}px,
Pos: left=${rect.left}px, top=${rect.top}px`;