private void addBrickToPositionInProject(int position, Brick brick) { Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite(); int[] temp = getScriptAndBrickIndexFromProject(position); int scriptPosition = temp[0]; int brickPosition = temp[1]; Script script = currentSprite.getScript(scriptPosition); if (brick instanceof NestingBrick) { ((NestingBrick) draggedBrick).initialize(); List<NestingBrick> nestingBrickList = ((NestingBrick) draggedBrick).getAllNestingBrickParts(true); for (int i = 0; i < nestingBrickList.size(); i++) { if (nestingBrickList.get(i) instanceof DeadEndBrick) { if (i < nestingBrickList.size() - 1) { Log.w(TAG, "Adding a DeadEndBrick in the middle of the NestingBricks"); } position = getPositionForDeadEndBrick(position); temp = getScriptAndBrickIndexFromProject(position); script.addBrick(temp[1], (Brick) nestingBrickList.get(i)); } else { script.addBrick(brickPosition + i, (Brick) nestingBrickList.get(i)); } } } else { script.addBrick(brickPosition, brick); } }
private void addBrickToPositionInUserScript(int position, Brick brick) { position = getPositionInUserScript(position); if (brick instanceof NestingBrick) { ((NestingBrick) draggedBrick).initialize(); List<NestingBrick> nestingBrickList = ((NestingBrick) draggedBrick).getAllNestingBrickParts(true); for (int i = 0; i < nestingBrickList.size(); i++) { script.addBrick(position + i, (Brick) nestingBrickList.get(i)); } } else { script.addBrick(position, brick); } }
private int getDraggedNestingBricksToPosition(NestingBrick nestingBrick, int to) { List<NestingBrick> nestingBrickList = nestingBrick.getAllNestingBrickParts(true); int restrictedTop = 0; int restrictedBottom = brickList.size(); int tempPosition; int currentPosition = to; boolean passedBrick = false; for (NestingBrick temp : nestingBrickList) { tempPosition = brickList.indexOf(temp); if (temp != nestingBrick) { if (!passedBrick) { restrictedTop = tempPosition; } if (passedBrick) { restrictedBottom = tempPosition; break; } } else { passedBrick = true; currentPosition = tempPosition; } } for (int i = currentPosition; i > restrictedTop; i--) { if (checkIfScriptOrOtherNestingBrick(brickList.get(i), nestingBrickList)) { restrictedTop = i; break; } } for (int i = currentPosition; i < restrictedBottom; i++) { if (checkIfScriptOrOtherNestingBrick(brickList.get(i), nestingBrickList)) { restrictedBottom = i; break; } } to = to <= restrictedTop ? restrictedTop + 1 : to; to = to >= restrictedBottom ? restrictedBottom - 1 : to; return to; }
@Override public void drag(int from, int to) { int toOriginal = to; if (to < 0 || to >= brickList.size()) { to = brickList.size() - 1; } if (from < 0 || from >= brickList.size()) { from = brickList.size() - 1; } if (draggedBrick == null) { draggedBrick = (Brick) getItem(from); notifyDataSetChanged(); } if (firstDrag) { fromBeginDrag = from; firstDrag = false; } if (draggedBrick instanceof NestingBrick) { NestingBrick nestingBrick = (NestingBrick) draggedBrick; if (nestingBrick.isInitialized()) { to = getDraggedNestingBricksToPosition(nestingBrick, to); } } else if (draggedBrick instanceof ScriptBrick) { int currentPosition = to; brickList.remove(draggedBrick); brickList.add(to, draggedBrick); to = getNewPositionForScriptBrick(to, draggedBrick); dragTargetPosition = to; if (currentPosition != to) { retryScriptDragging = true; } else { retryScriptDragging = false; } } to = getNewPositionIfEndingBrickIsThere(to, draggedBrick); if (!(draggedBrick instanceof ScriptBrick)) { if (to != 0) { dragTargetPosition = to; } else { dragTargetPosition = 1; to = 1; } } brickList.remove(draggedBrick); if (dragTargetPosition >= 0 && dragTargetPosition <= brickList.size()) { brickList.add(dragTargetPosition, draggedBrick); toEndDrag = to; } else { brickList.add(toOriginal, draggedBrick); toEndDrag = toOriginal; } animatedBricks.clear(); notifyDataSetChanged(); }