Esempio n. 1
0
  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);
    }
  }
Esempio n. 2
0
  private void addScriptToProject(int position, ScriptBrick scriptBrick) {
    Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();

    int[] temp = getScriptAndBrickIndexFromProject(position);

    int scriptPosition = temp[0];
    int brickPosition = temp[1];

    Script newScript = scriptBrick.getScriptSafe();
    if (currentSprite.getNumberOfBricks() > 0) {
      int addScriptTo = position == 0 ? 0 : scriptPosition + 1;
      currentSprite.addScript(addScriptTo, newScript);
    } else {
      currentSprite.addScript(newScript);
    }

    Script previousScript = currentSprite.getScript(scriptPosition);
    if (previousScript != null) {
      Brick brick;
      int size = previousScript.getBrickList().size();
      for (int i = brickPosition; i < size; i++) {
        brick = previousScript.getBrick(brickPosition);
        previousScript.removeBrick(brick);
        newScript.addBrick(brick);
      }
    }
    ProjectManager.getInstance().setCurrentScript(newScript);
  }
Esempio n. 3
0
  public void testBehaviourWithoutBricks() {
    Project project = ProjectManager.getInstance().getCurrentProject();
    assertNotNull("current project was null", project);

    Sprite blueSprite = project.getSpriteList().get(4);
    while (blueSprite.getNumberOfScripts() > 0) {
      blueSprite.removeScript(blueSprite.getScript(0));
    }

    assertEquals("there shouldn't be any script left", 0, blueSprite.getNumberOfScripts());
    assertEquals("there shouldn't be any script left", 0, blueSprite.getNumberOfBricks());
    StorageHandler.getInstance().loadProject(project.getName());

    solo.waitForActivity(StageActivity.class.getSimpleName());
    solo.sleep(1400);

    byte[] screenArray = StageActivity.stageListener.getPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, -19, -19, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, -19, 19, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, 19, -19, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, 19, 19, SCREEN_WIDTH, SCREEN_HEIGHT);

    solo.sleep(2000);
  }
Esempio n. 4
0
 public void handleScriptDelete(Sprite spriteToEdit, Script scriptToDelete) {
   spriteToEdit.removeScript(scriptToDelete);
   if (spriteToEdit.getNumberOfScripts() == 0) {
     ProjectManager.getInstance().setCurrentScript(null);
     updateProjectBrickList();
   } else {
     int lastScriptIndex = spriteToEdit.getNumberOfScripts() - 1;
     Script lastScript = spriteToEdit.getScript(lastScriptIndex);
     ProjectManager.getInstance().setCurrentScript(lastScript);
     updateProjectBrickList();
   }
 }
Esempio n. 5
0
 @Override
 public void setTouchedScript(int index) {
   if (index >= 0
       && index < brickList.size()
       && brickList.get(index) instanceof ScriptBrick
       && draggedBrick == null) {
     int scriptIndex = getScriptIndexFromProject(index);
     if (scriptIndex == -1) {
       Log.e(TAG, "setTouchedScript() Could not get ScriptIndex. index was " + index);
       return;
     }
     ProjectManager.getInstance().setCurrentScript(sprite.getScript(scriptIndex));
   }
 }
Esempio n. 6
0
  private void moveExistingProjectBrick(int from, int to) {
    Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();

    int[] tempFrom = getScriptAndBrickIndexFromProject(from);
    int scriptPositionFrom = tempFrom[0];
    int brickPositionFrom = tempFrom[1];

    Script fromScript = currentSprite.getScript(scriptPositionFrom);

    Brick brick = fromScript.getBrick(brickPositionFrom);
    if (draggedBrick != brick) {
      Log.e(TAG, "Want to save wrong brick");
      return;
    }
    fromScript.removeBrick(brick);

    int[] tempTo = getScriptAndBrickIndexFromProject(to);
    int scriptPositionTo = tempTo[0];
    int brickPositionTo = tempTo[1];

    Script toScript = currentSprite.getScript(scriptPositionTo);

    toScript.addBrick(brickPositionTo, brick);
  }
Esempio n. 7
0
 private void addScriptEndCallbacks() {
   Project project = ProjectManager.getInstance().getCurrentProject();
   for (Sprite sprite : project.getSpriteList()) {
     for (int i = 0; i < sprite.getNumberOfScripts(); i++) {
       sprite
           .getScript(i)
           .addBrick(
               new CallbackBrick(
                   sprite,
                   new CallbackBrick.BrickCallback() {
                     @Override
                     public void onCallback() {
                       programWaitLock.release();
                     }
                   }));
     }
   }
 }
