示例#1
0
  public void addNewBrick(int position, Brick brickToBeAdded, boolean initInsertedBrick) {

    if (draggedBrick != null) {
      Log.w(TAG, "Want to add Brick while there is another one currently dragged.");
      return;
    }

    Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();
    int scriptCount = currentSprite.getNumberOfScripts();
    if (scriptCount == 0 && brickToBeAdded instanceof ScriptBrick) {
      currentSprite.addScript(((ScriptBrick) brickToBeAdded).getScriptSafe());
      initBrickList();
      notifyDataSetChanged();
      return;
    }

    if (position < 0) {
      position = 0;
    } else if (position > brickList.size()) {
      position = brickList.size();
    }

    if (brickToBeAdded instanceof ScriptBrick) {

      brickList.add(position, brickToBeAdded);
      position = getNewPositionForScriptBrick(position, brickToBeAdded);
      brickList.remove(brickToBeAdded);
      brickList.add(position, brickToBeAdded);
      scrollToPosition(position);

    } else {

      position = getNewPositionIfEndingBrickIsThere(position, brickToBeAdded);
      position = position <= 0 ? 1 : position;
      position = position > brickList.size() ? brickList.size() : position;
      brickList.add(position, brickToBeAdded);
    }

    this.initInsertedBrick = initInsertedBrick;
    this.positionOfInsertedBrick = position;

    if (scriptCount == 0 && userBrick == null) {
      Script script = new StartScript();
      currentSprite.addScript(script);
      brickList.add(0, script.getScriptBrick());
      ProjectManager.getInstance().setCurrentScript(script);
      clearCheckedItems();
      positionOfInsertedBrick = 1;
    }

    notifyDataSetChanged();
  }
示例#2
0
 public BrickAdapter(Context context, Sprite sprite, DragAndDropListView listView) {
   this.context = context;
   this.sprite = sprite;
   dragAndDropListView = listView;
   insertionView = View.inflate(context, R.layout.brick_insert, null);
   initInsertedBrick = false;
   addingNewBrick = false;
   firstDrag = true;
   retryScriptDragging = false;
   animatedBricks = new ArrayList<Brick>();
   this.selectMode = ListView.CHOICE_MODE_NONE;
   initBrickList();
 }
示例#3
0
  public void removeDraggedBrick() {
    if (!addingNewBrick) {
      draggedBrick = null;
      firstDrag = true;
      notifyDataSetChanged();

      return;
    }

    brickList.remove(draggedBrick);

    firstDrag = true;
    draggedBrick = null;
    addingNewBrick = false;

    initBrickList();
    notifyDataSetChanged();
  }
示例#4
0
  public void removeFromBrickListAndProject(int index, boolean removeScript) {
    if (addingNewBrick) {
      brickList.remove(draggedBrick);
    } else if (script == null) {
      int[] temp = getScriptAndBrickIndexFromProject(index);
      Script script = ProjectManager.getInstance().getCurrentSprite().getScript(temp[0]);
      if (script != null) {

        Brick brick = script.getBrick(temp[1]);
        if (brick instanceof NestingBrick) {
          for (NestingBrick tempBrick : ((NestingBrick) brick).getAllNestingBrickParts(true)) {
            script.removeBrick((Brick) tempBrick);
          }
        } else {
          script.removeBrick(brick);
        }
        if (removeScript) {
          brickList.remove(script);
        }
      }
    } else {

      Brick brick = script.getBrick(getPositionInUserScript(index));
      if (brick instanceof NestingBrick) {
        for (NestingBrick tempBrick : ((NestingBrick) brick).getAllNestingBrickParts(true)) {
          script.removeBrick((Brick) tempBrick);
        }
      } else {
        script.removeBrick(brick);
      }
    }

    firstDrag = true;
    draggedBrick = null;
    addingNewBrick = false;

    initBrickList();
    notifyDataSetChanged();
  }
示例#5
0
 public void updateProjectBrickList() {
   initBrickList();
   notifyDataSetChanged();
 }
