Block position on workspace

678 views
Skip to first unread message

guido

unread,
Oct 26, 2016, 10:37:42 AM10/26/16
to Blockly
Hi, i am trying to re-position a block when its touching the toolbox zone. (IMAGE 1)

If the user starts dragging the block, and this block will NOT be deleted, i want to put it back in the same position X position (not Y) as the "when run" block (IMAGE 2)

It is good to know that the block is deleted if the mouse (dragging the block) enters de toolbox zone.

(IMAGE 1)













(IMAGE 2)















With this code, i was able to do it:

 var posXY = block.getRelativeToSurfaceXY();

 if (posXY.x < 20){    //blocks are positioned
     block.moveBy(20 - posXY.x,0);
}


The blocks under "when run" are positioned in x = 20. so if its < 20, i take the relativePosition and call the moveBy function.

The problem apears when this block has childs, they disorder : 


















Thank u very much in advance!

Rachel Fenichel

unread,
Oct 26, 2016, 2:43:50 PM10/26/16
to Blockly
Looks like you are calling that on each dragged block but only need to call it on the root block.

guido

unread,
Oct 26, 2016, 2:57:04 PM10/26/16
to Blockly
Hi, thank you for the response!

I just did it this way, I this i was making it only on the root block.

(...)

eventMouseUp = Blockly.bindEvent_(block.svgGroup_, "mouseup", null, function(e){
        if (block.workspace.isDeleteArea(e) === false){
            var posXY = block.getRelativeToSurfaceXY();

            if (posXY.x < 20 && !Blockly.highlightedConnection_){    //blocks are positioned if are near toolbox zone AND it is not going to be connected
                var newXY = block.workspace.moveDrag(e);
                var group = block.getSvgRoot();
                var x = group.translate_;
                group.translate_ = 'translate(' + 20 + ',' + newXY.y + ')';
                group.setAttribute('transform', group.translate_ + group.skew_);
                block.disconnectUiEffect();
            }
        }
    });


Austin Bart

unread,
Oct 26, 2016, 10:00:18 PM10/26/16
to Blockly
This is actually kind of a nifty way to help students line up their code blocks. I'd be interested in hearing if your users find it helpful.

guido

unread,
Oct 27, 2016, 9:56:33 AM10/27/16
to Blockly
Hi Austin, in code.org they have some games that implemented blockly, and they all have this way of reordering blocks, so we can asume that it's helpful.

Anyway, i had to do a lot of things to make that work, there were a lot of problems, like if a block was dragged from the toolbox and not from workspace was not repositioned, only if it was already on workspace, also when there are childs in the block you are repositiong, and so on...

When i know i let you know :)
Reply all
Reply to author
Forward
0 new messages