protected void copyBrickListAndProject(int itemPosition) { Brick origin = (Brick) (dragAndDropListView.getItemAtPosition(itemPosition)); Brick copy; try { copy = origin.clone(); addNewBrick(itemPosition, copy, true); notifyDataSetChanged(); } catch (CloneNotSupportedException exception) { Log.e(TAG, Log.getStackTraceString(exception)); } }
private void scrollToPosition(final int position) { DragAndDropListView list = dragAndDropListView; if (list.getFirstVisiblePosition() < position && position < list.getLastVisiblePosition()) { return; } list.setIsScrolling(); if (position <= list.getFirstVisiblePosition()) { list.smoothScrollToPosition(0, position + 2); } else { list.smoothScrollToPosition(brickList.size() - 1, position - 2); } }
@Override public View getView(int position, View convertView, ViewGroup parent) { if (draggedBrick != null && dragTargetPosition == position) { return insertionView; } listItemCount = position + 1; Object item = getItem(position); if (item instanceof ScriptBrick && (!initInsertedBrick || position != positionOfInsertedBrick)) { View scriptBrickView = ((Brick) item).getView(context, position, this); if (draggedBrick == null) { scriptBrickView.setOnClickListener(this); } return scriptBrickView; } View currentBrickView; // dirty HACK // without the footer, position can be 0, and list.get(-1) caused an Indexoutofboundsexception // no clean solution was found if (position == 0) { if (item instanceof AllowedAfterDeadEndBrick && brickList.get(position) instanceof DeadEndBrick) { currentBrickView = ((AllowedAfterDeadEndBrick) item).getNoPuzzleView(context, position, this); } else { currentBrickView = ((Brick) item).getView(context, position, this); } } else { if (item instanceof AllowedAfterDeadEndBrick && brickList.get(position - 1) instanceof DeadEndBrick) { currentBrickView = ((AllowedAfterDeadEndBrick) item).getNoPuzzleView(context, position, this); } else { currentBrickView = ((Brick) item).getView(context, position, this); } } // this one is working but causes null pointer exceptions on movement and control bricks?! // currentBrickView.setOnLongClickListener(longClickListener); // Hack!!! // if wrapper isn't used the longClick event won't be triggered @SuppressLint("ViewHolder") ViewGroup wrapper = (ViewGroup) View.inflate(context, R.layout.brick_wrapper, null); if (currentBrickView.getParent() != null) { ((ViewGroup) currentBrickView.getParent()).removeView(currentBrickView); } LinearLayout brickElement = (LinearLayout) currentBrickView; final CheckBox checkbox = ((Brick) getItem(position)).getCheckBox(); wrapper.addView(currentBrickView); if (draggedBrick == null) { if ((selectMode == ListView.CHOICE_MODE_NONE)) { wrapper.setOnClickListener(this); if (!(item instanceof DeadEndBrick)) { wrapper.setOnLongClickListener(dragAndDropListView); } } else { brickElement.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { checkbox.setChecked(!checkbox.isChecked()); } }); } } if (position == positionOfInsertedBrick && initInsertedBrick && (selectMode == ListView.CHOICE_MODE_NONE)) { initInsertedBrick = false; addingNewBrick = true; dragAndDropListView.setInsertedBrick(position); dragAndDropListView.setDraggingNewBrick(); dragAndDropListView.onLongClick(currentBrickView); return insertionView; } if (animatedBricks.contains(brickList.get(position))) { Animation animation = AnimationUtils.loadAnimation(context, R.anim.blink); wrapper.startAnimation(animation); animatedBricks.remove(brickList.get(position)); } return wrapper; }
@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); }
private int calculateItemPositionAndTouchPointY(View view) { return dragAndDropListView.pointToPosition(view.getLeft(), view.getTop()); }
@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(); } }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { FragmentManager fragmentManager = getSupportFragmentManager(); for (String tag : FormulaEditorListFragment.TAGS) { FormulaEditorListFragment fragment = (FormulaEditorListFragment) fragmentManager.findFragmentByTag(tag); if (fragment != null && fragment.isVisible()) { return fragment.onKey(null, keyCode, event); } } FormulaEditorVariableListFragment formulaEditorVariableListFragment = (FormulaEditorVariableListFragment) getSupportFragmentManager() .findFragmentByTag(FormulaEditorVariableListFragment.VARIABLE_TAG); if (formulaEditorVariableListFragment != null && formulaEditorVariableListFragment.isVisible()) { return formulaEditorVariableListFragment.onKey(null, keyCode, event); } FormulaEditorFragment formulaEditor = (FormulaEditorFragment) getSupportFragmentManager() .findFragmentByTag(FormulaEditorFragment.FORMULA_EDITOR_FRAGMENT_TAG); if (formulaEditor != null && formulaEditor.isVisible()) { scriptFragment.getAdapter().updateProjectBrickList(); return formulaEditor.onKey(null, keyCode, event); } if (soundFragment != null && soundFragment.isVisible() && soundFragment.onKey(null, keyCode, event)) { return true; } if (lookFragment != null && lookFragment.isVisible() && lookFragment.onKey(null, keyCode, event)) { return true; } int backStackEntryCount = fragmentManager.getBackStackEntryCount(); for (int i = backStackEntryCount; i > 0; --i) { String backStackEntryName = fragmentManager.getBackStackEntryAt(i - 1).getName(); if (backStackEntryName != null && (backStackEntryName.equals(LookFragment.TAG) || backStackEntryName.equals(SoundFragment.TAG))) { fragmentManager.popBackStack(); } else { break; } } if (keyCode == KeyEvent.KEYCODE_BACK && currentFragmentPosition == FRAGMENT_SCRIPTS) { DragAndDropListView listView = scriptFragment.getListView(); if (listView.isCurrentlyDragging()) { listView.resetDraggingScreen(); BrickAdapter adapter = scriptFragment.getAdapter(); adapter.removeDraggedBrick(); return true; } } return super.onKeyDown(keyCode, event); }