Skip to content

Home Information - Your home, your data, organized.

Python

Have basic SVG element drag and drop working. WIP.

Anthony Cassandra7271244e4307
diff --git a/src/hi/apps/common/svg_models.py b/src/hi/apps/common/svg_models.py
index 53d7ff85..9200a688 100644
--- a/src/hi/apps/common/svg_models.py
+++ b/src/hi/apps/common/svg_models.py
@@ -90,7 +90,7 @@ class SvgItem:
""" Translation needed to put the item's center at the SVG position x. """
if self.scale < 0.000001:
return 0
- return ( self.position_x / self.scale ) - self.bounds_center_x
+ return ( self.position_x / self.scale ) - self.bounds_center_x
@property
def translate_y(self):
diff --git a/src/hi/apps/entity/models.py b/src/hi/apps/entity/models.py
index a234fb7d..ca8c56e1 100644
--- a/src/hi/apps/entity/models.py
+++ b/src/hi/apps/entity/models.py
@@ -317,7 +317,7 @@ class EntityPosition( SvgPositionModel ):
bounding_box = self.entity.entity_type.svg_bounding_box,
# rotate = float( self.svg_rotation ),
# scale = float( self.svg_scale ),
- rotate = 5,
+ rotate = 0,
scale = 4.0,
)
diff --git a/src/hi/apps/entity/templates/entity/svg/type.other.svg b/src/hi/apps/entity/templates/entity/svg/type.other.svg
index 2d33bf80..eee0a739 100644
--- a/src/hi/apps/entity/templates/entity/svg/type.other.svg
+++ b/src/hi/apps/entity/templates/entity/svg/type.other.svg
@@ -1,5 +1,8 @@
-<rect id="{{ svg_item.html_id }}"
- hi-state="default"
+<rect hi-state="default"
+ class="hi-entity-other-bg"
fill="blue"
- x="0" y="0" width="32" height="32"
- transform="scale( {{ svg_item.scale }} ) translate( {{ svg_item.translate_x }} {{ svg_item.translate_y }} ) rotate( {{ svg_item.rotate }} {{ svg_item.bounds_center_x }} {{ svg_item.bounds_center_y }} )"/>
+ x="0" y="0" width="32" height="32" />
+<rect hi-state="default"
+ class="hi-entity-other-fg"
+ fill="green"
+ x="0" y="0" width="16" height="16" />
diff --git a/src/hi/apps/location/templates/location/location_view.html b/src/hi/apps/location/templates/location/location_view.html
index b27581d8..c3c4a414 100644
--- a/src/hi/apps/location/templates/location/location_view.html
+++ b/src/hi/apps/location/templates/location/location_view.html
@@ -1,5 +1,5 @@
{% load common_template_helpers %}
-<div class="hi-location-view">
+<div id="hi-location-view-main" class="hi-location-view">
<svg id="{{ location_view_data.location_view.html_id }}" xmlns="http://www.w3.org/2000/svg"
viewBox="{{ location_view_data.location_view.svg_view_box }}"
transform="rotate({{ location_view_data.location_view.svg_rotation }} 0 0)">
@@ -7,9 +7,15 @@
{% for entity_path in location_view_data.entity_paths %}
{% endfor %}
-
+
{% for entity_position in location_view_data.entity_positions %}
- {% include entity_position.svg_item.template_name with svg_item=entity_position.svg_item %}
+ {% with entity_position.svg_item as svg_item %}
+ <g id="{{ svg_item.html_id }}"
+ class="draggable"
+ transform="scale( {{ svg_item.scale }} ) translate( {{ svg_item.translate_x }} {{ svg_item.translate_y }} ) rotate( {{ svg_item.rotate }} {{ svg_item.bounds_center_x }} {{ svg_item.bounds_center_y }} )">
+ {% include entity_position.svg_item.template_name %}
+ </g>
+ {% endwith %}
{% endfor %}
{% for collection_position in location_view_data.collection_positions %}
diff --git a/src/hi/settings/base.py b/src/hi/settings/base.py
index a0c8164c..dad6999c 100644
--- a/src/hi/settings/base.py
+++ b/src/hi/settings/base.py
@@ -279,6 +279,7 @@ PIPELINE = {
'source_filenames': (
'js/svg-utils.js',
'js/polling.js',
+ 'js/edit.js',
'js/main.js',
),
'output_filename': 'js/js_after_content_custom.js',
diff --git a/src/hi/static/js/edit.js b/src/hi/static/js/edit.js
new file mode 100644
index 00000000..e2dbaa3f
--- /dev/null
+++ b/src/hi/static/js/edit.js
@@ -0,0 +1,262 @@
+(function() {
+ window.Hi = window.Hi || {};
+
+ const HiEdit = {
+ editThing: function() {
+ return true;
+ }
+ };
+
+ window.Hi.edit = HiEdit;
+
+ const DEBUG = true;
+ const clickThreshold = 300; // Adjust the threshold as needed (in milliseconds)
+ let dragData = null;
+
+ $(document).on('mousedown', function(event) {
+ startDrag( event );
+
+ });
+ $(document).on('mousemove', function(event) {
+ updateDrag( event );
+ });
+ $(document).on('mouseup', function() {
+ endDrag( event );
+ });
+ $(document).on('keydown', function(event) {
+ console.log(`Key down: ${event.key}`);
+ });
+ $(document).on('keyup', function(event) {
+ console.log(`Key up: ${event.key}`);
+ });
+
+ function startDrag( event ) {
+ displayEventInfo( event );
+ displayElementInfo( 'Event Target', $(event.target) );
+
+ const enclosingSvgGroup = $(event.target).closest('g');
+ if ( enclosingSvgGroup.length > 0 ) {
+ dragData = {};
+ dragData.element = enclosingSvgGroup;
+ displayElementInfo( 'Drag Element', dragData.element );
+
+ dragData.startTime = Date.now();
+
+ dragData.baseSvg = $('#hi-location-view-main > svg');
+ displayElementInfo( 'Base SVG', dragData.baseSvg );
+
+ let transform = dragData.element.attr('transform') || '';
+ let { scale, translate, rotate } = getTransformValues( transform );
+ let svgPoint = toSvgPoint( dragData.baseSvg[0], event.clientX, event.clientY );
+
+ dragData.offsetSvgX = (svgPoint.x / scale.x) - translate.x;
+ dragData.offsetSvgY = (svgPoint.y / scale.y) - translate.y;
+
+ dragData.svgCenter = getSvgCenterPoint( dragData.element, dragData.baseSvg[0] );
+
+ dragData.scale = scale;
+ dragData.rotate = rotate;
+
+ if ( DEBUG ) {
+ console.log( `Drag Start:
+ SVG Cursor Point: ( ${svgPoint.x}, ${svgPoint.y} ),
+ SVG Cursor Offset: ( ${dragData.offsetSvgX}, ${dragData.offsetSvgY} ),
+ SVG Center Point: ( ${dragData.svgCenter.x}, ${dragData.svgCenter.y} )`);
+ }
+ }
+ }
+
+ function updateDrag( event ) {
+ if ( dragData == null ) {
+ return;
+ }
+ displayEventInfo( event );
+ displayElementInfo( 'Drag Element', dragData.element );
+
+ let svgPoint = toSvgPoint( dragData.baseSvg[0], event.clientX, event.clientY );
+
+ let scale = dragData.scale;
+ let rotate = dragData.rotate
+
+ let newX = (svgPoint.x / scale.x) - dragData.offsetSvgX;
+ let newY = (svgPoint.y / scale.y) - dragData.offsetSvgY;
+
+ let transform = dragData.element.attr('transform') || '';
+
+ let newTransform = `scale(${scale.x} ${scale.y}) translate(${newX}, ${newY}) rotate(${rotate.angle}, ${rotate.cx}, ${rotate.cy})`;
+
+ dragData.element.attr('transform', newTransform);
+
+ dragData.svgCenter = getSvgCenterPoint( dragData.element, dragData.baseSvg[0] );
+
+ if ( DEBUG ) {
+ console.log( `Drag Update:
+ SVG Cursor Point: ( ${svgPoint.x}, ${svgPoint.y} ),
+ TRANSFORM Result: ${newTransform},
+ SVG Center Point: ( ${dragData.svgCenter.x}, ${dragData.svgCenter.y} )`);
+ }
+ }
+
+ function endDrag( event ) {
+ if ( dragData == null ) {
+ return;
+ }
+
+ displayEventInfo( event );
+ displayElementInfo( 'Drag Element', dragData.element );
+
+ updateDrag( event );
+
+ let dragEndTime = Date.now();
+ const elapsedTime = dragEndTime - dragData.startTime;
+ if (elapsedTime <= clickThreshold) {
+ console.log( `CLICK: ${elapsedTime}` );
+ }
+ dragData = null;
+ }
+
+ function getTransformValues(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];
+ }
+ }
+
+ 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 getSvgCenterPoint( element, svgElement ) {
+
+ try {
+ rect = element[0].getBoundingClientRect();
+ if ( rect ) {
+ const screenCenterX = rect.left + ( rect.width / 2.0 );
+ const screenCenterY = rect.top + ( rect.height / 2.0 );
+ return toSvgPoint( svgElement, screenCenterX, screenCenterY );
+ }
+ } catch (e) {
+ console.debug( `Problem getting bounding box: ${e}` );
+ }
+ return { x: 0, y: 0 }
+ }
+
+ function toSvgPoint( svgElement, clientX, clientY) {
+ let point = svgElement.createSVGPoint();
+ point.x = clientX;
+ point.y = clientY;
+ return point.matrixTransform( svgElement.getScreenCTM().inverse());
+ }
+
+ function displayEventInfo( event ) {
+ if ( ! DEBUG ) { return; }
+ if ( ! event ) {
+ console.log( 'No element to display info for.' );
+ return;
+ }
+ console.log( `Event:
+ Type: ${event.type},
+ Pos: ( ${event.clientX}, ${event.clientY} )` );
+ }
+
+ function getSvgViewBox( svgElement ) {
+ let x = null;
+ let y = null;
+ let width = null;
+ let height = null;
+
+ if (svgElement.length < 1 ) {
+ return { x, y, width, height };
+ }
+ let viewBoxValue = svgElement.attr('viewBox');
+ if ( ! viewBoxValue) {
+ return { x, y, width, height };
+ }
+
+ let viewBoxArray = viewBoxValue.split(' ').map(Number);
+ x = viewBoxArray[0];
+ y = viewBoxArray[1];
+ width = viewBoxArray[2];
+ height = viewBoxArray[3];
+
+ return { x, y, width, height }
+ }
+
+ function displayElementInfo( label, element ) {
+ if ( ! DEBUG ) { return; }
+ if ( ! element ) {
+ console.log( 'No element to display info for.' );
+ return;
+ }
+ const elementTag = element.prop('tagName');
+ const elementId = element.attr('id') || 'No ID';
+ const elementClasses = element.attr('class') || 'No Classes';
+
+ let rectStr = 'No Bounding Rect';
+ try {
+ rect = element[0].getBoundingClientRect();
+ if ( rect ) {
+ rectStr = `Dim: ${rect.width}px x ${rect.height}px,
+ Pos: left=${rect.left}px, top=${rect.top}px`;
+ }
+ } catch (e) {
+ }
+
+ let offsetStr = 'No Offset';
+ const offset = element.offset();
+ if ( offset ) {
+ offsetStr = `Offset: ( ${offset.left}px, ${offset.top}px )`;
+ }
+
+ let svgStr = 'Not an SVG';
+ if ( elementTag == 'svg' ) {
+ let { x, y, width, height } = getSvgViewBox( element );
+ if ( x != null ) {
+ svgStr = `Viewbox: ( ${x}, ${y}, ${width}, ${height} )`;
+ } else {
+ svgStr = 'No viewbox attribute';
+ }
+ }
+
+ console.log( `${label}:
+ Name: ${elementTag},
+ Id: ${elementId},
+ Classes: ${elementClasses},
+ ${svgStr},
+ ${offsetStr},
+ ${rectStr}`) ;
+
+ }
+
+})();