示例#6
0
  @Override
  public void drop() {
    int to = toEndDrag;

    if (to < 0 || to >= brickList.size()) {
      to = brickList.size() - 1;
    }

    if (retryScriptDragging || to != getNewPositionForScriptBrick(to, draggedBrick)) {
      scrollToPosition(dragTargetPosition);
      draggedBrick = null;
      initInsertedBrick = true;
      positionOfInsertedBrick = dragTargetPosition;
      notifyDataSetChanged();

      retryScriptDragging = false;
      return;
    }
    int tempTo = getNewPositionIfEndingBrickIsThere(to, draggedBrick);
    if (to != tempTo) {
      to = tempTo;
    }

    if (addingNewBrick) {
      if (draggedBrick instanceof ScriptBrick) {
        addScriptToProject(to, (ScriptBrick) draggedBrick);
      } else {
        if (script != null) {
          addBrickToPositionInUserScript(to, draggedBrick);
        } else {
          addBrickToPositionInProject(to, draggedBrick);
        }
      }

      if (draggedBrick instanceof UserBrick) {
        int positionOfUserbrickInScript = 0;
        for (int index = 0; index <= to; index++) {
          if (brickList.get(index) instanceof UserBrick
              && ((UserBrick) brickList.get(index)).getUserBrickId()
                  == ((UserBrick) draggedBrick).getUserBrickId()) {
            positionOfUserbrickInScript++;
          }
        }
        for (int parameterIndex = 0;
            parameterIndex
                < ProjectManager.getInstance()
                    .getCurrentProject()
                    .getUserVariables()
                    .getOrCreateVariableListForUserBrick(
                        ((UserBrick) draggedBrick).getUserBrickId())
                    .size();
            parameterIndex++) {
          ((UserBrick) draggedBrick)
              .addUserBrickPositionToParameter(
                  Pair.create(positionOfUserbrickInScript, parameterIndex));
        }

        ArrayList<Pair<Integer, Integer>> userBrickPositionToParameterList =
            ((UserBrick) draggedBrick).getUserBrickPositionToParameter();

        int numberOfUserBricksInScript = userBrickPositionToParameterList.size();
        int frequencyOfEqualFirstParameters = 0;
        for (int newIndex = 0; newIndex < userBrickPositionToParameterList.size(); newIndex++) {
          if (userBrickPositionToParameterList.get(newIndex).first
              == userBrickPositionToParameterList.get(numberOfUserBricksInScript - 1).first) {
            frequencyOfEqualFirstParameters++;
          }
        }
        if (frequencyOfEqualFirstParameters
            != ProjectManager.getInstance()
                .getCurrentProject()
                .getUserVariables()
                .getOrCreateVariableListForUserBrick(((UserBrick) draggedBrick).getUserBrickId())
                .size()) {
          for (int userBrickPosition = positionOfUserbrickInScript;
              userBrickPosition < numberOfUserBricksInScript;
              userBrickPosition++) {
            Pair<Integer, Integer> userBrickPositionToParameter =
                ((UserBrick) draggedBrick).getUserBrickPositionToParameter().get(userBrickPosition);
            if (userBrickPositionToParameter.first >= userBrickPosition) {
              ((UserBrick) draggedBrick)
                  .setUserBrickPositionToParameter(
                      Pair.create(
                          userBrickPositionToParameter.first + 1,
                          userBrickPositionToParameter.second),
                      ((UserBrick) draggedBrick)
                          .getUserBrickIndexInScript(userBrickPositionToParameter));
            }
          }
        }
        // TODO: test if everything gets updated if userbrick gets moved (should work) and delete
        // arraylist entry when userbrick gets deleted, when new variable gets created update
      }

      addingNewBrick = false;
    } else {
      if (script != null) {
        moveUserBrick(fromBeginDrag, toEndDrag);
      } else {
        moveExistingProjectBrick(fromBeginDrag, toEndDrag);
      }
    }

    draggedBrick = null;
    firstDrag = true;

    initBrickList();
    notifyDataSetChanged();

    int scrollTo = to;
    if (scrollTo >= brickList.size() - 1) {
      scrollTo = getCount() - 1;
    }
    dragAndDropListView.smoothScrollToPosition(scrollTo);
  }