Changeset 37568


Ignore:
Timestamp:
2023-03-25T22:18:10+13:00 (13 months ago)
Author:
davidb
Message:

Improvements to interaction with scalable/rotatable windows: restricts to the actual size of the scaled/rotated window; ignores pointer events that fall outside of the scaled/rotated window

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gs3-extensions/tabletop-dl/trunk/tabletop-interact.js

    r37561 r37568  
    11
     2function dragRestrictionRect(x,y,interactable_elem)
     3{
     4    // This function effectively does the same as restrictEdges { outer: 'parent', ... }
     5    // but ....
     6    // Upgraded to cope with the more complicated case where the interactable has
     7    // been rotated and scaled.  Without the extra checks, the 'outer' div that is
     8    // used for a rotated and scale interactable has is the one for the original element
     9    // prior to any scale or rotation applied to it (which is applied to .scale-element,
     10    // which is a child of the outer element)
     11
     12   
     13    let elem = interactable_elem.element;
     14    let parent_elem = elem.parentElement;
     15    //console.log("parent elem:");
     16    //console.log(parent_elem);
     17   
     18    let parent_rect = parent_elem.getBoundingClientRect();
     19    let restrict_to_rect = {
     20    left:   parent_rect.left,
     21    right:  parent_rect.right,
     22    top:    parent_rect.top,
     23    bottom: parent_rect.bottom
     24    };
     25   
     26   
     27    let delta_xl = x - restrict_to_rect.left;
     28    let delta_xr = restrict_to_rect.right - x;
     29    let delta_yt = y - restrict_to_rect.top;
     30    let delta_yb = restrict_to_rect.bottom - y;
     31   
     32    let dragged_rect;
     33   
     34    let $dragged_elem = $(elem).find('.scale-element');
     35    if ($dragged_elem.length>0) {
     36    dragged_rect = $dragged_elem[0].getBoundingClientRect();
     37    }
     38    else {
     39    dragged_rect = elem.getBoundingClientRect();
     40    }
     41               
     42    //console.log(dragged_rect);
     43    //console.log("restrict to:");
     44    //console.log(restrict_to_rect);
     45   
     46    // test bounds of dragged_rect, to restrict_to_rect
     47    // factor in any overshoots in the encoded rect returned
     48
     49    if ((dragged_rect.left >= restrict_to_rect.left) || (dragged_rect.right <= restrict_to_rect.right)) {
     50    // want to avoid situation where for dragged_rect both left and right are
     51    // outside of the restrict_to_rect
     52   
     53    if (dragged_rect.left < restrict_to_rect.left) {
     54        // encode how much the overshoot it
     55        restrict_to_rect.left += (restrict_to_rect.left - dragged_rect.left);
     56       
     57        // Now factor in the offset that comes from the supplied (x,y);         
     58        restrict_to_rect.left  += (x - parent_rect.left);
     59        restrict_to_rect.right += (x - parent_rect.left);
     60       
     61    }
     62    if (dragged_rect.right > restrict_to_rect.right) {
     63        // encode how much the overshoot it
     64        restrict_to_rect.right -= (dragged_rect.right - restrict_to_rect.right);
     65       
     66        // Now factor in the offset that comes from the supplied (x,y);         
     67        restrict_to_rect.left  -= (parent_rect.right - x) + 1;
     68        restrict_to_rect.right -= (parent_rect.right - x) + 1;
     69
     70    }
     71    }
     72    if ((dragged_rect.top >= restrict_to_rect.top) || (dragged_rect.bottom <= restrict_to_rect.bottom)) {
     73    // want to avoid situation where for dragged_rect both left and right are
     74    // outside of the restrict_to_rect
     75           
     76    if (dragged_rect.top < restrict_to_rect.top) {
     77        // encode how much the overshoot it
     78        restrict_to_rect.top += (restrict_to_rect.top - dragged_rect.top);
     79       
     80        // Now factor in the offset that comes from the supplied (x,y);         
     81        restrict_to_rect.top    += (y - parent_rect.top);
     82        restrict_to_rect.bottom += (y - parent_rect.top);
     83    }
     84    if (dragged_rect.bottom > restrict_to_rect.bottom) {
     85        // encode how much the overshoot it
     86        restrict_to_rect.bottom -= (dragged_rect.bottom - restrict_to_rect.bottom);
     87       
     88        // Now factor in the offset that comes from the supplied (x,y);         
     89        restrict_to_rect.top    -= (parent_rect.bottom - y) + 1;
     90        restrict_to_rect.bottom -= (parent_rect.bottom - y) + 1;       
     91    }
     92    }
     93       
     94    return restrict_to_rect;
     95}
     96   
     97   
    298function dragMoveListener(event)
    399{
     
    20116    let core_draggable_options = {
    21117
    22     listeners: { move: dragMoveListener },
     118    //listeners: { move: dragMoveListener },
    23119
    24120    /*
    25     modifiers: [
    26         interact.modifiers.restrictRect({
    27         restriction: 'parent',
    28         endOnly: true
    29         })
    30     ], 
     121      onstart: function(event) {
     122        let target = event.target;
     123    },
     124
    31125    */
     126   
     127    onmove: dragMoveListener,
     128
     129    /*
     130    onend: function(event) {
     131        let target = event.target;
     132    },
     133    */
     134   
    32135   
    33136    modifiers: [
    34137        interact.modifiers.restrict({       
    35         restriction: function(x,y,interactable_elem) {
    36             console.log(`Called draggable restriction function with: x=${x}, y=${y}`);
     138        restriction: dragRestrictionRect,
     139       
     140/*
     141        function(x,y,interactable_elem) {
     142            //console.log(`!!! Called draggable restriction function with: x=${x}, y=${y}`);
    37143
    38144            let elem = interactable_elem.element;
     
    70176            //let delta_yt = y - dragged_rect.top;
    71177            //let delta_yb = dragged_rect.bottom - y;
    72 
    73             console.log(dragged_rect);
    74             console.log("restrict to:");
    75             console.log(restrict_to_rect);
     178           
     179            //console.log(dragged_rect);
     180            //console.log("restrict to:");
     181            //console.log(restrict_to_rect);
    76182           
    77183            // test bounds of dragged_rect, to restrict_to_rect
     
    130236            //restrict_to_rect.height = restrict_to_rect.bottom - restrict_to_rect.top + 1;
    131237           
    132             console.log(`Returning for (${x},${y}) as delta [xl<->xr],[yt,<->yb] [(${delta_xl}<->${delta_xr}],[${delta_yt})<->${delta_yb}]`);
    133             console.log(restrict_to_rect);
     238            //console.log(`Returning for (${x},${y}) as delta [xl<->xr],[yt,<->yb] [(${delta_xl}<->${delta_xr}],[${delta_yt})<->${delta_yb}]`);
     239            //console.log(restrict_to_rect);
    134240               
    135241            return restrict_to_rect;
    136         },
    137 
     242            },*/       
    138243       
    139244        endOnly: true
     
    233338{
    234339    let core_gesturable_options = {
    235    
    236340    listeners: {
    237341        start (event) {
    238         angleScale.angle -= event.angle;
     342        angleScale.angle -= event.angle;       
    239343        },
    240344        move (event) {
     345       
    241346        let currentAngle = angleScale.angle + event.angle;
    242347        let currentScale = angleScale.scale * event.scale;
     
    336441
    337442
    338 
    339 
    340443    let doc_url = "http://localhost:8383/greenstone3/library/collection/gs2mgdemo/document/HASHe3aae2159b0dda6553cee4";
    341444
     
    350453
    351454       
    352         const win2 = new WinBox({
     455        let win2 = new WinBox({
    353456        id:    "my-winbox2",
    354457        title: 'Winbox + Interact Example: Drag, Scale and Rotate',
     
    358461        height: '600px',
    359462        border: "0.3em",
    360         //background: "#29e",
    361         //html: "<p>Some Sample Text</p>"
    362463        html: html_result
    363464        });
     
    369470        let $scale_element_div = $('<div>')
    370471        .attr("class","scale-element")
     472        .css("pointer-events","all")
    371473            .css("position","relative")
    372474            .css("width","100%")
     
    386488        angle: 0,
    387489        scale: 1
    388         }
     490        };
    389491       
    390492       
     
    392494        let gesturable_options = getCoreGesturableOptions(this_elem,this_angle_scale);
    393495       
    394         interact('#my-winbox2')
     496        var my_winbox2 = interact('#my-winbox2')
    395497        .gesturable(gesturable_options)
    396498        .draggable(gdraggable_options);
     499
     500        $('#my-winbox2').css("pointer-events","none");
    397501       
    398502    });
    399    
    400503});
Note: See TracChangeset for help on using the changeset viewer.