Skip to content

Home Information - Your home, your data, organized.

Python

Added scale and rotate keydown events. WIP

Anthony Cassandrae42f039adf25
diff --git a/src/hi/apps/collection/enums.py b/src/hi/apps/collection/enums.py
index 4d50b00d..ce2fff7d 100644
--- a/src/hi/apps/collection/enums.py
+++ b/src/hi/apps/collection/enums.py
@@ -4,12 +4,18 @@ from hi.apps.common.svg_models import SvgViewBox
class CollectionType(LabeledEnum):
- DEFAULT = ( 'Default', '' )
- ZONE = ( 'Zone', '' )
+ APPLIANCES = ( 'Appliances', '' )
+ DEVICES = ( 'Devices', '' )
+ ELECTRONICS = ( 'Electronics', '' )
+ GARDENING = ( 'Gardening', '' )
+ LANDSCAPING = ( 'Landscaping', '' )
+ TOOLS = ( 'Tools', '' )
+ OTHER = ( 'Other', '' )
+ ZONE = ( 'Zone', '' )
@classmethod
def default(cls):
- return cls.DEFAULT
+ return cls.OTHER
@property
def svg_icon_bounding_box(self):
diff --git a/src/hi/apps/edit/templates/edit/panes/side.html b/src/hi/apps/edit/templates/edit/panes/side.html
index 0a0ec272..a02bc9d9 100644
--- a/src/hi/apps/edit/templates/edit/panes/side.html
+++ b/src/hi/apps/edit/templates/edit/panes/side.html
@@ -7,9 +7,11 @@
</a>
</div>
</div>
-<div id="hi-edit-entity">
+<div id="hi-edit-item">
{% if entity %}
{% include "edit/panes/entity.html" %}
+ {% elif collection %}
+ {% include "edit/panes/collection.html" %}
{% else %}
<div><em>Click to see details.</em></div>
{% endif %}
diff --git a/src/hi/apps/edit/views.py b/src/hi/apps/edit/views.py
index f49466a2..cd9bc18a 100644
--- a/src/hi/apps/edit/views.py
+++ b/src/hi/apps/edit/views.py
@@ -84,7 +84,7 @@ class EditDetailsView( View, EditViewMixin ):
content = template.render( context, request = request )
insert_map = {
- DIVID['EDIT_ENTITY']: content,
+ DIVID['EDIT_ITEM']: content,
}
return antinode.response(
insert_map = insert_map,
@@ -112,7 +112,7 @@ class EditDetailsView( View, EditViewMixin ):
content = template.render( context, request = request )
insert_map = {
- DIVID['EDIT_COLLECTION']: content,
+ DIVID['EDIT_ITEM']: content,
}
return antinode.response(
insert_map = insert_map,
diff --git a/src/hi/apps/location/location_view_manager.py b/src/hi/apps/location/location_view_manager.py
index 0f3c2010..a723bff0 100644
--- a/src/hi/apps/location/location_view_manager.py
+++ b/src/hi/apps/location/location_view_manager.py
@@ -36,7 +36,7 @@ class LocationViewManager(Singleton):
collection = collection_view.collection
collection_position = collection.positions.filter( location = location ).first()
if collection_position:
- collection_positions.append( collection )
+ collection_positions.append( collection_position )
else:
unpositioned_collections.append( collection )
continue
diff --git a/src/hi/apps/location/templates/location/location_view.html b/src/hi/apps/location/templates/location/location_view.html
index aea43100..bf25121e 100644
--- a/src/hi/apps/location/templates/location/location_view.html
+++ b/src/hi/apps/location/templates/location/location_view.html
@@ -15,6 +15,7 @@
{% endfor %}
{% for collection_position in location_view_data.collection_positions %}
+ {% include "location/svg_position_item.html" with svg_position_item=collection_position.svg_item %}
{% endfor %}
</svg>
diff --git a/src/hi/constants.py b/src/hi/constants.py
index 71e4b9f1..ee14dff5 100644
--- a/src/hi/constants.py
+++ b/src/hi/constants.py
@@ -13,11 +13,6 @@ DIVID = {
# Editing
'EDIT_SIDE': 'hi-edit-side-content',
- 'EDIT_ENTITY': 'hi-edit-entity',
- 'EDIT_COLLECTION': 'hi-edit-collection',
- 'EDIT_ENTITY_VIEWS': 'hi-edit-entity-views',
- 'EDIT_ENTITY_ATTRIBUTES': 'hi-edit-entity-attributes',
- 'EDIT_ENTITY_SENSORS': 'hi-edit-entity-sensors',
- 'EDIT_ENTITY_CONTROLLERS': 'hi-edit-entity-controllers',
+ 'EDIT_ITEM': 'hi-edit-item',
}
diff --git a/src/hi/static/css/main.css b/src/hi/static/css/main.css
index 3cbac081..df4a07e6 100644
--- a/src/hi/static/css/main.css
+++ b/src/hi/static/css/main.css
@@ -174,3 +174,7 @@ a.underline:visited {
height: auto;
display: block;
}
+
+svg .highlighted {
+ filter: drop-shadow(0 0 10px red);
+}
diff --git a/src/hi/static/js/edit.js b/src/hi/static/js/edit.js
index 0549ca4f..9cfa25a9 100644
--- a/src/hi/static/js/edit.js
+++ b/src/hi/static/js/edit.js
@@ -22,6 +22,14 @@
let dragData = null;
const dragThreshold = 3;
+ let lastMousePosition = { x: 0, y: 0 };
+ let itemEditState = {
+ currentElement: null,
+ mode: 'move',
+ scaleStart: 1.0,
+ rotateStart: 0.0,
+ }
+
$(document).ready(function() {
$(document).on('mousedown', locationViewAreaSelector, function(event) {
isDragging = false;
@@ -68,10 +76,33 @@
}, doubleClickDelayMs );
}
});
- $(document).on('keydown', locationViewAreaSelector, function(event) {
- handleKeyDown( event );
+ $(document).on('mousemove', function(event) {
+ const currentMousePosition = {
+ x: event.pageX,
+ y: event.pageY
+ };
+ if ( itemEditState.mode == 'scale' ) {
+ itemEditScaleUpdate( currentMousePosition );
+ } else if ( itemEditState.mode == 'rotate' ) {
+ itemEditRotateUpdate( currentMousePosition );
+ }
+
+ lastMousePosition = currentMousePosition;
+ });
+ $(document).on('keydown', function(event) {
+ const targetArea = $(locationViewAreaSelector);
+ const targetOffset = targetArea.offset();
+ const targetWidth = targetArea.outerWidth();
+ const targetHeight = targetArea.outerHeight();
+
+ if (lastMousePosition.x >= targetOffset.left &&
+ lastMousePosition.x <= targetOffset.left + targetWidth &&
+ lastMousePosition.y >= targetOffset.top &&
+ lastMousePosition.y <= targetOffset.top + targetHeight) {
+ handleKeyDown(event);
+ }
});
- $(document).on('keyup', locationViewAreaSelector, function(event) {
+ $(document).on('keyup', function(event) {
handleKeyUp( event );
});
});
@@ -91,6 +122,11 @@
return;
}
+ itemEditState.currentElement = enclosingSvgGroup;
+
+ $('.draggable').removeClass('highlighted');
+ $(enclosingSvgGroup).addClass('highlighted');
+
AN.get( `/edit/details/${svgItemId}` );
}
@@ -105,9 +141,45 @@
displayElementInfo( 'SVG Target Element', enclosingSvgGroup );
}
-
+
function handleKeyDown( event ) {
displayEventInfo( 'Key Down', event );
+
+ if ( ! itemEditState.currentElement ) {
+ return;
+ }
+
+ if ( event.key == 's' ) {
+ itemEditRotateAbort();
+ itemEditScaleStart();
+ itemEditState.mode = 'scale';
+
+ } else if ( event.key == 'r' ) {
+ itemEditScaleAbort();
+ itemEditRotateStart();
+ itemEditState.mode = 'rotate';
+
+ } else if ( event.key == 'Escape' ) {
+ itemEditScaleAbort();
+ itemEditRotateAbort();
+ itemEditState.mode = 'move';
+
+ } else if ( event.key == 'Enter' ) {
+ if ( itemEditState.mode == 'scale' ) {
+ itemEditScaleApply();
+ }
+ else if ( itemEditState.mode == 'rotate' ) {
+ itemEditRotateApply();
+ } else {
+ return;
+ }
+ itemEditState.mode = 'move';
+ } else {
+ return;
+ }
+
+ event.stopPropagation();
+ event.preventDefault();
}
function handleKeyUp( event ) {
@@ -240,7 +312,10 @@
if (rotateValues.length === 3) {
rotate.cx = rotateValues[1];
rotate.cy = rotateValues[2];
- }
+ } else {
+ rotate.cx = 0;
+ rotate.cy = 0;
+ }
}
if ( DEBUG ) {
@@ -252,6 +327,97 @@
return { scale, translate, rotate };
}
+ function updateSvgTransform( 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 );
+
+ const baseSvgElement = $(baseSvgSelector);
+ const center = getSvgCenterPoint( element, baseSvgElement );
+
+ let svgItemId = element.attr('id');
+ data = {
+ svg_x: center.x,
+ svg_y: center.y,
+ svg_scale: scale.x,
+ svg_rotate: rotate.angle,
+ };
+ AN.post( `/edit/svg/position/${svgItemId}`, data );
+ }
+
+ function itemEditScaleStart() {
+ console.log( 'Scale Start' );
+ let transform = itemEditState.currentElement.attr('transform');
+ let { scale, translate, rotate } = getTransformValues( transform );
+ itemEditState.scaleStart = scale;
+ }
+
+ function itemEditRotateStart() {
+ let transform = itemEditState.currentElement.attr('transform');
+ let { scale, translate, rotate } = getTransformValues( transform );
+ itemEditState.rotateStart = rotate;
+ }
+
+ function itemEditScaleUpdate( currentMousePosition ) {
+ console.log( 'Scale Update' );
+ const deltaX = currentMousePosition.x - lastMousePosition.x;
+ const deltaY = currentMousePosition.y - lastMousePosition.y;
+ if (( deltaX <= dragThreshold ) && ( deltaY <= dragThreshold )) {
+ return;
+ }
+ let transform = itemEditState.currentElement.attr('transform');
+ let { scale, translate, rotate } = getTransformValues( transform );
+ scale.x = scale.x + deltaX;
+ updateSvgTransform( itemEditState.currentElement, scale, translate, rotate );
+ }
+
+ function itemEditRotateUpdate( currentMousePosition ) {
+ const deltaX = currentMousePosition.x - lastMousePosition.x;
+ const deltaY = currentMousePosition.y - lastMousePosition.y;
+ if (( deltaX <= dragThreshold ) && ( deltaY <= dragThreshold )) {
+ return;
+ }
+ let transform = itemEditState.currentElement.attr('transform');
+ let { scale, translate, rotate } = getTransformValues( transform );
+ rotate.angle = rotate.angle + deltaX;
+ updateSvgTransform( itemEditState.currentElement, scale, translate, rotate );
+ }
+
+ function itemEditScaleApply() {
+ console.log( 'Scale Apply' );
+ saveSvgPosition( itemEditState.currentElement );
+ }
+
+ function itemEditRotateApply() {
+ saveSvgPosition( itemEditState.currentElement );
+ }
+
+ function itemEditScaleAbort() {
+ if ( itemEditState.mode != 'scale' ) {
+ return;
+ }
+ console.log( 'Scale Abort' );
+ let transform = itemEditState.currentElement.attr('transform');
+ let { scale, translate, rotate } = getTransformValues( transform );
+ scale = itemEditState.scaleStart;
+ updateSvgTransform( itemEditState.currentElement, scale, translate, rotate );
+ }
+
+ function itemEditRotateAbort() {
+ if ( itemEditState.mode != 'rotate' ) {
+ return;
+ }
+ let transform = itemEditState.currentElement.attr('transform');
+ let { scale, translate, rotate } = getTransformValues( transform );
+ rotate = itemEditState.rotateStart;
+ updateSvgTransform( itemEditState.currentElement, scale, translate, rotate );
+ }
+
function getSvgCenterPoint( element, svgElement ) {
try {