Esempio n. 8
0
  private int[] getScriptAndBrickIndexFromProject(int position) {
    int[] returnValue = new int[2];

    if (position >= brickList.size()) {

      returnValue[0] = sprite.getNumberOfScripts() - 1;
      if (returnValue[0] < 0) {
        returnValue[0] = 0;
        returnValue[1] = 0;
      } else {
        Script script = sprite.getScript(returnValue[0]);
        if (script != null) {
          returnValue[1] = script.getBrickList().size();
        } else {
          returnValue[1] = 0;
        }
      }

      return returnValue;
    }

    int scriptPosition = 0;
    int scriptOffset;
    for (scriptOffset = 0; scriptOffset < position; ) {
      scriptOffset += sprite.getScript(scriptPosition).getBrickList().size() + 1;
      if (scriptOffset < position) {
        scriptPosition++;
      }
    }
    scriptOffset -= sprite.getScript(scriptPosition).getBrickList().size();

    returnValue[0] = scriptPosition;
    List<Brick> brickListFromProject = sprite.getScript(scriptPosition).getBrickList();
    int brickPosition = position;
    if (scriptOffset > 0) {
      brickPosition -= scriptOffset;
    }

    Brick brickFromProject;
    if (brickListFromProject.size() != 0 && brickPosition < brickListFromProject.size()) {
      brickFromProject = brickListFromProject.get(brickPosition);
    } else {
      brickFromProject = null;
    }

    returnValue[1] = sprite.getScript(scriptPosition).getBrickList().indexOf(brickFromProject);
    if (returnValue[1] < 0) {
      returnValue[1] = sprite.getScript(scriptPosition).getBrickList().size();
    }

    return returnValue;
  }
Esempio n. 9
0
  public void initBrickList() {
    brickList = new ArrayList<Brick>();

    if (userBrick != null) {
      initBrickListUserScript();
      return;
    }
    Sprite sprite = ProjectManager.getInstance().getCurrentSprite();

    int numberOfScripts = sprite.getNumberOfScripts();
    for (int scriptPosition = 0; scriptPosition < numberOfScripts; scriptPosition++) {
      Script script = sprite.getScript(scriptPosition);
      brickList.add(script.getScriptBrick());
      script.getScriptBrick().setBrickAdapter(this);
      for (Brick brick : script.getBrickList()) {
        brickList.add(brick);
        brick.setBrickAdapter(this);
      }
    }
  }
Esempio n. 10
0
  private void setDefinitionBrickReferences(
      Sprite cloneSprite, List<UserBrick> originalPrototypeUserBricks) {
    for (int scriptPosition = 0;
        scriptPosition < cloneSprite.getScriptList().size();
        scriptPosition++) {
      Script clonedScript = cloneSprite.getScript(scriptPosition);
      for (int brickPosition = 0;
          brickPosition < clonedScript.getBrickList().size();
          brickPosition++) {
        Brick clonedBrick = clonedScript.getBrick(brickPosition);
        if (!(clonedBrick instanceof UserBrick)) {
          continue;
        }
        UserBrick clonedUserBrick = ((UserBrick) clonedBrick);
        UserBrick originalUserBrick =
            ((UserBrick) getScript(scriptPosition).getBrick(brickPosition));
        int originalIndexOfDefinitionBrick = 0;
        for (int prototypeUserBrickPosition = 0;
            prototypeUserBrickPosition < originalPrototypeUserBricks.size();
            prototypeUserBrickPosition++) {
          UserBrick originalPrototypeUserBrick =
              originalPrototypeUserBricks.get(prototypeUserBrickPosition);
          if (originalPrototypeUserBrick
              .getDefinitionBrick()
              .equals(originalUserBrick.getDefinitionBrick())) {
            originalIndexOfDefinitionBrick = prototypeUserBrickPosition;
            break;
          }
        }

        UserBrick clonedPrototypeUserBrick =
            cloneSprite.getUserBrickList().get(originalIndexOfDefinitionBrick);
        UserScriptDefinitionBrick correctClonedDefinitionBrick =
            clonedPrototypeUserBrick.getDefinitionBrick();
        clonedUserBrick.setDefinitionBrick(correctClonedDefinitionBrick);

        clonedPrototypeUserBrick.updateUserBrickParametersAndVariables();
        clonedUserBrick.updateUserBrickParametersAndVariables();
      }
    }
  }
Esempio n. 11
0
  public void removeUnusedBroadcastMessages() {
    List<String> usedMessages = new ArrayList<String>();
    for (Sprite currentSprite : spriteList) {
      for (int scriptIndex = 0; scriptIndex < currentSprite.getNumberOfScripts(); scriptIndex++) {
        Script currentScript = currentSprite.getScript(scriptIndex);
        if (currentScript instanceof BroadcastMessage) {
          addBroadcastMessage(
              ((BroadcastMessage) currentScript).getBroadcastMessage(), usedMessages);
        }

        for (int brickIndex = 0; brickIndex < currentScript.getBrickList().size(); brickIndex++) {
          Brick currentBrick = currentScript.getBrick(brickIndex);
          if (currentBrick instanceof BroadcastMessage) {
            addBroadcastMessage(
                ((BroadcastMessage) currentBrick).getBroadcastMessage(), usedMessages);
          }
        }
      }
    }
    MessageContainer.removeUnusedMessages(usedMessages);
  }
