If we want to allow dragging of a (2D) shape only inside the shape of the parent we can use this method to achieve our goal. It uses a pretty brute force method but works quite well. Basically we check in our drag gesture listener if any of the child's vertices are outside of the parent's shape after translation. If this is the case, we translate the child back.

childShape.removeAllGestureEventListeners(DragProcessor.class);
childShape.addGestureListener(DragProcessor.class, new IGestureEventListener() {
                        public boolean processGestureEvent(MTGestureEvent ge) {
                                DragEvent de = (DragEvent)ge;
 
                                childShape.translateGlobal(de.getTranslationVect());
 
                                Vector3D[] v1 = parentShape.getVerticesGlobal();
                                Vector3D[] v2 = childShape.getBounds().getVectorsGlobal();
                                boolean inside = true;
                                for (Vector3D vector3d : v2) {
                                        if (!ToolsGeometry.isPolygonContainsPoint(v1, vector3d)){
                                                inside = false;
                                        }
                                }
 
                                if (!inside){
                                        childShape.translateGlobal(de.getTranslationVect().getScaled(-1));
                                }
                                return false;
                        }
                });
Powered by MediaWiki contact