Esempio n. 12
0
  private int getScriptIndexFromProject(int index) {
    int scriptIndex = 0;
    Script temporaryScript = null;
    for (int i = 0; i < index; ) {
      temporaryScript = sprite.getScript(scriptIndex);
      if (temporaryScript == null) {
        Log.e(
            TAG,
            "getScriptIndexFromProject() tmpScript was null. Index was "
                + index
                + " scriptIndex was "
                + scriptIndex);
        return -1;
      }
      i += temporaryScript.getBrickList().size() + 1;
      if (i <= index) {
        scriptIndex++;
      }
    }

    return scriptIndex;
  }
Esempio n. 13
0
  @Override
  public void onClick(final View view) {
    if (!viewSwitchLock.tryLock()) {
      return;
    }

    animatedBricks.clear();
    final int itemPosition = calculateItemPositionAndTouchPointY(view);
    final List<CharSequence> items = new ArrayList<CharSequence>();

    if (brickList.get(itemPosition) instanceof ScriptBrick) {
      int scriptIndex = getScriptIndexFromProject(itemPosition);
      ProjectManager.getInstance().setCurrentScript(sprite.getScript(scriptIndex));
    }

    if (!(brickList.get(itemPosition) instanceof DeadEndBrick)
        && !(brickList.get(itemPosition) instanceof ScriptBrick)) {
      items.add(context.getText(R.string.brick_context_dialog_move_brick));
    }
    if ((brickList.get(itemPosition) instanceof UserBrick)) {
      items.add(context.getText(R.string.brick_context_dialog_show_source));
    }
    if (brickList.get(itemPosition) instanceof NestingBrick) {
      items.add(context.getText(R.string.brick_context_dialog_animate_bricks));
    }
    if (!(brickList.get(itemPosition) instanceof ScriptBrick)) {
      items.add(context.getText(R.string.brick_context_dialog_copy_brick));
      items.add(context.getText(R.string.brick_context_dialog_delete_brick));
    } else {
      items.add(context.getText(R.string.brick_context_dialog_delete_script));
    }
    if (brickHasAFormula(brickList.get(itemPosition))) {
      items.add(context.getText(R.string.brick_context_dialog_formula_edit_brick));
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(context);

    boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
    view.buildDrawingCache(true);

    if (view.getDrawingCache() != null) {
      Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(drawingCacheEnabled);

      ImageView imageView = dragAndDropListView.getGlowingBorder(bitmap);
      builder.setCustomTitle(imageView);
    }

    builder.setItems(
        items.toArray(new CharSequence[items.size()]),
        new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int item) {
            CharSequence clickedItemText = items.get(item);
            if (clickedItemText.equals(context.getText(R.string.brick_context_dialog_move_brick))) {
              view.performLongClick();
            } else if (clickedItemText.equals(
                context.getText(R.string.brick_context_dialog_show_source))) {
              launchAddBrickAndSelectBrickAt(context, itemPosition);
            } else if (clickedItemText.equals(
                context.getText(R.string.brick_context_dialog_copy_brick))) {
              copyBrickListAndProject(itemPosition);
            } else if (clickedItemText.equals(
                    context.getText(R.string.brick_context_dialog_delete_brick))
                || clickedItemText.equals(
                    context.getText(R.string.brick_context_dialog_delete_script))) {
              showConfirmDeleteDialog(itemPosition);
            } else if (clickedItemText.equals(
                context.getText(R.string.brick_context_dialog_animate_bricks))) {
              int itemPosition = calculateItemPositionAndTouchPointY(view);
              Brick brick = brickList.get(itemPosition);
              if (brick instanceof NestingBrick) {
                List<NestingBrick> list = ((NestingBrick) brick).getAllNestingBrickParts(true);
                for (NestingBrick tempBrick : list) {
                  animatedBricks.add((Brick) tempBrick);
                }
              }
              notifyDataSetChanged();
            } else if (clickedItemText.equals(
                context.getText(R.string.brick_context_dialog_formula_edit_brick))) {
              clickedEditFormula(brickList.get(itemPosition), view);
            }
          }
        });
    alertDialog = builder.create();

    if ((selectMode == ListView.CHOICE_MODE_NONE)) {
      alertDialog.show();
    }
  }
Esempio n. 14
0
  public Brick getChild(int scriptPosition, int brickPosition) {
    Sprite sprite = ProjectManager.getInstance().getCurrentSprite();

    return sprite.getScript(scriptPosition).getBrick(brickPosition);
  }